国外学校的一道计算机题,大家给看看,英文的
勿为 2014-03-07 07:42:22 我同事的儿子在国外读书遇到的一道难题,要3月8日早上5点之前完成:
For this problem, the classes you write must be implemented using the class keyword.
In this problem you will write a C++ program to administer the game of Tic-Tac-Toe
(http://en.wikipedia.org/wiki/Tic-Tac-Toe), which involves two players, X and O. Play-
ers take turns claiming squares on a 3x3 grid, until one player has claimed a straight line of
three squares (that player wins the game) or until the grid is full (and no one wins). Your
program will play several rounds of this game and report the winner. A sample interaction
follows (your input is in italics):
game stdin stdin
X's move
NW
O's move
C
X's move
SW
O's move
N
X's move
W
X wins
Score is
X 1
O 0
quit
In between games, two commands are recognized:
game sX sO Starts a game. sX denotes the name of the le from which X's moves will
be taken. stdin indicates that the moves will come from cin, i.e., X's moves will be
interactive. Similarly for O.
quit Ends the program
Within a game, players take turns claiming squares. The nine squares are arranged as follows:
NW N NE
W C E
SW S SE
When a player's moves come from stdin, it is considered invalid input to claim a square that
has already been taken. On the other hand, when the moves come from a le, it is hard to
know in advance what squares will have been taken. Thus, when the moves come from a le,
a player's move is dened to be the next square in the le that is not already claimed. Note
that it would be redundant for a square to occur more than once in the le, and therefore we
consider any le that contains a square more than once to constitute invalid input.
A sample interaction where both players' moves come from les is presented below. Suppose
that movesX.txt contains the following:
NW SE NE C E N S W SW
Suppose that movesO.txt contains the following:
C SE SW E N NW NE S W
Then the interaction would be as follows:
game movesX.txt movesO.txt
X's move
(plays NW)
O's move
(plays C)
X's move
(plays SE)
O's move
(plays SW)
X's move
(plays NE)
O's move
(plays E)
X's move
(plays N)
X wins
Score is
X 1
O 0
quit
Note, in particular, that when the moves come from a le, your program prints to stdout the
move that was made (e.g., (plays NW) above).
You may assume that if a player's moves are taken from a le, then the le will contain
enough moves to complete the game.
Within a game, play alternates between X and O. X plays rst in odd-numbered games
(starting from 1), and O plays rst in even-numbered games.
A win is worth one point. A loss is worth no points. If a game is drawn (no one wins), then
no points are awarded for that game. In the case of a draw, print Draw to stdout, instead of
X wins or O wins.
To structure this game, you must include at least the following classes:
ScoreBoard which tracks the number of games won by each player and the current
state of the board. This class must be responsible for all output to the screen, except
X's move and O's move (these will be printed by code in the Player class, described
below).
Player which encapsulates a game player, and keeps track of the source from which a
player is receiving input.
You must use the singleton pattern to ensure that there is only one scoreboard in the game.
In addition, you must generalize the singleton pattern to ensure that only two Player objects
can be constructed.
Each Player object must possess a pointer to this scoreboard, and these pointers will be
intialized in the way prescribed by the singleton pattern. Each player object must be re-
sponsible for registering its move with the scoreboard by calling a ScoreBoard::makeMove
method. This method should take parameters indicating which player is calling the method,
and the amount to be deducted from the total. If necessary, a player object may query the
board by calling a method ScoreBoard::isOccupied to nd out whether a given position is
taken. This method would only be called if the player object's input comes from a le, for
the purpose of determining the next unoccupied position in the le.
Your main program will be responsible for keeping track of which player's turn it is. It will
call a method in ScoreBoard to start a game, whenever the user enters a game command. In
addition, it will alternately call a method on the two player objects that will cause the player
to get the next move from its input source and then pass that move on to the scoreboard.
The chain of method calls is therefore roughly the following:
main program, in response to a game command from the user, calls a method ScoreBoard::startGame
to initiate a game.
main program calls a method for each of the two player objects, to pass to them their
respective input streams
main program alternately calls Player::makeMove for player objects A and B. This
method should take no parameters.
The Player::makeMove method will be responsible for fetching the next move and calling
the ScoreBoard::makeMove method to register the move with the scoreboard.
Your solution must use const declarations for variables, members, and parameters whenever
possible.
Your solution must not leak memory.
You may assume that all input is valid.
Due on Due Date 1: Design a test suite for this program (call the suite le suiteq4.txt
and zip the suite into a3q4a.zip).
Due on Due Date 2: Full implementation in C++. Your mainline code should be in
le a3q4b.cc, and your entire submission should be zipped into a3q4b.zip and submitted.
Your zip le should contain, at minimum, the les a3q4b.cc, scoreboard.h, scoreboard.cc,
player.h, and player.cc. If you choose to write additional classes, they must each reside
in their own .h and .cc les as well.