Thanks to Julio Marchi for this space in MSX All
A puzzle game where you must place in order 8 blocks on a 3x3 board, by moving the blocks to the hole.
This game is a variation of N-Puzzle game, where we can also find the 15-Puzzle, 24-Puzzle etc.
The game will be programmed in C and it also compatible with PCs. Thus, it can be easly ported to Pascal.
The game board can be built using a 3x3 matrix or an array of 9 positions. Both solutions will be presented.
For the 3x3 matrix, we must store the X and Y hole coordinates in HX and HY, respectively. For the array, we only store the hole position in pos. The variable moves counts the number of moves performed by the player to solve the game.
Once C arrays and matrixes ranges from 0 to N-1, the hole coordinates or position will also range from 0 to N-1. Nevertheless, the block labels ranges from 1 to 8. So, we must pay attention to this issue.
The initial board configuration cannot came up from a random sequence numbers, once it may result on a game without solution. According to that, the solution to get a valid initial sequence will be the same used in the real game: from an ordered sequence, do N moves to shuffle them.
First of all, a function will place the numbers in order, reserving the value 0 for the hole.
Then, it is necessary to perform N successive moves to shuffle the numbers. Thus, before we must take a look how to swap blocks.
The possible moves analysis are always performed based on the hole position.
For the 3x3 matrix, the analysis is done at executing time. From a proposed shift dx and dy to the hole position HX e HX, we check if:
For the array of 9, we will store all possible moves for each hole position in an array. The swapping is done directly between the current hole position and one of the possible positions.
The next figure shows the possible moves (green) for each hole position (red).
Now we have:
Once standard C do not return the length of a string, we store the total amount of moves on a new array called total_moves.
The swapping codes are:
On the array of 9, the possible move value comes from the character (ASCII code) conversion to integer value in the function get_pm_value. In order to adapt the position value to the C arrays and matrix indexes, this function also converts the position to the ranging from 0 to N-1. Example of this process in Basic:
Q = "2" P = ASC(Q) - 48 PRINT P 2 P = P-1 PRINT P 1For both cases the parameters passed to the functions are the new hole position.
After swapping, the hole position is updated.
After we see how to swap blocks, me are ready to shuffle.
For the 3x3 matrix, it picks up randomly values for dx and dy and only goes on if the move is valid. Otherwise, it tries another values until they are valid.
The solution for the Array of 9 more intelligent, once it is possible to pick up directly one of a valid move stored on possible_moves array.
Initialization and board print
The following codes complete the first step. A function is responsible for the board initialization and another one for printing the board.
This is a single player game. So, the program must read the block label from the keyboard the player whishes to move. For that:
Converting from block label to real board position
The user selects the block label (number) and not its coordinates. According to that, we must locate the block position on the board.
After locating the referred block, it performs a swapping.
If the move is invalid, the function notifies returning 0.
Checking for game solution
The checking for game solution routine is quite simple, once all we have to do is to verify if the numbers are ordered in the board.
Obs: when the variable num is equal 9, it must be considered as 0 in order to compare with the hole code.
The following routine controls the game in both cases.
- Inteligência Artificial em Basic, M. James, Editora Campus, 1985.