Othello
Main.m
Description:
- Application entry-point for end-users. Plays one game of Othello.
Variables:
-
playerNames
:- Cell array of player names.
- Used as an argument for the constructor of
Othello
.
-
othello
:- Object of the
Othello
class. - Used to play a round Othello.
- Object of the
@Othello/Othello.m
Description:
- Classdef of
Othello
. - Subclass of
Game
.
Properties:
-
Board
:- Object of the
BoardWrapper
class. - Private.
- Provides one Othello board.
- Handles all board interactions.
- Object of the
-
PlayerWantsToQuit
- Whether or not a player chose to quit.
- Private.
- False by default.
- Set to true when the figure of the board is closed.
- If true, the game will end as quickly as possible without determining a winner.
-
NoPlayersCanMove
- Whether or not the players have available moves.
- Private.
- False by default.
- Set to true when all players have no available moves.
- If true, the game will end and a winner will be determined.
-
PlayerNames
- Cell array containing the names of the players.
- Private.
- Value provided from the constructor.
-
INPUT_DELAY
- Delay in seconds for user input to be queried.
- Private, constant.
- 0.02 by default.
Methods:
-
obj = Othello(playerNames)
Description:
- Constructor.
- Public.
Parameters:
-
playerNames
- Names of players.
- Cell array.
Returns:
-
obj
- The class object.
-
Close(obj)
Description:
- Closes the board figure.
- Public.
- Should be called after the game ends.
-
Initialize(obj)
Description:
- Instantiates
obj.Board
with an object of classBoardWrapper
. - Protected.
- Instantiates
-
move = GetPlayerMoveCoordinates(obj)
Description:
- Get the coordinates of the move specified by the player. If the figure window is closed (i.e. the player chose to quit),
moveCoordinates
is -1. - Protected.
Returns:
-
moveCoordinates
- Coordinates of the move specified by the player.
- If the figure window is closed (i.e. the player chose to quit), the value is -1.
- Get the coordinates of the move specified by the player. If the figure window is closed (i.e. the player chose to quit),
-
MakePlayerMove(obj)
Description:
- Make the player specify a legal move, then execute it.
- Protected.
-
DisplayStatus(obj)
Description:
- Display the current status of the game.
- Currently it does nothing, since no additional information needs to be displayed.
- Protected.
-
result = IsGameOver(obj)
Description:
- Determines whether or not the game is over.
- Protected.
Returns:
-
result
- Whether or not the game is over.
-
player = GetWinningPlayer(obj)
Description:
- Gets the winning player.
- Protected.
Returns:
-
player
- -1 if a player deliberately quits before the game ends naturally.
- 0 if there is a tie.
- Otherwise, it’s equal to the player turn of the winner.
-
AnnounceWinner(obj, winner)
Description:
- Announce the winner in a dialog box.
- Protected.
Parameters:
-
winner
- The player turn of the winner.
-
[result, auxiliary] = IsLegalMoveCoordinate(obj, coordinate)
Description:
- Determine whether placing a piece belonging to the player (specified by
obj.PlayerTurn
) atcoordinate
on the board is a legal move. - Private.
Parameters:
-
coordinate
- The coordinate of the desired move
Returns:
-
result
- Whether placing a piece belonging to the player specified by
obj.PlayerTurn
atcoordinate
on the board is a legal move.
- Whether placing a piece belonging to the player specified by
-
auxiliary
-
Struct with the following values:
-
flankEndpoints
- Flank endpoints of the first legal move found.
- This value should only be used when
result
is true.
-
-
- Determine whether placing a piece belonging to the player (specified by
-
[result, auxiliary] = CanPlayerMove(obj)
Description:
- Determine whether the player has available moves.
Returns:
-
result
- Whether the player has available moves.
-
auxiliary
-
Struct with the following values:
-
coordinate
- The coordinate of the first legal move found.
- This value should only be used when
result
is true.
-
flankEndpoints
- Flank endpoints of the first legal move found.
- This comes from
IsLegalCoordinate
. - This value should only be used when
result
is true.
-
- Can be used as an optimization if the player happens to select the same coordinate that was determined to be legal.
-
-
[result, auxiliary] = CanPlayersMove(obj)
Description:
- Determine whether the players have available moves.
Returns:
-
result
- Vector containing booleans for each player.
- Each boolean contains the result of
CanPlayerMove
for each player.
-
auxiliary
- Struct containing the same auxiliary info from
CanPlayerMove
for the current player.
- Struct containing the same auxiliary info from
@Game/Game.m
Description:
- Classdef of
Game
. - Abstract class.
- Handle class.
- Sueprclass of
Othello
.
Properties:
-
AMOUNT_OF_PLAYERS
- Number of players.
- Public, constant.
- Default value is 2.
-
PlayerTurn
- The player who currently has the turn.
- Protected.
- Default value is 1.
Methods:
-
winner = Play(obj)
Description:
- Start playing the game.
- Public.
Returns:
-
winner
- The winner of the game. See
GetWinningPlayer
.
- The winner of the game. See
-
Initialize(obj)
Description:
- Does nothing.
- Protected.
-
MakePlayerMove(obj)
Description:
- Does nothing.
- Protected.
-
SetNextPlayerTurn(obj)
Description:
- Sets the turn of the next player.
- Protected.
-
AnnounceWinner(obj, winner)
Description:
- Does nothing.
- Protected.
Parameters:
-
winner
- The winner of the game. See
GetWinningPlayer
.
- The winner of the game. See
-
DisplayStatus(obj)
Description:
- Displays the status of the game.
- Protected, abstract.
-
result = IsGameOver(obj)
Description:
- Determines whether or not the game is over.
- Protected, abstract.
Returns:
-
result
- Whether or not the game is over.
-
player = GetWinningPlayer(obj)
Description:
- Gets the winning player.
- Protected, abstract.
Returns:
-
player
- -1 if a player deliberately quits before the game ends naturally.
- 0 if there is a tie.
- Otherwise, it’s equal to the player turn of the winner.
+Othello/@BoardWrapper/BoardWrapper.m
Description:
- Classdef of
BoardWrapper
. - Provides one Othello board.
- Handle class.
Properties:
-
FigureHandle
- Handle to the figure showing the board.
- Public.
-
ImageHandle
- Handle to the image showing the board.
- Public.
-
EMPTY_PIECE
- Representation of an empty piece in
BoardModel
. - Public, constant.
- Default value is 0.
- Representation of an empty piece in
-
NUM_SPACES_AXIS
- Number of spaces along the X and Y axes of the board.
- Private, constant.
- Default value is 8.
-
SPACE_WIDTH
- Width in pixels of a space on the board
- Private, constant.
- Default value is 20.
-
BORDER_WIDTH
- Width in pixels of a border on the board
- Private, constant.
- Default value is 2.
-
AXES_OFFSET
- Pixel amount that the axes are offset from the image. This is just a guess, but it seems like it works.
- Private, constant.
- Default value is
[0.5, 0.5]
.
-
BOARD_SPACE_COLOR
- Color of a space on the board.
- RGB triplet.
- Private, constant.
- Default value is
[0.35, 0.65, 0.7]
.
-
BOARD_BORDER_COLOR
- Color of a border on the board.
- RGB triplet.
- Private, constant.
- Default value is
[0.05, 0.05, 0.05]
.
-
PIECE_COLORS
- Color abbreviations of plot markers for the players.
- Private, constant.
- Default value is
['k', 'w']
.
-
BoardModel
- Model of the board depicted as a simple 2D matrix.
- Private.
-
FlankSteps
- Pre-calculated translational vectors (steps) for the possible flanking paths.
- Private.
Methods:
-
obj = BoardWrapper()
Description:
- Constructor.
- Public.
-
Code(obj)
Description:
- Closes the board figure.
- Public.
-
boardSize = GetBoardSize(obj)
- Returns the size of the board in spaces.
- Public.
Returns:
-
boardSize
- The size of the board in spaces.
-
SetSpaceWithFlanks(obj, coordinate, flankEndpoints, playerPiece)
Description:
- Set the space and the fill the flanks.
- Public.
Parameters:
-
coordinate
- The coordinate of the piece.
-
flankEndpoints
- The flank endpoints around the piece.
-
playerPiece
- The player who owns the piece.
-
piece = GetSpace(obj, coordinate)
Description:
- Returns the piece on the space with the given coordinate.
- Public.
Parameters:
-
coordinate
- The coordinate of the space.
-
GetSpaceCoordinatesOnClick(obj, src, ~)
Description:
- Callback function when the board image is clicked.
- Returns the coordinates of the space clicked.
- The return information is transfered via
guidata
. - Public.
Parameters:
-
src
- The handle to the element triggering the callback.
-
[result, auxiliary] = IsFlankingMove(obj, coordinate, playerPiece)
Description:
- Determines whether the given coordinate is a flanking move.
- Public.
Parameters:
-
coordinate
- The coordinate of the piece.
-
playerPiece
- The player who owns the piece.
Returns:
-
result
- Whether or not it is a flanking move.
-
auxiliary
- Contains the flank endpoints.
-
result = IsOnBoard(obj, coordinate)
Description:
- Determines whether or not the given coordinate is on the board.
- Public.
Parameters:
-
coordinate
- Whether or not the given coordinate is on the board.
-
count = CountSpacesWithPiece(obj, piece)
Description:
- Counts the spaces with a given piece.
- Public.
Parameters:
-
piece
- The piece to count.
Returns:
-
count
- The number of spaces with the given piece.
-
InitializeBoard(obj)
Description:
- Initialize and display the board figure and image.
- Private.
-
SetStartingPieces(obj)
Description:
- Place the starting pieces on the board.
-
SetSpace(obj, coordinate, piece)
Description:
- Place a piece on the space with the given coordinate.
- Private.
Parameters:
-
coordinate
- The coordinate of the space.
-
piece
- The piece to be placed.
-
MakeBoardModel(obj)
Description:
- Initialize the board model.
- Private.
-
flankEndpoints = GetEndpointsOfFlanks(obj, coordinate, playerPiece)
Description:
- Gets endpoints of the flanks around the space with a given coordinate.
- Private.
Parameters:
-
coordinate
- The coordinate of the space.
-
playerPiece
- The player who owns the piece.
Returns:
-
flankEndpoints
- The endpoints of the flanks.
-
FillFlanks(obj, coordinate, flankEndpoints, playerPiece)
Description:
- Fill the flanks around the space on the coordinate.
- Private.
Parameters:
-
coordinate
- The coordinate of the space.
-
flankEndpoints
- The flank endpoints.
-
playerPiece
- The player who owns the piece.
-
MakeFlankSteps(obj)
Description:
- Generate the pre-calculated translational vectors for the possible flanking paths.
- Private.