Tictac
PROBLEM
Tic-tac is a traditional two-player game. The game is played on a board, which has a 3x3 matrix (three rows and three columns). One of the players (player X) uses markers "X" and the other (player 0) uses markers "0". Initially, all cells of the matrix are empty. When the game is played, the players take turns placing their markers in empty cells of the matrix. Player X starts. A player wins the game, if he/she gets three of his/her markers on the same row or same column or a diagonal of the matrix. The game continues until either player wins or all cells contain a marker. If neither player wins, then it is a draw. The game has the property that if both players play optimally to avoid loosing, then the game will be a draw.
You are to write a program, which plays this game as player X aiming to either win or draw. The opponent plays optimally to win, that is, once given a chance, it will win the game and your program will loose.
INPUT AND OUTPUT
Your program reads input from standard input and writes output to standard output. When your program starts, it should first write the first move to standard output. Then it should read player 0's move from standard input.
The positions of the game board are numbered from 1 to 9 as in Figure 1 so that the top left corner is number 1 and the bottom right corner is number 9. For example, if your move is 5, you place a marker in the middle of the board.
------------- | 1 | 2 | 3 | ------------- | 4 | 5 | 6 | ------------- | 7 | 8 | 9 | -------------
Figure 1. The tic-tac board.
EXAMPLE
Consider the following example. The board after the first move is given in Figure 2 and the end position of the game in which player 0 has won is given in Figure 3.
stdin stdout explanation 1 Player X places a marker in position 1. 2 Player 0 places a marker in position 2. 4 Player X places a marker in position 4. 7 Player 0 places a marker in position 7. 6 Player X places a marker in position 6. 5 Player 0 places a marker in position 5. 8 Player X places a marker in position 8. 3 Player 0 places a marker in position 3.
Player 0 has three pieces in a diagonal and wins the game and playing stops.
------------- | X | | | ------------- | | | | ------------- | | | | -------------
Figure 2.
------------- | X | 0 | 0 | ------------- | X | 0 | X | ------------- | 0 | X | | -------------
Figure 3.
PROGRAMMING INSTRUCTIONS
In what follows we assume that target is an integer variable used for reading or writing the move.
If you program in C++ and use streams, you should implement reading standard input and writing standard output as follows:
cin>>target;
cout<<target<<endl<<flush;
If you use fgets and printf in C or C++, you should implement reading standard input and writing standard output as follows:
fgets(target, stdin);
printf(target); fflush(stdout);
If you program in Pascal, you should implement reading standard input and writing standard output as follows:
ReadLn(target);
WriteLn(target);
SCORING
If your program plays according to the rules and wins or it is a draw, you get full points, otherwise you get 0 points.