Lab #4
The goal of this assignment is to work with 2-dimensional arrays and to
get more practice with function calls.
NOTE: Spend time writing and revising the C++.
I highly recommend that you take a printout of the C++ and read through
each line making notes about how it will be converted into assembly.
(Then go back through again and revise more).
Spending 2 hours doing a meticulus carfeul conversion could save you ten
hours of debugging!
Lab Report Format & Requirements (What you need to do and what you
need to turn in):
- Program written in any high level language you choose (C, C++, Pascal,
Fortran, Cobol, Lisp, Java, etc.)
Be sure to comment and fully test this version.
- A revised version of the program with comments and notes indicating
how each part will be converted into assembly language.
Relevant info would include: where labels will be created and which registers
will be used and for what purposes. (Do this prior to writing the MIPS version
of the program)
- Program written in MIPS Assembly - This should be well commented
- "Sample Runs" Sample runs should show that your prorgam will enfore
the rules of the game and perform the required fucntions. Comment the output
and explain the test cases.
- Match the structure of the generated output as closely as possible
- Answer the following questions:
- What test cases did you use and why? (What was each case meant to test?)
- What mistakes did you make in writing the program?
- How might you revise/improve the program?
- How long did this assignment take you?
- How long did you spend debugging?
- Were there any special debugging techniques that you used?
- Were there any special development techniques that you used?
The Program:
Summary:
Write a program that will allows 2 people to play the board
game Othello (also called Reversi).
See http://www.othello.nl/guides/rules/spelregelsuk.html
for rules and examples of the game.
We will be creating a new version of the game with the following modifications:
The board can be any even size from 4x4 to 20x20.
(Since we may have more than ten row, allow the user to use letters
('A'-'T') to pick which row and column to put their piece in)
The "initial" board should always have 4 pieces in the center of the board.
Your program must enforce all the rules of Othello and validate all input:
1) Allow the used to pick only a valid board size (an even
number from 4-20)
2) Ensure that when the user enters a row and column that they are on the
board and that they are valid moves: No piece occupies the chosen square
and that placing a piece there will capture at least one piece.
3) Ensure that your program will be able to determine when the game ends
(this can happen two ways: when the board is full or when neither player
can make any moves)
4) Determine who has won the game.
5) When printing the game-board it should display a 'X' in any square that
would be a valid move.
6) Match the structure of the sample runs (below) as closely as possible.
To make this assignment much easier, I've written two key functions for you,
but you won't actually be able to use these functions until your demo. Instead,
I have provided "stubs" that have the same interface as my functions. You
can use the stubs to develop your program (to ensure that you will be able
to pass arguments to my functions):
check_move:
This function checks to see if the player can make a move
in the place given by row and col.
It returns the number of pieces that will be "fliped" by this move. (0 indicates
that it's not a valid move)
int check_move(char *board, int size, int row, int col, char player)
char *board:
a row major order array of characters representing the
board.
'*' represents a black piece and '+' represents a white piece.
An unused square is a null (0)
(If the board size is 6, then this is a 36 element array (6x6))
int size:
Indicates the size of the board. Ex: size=6 means a 6x6
board.
int row:
Indicates the row to place a move in. Ex: row=0 means
the top row. (Row A)
int col:
Indicates the column to place a move in. Ex: col=0 means
the left most column (Column A)
char player:
The player who is wanting to place a move here.
This should be either a '*' or a '+'
Return Value:
This function counts the number of pieces that will be
captures if this move is made. (A return value of 0 indicates that the move
isn't valid
I've provided both a C "stub" function and a MIPS stub function.
Assuming that your program works with these stub functions and follows all
register and stack conventions, it should work with my functions.
flip_pieces:
This function flips all the pieces that are should be changed
as a result of the move just placed at the position given by row and col.
int flip_pieces(char *board, int size, int row, int col)
char *board
a row major order array of characters representing the
board.
'*' represents a black piece and '+' represents a white piece.
An unused square is a null (0)
(If the board size is 6, then this is a 36 element array (6x6))
This must have a new piece in position (row,col).
YOU must put the piece on the board BEFORE calling this function!
int size:
Indicates the size of the board. Ex: size=6 means a 6x6
board.
int row:
Indicates the row of the last move. Ex: row=0 means the
top row. (Row A)
int col:
Indicates the column of the last move. Ex: col=0 means
the left most column (Column A)
I've provided both a C "stub" function and a MIPS stub function.
Assuming that your program works with these stub functions and follows all
register and stack conventions, it should work with my functions.
Suggested Program Structure:
You may write this program in any way that you'd like as long
as it meets the requirements and is able to call check_move and flip_pieces,
but I suggest the following structure:
init_board (~5% of grade):
Reads in and verifies the users choice of board size.
Initializes the board to zeros and white/black pieces in the center.
Returns the size to the main program.
print_board (~20% of grade):
Prints out the game board.
For each square of the board it prints either a '*', a '+', or an 'X' (in
any square that will be a valid move for the current player) or a space.
Get this to print the board first then add support for 'X''s later.
Returns the number of moves that the player may make.
check_board (~10% of grade):
Determines the winner of the game by counting the number
of '*''s and '+''s and prints out the totals and a message indicating the
winner.
main (~50% of grade):
Main loop of the game.
Initializes the board (init_board)
Loops For the entire game (Until board full or neither player can move):
Prints the board (print_board)
If a user can move, get their move and place it
Change player
Check for a winner (check_board)
Extra Credit
There are two extra credit portions of this assignment:
Option 1: +2% (of Overall Grade) Extra Credit
Develop a simple computer opponent to play the game.
Use the check_move function to pick the move that will give the most pieces.
(This is considered a simple "greedy" strategy)
(I would also give an additional 1% for using a 2-level "min-max" strategy)
Option 2: +5% (of Overall Grade) Extra Credit
Write your own versions of check_move and flip_pieces.
These must be proper MIPS functions that follow all stack and register conventions.
They must also use the same arguments (and order of arguments) as my versions.
(This will allow me yo use your versions and you to use my versions)
Hints & Suggestions:
- Start Early
- Plan, Write, Revise.
Plan out your C version, write, and revise it.
Plan out your ASM version from the C version, revise it, and write it.
- Proofread!
Read through your program and look carefully for anyplace where you violate
the stack and register conventions!
Remember that after a function call the $t and $v registers are corrupted.
Ensure that all arguments to functions are initialized properly prior to
the call.
Ensure that you are using the stack correctly (and setup the correct amount
of space for callees)
- Test your fucntions with the stubs provided.
- Use the syscalls for single characters when needed (see Improved Syscall
table on class webpage)
Stub Functions:
C Stub Functions
MIPS Stub Functions
Sample Runs:
Enter board size [4,6,8,...,20]: 2
Enter board size [4,6,8,...,20]: 3
Enter board size [4,6,8,...,20]: 21
Enter board size [4,6,8,...,20]: 22
Enter board size [4,6,8,...,20]: 7
Enter board size [4,6,8,...,20]: 4
ABCD
A X
B +*X
C X*+
D X
+ enter your row: A
+ enter your column: C
ABCD
A X+X
B ++
C *+X
D
* enter your row: A
* enter your column: C
* enter your row: A
* enter your column: E
* enter your row: E
* enter your column: A
* enter your row: A
* enter your column: B
ABCD
A X*+
B X*+
C X*+
D X
+ enter your row: A
+ enter your column: D
+ enter your row: A
+ enter your column: A
ABCD
A +++X
B ++
C *+X
D
* enter your row: C
* enter your column: D
ABCD
A +++
B ++
C ***
D XXXX
+ enter your row: D
+ enter your column: C
ABCD
A +++X
B ++
C *+*
D +
* enter your row: A
* enter your column: D
ABCD
A +++*
B X+*X
C X*+*
D X+
+ enter your row: B
+ enter your column: A
ABCD
A +++*
B ++*
C X++*
D X +
* enter your row: D
* enter your column: A
ABCD
A +++*
B ++*X
C X*+*
D *X+
+ enter your row: B
+ enter your column: D
ABCD
A +++*
B ++++
C *+*
D * +
ABCD
A +++*
B ++++
C X*+*
D *X+X
+ enter your row: D
+ enter your column: D
ABCD
A +++*
B ++++
C *++
D * ++
ABCD
A +++*
B ++++
C X*++
D *X++
+ enter your row: C
+ enter your column: A
ABCD
A +++*
B ++++
C ++++
D * ++
ABCD
A +++*
B ++++
C ++++
D * ++
* has: 2
+ has: 13
+ Wins!