I'm currently trying to learn openGL and I am using GLFW.
in the window, I am using the glfwSetKeyCallback function for single key presses/ key combinations (like CTRL + q or ESC for closing window with a switch statement) and I am using another function that runs every few milisecond for constant keypresses (like W for moving forward or A for moving left using multiple if statements)
seperating the two usecases in two different function makes it easier for me, but I was wondering if it was a good idea to use them like that or if I should use all the different usecases in a single function, either the callback or the one that is called every few miliseconds. (I hope that was clear)
Thanks in advance for your advice !
I have an array where each element is a hash representing a simplified version of an entire Chess Board. I am trying to implement the fifty-move draw rule which states a draw can be claimed if in fifty moves, no piece has been captured and no pawn has moved.
In doing so, I'm trying to keep DRY and use code that I've already implemented for another draw scenario, which is currently working properly.
A new "snapshot" of the board is saved after each turn and looks like the following (after a pawn has moved from "a2" to "a4" on the first turn):
board_snapshot = [{
"a1"=>"Rook", "a2"=>nil, "a3"=>nil, "a4"=>"Pawn", "a5"=>nil, "a6"=>nil,
"a7"=>"Pawn", "a8"=>"Rook", "b1"=>"Knight", "b2"=>"Pawn", "b3"=>nil,
"b4"=>nil, "b5"=>nil, "b6"=>nil, "b7"=>"Pawn", "b8"=>"Knight",
"c1"=>"Bishop", "c2"=>"Pawn", "c3"=>nil, "c4"=>nil, "c5"=>nil, "c6"=>nil,
"c7"=>"Pawn", "c8"=>"Bishop", "d1"=>"Queen", "d2"=>"Pawn", "d3"=>nil,
"d4"=>nil, "d5"=>nil, "d6"=>nil, "d7"=>"Pawn", "d8"=>"Queen", "e1"=>"King",
"e2"=>"Pawn", "e3"=>nil, "e4"=>nil, "e5"=>nil, "e6"=>nil, "e7"=>"Pawn",
"e8"=>"King", "f1"=>"Bishop", "f2"=>"Pawn", "f3"=>nil, "f4"=>nil,
"f5"=>nil, "f6"=>nil, "f7"=>"Pawn", "f8"=>"Bishop", "g1"=>"Knight",
"g2"=>"Pawn", "g3"=>nil, "g4"=>nil, "g5"=>nil, "g6"=>nil, "g7"=>"Pawn",
"g8"=>"Knight", "h1"=>"Rook", "h2"=>"Pawn", "h3"=>nil, "h4"=>nil,
"h5"=>nil, "h6"=>nil, "h7"=>"Pawn", "h8"=>"Rook"
}]
In pseudocode, I'm thinking of implementing this fifty move rule check by creating a method which looks at the previous fifty board snapshots to see if the amount of nil values are the same (no piece captured) and if so, somehow looking to see that each of the Pawns are on the same square.
I've found a way to compare two boards to see if the nil values are the same:
board_snapshot[index].values.count(nil) == board_snapshot[index + 1].values.count(nil)
However, I'm still having trouble coming up with a way to iterate over 50 board "snapshots" to run this test on each one. Also not sure how to iterate over the 50 "snapshots" to ensure that no Pawn has moved.
If it would just be easier to implement this rule by creating a "counter" which resets when a piece is captured and when a Pawn is moved let me know, I was trying to be efficient and utilize code that was already around.
I think #sawa has the right idea in the comments. You only need to check that the Pawns are in the same position as they were 50 moves ago (since pawns can't move backwards, they can't move in one snapshot and be returned in the next)
board_snapshot.last.delete_if{|_,v| v != "Pawn"} == board_snapshot[-50].delete_if{|_,v| v != "Pawn"}
Similarly (and using your suggested code)
board_snapshotlast.values.count(nil) == board_snapshot[-50].values.count(nil)
Since pieces can't be added to the board, you don't need to worry about a piece disappearing in one move and reappearing in the next move.
I just started with C, but I had some knowledge of PHP, so I decided to do some 'more complicated' stuff, as for a beginner :)
I used two nested loops to print an 50x50 array. It isn't very slow, but I included a movement with arrow keys to it to move one symbol, X (player) around the array. Every time a move is made, whole array needs to be refreshed, which I did by:
system("cls");
for(x=0;x<50;x++)
{
for(y=0;y<50;y++)
{
printf("%c",table[x][y]);
}
printf("\n");
}
Which is very sloppy solution and whole array 'blinks' while it refreshes after every move.
Is there any more efficient way of doing that in C?
You would probably have to use some sort of shell graphics library like ncurses to move stuff around your array without it blinking when you redraw it. There's not really a simple way to avoid that when you're just using printf to display your grid as output.
I assume you're using Windows (because of the cls).
Maybe ANSI.SYS escape sequences are the simplest way without a library.
You can probably avoid flickering if you move the cursor and overwrite the display contents without clearing the old contents.
There's an example on "Reading and Writing Blocks of Characters and Attributes" with the Win32 Console:
http://msdn.microsoft.com/en-us/library/ms685032%28v=vs.85%29.aspx
Edit: explained the link.
Ok so, I'm making a 2d dungeon crawler and I want to randomize a map for it. Right now it's look like I'm going to use a Random Walk algoritm for the path, combined with a Perlin Noise for different the underworld enviroments (currently only 1, as I'm using my own shitty looking tile set consisting of only 1 rook image and 1 grass image, but whatever :D)
So in figuring out how random walks work, it looks like I'm supposed to do something along the lines of this:
*create two-dimensional array sized after the map.
*pick random start postion and end postiont (I chose to put these on opposite sides of the map, randomly distributed across its side.
*follow these steps until you hit your finish point:
*pick a direction to 'walk' at random (only up, down, left, right because you otherwise I'm left with diagonal passes which the player can't walk through)
*'walk' that direction for a random amount of steps (I randomize amount of steps first then walk one by one for bound checking later, rather than just drawing a line).
*Everytime you 'walk' on a tile, turn that tile to 1 from originally 0.
*repeat above steps until you hit your finish point.
This leaves me with too much open ground, and too much closed ground. What I'm looking for is a path covered with rooms, sort of, but I want to control how big the 'rooms' become. I don't want 'rooms' to get too big, which some become. So I want the feeling of being in an enclosed space, but also I want to use as much of the map grid as possible.
Is a random walk not suited for this? I was thinking about making every step have a certain width to it, maybe that could work.
Or maybe I'm just implementing it wrong! I'm not a math genious sadly ;P
I am creating a pacman in c and currently I am usisng a single thread for each ghost and each ghost represents a '#' but when I run it all the screen gets full of ghosts and not all the ghosts move just one or two.
Im using this logic
create a struct of 5 ghost, each ghost contains the x,y position.
create an array of 5 threads and each thread implements one ghost
each ghost moves randomly on the screen, for each space that it moves I print
a space in the old position and then I print a '#' in the new position.
Could you provide me please an example of how to implement the movement of the ghost,
or the implementation Im doing is the correct way?
Thank you
One thread per agent is not a very common approach to building games. It quickly becomes unworkable for large scenes. The conventional solution is to define a state machine representing a ghost, with some kind of "advance" method that gives it a chance to adjust its internal state to the next time quantum. Create multiple instances of this state machine, and call all their "advance" methods on each iteration of the game loop. All of this can happen in a single thread.
There's quite a bit more to it than this, but it'll get you started.
Trying to update the screen simultaneously from several threads requires a mutex around the screen update code.