Question: The job is to write a script ttt that plays TicTacToe with the command-line. Now for this version, you don't have to put any game
The job is to write a script "ttt" that plays TicTacToe with the command-line. Now for this version, you don't have to put any game logic for the computer. Just have the computer randomly pick an open place on the TicTacToe board and place his mark. Here is a session that I have run with a completed script to play TicTacToe (it's about 200 lines long). :~$ ./ttt start o Player: 'O', Computer: 'X' _ | _ | _ ---+---+--- _ | X | _ ---+---+--- _ | _ | _ :~$ ./ttt play 1 1 Player: 'O', Computer: 'X' O | _ | _ ---+---+--- _ | X | _ ---+---+--- X | _ | _ :~$ ./ttt play 1 3 Player: 'O', Computer: 'X' O | _ | O ---+---+--- _ | X | _ ---+---+--- X | _ | X :~$ ./ttt play 1 2 Game over: 'O' wins on first row Player: 'O', Computer: 'X' O | O | O ---+---+--- _ | X | _ ---+---+--- X | _ | X :~$ ./ttt print Game over: 'O' wins on first row Player: 'O', Computer: 'X' O | O | O ---+---+--- _ | X | _ ---+---+--- X | _ | X moves: put 'X' in location (2, 2) put 'O' in location (1, 1) put 'X' in location (0, 1) put 'O' in location (1, 3) put 'X' in location (1, 0) put 'O' in location (1, 2) :~$ ./ttt usage: ttt command where command is: start [X|O] start a tic-tac-toe game as X or O (default X) play row col play your piece at row (1-3), col (1-3) print print current state of the game with moves :~$
Notice that you start the game by selecting X or O (case insensitive). If you don't select then the computer assigns you X. The state of the game is in a hidden directory ~/.ttt with files for the board, moves, computer's playing piece, and a file, gameover, with the winner's message. All these provide different semantics for play, for example, once the game is over, then it will not let you add anymore marks on the board. Here is a list of my directory and the contents of each:
:~$ ls -l .ttt total 16 -rw-rw---- 1 jeffj jeffj 17 Oct 1 19:17 board -rw-rw---- 1 jeffj jeffj 33 Oct 1 19:17 gameover -rw-rw---- 1 jeffj jeffj 162 Oct 1 19:17 moves -rw-rw---- 1 jeffj jeffj 1 Oct 1 19:17 piece :~$ more .ttt/board O O O _ X _ X _ X :~$ more .ttt/piece X :~$ more .ttt/moves put 'X' in location (2, 2) put 'O' in location (1, 1) put 'X' in location (0, 1) put 'O' in location (1, 3) put 'X' in location (1, 0) put 'O' in location (1, 2) :~$ more .ttt/gameover Game over: 'O' wins on first row :~$
A couple more pointers:
It is best to keep the board in an array and calculate into the array using a "row/offset" as you do in C/C++. Then you can use the $(( )) bash environment to do the calculations. Remember, you can put the board data into a variable and convert it to an array very easily with the declare built-in command.
Break up the code using functions to do small jobs, like checking the files, printing the board, etc.
If you want to use a random number generator, then look up the $RANDOM built-in variable in bash. There is another more classic way but either one is very useful in this project.
Provide some robustness in your script to check for errors on input. This could be done last or if you do top-down as I suggested, put in as you go. Be sure to stop coding and test often. Get stability in what you've added before moving on.
Finally, pay attention to SPACES. These are important to bash because it has no other way of knowing how to break up the input...EVERYTHING is a command line!
Ok, here is another run of this code for you to study as you design your own:
:~$ ./ttt start Player: 'X', Computer: 'O' _ | _ | _ ---+---+--- _ | _ | _ ---+---+--- _ | _ | _ :~$ ./ttt play 2 2 Player: 'X', Computer: 'O' _ | _ | _ ---+---+--- _ | X | O ---+---+--- _ | _ | _ :~$ ./ttt play 1 3 Player: 'X', Computer: 'O' _ | _ | X ---+---+--- _ | X | O ---+---+--- _ | _ | O :~$ ./ttt play 1 3 Error: invalid move: location (1,3) not open: found 'X' :~$ ./ttt play 1 4 Error: row or col out of range (1-3): found row=1, col=4 :~$ ./ttt play Error: row or col missing: try 'ttt play r c' :~$ ./ttt play 3 1 Game over: 'X' wins on diagonal Player: 'X', Computer: 'O' _ | _ | X ---+---+--- _ | X | O ---+---+--- X | _ | O :~$ ./ttt play 1 1 Game over: 'X' wins on diagonal Player: 'X', Computer: 'O' _ | _ | X ---+---+--- _ | X | O ---+---+--- X | _ | O :~$ ./ttt print Game over: 'X' wins on diagonal Player: 'X', Computer: 'O' _ | _ | X ---+---+--- _ | X | O ---+---+--- X | _ | O moves: put 'X' in location (2, 2) put 'O' in location (0, 0) put 'X' in location (1, 3) put 'O' in location (1, 0) put 'X' in location (3, 1) :~$
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
