|
| 1 | +// This program plays Tic Tac Toe |
| 2 | +// The machine goes first and the way this is set up |
| 3 | +// there's no way the human player can win. At best |
| 4 | +// it will be a draw. |
| 5 | +computerNext = function(x) |
| 6 | + return x - 8 * floor((x - 1) / 8) |
| 7 | +end function |
| 8 | +
|
| 9 | +print " " * 30 + "TIC TAC TOE" |
| 10 | +print " " * 15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY" |
| 11 | +print; print; print |
| 12 | +
|
| 13 | +print "The game board is numbered:" |
| 14 | +print |
| 15 | +print "1 2 3" |
| 16 | +print "8 9 4" |
| 17 | +print "7 6 5" |
| 18 | +print |
| 19 | +
|
| 20 | +while true |
| 21 | + computer = 9 |
| 22 | + gameOver = false |
| 23 | + // MOVE ONE line 240 in original |
| 24 | + // Computer always moves first and takes the center |
| 25 | + print "Computer moves " + computer |
| 26 | + player = input("Your move? ").val |
| 27 | + playerFirstMove = player |
| 28 | + |
| 29 | + // MOVE TWO line 280 |
| 30 | + // Computer's 2nd move - always the next space clockwise |
| 31 | + // from the player's |
| 32 | + computer = computerNext(player + 1) |
| 33 | + canWinAt = computerNext(computer+4) |
| 34 | + print "Computer moves " + computer |
| 35 | + player = input("Your move? ").val |
| 36 | + |
| 37 | + // MOVE THREE line 300 |
| 38 | + // Computer has two consecutive cells. This includes the |
| 39 | + // middle so, to complete this 3-in-a-row, get the opposite |
| 40 | + // value of comp's last move - which is four cells clockwise away. |
| 41 | + |
| 42 | + if player != canWinAt then |
| 43 | + computer = canWinAt |
| 44 | + print "Computer moves " + computer |
| 45 | + print "... and wins ********" |
| 46 | + gameOver = true |
| 47 | + else |
| 48 | + // Blocked - so two cells away from comp's last move |
| 49 | + // line 360 |
| 50 | + computer = computerNext(computer + 2) |
| 51 | + print "Computer moves " + computer |
| 52 | + end if |
| 53 | + |
| 54 | + if gameOver == false then |
| 55 | + canWinAt = computerNext(computer+4) |
| 56 | + player = input("Your move? ").val |
| 57 | + |
| 58 | + // MOVE FOUR - line 400 |
| 59 | + if player != canWinAt then |
| 60 | + computer = canWinAt |
| 61 | + print "Computer moves " + computer |
| 62 | + print "... and wins ********" |
| 63 | + gameOver = true |
| 64 | + else |
| 65 | + // Foiled again! - line 450 |
| 66 | + if playerFirstMove % 2 == 0 then |
| 67 | + computer = computerNext(computer + 7) |
| 68 | + print "Computer moves " + computer |
| 69 | + print "... and wins ********" |
| 70 | + gameOver = true |
| 71 | + else // line 500 |
| 72 | + computer = computerNext(computer + 3) |
| 73 | + print "Computer moves " + computer |
| 74 | + end if |
| 75 | + end if |
| 76 | + end if |
| 77 | + |
| 78 | + if gameOver == false then |
| 79 | + // line 520 |
| 80 | + player = input("Your move? ").val |
| 81 | + if player != computerNext(computer + 4) then |
| 82 | + computer = computerNext(computer + 4) |
| 83 | + else |
| 84 | + computer = computerNext(computer + 6) |
| 85 | + end if |
| 86 | + print "Computer moves " + computer |
| 87 | + print "The game is a draw." |
| 88 | + end if |
| 89 | + print |
| 90 | +end while |
0 commit comments