|
| 1 | +solutions = [[0,1,2],[3,4,5],[6,7,8]] |
| 2 | +solutions += [[0,3,6],[1,4,7],[2,5,8]] |
| 3 | +solutions += [[0,4,8],[2,4,6]] |
| 4 | + |
| 5 | +TicTacToe = {} |
| 6 | +TicTacToe.grid = (" " * 9).split("") |
| 7 | +TicTacToe.markers = {true: "X",false:"O"} |
| 8 | +TicTacToe.AI = "X" |
| 9 | +TicTacToe.human = "O" |
| 10 | +TicTacToe.next = "X" |
| 11 | +TicTacToe.lastMove = -1 |
| 12 | + |
| 13 | +TicTacToe.show = function |
| 14 | + print " " + self.grid[0:3].join(" ! ") |
| 15 | + print "---+---+---" |
| 16 | + print " " + self.grid[3:6].join(" ! ") |
| 17 | + print "---+---+---" |
| 18 | + print " " + self.grid[6:].join(" ! ") |
| 19 | + print |
| 20 | + print |
| 21 | +end function |
| 22 | + |
| 23 | +TicTacToe.set = function(player, pos) |
| 24 | + if self.grid[pos] != " " then |
| 25 | + return false |
| 26 | + end if |
| 27 | + self.grid[pos] = player |
| 28 | + self.lastMove = pos |
| 29 | + return true |
| 30 | +end function |
| 31 | + |
| 32 | +TicTacToe.setMarkers = function(mark) |
| 33 | + self.human = self.markers[mark == "X"] |
| 34 | + self.AI = self.markers[mark == "O"] |
| 35 | +end function |
| 36 | + |
| 37 | +TicTacToe.getWinner = function |
| 38 | + for mark in self.markers.values |
| 39 | + for solution in solutions |
| 40 | + cnt = 0 |
| 41 | + for i in solution |
| 42 | + cnt += (self.grid[i] == mark) |
| 43 | + end for |
| 44 | + if cnt == 3 then return mark |
| 45 | + end for |
| 46 | + end for |
| 47 | + return null |
| 48 | +end function |
| 49 | + |
| 50 | +TicTacToe.potentialWins = function |
| 51 | + potential = {"X": [], "O": []} |
| 52 | + for mark in self.markers.values |
| 53 | + for solution in solutions |
| 54 | + cnt = 0 |
| 55 | + emptyCells = [] |
| 56 | + for i in solution |
| 57 | + cnt += (self.grid[i] == mark) |
| 58 | + if self.grid[i] == " " then emptyCells.push(i) |
| 59 | + end for |
| 60 | + if cnt == 2 and emptyCells.len == 1 then potential[mark].push(emptyCells[0]) |
| 61 | + end for |
| 62 | + end for |
| 63 | + return potential |
| 64 | +end function |
| 65 | + |
| 66 | +TicTacToe.moveAvailable = function |
| 67 | + return self.grid.indexOf(" ") != null |
| 68 | +end function |
| 69 | + |
| 70 | +TicTacToe.selectAI = function |
| 71 | + if self.grid[4] == " " then return 4 |
| 72 | + |
| 73 | + potential = self.potentialWins |
| 74 | + |
| 75 | + AIWins = potential[self.AI] |
| 76 | + if AIWins.len >0 then return AIWins[0] |
| 77 | + |
| 78 | + HumanWins = potential[self.human] |
| 79 | + |
| 80 | + if HumanWins.len > 0 then return HumanWins[0] |
| 81 | + |
| 82 | + if [1,3,5,7].indexOf(self.lastMove) != null then |
| 83 | + for corner in [8,6,2,0] |
| 84 | + if self.grid[corner] == " " then |
| 85 | + self.grid[corner] = self.AI |
| 86 | + potential = self.potentialWins |
| 87 | + self.grid[corner] = " " |
| 88 | + AIWins = potential[self.AI] |
| 89 | + if AIWins.len > 0 then return corner |
| 90 | + end if |
| 91 | + end for |
| 92 | + else |
| 93 | + for side in [1,3,5,7] |
| 94 | + if self.grid[side] == " " then |
| 95 | + self.grid[side] = self.AI |
| 96 | + potential = self.potentialWins |
| 97 | + self.grid[side] = " " |
| 98 | + AIWins = potential[self.AI] |
| 99 | + if AIWins.len > 0 then return side |
| 100 | + end if |
| 101 | + end for |
| 102 | + end if |
| 103 | + for ix in range(0,8) |
| 104 | + if self.grid[ix] == " " then return ix |
| 105 | + end for |
| 106 | + |
| 107 | + return null |
| 108 | +end function |
| 109 | + |
| 110 | +print " " * 15 + "Creative Computing Morristown, New Jersey" |
| 111 | +print; print; print |
| 112 | +print "The board is numbered." |
| 113 | +print; print; print |
| 114 | +print " 1 2 3" |
| 115 | +print " 4 5 6" |
| 116 | +print " 7 8 9" |
| 117 | +print |
| 118 | +ans = input("Do you want to 'X' or 'O'? ") |
| 119 | +if ans.upper != "X" then ans = "O" |
| 120 | +TicTacToe.setMarkers(ans.upper) |
| 121 | +winner = null |
| 122 | +print |
| 123 | +while TicTacToe.moveAvailable and winner == null |
| 124 | + |
| 125 | + if TicTacToe.next == TicTacToe.AI then |
| 126 | + move = TicTacToe.selectAI |
| 127 | + TicTacToe.set(TicTacToe.AI, move) |
| 128 | + TicTacToe.next = TicTacToe.human |
| 129 | + print "The computer moves to..." |
| 130 | + else |
| 131 | + move = input("Where do you move? ").val |
| 132 | + if move < 1 or move > 9 then |
| 133 | + print "Thanks for the game." |
| 134 | + exit |
| 135 | + else if not TicTacToe.set(TicTacToe.human, move-1) then |
| 136 | + print "That square is occupied." |
| 137 | + print |
| 138 | + continue |
| 139 | + else |
| 140 | + TicTacToe.next = TicTacToe.AI |
| 141 | + end if |
| 142 | + end if |
| 143 | + |
| 144 | + TicTacToe.show |
| 145 | + winner = TicTacToe.getWinner |
| 146 | +end while |
| 147 | + |
| 148 | +if winner == null then |
| 149 | + print "It's a draw. Thank you." |
| 150 | +else if winner == TicTacToe.AI then |
| 151 | + print "I win, turkey!!" |
| 152 | +else |
| 153 | + print "You beat me! Good game!" |
| 154 | +end if |
| 155 | + |
0 commit comments