C/C++ 编程代写
当前位置:以往案例 > >案例井字棋游戏C语言游戏案例实现
2018-12-21


In this project, you will create 3 different implementations of the “Tic- Tac-Toe” game in 3 tasks:


1D Implementation – a simple implementation of the game for two players using only ’X’-es. The first player who succeeds in placing 3 consecutive ’X’-es in a row wins the game.

2D Implementation – a game for two players, ’X’ and ’O’, who take turns marking the spaces in a n n grid. The first player who succeeds in placing 3 consecutive ’X’-es (or ’O’-s) in a horizontal, vertical, or diagonal (also antidiagonal) row wins the game.

Human vs Computer – 2D implementation but instead of 2 human players, the second player is the computer.


Your answer to each of the tasks which will be assessed individually.


Instructions and Submission Guidelines:

You must write C programs to solve each of the tasks. Each C pro- gram must be saved as a .c file and must be named using the con- vention t{task number}.c. For ple, the program for Task 1 should be named as t1.c. Similarly, t2.c should be the solution for Task 2, and so on.

You should provide appropriate comments to make your source code readable. Otherwise, if your code does not work and there are no comments, you may lose all the marks!

You must submit the files on or before the submission deadline to this

website: https://apps.ecs.vuw.ac.nz/submit/XMUT241/project_

2. Late submissions may not be marked.




Assessment Criteria:

Correctness: The program must satisfy all the functional require- ments explicitly stated in the task. It must provide the correct output for every expected/valid input. (Approximately 50% of the marks.)

Completeness: The program must satisfy all the functional require- ments implicitly stated in the task. It must be able to handle all unex- pected inputs and not terminate abnormally. (Approximately 15% of the marks.)

Readability: The source code must be readable, through the use of proper indentation, and sufficiently commented. (Approximately 15% of the marks.)

“Compilability”: The program must compile without warnings. (Ap- proximately 20% of the marks.)


Task 1.
[30 Marks]

In the first task, you have to write a program that for a 1D implementation of the game. In this simple implementation, two players take turns to mark the array with the ’X’ character. The player to first make 3 ’X’-es side-by- side in the array wins the game. The main goal here is to create the basic functionality of the game on which you can later build in Task 2.

To build your program, you must follow the guidelines below.

Function Prototypes
First create a function which outputs (prints) the state of the field (1D array). To achieve this, you have to use the following function prototype:



void draw_field(const char field[], int size)




The size of the field is entered by the user and must be at least 3 and at most

64. All the fields (positions) of the array will be initialised with a space (’ ’). Here is a sample output for the function:



$ ./tictactoe

Enter the size of field: 9




+-+-+-+-+-+-+-+-+-+

| | | | | | | | | |


+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9


Create another function which puts an ’X’ character at the position selected (entered) by the user. The function prototype for this function is the follow- ing:

int add_cross(char field[], int size, int position)




If the selected position is already in use (there is an ’X’ character) the func- tion will return 0. Otherwise (i.e. the position is still free), the function will return 1. If the entered position is outside the field, the function will return

-1.




Player A: 2

+-+-+-+-+-+-+-+-+-+

| |X| | | | | | | |

+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9



Player B: 5

+-+-+-+-+-+-+-+-+-+

| |X| | |X| | | | |





+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9






Now you have to implement another function to detect whether the game is already solved (won). To achieve this, you have to use the following function prototype:

int is_solved(const char field[], int size)




This function will return 1 if there is a min. number of 3 consecutive ’X’ characters (next to each other) in the field. Otherwise the function will return 0. With the functions above, finish the implementation of the game. Here is a sample output:







$ ./tictactoe

Enter the size of field: 9

+-+-+-+-+-+-+-+-+-+

| | | | | | | | | |

+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9

Player A: 2

+-+-+-+-+-+-+-+-+-+

| |X| | | | | | | |

+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9

Player B: 5

+-+-+-+-+-+-+-+-+-+

| |X| | |X| | | | |

+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9

Player A: 9

+-+-+-+-+-+-+-+-+-+

| |X| | |X| | | |X|

+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9

Player B: 10 Wrong position! Player A: 2

X is already there! Player B: 8

+-+-+-+-+-+-+-+-+-+

| |X| | |X| | |X|X|

+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9

Player A: 7

+-+-+-+-+-+-+-+-+-+

| |X| | |X| |X|X|X|

+-+-+-+-+-+-+-+-+-+ 1 2 3 4 5 6 7 8 9



在线提交订单