Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CSCI1510 Computer Principles and C Programming, Spring 2018-2019

Department of Computer Science and Engineering, The Chinese University of Hong Kong

Assignment 5: Ataxx
Due: 20:00, Wed 10 Apr 2019 Full marks: 100

Introduction
The objective of this assignment is to practice the use of arrays with loops. You will implement a board
game called Ataxx (同化棋). Two players, namely O and X, take turns to move chess pieces on an 𝑛 ×
𝑛 game board. A piece has two sides in O and X respectively. At the beginning of a game, O and X each
occupies two corners of the board. Figure 1 shows the initial 7 × 7 game board. An empty square is
denoted as ‘.’.

Each player can move their piece to an empty destination square which is one or two squares away
from the source horizontally, vertically, or diagonally. Figure 2 shows the possible destinations of an X
move. The green and red destinations are one and two squares from the source respectively. In a
move, if the destination is two squares from the source, then the source will become empty (.) after
the move. But if the destination is one square from the source, then the piece is “split” such that both
the source and destination are occupied. After a move, all the opponent’s pieces that are adjacent
(horizontal, vertical, or diagonal) to the destination are flipped. (That is, from X to O, or vice versa.)
Figure 3 shows an example X move from position C4 to D4. The destination D3 is one square from the
source C4, so X is split. The two opponent’s pieces in E2 and E4 are adjacent to the destination D3 so
they are flipped to become X. If a player has no valid moves, (s)he has to pass his turn. A game ends
when either the board is full or there are two consecutive passes (one by each player). The player with
more pieces on the board wins. A game can be a draw when both players have same number of pieces.

A B C D E F G
0 O . . . . . X . . . . . . .
1 . . . . . . . . . . . . . .
2 . . . . . . . . . . . . . .
3 . . . . . . . . . . X . . .
4 . . . . . . . . . . . . . .
5 . . . . . . . . . . . . . .
6 X . . . . . O . . . . . . .
Figure 1: Initial 𝟕 × 𝟕 Game Board Figure 2: Possible Destinations of an X Move

A B C D E F G A B C D E F G
0 . . . . . . . 0 . . . . . . .
1 . . . . . . . 1 . . . . . . .
X in square C4 splits to D3
2 . . . X O O . 2 . . . X X O .

3 . . . . X O . 3 . . . X X O .
4 . . X . O X . 4 . . X . X X .
5 . . . . . . . 5 . . . . . . .
6 . . . . . . . 6 . . . . . . .
Figure 3: Example X Move from Position C4 to Position D3, and the Flips

Program Specification
This section describes the board representation, program flow, and some special requirements.

Copyright © 2019 CSE, CUHK Page 1 of 4


CSCI1510 Computer Principles and C Programming, Spring 2018-2019
Department of Computer Science and Engineering, The Chinese University of Hong Kong

Board Representation
The board will be represented by a two-dimensional array of char. The array elements should be
either ‘O’, ‘X’, or ‘.’.

#define N 7 // Board size



char board[N][N]; // Local 2-D array
When N is 7, the array elements board[0][0], board[0][N - 1], board[N - 1][0], and
board[N - 1][N - 1] denote the four corner squares A0, G0, A6, and G6 respectively.

Program Flow
1. Initialize the game board as completely empty, except that the top-left ↖ and bottom-right ↘
corners should be O, and the other two corners ↗↙ should be X.
2. Player O takes the first turn.
3. Prompt the current player to enter the source and destination of a move. Each input position is
always a letter immediately followed by a non-negative integer. (E.g., B1 D3 means moving from
source B1 to destination D3.
 Hint: You can use scanf(" %c%d %c%d", …) to read the inputs. Note the spaces before %c.
4. Convert the column letters to corresponding column array indices. (E.g., B1, D3.)
5. A user input is valid if all of the following conditions are true:
 The rows and columns of the source and destination are within the board.
 The source is the current player’s piece.
 The destination is empty.
 The destination is one or two squares away from the source.
(Note that lowercase column letters are considered invalid.)
6. If an input move is invalid, it is considered as a pass. (There is no need to ask the current player to
input again.) If the move is valid, then update the source and/or destination squares accordingly,
and flip all the opponent’s pieces that are adjacent to the destination.
7. Repeat steps 3–6 with alternate players O and X, until either the board has no more empty squares
or there are two consecutive passes (one by each player).
8. Once the game finishes, display the message “O wins!”, “X wins!”, or “Draw!” accordingly.

Special Requirements
 Global variables (variables declared outside any functions) are not allowed.
 Your program should be decomposed into at least four functions (including main()). At least two
functions should have array parameter(s).
 Your program should be scalable to other values for the named constant N. That is, your program
should still work normally for board size other than 7. When grading, we may modify your program
by changing N to other values (may be 5–26) for testing.

Sample Run
In the following sample run, the blue text is user input and the other text is the program printout.
More sample runs are provided in Blackboard. Besides, you can try the provided sample program for
other input. Your program output should be exactly the same as the sample program (same text,
symbols, letter case, spacings, etc.). Note that there is a space after the ‘:’ in the program printout.

Copyright © 2019 CSE, CUHK Page 2 of 4


CSCI1510 Computer Principles and C Programming, Spring 2018-2019
Department of Computer Science and Engineering, The Chinese University of Hong Kong

A B C D E F G
0 O . . . . . X
1 . . . . . . .
2 . . . . . . .
3 . . . . . . .
4 . . . . . . .
5 . . . . . . .
6 X . . . . . O
O's turn (from to): A0 B1
A B C D E F G
0 O . . . . . X
1 . O . . . . .
2 . . . . . . .
3 . . . . . . .
4 . . . . . . .
5 . . . . . . .
6 X . . . . . O
X's turn (from to): A6 B5
A B C D E F G
0 O . . . . . X
1 . O . . . . .
2 . . . . . . .
3 . . . . . . .
4 . . . . . . .
5 . X . . . . .
6 X . . . . . O
O's turn (from to): B1 D3
A B C D E F G
0 O . . . . . X
1 . . . . . . .
2 . . . . . . .
3 . . . O . . .
4 . . . . . . .
5 . X . . . . .
6 X . . . . . O
X's turn (from to): B4 C4
Invalid! X passed!
A B C D E F G
0 O . . . . . X
1 . . . . . . .
2 . . . . . . .
3 . . . O . . .
4 . . . . . . .
5 . X . . . . .
6 X . . . . . O
O's turn (from to): D3 C4

Copyright © 2019 CSE, CUHK Page 3 of 4


CSCI1510 Computer Principles and C Programming, Spring 2018-2019
Department of Computer Science and Engineering, The Chinese University of Hong Kong

A B C D E F G
0 O . . . . . X
1 . . . . . . .
2 . . . . . . .
3 . . . O . . .
4 . . O . . . .
5 . O . . . . .
6 X . . . . . O
X's turn (from to): A6 B4
A B C D E F G
0 O . . . . . X
1 . . . . . . .
2 . . . . . . .
3 . . . O . . .
4 . X X . . . .
5 . X . . . . .
6 . . . . . . O
O's turn (from to): D3 H7
Invalid! O passed!
A B C D E F G
0 O . . . . . X
1 . . . . . . .
2 . . . . . . .
3 . . . O . . .
4 . X X . . . .
5 . X . . . . .
6 . . . . . . O
X's turn (from to): B1 C2
Invalid! X passed!
A B C D E F G
0 O . . . . . X
1 . . . . . . .
2 . . . . . . .
3 . . . O . . .
4 . X X . . . .
5 . X . . . . .
6 . . . . . . O
X wins!

Submission and Marking


 Your program file name should be ataxx.c. Submit the file in Blackboard
(https://blackboard.cuhk.edu.hk/).
 Insert your name, student ID, and e-mail as comments at the beginning of your source file.
 You can submit your assignment multiple times. Only the latest submission counts.
 Your program should be free of compilation errors and warnings.
 Your program should include suitable comments as documentation.
 Do NOT plagiarize. Sending your work to others is subjected to the same penalty as the copier.

Copyright © 2019 CSE, CUHK Page 4 of 4

You might also like