Variables listed as they are encountered in the program
my_scene: Coloring and style of game pieces in the sprites
empty_sprite: Used as a value in a matrix to later display an empty place on the game board
black_sprite: Used as a value in a matrix to later display a black piece in a place on the game board
white_sprite: Used as a value in a matrix to later display a white piece in a place on the game board
board_display: The matrix in which the sprite values are used, initially an 8×8 matrix of ones for an empty board
player_1_type: By user input determines if player 1 (white) is a bot or a player (defaults to player if input is invalid)
player_2_type: By user input determines if player 2 (black) is a bot or a player (defaults to player if input is invalid)
board_full: Used by the function is_board_full to determine whether the game is over after each players’ move
bot_player_type: Used as a value to determine if players were input as bots
player_type: Defines if the players are bots, differentiating between an automatic turn or receiving mouse input
player_color: Defines which player is currently taking a turn
opponent_color: Defines which player is NOT currently taking a turn
row: Horizontal division of the Othello 8×8 game board and matrix (going from top to bottom)
column: Vertical division of the Othello 8×8 game board and matrix (going from left to right)
pieces_to_capture: List of coordinates of the pieces that need to be flipped to the current players’ color upon placing in the selected spot
row_increment: The direction (-1, 0, or 1) in which the row is changing based on the direction given by calc_pieces_to_capture
column_increment: The direction (-1, 0, or 1) in which the column is changing based on the direction given by calc_pieces_to_capture
idx: Index, or measure, of how far in a row from the current piece (being checked for placement) there are opponents’ pieces in a line
capture_count: Amount of points in pieces_to_capture
best_moves: All points where capture_count is maximized
best_moves_capture_count: Highest known capture_count of the placements calculated thus far
move_index: Selects a random point from the best_moves list of points
move: Coordinates of point in move_index
is_valid_move: T/F determines if a selected move is valid (on an empty sprite on the board, capturing at least one piece)
i: Incrementation of pieces when they are being captured
piece: Select piece by coordinated in pieces_to_capture, selected by i
score: Sum of given players’ pieces on the game board
score_white: Player 1’s (white’s) score
score_black: Player 2’s (black’s) score
Code Explanation:
To set up Othello a game board and matrix are created with the addition of starting pieces, and the player types are determined. The game board display (where the program will be playing the game) comes from, a png file containing the graphics for the game pieces and empty spots, set to be my_scene. my_scene is later used as pieces are placed, captured, and counted throughout the game. Values are then set for the types of sprites, and an 8×8 matrix of ones, or empty sprites, is created as the board display. Four starting pieces are then added to the center of the board, two of each white and black, alternating in a checkered pattern. The two player types are then determined, defaulting to being people, but the user has the option to choose which players are bots.
The base operation of the game occurs in the main Othello code, alternating between player 1 (white) and player 2 (black) as they take their turns. The function is_board_full is called upon to stop gameplay after any turn where the board is full. Within a players’ turn the placement of their piece and capturing of any opponents’ piece is done within the function take_turn. The purpose of the main code is to pass on player_color and opponent_color, as they alternate between white and black, depending on whose turn it is. After defining variables necessary for the code to determine player types and colors, these variables are passed to several seeded functions, listed below. These functions calculate if the board is full, if a player has a valid move, what pieces can be captured by a certain move, as well as capturing pieces according to said certain move and calculating how many of each players’ piece is on the board. Once the board is full, signifying the end of the game, the scores of both players are calculated and the winner is output along with both scores.
Functions listed as they are encountered in the program
- [is_board_full]: Found in Othello source code
- Checks all places on the board, only returning as true (1) when there are no places on the board are equal to empty_sprite
- [take_turn]: Found in Othello source code
- Based on player_type, passes variables to bot_move and player_move, using the output of the used functions to add onto board_display and the game board
- [bot_move]: Found in take_turn
- Checks every possible placement on the board, putting those that capture the most opponent pieces into an array and choosing a random one of the moves in said array as the bots’ move
- If no pieces can be captured, bot_move passes the turn
- [calc_pieces_to_capture]: Found in bot_move
- Gives check_direction the coordinates of all eight cardinal and intermediate directions, adding any returned values to an array of pieces, which accumulates as all directions are checked
- [check_direction]: Found in calc_pieces_to_capture
- Uses the direction given to check for opponent pieces, returning a list of these pieces
- [player_move]: Found in take_turn
- Takes mouse input and either places a piece on the clicked spot (capturing corresponding pieces) or gives a reason why placement is invalid and continues accepting mouse clicks
- [capture_pieces]: Found in take_turn
- Given an array from pieces_to_capture based off of the current players’ move, changes all pieces in the array to the players’ color
- [calculate_score]: Found in Othello source code
- Adds up all the pieces on the board of given players’ color and returns the sum value