Finally checkers is complete

By // No comments:
Wohoooooooo!
I have finally completed my checkers game. Last time I posted, I had not made the visibility of King, its movement and its capture. I had also not done the win condition.

But, now everything is complete.

How I did this all?

1) It was actually very simple. The work I needed to do was repetitive. My lines of code did increase from 350 to 750 but again the stuff that I did was copy and paste from my own code with some minor changes.
2) For movement of kings,Since kings move in both directions, I just used the logic of movement of black and white pieces and their capture.

So finally white pieces can move and capture black pieces. Black pieces move and capture white pieces. White king moves, captures black king and black pieces. Black king moves, captures white king and white pieces.

Two integers to maintain the count of pieces. If either of the pieces go to zero, that particular player wins.  

Piece capture complete

By // No comments:
SO the update is that I have finally completed by piece capture. This means that I am able to capture pieces for both black and white according to the turn. Doing this was really a difficult job but ultimately turned out to be very easy.

Last time, I wrote on my blog, I was able to select the pieces but they could not move. The movement part seemed very hard. The problem with the old approach was that I was using a nested for loop to check which box is selected. That made by logic extremely complicated and I was not able to move forward. But, ultimately, I came across a very simple logic to check which box is being clicked.

How did I do it?
If we know that our box width and height are both 60. We have 64 boxes. When we click on the board, the Mouse key press function returns us the values of x and y from the screen. These values of x and y represent the position on the board. So we use the formula:

w = x // 60h = y // 60box = ((w * h) + ((8 - w) * (h - 1)) - 1) // 2


By this formula, we get the position of the box. Then, I made a function which checked if the move is legal. For that I passed the selected box and the new box in the function. This function was called in mouse press function. Since in the checker board, I have only used the red boxes, so my total boxes are 32. If you draw and label the checkers board from 0-31, you will clearly see that for some boxes, if move is legal, movement from selected boxes will be +3, for some it will be +4 and for some, it will be +5. So I mapped out all the possibilities of the boxes to check if the move was legal. Then, if the move was legal, I changed the state of that boxes.

Same thing was done for the capture as well. If the move is legal, the state will change.
For changing the state I have made a do move function which works only if move is legal. It changes the sate of the boxes according to the move that was made.

To recap what I have done uptill now:
1) Loaded blue and red tile. Used nested for loops to make a board 8x8 consisting of blue and red boxes.
2) Loaded sprites of black, white, double black, double white, selected white and selected black. Then, I made each of these sprites for every box.  Then stored all these sprites in separate lists(e.g black sprite in one list, white in another and so on). 
3) I made a list of length 32 of type integer which stored the state of the particular box.
4) a variable for turn of type integer to check if turn was white(0) or black(1).
5) a variable for selected box of type integer to check if any box is selected. If a box is selected, which box is selected. If no box is selected, then it is zero.
6) Then I did what is explained above the Recap.

More to come.....

Checkers board and pieces, GUI completed

By // No comments:

The update is that I have finally completed my GUI that is I have finally made a checkers board and also made the sprites. I have also started handling mouse events.





How did I do all of this?

Well, don't worry, it was very easy to do this stuff. Since I am using Python as the programming language, and pyglet as the gaming library, all the syntax was very easy to understand. I am using pycharm as the IDE, so it saves time.

1) I made a window screen of a size of my choice which only showed black screen.

2) Then, I proceeded to make load squares for making a chess board. Using two images and some logic with for loops, I was able to display the checkers board.

3) Then, I loaded the sprites for the game which were two pieces that are black and white. Then, I defined the position of the sprites and finally I was able to display the sprites. 

4) But, there was one problem, my code was very unorganised and alot of repeated work needed to be done. So I changed my code so now it using Object Oriented programming.

5) First I made separate files of square, board, sprites and main but they were unhelpful. So now, I have just made one file having a class of checkers and that does all the stuff through its functions. 

6) The sad part is that I am still working on the logic of valid movement of pieces and conquering pieces but the good part is that I have learned the syntax of mouse controlled event so as soon as the logic is made, it will be easily implemented. 

7) One interesting thing I have found out is bitboarding and that seems a good way forward. Basically, you make a matrix storing all the board squares, in our case, we are just using red squares, so they are 32. Then, we save the value 0,1 or 2 in place of that square, 0 represents empty, 1 represents white, 2 represents black.