一道玩井子游戏的题目...
Since the Tic-Tac-Toe board is like a 3×3 grid we will consider this board to have 9positions as demonstrated by the following diagram.
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
Each player in turn places their symbol (an O or an X) on one of the 9 positions. The
game ends when one of the players places three O's or X's in a line
(horizontal/vertical/diagonal) OR when all positions have been filled.
Whoever completes the first line wins, if no line has been completed and all positions
have been filled then we have a draw.
SPECIFICATIONS:
Here are some details on what you have to do.
1. You should begin by outputting some Welcome message, welcoming players into
your game.
2. Then your program should output the initial Tic-Tac-Toe board (see Sample Input
and Output section for details) then ask Player X (since Player X starts) to choose
a position (from 1 through to 9) to place an X. Player X’s choice for position must
be verified as he/she may enter anything from a standard keyboard. Also a player
must always choose from one of the remaining positions.
3. Once we have a valid choice for a position from a player we put his/her symbol
(i.e. an X or an O) in that position.
4. Then your program should verify whether or not there will be a next round by first
checking if all positions have been filled. If all 9 positions have been filled then
this will be the final round.
5. Another situation where we may not have a next round is if one of the players
have won already. Therefore that’ll be your next move, i.e. to determine if we
have 3 X’s (or O’s) in a line (horizontal/vertical/diagonal). If we do have a winner
then make sure that you record it so that you can announce it later.
6. If there will be a next round then first switch to the other player (i.e. if current
player is Player X then next player will be Player O and vice versa) and repeat
from step 2.
7. If no more rounds left then either declare the winner OR say that we have a draw
(all 9 positions have been filled but no lines of three X’s or O’s
SAMPLE INPUT and OUTPUT
Each of the following cases demonstrates the input and output of one run of my program.
I have highlighted the inputs from the user.
Case 1:
**************************************************
--------------------------------------------------
WELCOME TO MY TIC-TAC-TOE
--------------------------------------------------
**************************************************
Tic-Tac-Toe board:
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 12
Incorrect input. Must enter one of the INTEGERS BETWEEN 1 and 9.
Choose one of the remaining positions? a45
Incorrect input. Must enter one of the INTEGERS BETWEEN 1 and 9.
Choose one of the remaining positions? -4
Incorrect input. Must enter one of the INTEGERS BETWEEN 1 and 9.
Choose one of the remaining positions? @#
Incorrect input. Must enter one of the INTEGERS BETWEEN 1 and 9.
Choose one of the remaining positions? 1
Tic-Tac-Toe board:
|---|---|---|
| X | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 1
Position taken. Must enter one of the REMAINING POSITIONS.
Choose one of the remaining positions? 8
Tic-Tac-Toe board:
|---|---|---|
| X | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | O | 9 |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 2
Tic-Tac-Toe board:
|---|---|---|
| X | X | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | O | 9 |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 3
Tic-Tac-Toe board:
|---|---|---|
| X | X | O |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | O | 9 |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 4
Tic-Tac-Toe board:
|---|---|---|
| X | X | O |
|---|---|---|
| X | 5 | 6 |
|---|---|---|
| 7 | O | 9 |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 7
Tic-Tac-Toe board:
|---|---|---|
| X | X | O |
|---|---|---|
| X | 5 | 6 |
|---|---|---|
| O | O | 9 |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 9
Tic-Tac-Toe board:
|---|---|---|
| X | X | O |
|---|---|---|
| X | 5 | 6 |
|---|---|---|
| O | O | X |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 5
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FINAL Tic-Tac-Toe board:
---|---|---|
X | X | O |
X | O | 6 |
O | O | X |
---|---|---|
:) Winner is PLAYER O
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Case 2:
**************************************************
--------------------------------------------------
WELCOME TO MY TIC-TAC-TOE
--------------------------------------------------
**************************************************
Tic-Tac-Toe board:
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 5
Tic-Tac-Toe board:
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | X | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 7
Tic-Tac-Toe board:
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | X | 6 |
|---|---|---|
| O | 8 | 9 |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 1
Tic-Tac-Toe board:
|---|---|---|
| X | 2 | 3 |
|---|---|---|
| 4 | X | 6 |
|---|---|---|
| O | 8 | 9 |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 9
Tic-Tac-Toe board:
|---|---|---|
| X | 2 | 3 |
|---|---|---|
| 4 | X | 6 |
|---|---|---|
| O | 8 | O |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 8
Tic-Tac-Toe board:
|---|---|---|
| X | 2 | 3 |
|---|---|---|
| 4 | X | 6 |
|---|---|---|
| O | X | O |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 2
Tic-Tac-Toe board:
|---|---|---|
| X | O | 3 |
|---|---|---|
| 4 | X | 6 |
|---|---|---|
| O | X | O |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 6
Tic-Tac-Toe board:
|---|---|---|
| X | O | 3 |
|---|---|---|
| 4 | X | X |
|---|---|---|
| O | X | O |
|---|---|---|
PLAYER O, Its your turn!
Choose one of the remaining positions? 4
Tic-Tac-Toe board:
|---|---|---|
| X | O | 3 |
|---|---|---|
| O | X | X |
|---|---|---|
| O | X | O |
|---|---|---|
PLAYER X, Its your turn!
Choose one of the remaining positions? 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FINAL Tic-Tac-Toe board:
---|---|---|
X | O | X |
O | X | X |
O | X | O |
---|---|---|
:( BORING DRAW
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~