CSIS-120 Project 5: Tic-Tac-Tie Fall 2010 Due Monday, December

CSIS-120 Project 5: Tic-Tac-Tie
Fall 2010
Due Monday, December 13, before midnight
Procedures



Submit your .java files through the assignment manager on BlackBoard. I will
generate a JavaDoc from your source code, which will be graded as part of
programming style.
This homework is an individual assignment. No collaboration on answers is
allowed. Failure to heed this instruction will constitute a violation of the
academic honesty policy in the syllabus.
Your code must follow the basic conventions of programming style noted in
lecture and in your lab manual.
Grading Scheme:
25%
15%
15%
15%
15%
15%
Overall correctness/completeness of game play
Program style including JavaDoc
Correctness of game scoring
Overall efficiency of implementation
Quality of your class design
Quality of your output including user friendliness and error messages
Overview
Tic-Tac-Tie is a variation of Tic-Tac-Toe that uses a N X N grid and a scoring system
to break ties.
To win, a player must get N-in-a-row horizontally, vertically, or diagonally. Note
that “row” really means any of the three directions (horizontal, vertical, or
diagonal). In the event of a tie, which is very common, scoring rules are used to
determine the winner. The player with the highest score wins.
Scoring Rules:
General Rule: Every time a player places their mark (X or O) on the grid, they get
points for scoring 2 or more in a row. Specifically, they get two points for 2-in-a-row,
three points for 3-in-a-row, four points for 4-in-a-row, etc. More generally, the
player gets M points for M-in-a-row, where M must be greater than 1. Note that the
first player to get N-in-a-row (N is the grid size) wins automatically regardless of the
final score.
1
Rule 1 (multi-scoring): A player gets M points for every new M-in-a-row that their
pick creates. In the example grid below, placing an O in the location marked with ?
below would create three 2-in-a-rows for a total of 6 points.
X
X
O
O ?
X
O
In the example grid below, placing an O in the location marked with ? below would
create two 3-in-rows for 6 points and three 2-in-a-rows for 6 more points (12 points
total for one pick).
X
O3
X X
O1 O2 X
O4 ?6
X
O5
The subscripts indicate the order in which each O was placed. Note that the two 3in-a-rows have the subscripts 6-4-3 and 1-6-5 and the three 2-in-a-rows are 6-1, 6-2,
and 6-5.
Read about the next rules to understand why the other 2-in-a-rows were not scored
for this pick.
Rule 2 (no duplicate scoring): An important rule is that you cannot count any Min-a-row that was already scored in a previous move. In the example above,
subscripts 1-2, 1-4, 1-3, 2-4, and 3-4 were already scored in previous moves.
Rule 3 (no directional sub-chain scoring): You cannot count any M-in-a-row if it is
part of a directional chain of M+1 in-a-row. In the example above, we do not score 64 as a 2-in-a-row because it is part of a directional chain (6-4-3) of 3-in-a-row.
Rule 4 (split scoring): But, you must score sub-chains if they are split by the
picked location. For example, we count 6-1 and 6-5 in the previous example
because our pick at sub-script 6 splits the 2-in-a-rows.
Hint: While these rules seem very complex, they are actually designed to be
somewhat easy to implement. Every time a location is picked, your program must
start at the picked location and scan eight different directions to look for M-in-a-row.
When scanning in each of the eight directions, your program should score only the
largest (maximal) chain and not the smaller sub-chains. In the previous example,
we score 6-4-3 as a 3-in-a-row and do not score 4-3 as a 2-in-a-row. This strategy
2
will implement Rules 1 to 4 but it will not include larger chains that are created by
“filling holes” and merging chains in opposite directions.
Rule 5 (merged scoring):
Consider that we place an O in the location marked with ?:
X
O ?
X
X O X
O X X
O O O
X
X
If we scan left, we find 2-in-a-row (2 points). If we scan diagonally up-right, we find
3-in-a-row (3 points). If we scan right, we find 4-in-a-row (4 points).
But notice that we also created a 5-in-a-row, which must be scored. To account for
this possibility (called merged scoring), we must add up chains in opposite
directions (left-right, up-down, and the two diagonal directions). Notice that a left
chain of 2 plus a right chain of 4 actually equals 5-in-a-row, so we have to subtract
one so we do not count the picked location twice.
In the example above, the total score for the pick (location marked with ?) is 14 (2in-a-row + 3-in-a-row + 4-in-a-row + 5-in-a-row). Note that we are scoring the 2-ina-row and 4-in-a-row because of Rule 4 (split scoring).
In the example below, the total score would be 5 because of Rule 3. The 4-in-a-row
and the other smaller chains were already scored in previous turns.
?
X
X O X
X O X X
O O O O
X
X
To summarize, there are 12 different ways to score: 8 directional scores and 4
merged scores. This allows for an interesting game strategy where the score is
much higher if a player builds chains by “filling holes” to merge chains in opposite
directions (Rule 3 or 5) or by selecting locations with many adjacencies to create
multi-scoring possibilities (Rule 2).
3
Requirements and Consideration
Class Design
1. You should use more than one class to implement the game, but you are
required to have a TicTacTie class that implements the core game play and a
Driver class with a public static void main method
2. It might be useful to implement other classes for manipulating the grid,
possible choices, or the players, but a very well-design single class can still
earn you full credit.
3. Your TicTacTie class should have a constructor that creates the grid (N x N)
and any other objects needed for game play.
4. Your TicTacTie class should have a play method that implements game play.
5. Your Driver class’s main method should prompt the user for the size of the
grid, create a TicTacTie object and then call the play method.
Game Play
1. The game must be played by two opponents: the user/player and the
computer.
2.
The play method should display the empty N x N grid and then prompt the
user for a pick (row and column). The grid must be labeled as follows:
What is the size of the game grid (N x N) that you want to use? 4
Type any non-numeric character and hit enter to quit game play.
0
1
2
3
----------------0 |
|
|
|
|
----------------1 |
|
|
|
|
----------------2 |
|
|
|
|
----------------3 |
|
|
|
|
----------------Enter row: 0
Enter column:
3. The program should re-prompt the user if they select an invalid row or
column number, or if they select a location that is already marked.
Enter row: 0
Enter column: 4
Invalid column
Enter column:
4
4. The program should terminate if the user types a row or column number that
is non-numeric. The program should output that the user quit.
Enter column: q
Player quits
5. After the user selects a valid location, you should mark the grid with an O and
compute the player’s score.
6. Then, your program should pick a location, mark the grid with a X and
compute the computer’s score.
Enter row: 1
Enter column: 1
0
1
2
3
----------------0 |
|
|
|
|
----------------1 |
| O |
|
|
----------------2 |
|
|
|
|
----------------3 |
|
| X |
|
----------------Computer's score: 0
Player's score: 0
7. The program should immediately terminate if either the player or computer
gets N-in-a-row. In this case, the score is irrelevant and the program should
output the name of the winner (computer or player).
8. In the event of a tie, the program should terminate once the last remaining
grid location is selected. The program should output the player’s and
computer’s score and indicate the winner.
0
1
2
3
----------------0 | X | O | O | X |
----------------1 | O | O | O | X |
----------------2 | X | O | O | X |
----------------3 | X | X | X | O |
----------------Computer's score: 20
Player's score: 34
Player wins!
5
Computer Opponent

You can earn full credit by simply implementing a random computer
opponent, where the computer randomly selects a grid location from the unselected locations. However, you must implement the random strategy
efficiently where the computer only picks among empty locations.

For extra credit, you can implement an intelligent computer opponent. Hint:
The scoring rules described in this lab are precisely the kind of rules used to
implement an intelligent Tic-Tac-Tie opponent. The computer opponent
could select the grid location that creates the highest possible score or the
grid location that blocks the player from selecting a high scoring location.
6