Gomoku_Tutor
Preface
Quarto Book giải thích code cho Project Gomoku
Yêu cầu chính
2. Creating the Board
The purpose of this function is to create a data structure which will be used to keep track of the states of the boards. Your task here is to write the create_board(size) function, where size is the size of the board (e.g. to create a 9 by 9 board, we call create_board(9)). You should carefully decide the data structure to be used as well as the values to be stored to represent the unoccupied intersections of the grid.
This function returns the data structure which will be used to keep track of the occupancy status of the intersections on the board. The occupancy status of the intersections for the newly created board should all be unoccupied.
3. Is the target position occupied?
The purpose of this function is to examine whether a specific position on the board is occupied by a stone. Your task is to write a function is_occupied(board, x, y) where board is the current state of the board, x is the row index and y is the column index. Here, you can assume that x and y are both valid numeric indices (i.e., both x and y are greater than or equal to 0 and smaller than the size of the board).
This function returns a boolean value of either True or False.
4. Placing a Stone at a Specific Intersection
Now we want to replicate placing a stone on the board. Your task is to write a function place_on_board(board, stone, position) which:
- Takes the following parameters:
- A board
- A stone value (either “●” or “○”)
- A position (a tuple of strings (a, b) where a is the row index, e.g., “0”, and b is the column index, e.g., “A”)
If the move is successfully performed, return a boolean value of True
If the move is impossible (e.g., invalid or occupied position), return a boolean value of False
You will need to invoke functions implemented in previous steps.
5. Printing the Board
In order for players to know the states of the current boards, there needs to be a way to visualise the board. Your task here is to write a function print_board(board):
The parameter board can be the ones created and manipulated in previous steps “--” and “|” should be used to represent the grid of the board.
Apart from the board, the indices of the board need to be presented to enhance user friendliness as indicated in the examples below
This function does not return any value.
6. Check Available Moves
The purpose of this function is to find out whether there are available moves on the board. Your task is to write a function check_available_moves(board) where board is the current state of the board. This function returns the available moves as a list of tuples where each tuple represents a position on the board, e.g., [(“0”, “A”), (“3”, “D”)]. If the board is full, return an empty list.
You will need to invoke functions implemented in previous steps.
7. Check for the Winner
The purpose of this function is to identify the winner of the game. A winning condition is achieved when one of the players successfully forms a continuous line of five stones in their colour, either horizontally, vertically or diagonally. Your task is to write a function:
The parameter can be the ones created and manipulated in previous steps
If a continuous line of five stones in the same colour has been achieved, return the corresponding stone
If the board is full but none of the players achieves a continuous line of five stones, return a string value of “Draw”
If none of the players achieve the winning condition and there are still available moves on the board, return None
You will need to invoke functions implemented in previous steps.
8. Random Computer Player
The purpose of this function is to implement a computer opponent. The computer opponent will counter the player by randomly selecting one of the available moves around the player’s previous move.
The available moves for the computer player are based on both the player’s previous move and the availability of that move’s surrounding positions. Your task here is to implement the function:
The is in the same position format, e.g.,
Seeing the position of the player’s previous move as the centroid, the function needs to find all the available valid positions within a 3 * 3 square and randomly select one of these positions
If all positions within the 3 * 3 square are invalid, the function should randomly select from one of the available positions on the board
When randomly selecting the moves, you need to ensure all identified positions have the same likelihood of being selected
This function should return a tuple of strings which represents the next played position for the computer player, e.g., (“1”, “D”) You will need to invoke functions implemented in previous steps.
9. Play Game
The purpose of this function is to manage all aspects of the game play. You should invoke all functions implemented in previous steps here. Your task is to implement the function play_game():
This function accepts zero parameters.
When the function is invoked, it should display a menu to display all possible options for the user
Once the user selects an option, the program should execute the relevant function(s) or display additional prompts/ask for additional inputs as needed
The user should be able to return to the main menu at any time
When the user input option “1”, the function should:
Ask for a board size value from the user, the program should at least support size values of 9, 13 and 15.
Then, ask for a mode from the user, the mode should be either Player vs. Player or Player vs. Computer
Create a board based on the user specified size
If the user input option “1” while a game is in progress, print the instructional message to ask the user either 1) to reset and restart a game, or 2) to complete the current game
When the user input option “2”, the function should visualise the current state of the board to the user
When the user input option “3”, the function should:
Ask the user to place a stone at a position, the expected input format for the position should be “[row_index] [column_index]” (e.g., “2 F”)
The user should open the game with the black stone (i.e., “●”). You may need to keep track of the turns to determine the colour of stones to be placed next.
Whenever the user successfully places a stone on the board, you should check if any of the players has achieved one of the winning conditions
For Player vs. Player mode, only one stone from the corresponding player will be placed on the board
For Player vs. Computer mode, after playing and checking the player’s move, the computer player should play the move as described in section 3.8 and conduct similar checking of the winning conditions
If an ending condition is achieved (i.e., either one of the players win, or no more move is available), the game needs to automatically print the result (either print the stone value of the winner or inform the player of a draw game). Subsequently, both the board and the mode need to be reset
When the user input option “4”, the function should reset the game (i.e., reset the board and the selected mode)
When the user input option “5”, the function should exit the program. This is the only situation when the program terminates. The program should continue to run infinitely unless the user selects this option.
For this function, you will need to validate the inputs from the users and provide meaningful instructional messages. You will need to ensure a clear logic for the implemented function.