In the previous post, I explained the backdrop sprite, Map sprite and Cursor sprite. In this post, I will explain the Rule sprite, which contains core algorithm of this game – how to judge who wins the game. I will also analyze the code of several other auxilliary sprites. So let’s start!
There is not any costume for Rule sprite. It is the backend judge of the game. In “Cursor” sprite analyzed in the previous post, its “when I receive Check Valid” code segment will broadcast “Rule” message. Rule sprite receives this message and calls “Judge” block to finish the task.
Before introducing “Judge” block, I woud like to explain the list structure of “Direction_list”. When game starts, the list is emptied and then refilled into 16 items. What do they represent? They are grouped into 4 categories. Take the first 4 items as an example, they represent the searching routes alone the horizontal direction. Among them, item (1) and item (2) represent searching the chess pieces of the same colour along the negative x direction because the relative position to the previous item is (-1, 0). Item (3) and item (4) represent searching the chess pieces along the positive x-axis direction.
Please note the searching directions are not limited to horizontal or vertical ones, but also contain diagnoal ones. For example, item (13) and item (14) represent to check the chess pieces alone the south-west direction since its relative position to the previous one is (-1, -1). All the directions are illustrated in the following diagram.
In this loop, it repeats 4 times to get the total connected number of same colour chess pieces. The 4 iterations represent the horizontal, vertical and two diagonal directions, respectively. The iteration index is represented by variable “i”. In each iteration, the program repeats twice to search for the positive and negative routes alone that line. The two routes are represented by variable “j”.
I will explain the meaning of the formula (i * 4 + j *2 + 1) and (i * 4 + j * 2 + 2). For example, when i = 0, j = 0, the next searching item “temp_column” and “temp_row” in “Chess_board” list has a relative position to the previous one. Its relative position is specified by item (1) and item (2), that is (-1, 0), the negative hozironal direction. The search will continue until the chess piece color does not match the color of the current mover. In the next iteration, i keeps unchanged, while j increases by 1, so the relative position is specified in item (3) and item (4) which are (1, 0). It is now searching the positive horizontal direction. The searching continues until the searched chess piece does not match the color of the current mover or row/column index exceeds the range of the board.
After searching for the horizontal direction, if “connected_total” variable is equal to or exceeds 5, it means that there are at least 5 chess pieces of the current mover connecting alone one line. The program will broadcast “win” message. Otherwise, it will increase the value of i by 1 and reset j value to 0 to conduct the search in the next direction.
White Side Sprite
The code of White Side sprite looks simple, but a bit tricky. Why would the code exit when “Whose_turn” variable is equal to “White”? Should’t it exit when “Whose_turn” is Black instead?
This question should trace back to “when I receive Check Valid” segment of Cursor sprite. If the program judges that the current move is valid, it will change “Whose_turn” to opponent colour, and then broadcast “Put Chess” message and “Judge” message. Therefore, for White Side sprite, when it receives “Put Chess” message, the program has judged that the current move is valid and set “Whose_turn” to opponent’s colour. In this case, if “Whose_turn” is equal to White, it means that the curren mover is Black, so White Side sprite should do nothing.
Black Side Sprite
Black Side sprite uses the same method as White Side sprite, so I will not explain further.
Start button Sprite
Start button sprite is simple. Its main functionality is to broadcast “Start Game” message after user clicks it.
Winner Sprite is responsible for declaring which side wins the game. Here, just like White Side sprite and Black Side sprite, when “Whose_turn” variable is equal to White, it means that the current mover is Black, so Black Side wins the game. When “Whose_turn” variable is equal to Black, it is White Side that wins the game.
That is all for the analysis of the GomoKu game. As I metnioned, it has not cool visual effect, but we could learn a lot throug its algorithm design, including how to draw chess board, how to adjust cursor position and how to judge the winner. Who would say that Scratch is only suitable for starters and kids! Enjoy the coding and have fun!
Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.