ACM亚洲区预选赛高精度题- -| 回首页 | 2005年索引 | - -MySQL

C Program

                                      

Don't know how to do the program
1. Specification
Write a class Game that models the tic-tac-toe game. Tic-tac-toe (also called noughts and crosses) is a game
between two players, X and O, who take turns to mark the spaces in a N x N board. The default value for N
is 3. Player X always moves first. A player wins if he/she has exactly W of his/her symbols in a line (vertical,
horizontal or diagonal). The default value for W is 3.
The player X wins the following game with N = 3 and W = 3.
The class Game should contain at least the following :
a. Static member function getID
Syntax
The class should include the member function getID that returns your name and University number. The
function prototype is:
class Game {
private:
...
public:
...
static string getID() { return("Chan Tai Man, h0512345"); }
}
X
X
O
X
X O
X
X O O
X
X O O
X
move 1 move 2 move 3 move 4 move 5
replace it with your own information
p. 2
b. Constructor
Syntax
The class should include appropriate constructors to construct an empty board. The function prototype is:
Game(unsigned int N, unsigned int W);
N defines the dimension of the board. W defines the winning number of symbols in a line. For example,
the following statement creates a 10x10 board and it requires 5 symbols in a line for a player to win.
Game game1(10, 5); // create 10x10 board,
// player having 5 symbols in a line wins
If no parameters are specified, the default is a 3 x 3 board and requiring 3 symbols to win.
Game game2; // create 3x3 board,
// player having 3 symbols in a line wins
Assumption
To simplify your program, you may assume that N is in the range of 1 to 10. W is always less than or equal
to N.
c. Member function move
Syntax
The class should contain a member function move that represents a move made by a player.
The function prototype is:
int move(char player, unsigned int vert_coor, unsigned int hori_coor);
player can be 'X' or 'O' (letter O, not zero). vert_coor and hori_coor are the coordinates of the cell that is
to be marked by the player. Both coordinates start from 0.
The following statements result in an X being marked on cell (1, 2) of the 3x3 board.
Game game1(3, 3);
int status = game1.move('X', 1, 2); // status will be 0
The content of the resultant board will be:
X
hori_coor
0 1 2
0
1
2
vert_coor
p. 3
Return Code
The member function should return a status code to the caller, with the following meaning:
• 0 : The move is successful and the game continues.
• 1 : The move is successful and the game is over. The player making the move wins.
(probably the most difficult part of your class)
• 2 : The move is successful and the game is over. It is a draw which means all cells are marked
but there's no winner.
• -1 : The move is unsuccessful because the game is already over.
• -2 : The move is unsuccessful because the parameters are invalid.
E.g. Player not 'X' or 'O', or specified cell is outside the board.
• -3 : The move is unsuccessful because it is not the turn of the player to make the move.
N.B. player 'X' always makes the first move.
• -4 : The move is unsuccessful because the specified cell is already occupied.
• -99 : The move is unsuccessful because of other errors (will not be tested during marking)
d. Member function dump
Syntax
The class should contain a member function dump. It returns a string object that represents the current
content of the board. The function prototype is:
string dump(void);
The returned string contains (N+1) * N characters. Each character represents the content of one cell: 'X'
for a cross, 'O' (letter O, not zero) for a nought, '.' (full stop) for an empty cell. The content is dumped
row by row. Each row is terminated by a newline. Assuming the current content of a 3x3 board is:
And the following statement is executed:
string content = game1.dump();
content will be a string object containing 12 characters: X.O\nXOO\n.X.\n
X O
X O O
X
p. 4
e. Member function save
Syntax
The class should contain a member function save that saves the current game to a file. What is actually
saved is determined by you. The requirement is that the file created by save should be readable by another
member function load that can restore the game. The function prototype is:
int save(char *filename);
filename is the name of the file to be used to save the game. E.g.
Game game1(3, 3);
game1.move('X', 1, 2);
int status = game1.save("mygame.dat");
A file mygame.dat should be created. No other files should be created.
Return Code
The member function should return a status code to the caller, with the following meaning:
• 0 : The game is successfully saved.
• -1 : The game cannot be saved because of errors. E.g. file can't be opened for writing
f. Member function load
Syntax
The class should contain a member function load that restores a game from a file. The function prototype is:
int load(char *filename);
filename is the name of the file containing the game.
The following program demonstrates how a game is saved and then restored to another Game object.
int main() {
Game game1(3, 3);
game1.move('X', 1, 2);
game1.move('O', 0, 0);
game1.save("mygame.dat");
Game game2(3, 3);
int status = game2.load("mygame.dat");
if (status == 0) {
cout << game2.dump();
cout << game2.move('O', 2, 1);
}
}
It should also be possible to save a game in one program, and restore it to another Game object in another
program.
p. 5
The following will be printed:
O..
..X
...
-3
Return Code
The member function should return a status code to the caller, with the following meaning:
• 0 : The game is successfully loaded.
• -1 : The game cannot be loaded because of errors. E.g. file can't be found
2. Submission
You should write the class in a header file (e.g. ex4.h) and submit the header file on or before the deadline.
You must NOT include main() in your header file. During marking, your header file will be included in a
testing program like the following :
#include <iostream>
#include <string>
#include <fstream>
#include "ex4.h" // ex4.h is written by you
int main()
{
Game game1(8, 3);
game1.move('X', 4, 3);
game1.move('O', 1, 2);
string s1 = game1.dump();
cout << s1;
...
}
Then the testing program will be compiled and run on hkueee, and the output will be compared with the
expected output.
To submit the source file, login hkueee.hku.hk and type homework (or /usr/local/bin/homework).
You will be instructed onscreen how to submit your file. Submission through email will NOT be accepted.
Plagiarism (i.e. copying) is strictly prohibited. Zero marks will be given to both the
source and copy if discovered. You must not copy or let others copy your work. It's your
own responsibility to prevent others from copying your program directly or indirectly (e.g.
obtain your program through another person).
You can discuss with others in the design phase only. Coding must be done separately and group program is
NOT accepted.
Marks will be deducted 20% per day for late submission
move returns -3 because it
should be player X's turn
p. 6
3. Marking scheme
The assignment will be marked according to the following scheme:
1. Compilable submission (20%)
• You will be awarded the marks if the following testing program can be compiled and run
successfully.
#include <iostream>
#include <string>
#include "ex4.h" // ex4.h is written by you
int main()
{
cout << Game::getID(); // output your ID
}
2. Provision of correct constructors, basic move function and dump function (30%)
3. Provision of correct move function that returns appropriate status code and other functions (50%)
Final remark: If you develop your program on computers other than the Unix machines of the Dept (i.e. not
on hkueee, su1001 to su1005), you must upload the source file to hkueee; compile and test it before you
submit the source file. Please note that programs working on PC may not work properly on hkueee.

【作者: kimjac】【访问统计:】【2005年11月21日 星期一 13:28】【 加入博采】【打印

Trackback

你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=3597958

博客手拉手

[2005-11-16 21:31:42.0]    一个初学者对C语言学习的几点感悟

[2005-11-16 21:34:17.0]    一个初学者对C语言学习的几点感悟

[2005-11-16 22:54:08.0]    一个初学者对C语言学习的几点感悟

[2005-11-16 22:57:48.0]    一个初学者对C语言学习的几点感悟

[2005-11-18 22:47:31.0]    Good tutorial on C Template for beginners

回复

评论内容: