Code Overview - WayKoi/Othello GitHub Wiki

Written by Kyle Findlay

This code is a terminal implementation of the board game Othello, the rules of which can be found Here.

The program contains 6 different classes and an enum type

Note: Some classes are described further below

Name Description
AI This contains a couple of static functions that implement a computer player
App This class contains main and is responsible for all terminal prints and terminal interaction
Board This class contains the actual data structure representing the game board and implements the logic for finding possible moves on the board
Game The Game class implements the actual rules for Othello as well as saving and loading of a game
Move This is a data structure that contains move data:
  • Positon to place the piece
  • Colour of the piece being placed
  • An ArrayList of all the pieces that get flipped once the piece is placed
Pos This is a simple data structure that represents positions on the board. It simply contains an (x, y) position on the board
Space This is an enum type that denotes what is on a space on the board. the types are
  • None
  • Black
  • White

Board

Methods

Name Inputs Description Returns
GetReverse
  • Space rev
Reverses Space.White to Space.Black and vice versa. Leaves Space.None the same The reversed Space type
ReplacePiece
  • Space type
  • int x
  • int y
Replaces a piece at position (x, y) with type
PlayMove
  • Move move
Follows the instructions in move to update the board
GetPossibleMoves
  • Space Colour
Gets the list of all possible moves for the player of the colour Colour An ArrayList<Move> containing all the possible moves for the given colour
GetFlips
  • Space colour
  • int x
  • int y
Uses the colour and the position given to calculate all the flips that would happen if the piece was placed in the position given An ArrayList<Pos> that contains all spaces that would be flipped as a result of placing the piece in the given position
LookInDirection
  • ArrayList<Pos> flips
  • Space colour
  • Space look
  • int startx
  • int starty
  • int changex
  • int changey
Calculates the flips that would occur if the piece was placed in the start location (startx, starty) in the direction (changex, changey) and appends them to flips
CountSpaces
  • Space Colour
Counts all the spaces of a given colour on the board The amount of spaces of that colour on the board
toString Generates a string representation of the board where:
  • '.' means None
  • 'b' means Black
  • 'w' means White

Additional Notes

  • The board class creates a two dimensional array of size [BoardSize + 2, BoardSize + 2] this is to make the other functions of the Board class not go out of bounds while calculating flips
  • The outer edge of the board should always be empty

Game

Methods

Name Inputs Description Returns
GetPossiblePositions Gets the possible moves for the current turn player An ArrayList<Pos> of all the possible positions the current turn player can place a piece
GetBoardString A string representation of the board
PlacePiece
  • Pos position
Checks if the position given is possible for the current turn player and places the piece if it is. Also updates the board accordingly (flips pieces) true if the piece was placed, false if not
MakeAIMove Asks the AI for the move it would choose out of the possible moves and updates the board
SwitchTurn Switches the turn player and checks if the player is forced to pass. Also checks if the game is over
SaveToFile
  • String path
Saves the current game to a file at the path given. If no file exists it creates one and if one already exists it overwrites the file true if the save was successful and false if it was not
LoadBoard
  • String path
Loads a game from a file located at path true if the file was loaded correctly and false if it was not
CalculateWinner Counts all the pieces on the board and updates the Winner
⚠️ **GitHub.com Fallback** ⚠️