Lab 7
Lab 7
Lab #7: Reversi Game — Board Configuration and Move Legality Checking
Winter 2023
This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation
APS 105 — Computer Fundamentals
for a board-type game Labcalled Game (also
Reversi
#6: Reversi – Boardcalled Othello).
Configuration and The
Movegoal of this
Legality lab is to write code that
Checking
Fall 2015
sets up the input and checks the legality of moves in this game. These two labs make use of two-
dimensional arrays, as well as all of the previous material covered before two-dimensional arrays in
The goal of this lab is to write code that sets up the input and does move legality checking
this course. They require some careful thinking about how to convert human-thinking into working
for a board-type game called Reversi (also called Othello). It makes use of two-dimensional
software. Please read
arrays, andthrough this lab
all of the previous handout
material coveredcarefully.
in this course, and requires some careful
thinking about how to convert human-thinking into working software. The relevant ma-
terial necessary for this lab can be found in the Carter text up to the end of Chapter 6.
Objective
Lab 6 will be due in the week of November 2.
The goal of this lab is to write a program that will be used (in Lab 8) as part of a Reversi game,
Objective
as well as a little bit of- Part of the Codecode
the ‘thinking’ for a Reversi Game
that will be used in that lab to have the computer play
against a human opponent.
The goal of this lab is to write a program that will be used (in Lab 7) as part of a Reversi
Here is a briefgame, as well asof
description a little bit ofReversi
the full the ’thinking’
game. code that willisbeplayed
Reversi used in onthatalab to have
board the a chess board
(like
computer play against a human opponent.
or a checkers board) that has dimensions n × n, where n is even. In the picture below n = 4. The
Here is a brief description of the full Reversi game. Reversi is played on a board (like a
game uses tiles that
chess areorwhite
board on one
a checkers board) side, anddimensions
that has black onnthe ⇥ n, other
where nside (they
is even. can
In the be “flipped” over
picture
to change theirbelow n = 4. The
colour). Onegame uses plays
player tiles that are white
white; theonother
one side, and black
player playsonblack.
the otherThe
side picture below
(they can be “flipped” over to change their colour). One player plays white; the other
shows the initial board configuration, which has two white and two black tiles placed in advance at
player plays black. The picture below shows the initial board configuration, which has
the centre. Observe that
two white androws and tiles
two black columns areinlabelled
pre-placed with
the centre. letters.
Observe that rows and columns are
labelled with letters.
!" #" $" %"
!"
#"
$"
%"
A “turn” consists of a player laying a tile of his/her own colour on a candidate empty
board position, subject to the following two rules:
Figure 1: Starting positions on the Reversi game board.
1. There must be a continuous straight line of tile(s) of the opponent’s colour in at least
one of the eight directions from the candidate empty position (North, South, East,
A “turn” consists of a player
West, laying a tile of his/her own colour on a candidate empty board position,
and diagonals).
subject to the following two rules:
2. In the position immediately following the continuous straight line mentioned in #1
above, a tile of the player’s colour must already be placed.
1. There must be a continuous straight line of tile(s) of the opponent’s colour in at least one of the
After playing a tile at a position that meets the above critera, all of the lines of the oppo-
eight directions
nent’s tiles from thethe
that meet candidate empty
criteria above position
are flipped (North,
to the player’sSouth,
colour. East, West, and diagonals).
In the picture below, all of the candidate positions for White’s next move are shown shaded.
2. In the position immediately following the continuous straight line mentioned in #1 above, a
tile of the player’s colour must already be placed.
After playing a tile at a position that meets the above critera, all of the lines of the opponent’s tiles
that meet the criteria above are flipped to the player’s colour.
In the picture below, all of the candidate positions for White’s next move are shown shaded.
!" #" $" %"
!"
!"
$"
#"
%" !" #" $" %"
!"
$"
If the White player decides to play at row c, column a, the Black tile at row c, column b is
flipped and the2:
Figure board
All looks
of thelikecandidate
this: positions for White’s next move.
#" !" #" $" %"
%"
If the White player decides to play !"at row c, column a, the Black tile at row c, column b is flipped
If the
and the board White
looks like this:decides $"to play at row c, column a, the Black tile at row c, column b is
player
flipped and the board looks like this:
#"
!" #" $" %"
%"
!"
$"
If the White player decides to play at row c, column a, the Black tile at row c, column b is
flipped and the board looks #"like this:
%"
!" #" $" %"
!"
$" possible move positions for the Black player:
The picture below shows the
!" #" $" %"
#"
%"!"
The picture below shows the$"#"possible move positions for the Black player:
!"
Figure 3: White #"
plays at $"row c,%"column a.
!"$"
%"
The picture below shows the possible move positions for the Black player:
The picture below shows the#"%"possible move positions for the Black player:
!" #" $" %"
#"
%"
If the Black player lays a tile$"at b, a, the board appears like this:
%"
2
If the Black player lays a tile at b, a, the board appears like this:
Figure 4: All of the candidate positions for Black’s next move after White plays at (c, a).
If the Black player lays a tile at (b, a), the board appears like this:
2
2
!" #" $" %"
!"
#"
%"
#"
Finally, if the White player lays a tile at a, c the board appears like this:
$" !" #" $" %"
Figure 5: Black plays at (b, a).
!"
%"
Finally, if the White player lays a tile at (a, c) the board appears like this:
#"
Finally, if the White player lays a tile at a, c the board appears like this:
!" #" $" %"
$"
!"
%"
#"
Note that in White’s move, two lines of Black tiles were flipped: the line directly to the
South, and the line to the South
$" West.
The turns alternate between the players, unless one player has no available move, in which
case the only player with an available move is allowed to continue to make moves until a
%"
move becomes available for the opponent, at which point, the opponent is allowed to take
a turn and the alternating turns between the players resumes. The game ends when either:
Note
1) the that inboard
entire White’s move,
is full, two
or 2) lines player
neither of Black tiles
has were flipped:
an available move.the line directly to the
South, and the line to the South West.
For this lab, you will implement part of the game-playing functionality. You will complete
Figure 6: White responds to Black by playing at (a, c).
Thegame
the turns inalternate between
Lab 7, using thethe players, unless
functionality one player
you have built inhas nolab,
this available move,
so please be in which
mindful
case
to the clean
build only player with ancode
and re-usable available
for thismove
lab!is allowed to continue to make moves until a
move
Note that in For
White’s
thisbecomes
lab, youavailable
move, two
will forathe
lines
write Cof opponent,
Blackthat
program at will
which
tiles dopoint,
were the opponent
flipped:
the following:the(Note
lineisthat
allowed
directly tototake
the specificthe South, and
a turn and the alternating turns between the players resumes. The game ends when either:
the line to thedetails
South of input
West. and output will be given in the example runs below this section)
1) the entire board is full, or 2) neither player has an available move.
1. The first input to the program will be n, giving the size of the n ⇥ n board. You may
For this lab,
The turns alternate you will
between implement
the players, part of theone
unless game-playing functionality. You will complete
assume that the size of n will be even andplayer has
will never nolarger
be available
than 26,move, in which case the
and should
the game in Lab 7, using the functionality you have built in this lab, so please be mindful
only player with declare a static 2-dimensional
an available move is allowed array. to Your programto
continue should
makeinitialize
movesthe board
until as
a move becomes
to build clean and re-usable code for this lab!
shown above and print it.
available forForthethisopponent. At this point, the opponent is allowed to take
lab, you will write a C program that will do the following: (Note that the specific
a turn and the alternating
turns between 2.
theThe
details of next sequence
players
input resumes.
and of will
output inputs
Thebe will
game
given describe
in ends a when
board runs
the example configuration,
either:
below1)this representing
the entire board
section) a sit- is full, or 2)
uation part-way through a game of Reversi. Each line of input will consist of three
neither player has anfirst
The available
1. characters input move.
withtonothe program
spaces will be n,
in between. giving
The the size ofwill
first character theben⇥ n board.B You
a colour: or W;may
the
second character willof
assume that the size willrow
benthe be (a-z);
even and will never
the third be larger
character than
will be the26, and should
column (a-z).
declare
The three a characters
static 2-dimensional
represent a array. Your
tile of the program
specified should
colour initialize
placed the board
at the specified as
row
For lab 7, you will shown
and above The
implement
column. andpart
printofit.the game-playing
three-character sequence !!! functionality. You will complete
ends the board configuration entry the game
in Lab 8, using2.the
Thefunctionality you
next sequence of havewill
inputs built in this
describe lab, configuration,
a board so please berepresenting
mindful toa sit-
build clean and
re-usable code foruation 3
part-way through a game of Reversi.
this lab. Each line of input will consist of three
characters with no spaces in between. The first character will be a colour: B or W; the
You will write a Csecond character
program thatwill be the
will do row
the (a-z); the third(Note
following: character
thatwill
thebespecific
the column (a-z). of input and
details
The three characters represent a tile of the specified colour placed at the specified row
output will be given in the example runs below this section.)
and column. The three-character sequence !!! ends the board configuration entry
2. The next sequence of inputs will describe a board configuration, representing a situation part-
way through a game of Reversi. Each line of input will consist of three characters with no
spaces in between. The first character will be a colour: B or W; the second character will be the
3
row (a — z); the third character will be the column (a — z). The three characters represent
a tile of the specified colour placed at the specified row and column. The three-character
sequence !!! ends the board configuration entry phase. Character arithmetic can be used to
translate the rows/columns into array indices, e.g. 'b' - 'a' equals 1. Note: your program
should not check for move legality during this phase. This phase is simply to input an
intermediate board configuration.
3. Then, your program should print a list of the available moves for the White player, followed
by a list of the available moves for the Black player, given the board configuration input in the
previous step. The available moves for each player should be printed in the order of increasing
rows, then in the order of increasing columns (for available moves in the same row).
4. Next, your program should ask the user to input a move, represented in the same three-
character format. Your program should check if the move is valid, and if so, make the move,
flipping the tiles correspondingly. If the move is invalid, your program should indicate so.
5. Your program should print the final board configuration and terminate.
Your program must use the following characters to represent the state of each board position:
U - for unoccupied
B - occupied by black
W - occupied by white
For example, after the entire board shown in the last figure is entered, it would be printed as follows:
abcd
a UUWU
b BWWU
c WWWU
d UUUU
To print the board, your program should contain a function with the following prototype:
where board is the 2D array representing the current board state, and n is the board dimensions.
Here is an example execution of the program:
4
b BWBU
c WBWU
d UUUU
Available moves for W:
aa
bd
db
Available moves for B:
ab
cd
da
dc
Enter a move:
Wdb
Valid move.
abcd
a UUBU
b BWBU
c WWWU
d UWUU
Here is another example execution of the program where the final move is invalid:
5
Available moves for B:
ba
bc
ca
db
df
ed
ef
Enter a move:
Bbe
Invalid move.
abcdef
a UUUBUU
b UUUBUU
c UWWBUU
d UUBWWU
e UUUUUU
f UUUUUU
We strongly encourage you to break up your program into separate functions, and to carefully test
each function separately, before connecting it into the larger program. To help with this, you are
required to create the following helper functions and use them in your implementation:
which checks whether the specified (row, col) lies within the board dimensions.
It is very error prone to write separate code to check each of the eight possible line directions that
begin at a given tile. To simplify this, you are required to write and use the following function:
which checks whether (row, col) is a legal position for a tile of colour by “looking” in the direction
specified by deltaRow and deltaCol. deltaRow and deltaCol take on values of -1, 0, and 1, with
the restriction that they cannot both be 0. For example, if deltaRow = 1 and deltaCol = 0, the
function searches the South line. If deltaRow = -1 and deltaCol = 1, the function searches the
Northeast line. The idea is that, elsewhere in your program, you will call the helper function 8 times
to search the lines in the 8 possible directions.
Starting Point
You are provided with two source files: reversi.h and reversi.c, as your starting point. You
need to put these two files under the same folder for the C/C++ runner extension to compile.
When you submit your program on examify.ca, Please comment out the line where the header file
is included as shown here:
Before:
6
...
#include "reversi.h"
...
After:
...
// #include "reversi.h"
...
Marking
There are a total of 10 marks available in this lab, marked as usual on examify.ca. The public
tests on examify.ca do not test all the possible cases of input for your program. If your program
works using the public test cases on examify.ca, it does not mean that you will get full marks for
your program. You are responsible for testing your program using all the cases you can think of,
according to the specification given in writing in this lab handout.
To help you complete your Lab 7 and Lab 8, the APS 105 teaching team developed an online
interactive platform located at http://aps105.ece.utoronto.ca:8090/. For the purpose of Lab
7, the platform can help you familiarize with the rules in this game. The front-end of this website has
been released as open-source, available at https://github.com/Louis-He/reversi-reactjs.