Available: Available: Thu, Nov 3
Due: Sat, November 19, extended till Tue Nov 22,
22:00.
This assignment is to be done IN GROUPS OF TWO STUDENTS. Only one student will submit, but be sure to include the names and student numbers of both students in the files. Follow the instructions in the lab manual for submitting assignments through the Virtual campus. The following are specific instructions for this assignment:
· Question 1 should be answered in a Word file A4Q1.doc. Also provide the Java file A4Q1.java. Past the content of the java file into the Word file A4Q1.doc, after you added the traces for Q1b.
· Question 2 should be answered in a Java File A4Q2BoardLib.java. Paste the content of the java file into the Word file A4Q2.doc. A template has been provided for A4Q2BoardLib.java. Also provided is the complete file A4Q2TTT.java; please do not submit this file as part of your assignment. Inserting the Java source code into Word files will allow the marker to make comments on the source code in the Word file.
· Zip all the .doc, .java, and .class files in a4_xxxxxx.zip, where xxxxxx is your student number (for one of the two students), and submit it through the Virtual Campus).
Question 1a.
Write a recursive algorithm that converts a decimal number D in base 10 (between 0 and 1023) to a binary number B in base 2. A base 10 number uses the ten digits 0, 1, …, 9, while a base 2 number uses only the two digits 0 and 1.
For example:
D: 29
B: 11101
(since 1*24 + 1*23 + 1*2 2 + 0*21
+ 1*20 = 29)
D: 999
B: 1111100111
Question 1b. Trace your algorithm for D = 13
Question 1c. Translate your algorithm into a recursive Java method (in a class A4Q1). Test your method using the
Junit tests from A4Q1test.java. Use the Test button
in DrJava, not the Run button. Do not submit the tests. You can add more tests
for yourself.
It’s time
to play a game. You are asked to
complete a Tic Tac Toe program. Here are some guidelines.
A
Tic-Tac-Toe software program has partially been developed and provided in two
files: A4Q2TTT.java (which is complete) and A4Q2BoardLib.java (which you will need to
complete). The software is broken down into the following sub-programs:
1) Overall control (main() method) consists of the following steps.
a. Asking the user to start a game, if
response is not (y or Y), then terminate the program. The game is played as
follows:
i. Clear the board (implemented as
method void clearBoard(char [][] board)).
ii. Print the board (implemented as
method void printBoard(char [][] board)).
iii. Make a move (implemented as method void play(char [][] board, char player) – this includes prompting the user for a new
position on board for the next move that is played).
iv. Check for win or draw (implemented
as method boolean checkWinner(char [][] board)).
v. If the game is not completed, do
another move (go to step iii).
vi. Note that the board matrix to
represent the game board is created in the main method and its reference is passed
to other methods.
b. After each game, ask to start
another game (i.e. go back to ‘a’).
2) Checking for a win or draw is
divided into the following sub-tasks (that is, the method checkWinner
calls the methods identified below).
a. Check rows for win (implemented as
method char testRows(char [][] board)).
b. Check cols for win (implemented as
method char testCols(char [][] board)).
c. Check diagonals for win (implemented
as method char testDiag(char [][] board)).
d. Check for draw (implemented as
method boolean testDraw(char [][] board)).
Note: a
row, a column, or a diagonal completed with the three ‘X’s or with three ‘O’
means that we have a winner.
See the appendix for a sample of output.
The
following methods have been collected together into a Library, the class A4Q2BoardLib
(file A4Q2BoardLib.java):
·
Clear
the board (void clearBoard(char [][] board)).
·
Check
for win or draw (boolean checkWinner(char [][] board)).
·
Check
rows for win (char testRows(char [][] board)).
·
Check
cols for win (char testCols(char [][] board)).
·
Check
diagonals for win (char testDiag(char [][] board)).
·
Check
for draw (boolean testDraw(char [][] board)).
Your assignment is to complete the methods in A4Q2BoardLib.java.
Notes:
1) The method checkWinner displays the
message “Game is a draw” instead of “Player X has won” when a draw is detected.
2) Note that the coordinates are
entered on the same line. You will read in the values as an integer array using
ITI1120.readIntLine() method.
3)
You
will be using many if statements in your program. Most of the statements will
have an empty else section. The else portion of the if statement
need not be present. In the interest of
cleaner code, you may omit the else section of the if statement, but indicate
this using a comment as follows
if
(condition)
{
// <statements for when
condition is true>
} // no else – do nothing
4) Do not be intimidated by the number
of methods to create. Most of these
methods are very short and similar. For
example there is not much difference between checking for winning columns and
winning rows.
5) The testRows, testCols, and testDiag, return one of ‘-‘, ‘X’ or ‘O’
characters. If a hyphen is returned then
no winner was found, otherwise the character corresponding to the winner will
be returned.
6) Again, you are not asked to provide
algorithm models for your methods. It is
advisable that you draft out algorithms before coding, particularly because the
logic of this exercise can be challenging.
7) Documentation for each of the methods
is available in the provided template.
Please review for additional information.
8) Please submit only the
A4Q2BoardLib.java file; that is, do not make any changes to A4Q2TTT.java (this latter file is complete).
Note: You will get 5 bonus points if you implement testRows or testCols with a recursive method, without a loop!
Appendix – sample output
Start a new game (Y
or N): y
0 1 2
0 - - -
1 - - -
2 - - -
Player X, please
enter row and col between 0 and 2:
1 1
0 1 2
0 - - -
1 - X -
2 - - -
Player O, please
enter row and col between 0 and 2:
0 0
0 1 2
0 O - -
1 - X -
2 - - -
Player X, please
enter row and col between 0 and 2:
1
Player X, please
enter row and col between 0 and 2:
1 3
Player X, please
enter row and col between 0 and 2:
2 2
0 1 2
0 O - -
1 - X -
2 - - X
Player O, please
enter row and col between 0 and 2:
0 0
Space at 0 0 is
occupied
Player O, please
enter row and col between 0 and 2:
0 2
0 1 2
0 O - O
1 - X -
2 - - X
Player X, please
enter row and col between 0 and 2:
1 0
0 1 2
0 O - O
1 X X -
2 - - X
Player O, please
enter row and col between 0 and 2:
0 1
Player O has won
0 1 2
0 O O O
1 X X -
2 - - X
Start a new game (Y
or N): N