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.....
0 comments:
Post a Comment