Your midterm project is to implement a perl-based version of tic-tac-toe. A user will pick moves on the page, and you will make the computer's move using the method described below. You must use the method described below for your computer implementation.
There are lots of tic-tac-toe programs on the web. You may not use any other code either as the basis for your code, to help with parts of your code, nor may you take segments of it to use as your own. Do not consult other programs, in perl or in any other language. You may not use them for logic or for syntax. You must solve this problem entirely on your own and writing code entirely on your own. You may not work with other students, on the code or on the logic. This entire program from ideas to implementation in perl must be written by you working alone with no collaboration. Basically, this midterm is a test of your problem solving abilities.
Let the user make the first move. After they do, display an updated board with your move and theirs shown. Repeat for each move. If the game ends, either because you win or there is a tie, print out a message that says the result and do not allow the user to make another move.
You will be graded on your implementation of the algorithm, and the effectiveness of game play (i.e. does it work like it should).
0 | 1 | 2 ---+---+--- 3 | 4 | 5 ---+---+--- 6 | 7 | 8 Pick a space: 4 O | 1 | 2 ---+---+--- 3 | X | 5 ---+---+--- 6 | 7 | 8 Pick a space: 1 O | X | 2 ---+---+--- 3 | X | 5 ---+---+--- 6 | O | 8 Pick a space:Yours doesn't have to look like that, but there should be an easy way for a player to see the board and pick a space.
The algorithm really only computes the
computer's moves for the following
three squares:
* | * | ---+---+--- | * | ---+---+--- | |If the computer determines that it needs to move into one of these squares, then it takes the move and allows the player to make his/her move. If these squares are already occupied, or there isn't an important move in any of these squares, the computer rotates the board 90 degrees counter-clockwise and tries these same three positions (on the new board) again. After rotating the board four times, the computer winds up with the original configuration. If this occurs, and the board isn't completely filled, the computer takes the first available position on the game board (first trying the center, then moving from left to right, top to bottom).
To see if the computer can win, it checks for one of the
following patterns. The computer checks them in the following order.
Note: an "X", "O", or space appear in a square below means that character must be present. A "*" means that anything may appear in the square. "?" denotes the position (that must currently contain a blank) where the computer will put an "O" if the pattern matches.
Pattern #1 to check: ? | O | O ---+---+--- * | * | * ---+---+--- * | * | * Pattern #2 to check: ? | * | * ---+---+--- O | * | * ---+---+--- O | * | * Pattern #3 to check: ? | * | * ---+---+--- * | O | * ---+---+--- * | * | O Pattern #4 to check: O | ? | O ---+---+--- * | * | * ---+---+--- * | * | * Pattern #5 to check: * | ? | * ---+---+--- * | O | * ---+---+--- * | O | * Pattern #6 to check: * | O | * ---+---+--- * | ? | * ---+---+--- * | O | * Pattern #7 to check: O | * | * ---+---+--- * | ? | * ---+---+--- * | * | OThe following patterns check to see if the computer needs to block the player from winning on the next move.
Pattern #8 to check: ? | X | X ---+---+--- * | * | * ---+---+--- * | * | * Pattern #9 to check: ? | * | * ---+---+--- X | * | * ---+---+--- X | * | * Pattern #10 to check: ? | * | * ---+---+--- * | X | * ---+---+--- * | * | X Pattern #11 to check: X | ? | X ---+---+--- * | * | * ---+---+--- * | * | * Pattern #12 to check: * | ? | * ---+---+--- * | X | * ---+---+--- * | X | * Pattern #13 to check: * | X | * ---+---+--- * | ? | * ---+---+--- * | X | * Pattern #14 to check: X | * | * ---+---+--- * | ? | * ---+---+--- * | * | XThe following patterns represent positions the computer must move to block the player from winning in two moves.
Pattern #15 to check: | ? | X ---+---+--- | O | ---+---+--- X | | Pattern #16 to check: ? | X | ---+---+--- X | O | * ---+---+--- | * | * Pattern #17 to check: ? | | X ---+---+--- X | O | * ---+---+--- | * | * Pattern #18 to check: * | * | X ---+---+--- * | O | ---+---+--- | X | ? Pattern #19 to check: ? | | X ---+---+--- | O | * ---+---+--- X | * | * Pattern #20 to check: ? | * | * ---+---+--- * | X | * ---+---+--- * | * | Pattern #21 to check: * | ? | * ---+---+--- * | X | * ---+---+--- * | | *If the computer cannot match one of the above patterns, it rotates the board 90 degrees and tries again. After rotating the board four times without finding a match, the computer locates the first empty square and moves there (check the middle first, and then the rest). If there are not empty squares, the game is over.
Turn this in by uploading *one* perl file to the folder. Use "midterm" as the assignment name in your file name.