|
| 1 | +# |
| 2 | +# hw10pr3 |
| 3 | +# Connect Four: AI |
| 4 | +# |
| 5 | +# Due: 11/18/2013 |
| 6 | + |
| 7 | +""" Includes an errorchecker function which makes it more difficult to break while entering moves """ |
| 8 | +import random |
| 9 | +import time |
| 10 | +from graphics import * |
| 11 | + |
| 12 | +class Player: |
| 13 | + """ A player with a certain type of move token and can evaluate the board for future moves """ |
| 14 | + def __init__(self, ox, tbt, ply): |
| 15 | + """ the constructor """ |
| 16 | + self.ox = ox.upper() |
| 17 | + self.tbt = tbt |
| 18 | + self.ply = ply |
| 19 | + |
| 20 | + def __repr__( self ): |
| 21 | + """ creates an appropriate string """ |
| 22 | + s = "Player for " + self.ox + "\n" |
| 23 | + s += " with tiebreak type: " + self.tbt + "\n" |
| 24 | + s += " and ply == " + str(self.ply) + "\n\n" |
| 25 | + return s |
| 26 | + |
| 27 | + def oppCh(self): |
| 28 | + """ returns the opposite token as the player calling this """ |
| 29 | + if self.ox == 'O': |
| 30 | + return 'X' |
| 31 | + else: return 'O' |
| 32 | + |
| 33 | + def scoreBoard(self, b): |
| 34 | + """ returns a float value representing the score of the input b """ |
| 35 | + if b.winsFor(self.ox): |
| 36 | + return 100.0 |
| 37 | + elif b.winsFor(self.oppCh()): |
| 38 | + return 0.0 |
| 39 | + else: |
| 40 | + return 50.0 |
| 41 | + |
| 42 | + def tiebreakMove(self, scores): |
| 43 | + """ takes in a nonempty list of floating-point numbers. Choses the highest score, returning its column number: breaks ties based on the tiebreaking type of the player calling the function""" |
| 44 | + possible_moves = [] |
| 45 | + m = max(scores) |
| 46 | + for i in range(len(scores)): |
| 47 | + if scores[i] == m: |
| 48 | + possible_moves += [i] |
| 49 | + if self.tbt in ['random', 'Random', 'rdm', 'rand', 'RANDOM']: |
| 50 | + return random.choice(possible_moves) |
| 51 | + elif self.tbt in ['left', 'Left', 'LEFT']: |
| 52 | + return possible_moves[0] |
| 53 | + elif self.tbt in ['right', 'Right', 'RIGHT']: |
| 54 | + return possible_moves[-1] |
| 55 | + |
| 56 | + def scoresFor(self, b): |
| 57 | + """ Returns a list of scores, with the element of the list corresponding to the "goodness" of the input board after the number of moves represented by the index """ |
| 58 | + scores = [50.0]*len(b.data[0]) |
| 59 | + for col in range(len(b.data[0])): |
| 60 | + if b.data[0][col] != ' ': |
| 61 | + scores[col] = -1 |
| 62 | + elif self.scoreBoard(b) != 50.0: |
| 63 | + scores[col] = self.scoreBoard(b) |
| 64 | + elif self.ply == 0: |
| 65 | + scores[col] = self.scoreBoard(b) |
| 66 | + else: |
| 67 | + b.addMove(col, self.ox, "noShow") |
| 68 | + if self.scoreBoard(b) == 100.0: |
| 69 | + scores[col] = 100.0 |
| 70 | + else: |
| 71 | + p2 = Player(self.oppCh(), self.tbt, self.ply -1) |
| 72 | + p2scores = p2.scoresFor(b) |
| 73 | + if 100.0 in p2scores: |
| 74 | + scores[col] = 0.0 |
| 75 | + elif 50.0 in p2scores: |
| 76 | + scores[col] = 50.0 |
| 77 | + else: |
| 78 | + scores[col] = 100.0 |
| 79 | + b.delMove(col) |
| 80 | + return scores |
| 81 | + |
| 82 | + def nextMove(self,b): |
| 83 | + """ Takes in b, and object of type Board and returns an integer representing the column number that the player will next move to """ |
| 84 | + possmoves = self.scoresFor(b) |
| 85 | + return self.tiebreakMove(possmoves) |
| 86 | + |
| 87 | +class Board: |
| 88 | + """ A board with an arbitrary certain height and width and data representing tokens filling the connect four slots""" |
| 89 | + |
| 90 | + def __init__(self, width, height): |
| 91 | + """ constructs Board type objects """ |
| 92 | + self.width = width |
| 93 | + self.height = height |
| 94 | + W = self.width |
| 95 | + H = self.height |
| 96 | + self.data = [[' ']*W for row in range(H)] |
| 97 | + self.win = GraphWin("Connect_Four", 60*(width), 60*(height+1)) |
| 98 | + self.graphical_data = [] |
| 99 | + for row in range(height+1): |
| 100 | + new_row = [] |
| 101 | + for col in range(width): |
| 102 | + if row < height: |
| 103 | + #the center is a Point (pixel col, pixel row) |
| 104 | + center = Point(50 + 50*col, 50 + 50*row) |
| 105 | + c = Circle(center, 10) |
| 106 | + c.draw(self.win) |
| 107 | + new_row += [c] |
| 108 | + else: |
| 109 | + center = Point(50 + 50*col, 50 + 50*row) |
| 110 | + c = Text(center, col%10) |
| 111 | + c.draw(self.win) |
| 112 | + new_row += [c] |
| 113 | + self.graphical_data += [new_row] |
| 114 | + |
| 115 | + |
| 116 | + def __repr__(self): |
| 117 | + """ Prints a viusal representation of a board """ |
| 118 | + H = self.height |
| 119 | + W = self.width |
| 120 | + s = '' #the string to return |
| 121 | + for row in range(0,H): |
| 122 | + s += '|' |
| 123 | + for col in range(0,W): |
| 124 | + s += self.data[row][col] + '|' |
| 125 | + s += '\n' |
| 126 | + s += (2*W+1)*'-' #bottom of board |
| 127 | + n = ' ' #sets first character to a space |
| 128 | + for num in range(W): |
| 129 | + n += str(num%10) + ' ' #adds the next cardinal digit |
| 130 | + s += '\n' + n |
| 131 | + return s |
| 132 | + |
| 133 | + def addMove(self, col, ox, display = "show"): |
| 134 | + """Places the given character into the given column""" |
| 135 | + H = self.height |
| 136 | + for vert in range(H-1,-1,-1): |
| 137 | + if self.data[vert][col] == ' ': |
| 138 | + self.data[vert][col] = ox |
| 139 | + break |
| 140 | + if display == "noShow": |
| 141 | + return |
| 142 | + for row in range(self.height): |
| 143 | + for width in range(self.width): |
| 144 | + if self.data[row][width] == "X": |
| 145 | + color = 'red' |
| 146 | + self.graphical_data[row][width].setFill(color) |
| 147 | + elif self.data[row][width] == "O": |
| 148 | + color = 'black' |
| 149 | + self.graphical_data[row][width].setFill(color) |
| 150 | + else: |
| 151 | + color = 'white' |
| 152 | + self.graphical_data[row][width].setFill(color) |
| 153 | + |
| 154 | + def clear(self): |
| 155 | + """ Clears the board that called it """ |
| 156 | + for x in range(len(self.data)): |
| 157 | + for i in range(len(self.data[x])): |
| 158 | + self.data[x][i] = ' ' |
| 159 | + for row in range(self.height): |
| 160 | + for width in range(self.width): |
| 161 | + if self.data[row][width] == "X": |
| 162 | + color = 'red' |
| 163 | + self.graphical_data[row][width].setFill(color) |
| 164 | + elif self.data[row][width] == "O": |
| 165 | + color = 'black' |
| 166 | + self.graphical_data[row][width].setFill(color) |
| 167 | + else: |
| 168 | + color = 'white' |
| 169 | + self.graphical_data[row][width].setFill(color) |
| 170 | + return |
| 171 | + |
| 172 | + def setBoard(self, moveString): |
| 173 | + """ takes in a string of columns and places alternating checkers in those columns, starting with 'X' |
| 174 | + For example, call b.setBoard('012345') to see 'X's and 'O's alternate on the bottom row, or b.setBoard('000000') to see them alternate in the left column. |
| 175 | + moveString must be a string of integers """ |
| 176 | + nextCh = 'X' |
| 177 | + for colString in moveString: |
| 178 | + col = int(colString) |
| 179 | + if 0 <= col <= self.width: |
| 180 | + self.addMove(col, nextCh) |
| 181 | + if nextCh == 'X': nextCh = 'O' |
| 182 | + else: nextCh = 'X' |
| 183 | + |
| 184 | + def allowsMove(self, c): |
| 185 | + """ Checks if a given move is legal, if so returns True, else returns False """ |
| 186 | + if c < 0 or c >= self.width: |
| 187 | + return False |
| 188 | + if self.data[0][c] != ' ': |
| 189 | + return False |
| 190 | + return True |
| 191 | + |
| 192 | + def isFull(self): |
| 193 | + """ Checks if the top row is completely full, if so, return True, if not, return False """ |
| 194 | + for col in range(self.width): |
| 195 | + if self.allowsMove(col): #If you can drop a token in anywhere, then the board isn't full (assuming gravity, and we haven't hacked the system and made tokens float along the top. If he had done that, we could always just check each element of each list of the "data" list, much as we did in the self.clear() function. |
| 196 | + return False |
| 197 | + return True |
| 198 | + |
| 199 | + def delMove(self, c): |
| 200 | + """ Deltetes the topmost token from column c. If column c is empty, nothing happens """ |
| 201 | + for row in range(self.height): |
| 202 | + if self.data[row][c] != ' ': |
| 203 | + self.data[row][c] = ' ' |
| 204 | + return |
| 205 | + |
| 206 | + def winsFor(self, ox): |
| 207 | + """ Returns true if there are four checkers of a given type ox in a row, column, or diagonal of the board """ |
| 208 | + if self.four_in_row(ox) or self.four_in_col(ox) or self.four_in_diag(ox): |
| 209 | + return True |
| 210 | + return False |
| 211 | + |
| 212 | + def four_in_row(self, ox): |
| 213 | + """ Returns true if there are four checkers of a given type ox in a row of the board """ |
| 214 | + for row in range(len(self.data)): |
| 215 | + for col in range(len(self.data[0])-3): |
| 216 | + if self.data[row][col] == ox: |
| 217 | + if self.data[row][col+1] == ox: |
| 218 | + if self.data[row][col+2] == ox: |
| 219 | + if self.data[row][col+3] == ox: |
| 220 | + return True |
| 221 | + return False |
| 222 | + |
| 223 | + def four_in_col(self, ox): |
| 224 | + """ Returns true if there are four checkers of a given type ox in a column of the board """ |
| 225 | + for col in range(len(self.data[0])): |
| 226 | + for row in range(len(self.data) - 3): |
| 227 | + if self.data[row][col] == ox: |
| 228 | + if self.data[row+1][col] == ox: |
| 229 | + if self.data[row+2][col] == ox: |
| 230 | + if self.data[row+3][col] == ox: |
| 231 | + return True |
| 232 | + return False |
| 233 | + |
| 234 | + |
| 235 | + def four_in_diag(self, ox): |
| 236 | + """ Returns true if there are four checkers of a given type ox in a diagonal of the board """ |
| 237 | + for col in range(len(self.data[0])-3): #checks for a negative victory for ox |
| 238 | + for row in range(len(self.data) - 3): |
| 239 | + if self.data[row][col] == ox: |
| 240 | + if self.data[row+1][col+1] == ox: |
| 241 | + if self.data[row+2][col+2] == ox: |
| 242 | + if self.data[row+3][col+3] == ox: |
| 243 | + return True |
| 244 | + for col in range(len(self.data[0])-3): |
| 245 | + for row in range(3,len(self.data)): |
| 246 | + if self.data[row][col] == ox: |
| 247 | + if self.data[row-1][col+1] == ox: |
| 248 | + if self.data[row-2][col+2] == ox: |
| 249 | + if self.data[row-3][col+3] == ox: |
| 250 | + return True |
| 251 | + return False |
| 252 | + # diagonal positive victory for x: b.setBoard('01122323343') |
| 253 | + # diagonal negative victory for x: b.setBoard('43322121101') |
| 254 | + # row victory for x: b.setBoard('01232435415') |
| 255 | + # column victory for x: b.setBoard('1212131') |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + def playGame(self, px, po): |
| 260 | + """ Hosts a game of connect four, calling on the nextMove method for px and po, which will be objects of type player """ |
| 261 | + while True: |
| 262 | + print("Welcome to Connect Four!") |
| 263 | + print(self) |
| 264 | + print() |
| 265 | + turn = 'X' |
| 266 | + player = 'Player 1' |
| 267 | + while True: |
| 268 | + if (turn == 'X' and px == 'human') or (turn=='O' and po == 'human'): |
| 269 | + col = errorchecker(player) |
| 270 | + if self.allowsMove(col): |
| 271 | + self.addMove(col, turn) |
| 272 | + if self.winsFor(turn): |
| 273 | + print(self) |
| 274 | + print('Good game '+player+'!') |
| 275 | + time.sleep(1.5) |
| 276 | + break |
| 277 | + elif self.isFull(): |
| 278 | + print(self) |
| 279 | + print('Board is full, game over!') |
| 280 | + break |
| 281 | + elif turn == 'X': |
| 282 | + turn = 'O' |
| 283 | + player = 'Player 2' |
| 284 | + else: |
| 285 | + turn = 'X' |
| 286 | + player = 'Player 1' |
| 287 | + print(self) |
| 288 | + print() |
| 289 | + else: |
| 290 | + print("That is not a valid move, please choose again.") |
| 291 | + else: |
| 292 | + if turn == 'X': |
| 293 | + move = px.nextMove(self) |
| 294 | + else: |
| 295 | + move = po.nextMove(self) |
| 296 | + self.addMove(move, turn) |
| 297 | + if self.winsFor(turn): |
| 298 | + print(self) |
| 299 | + print('Good game '+player+'!') |
| 300 | + time.sleep(1.5) |
| 301 | + break |
| 302 | + elif self.isFull(): |
| 303 | + print(self) |
| 304 | + print('Board is full, game over!') |
| 305 | + break |
| 306 | + elif turn == 'X': |
| 307 | + turn = 'O' |
| 308 | + player = 'Player 2' |
| 309 | + else: |
| 310 | + turn = 'X' |
| 311 | + player = 'Player 1' |
| 312 | + print(self) |
| 313 | + print() |
| 314 | + |
| 315 | + self.clear() |
| 316 | + while True: |
| 317 | + x = errorchecker_playagain() |
| 318 | + if x == True: |
| 319 | + break |
| 320 | + elif x == False: |
| 321 | + print("Goodbye") |
| 322 | + return |
| 323 | + else: |
| 324 | + print("I don't recognize that input.") |
| 325 | + |
| 326 | +def errorchecker(player): |
| 327 | + """ Tries to convert x into a number, if unable, prompts for another input without crashing """ |
| 328 | + while True: |
| 329 | + try: |
| 330 | + x = input(player+" make your move: ") |
| 331 | + x = int(round(float(x))) |
| 332 | + return x |
| 333 | + except (ValueError,TypeError,NameError,IOError): #I found these types of errors on a python site: http://docs.python.org/2/tutorial/errors.html |
| 334 | + print("Oops! That was not a valid number. Try again...") |
| 335 | + |
| 336 | +def errorchecker_playagain(): |
| 337 | + """ Tries to make sense of what the user wants to do """ |
| 338 | + again = input('Would you like to play again? ') |
| 339 | + while True: |
| 340 | + try: |
| 341 | + if again in ['1', 'yes', 'Yes', 'okay', 'Alright', 'alright', 'y', 'Y', 'absolutely', 'Absolutely']: |
| 342 | + return True |
| 343 | + elif again in ['0', 'no', 'No', 'nope', 'Nope', 'n', 'N', 'NO', 'NOPE', 'never', 'Never', 'NEVER', 'NEVAR']: |
| 344 | + return False |
| 345 | + else: return 'Wat?' |
| 346 | + except (ValueError,TypeError,NameError,IOError): |
| 347 | + print("I don't recognize that input.") |
| 348 | + |
| 349 | + |
| 350 | +p1 = input("Player 1: Human or AI?\n") |
| 351 | +if p1.lower() in ['human', 'h', 'person']: |
| 352 | + p1 = 'human' |
| 353 | +elif p1.lower() in ['ai', 'a', 'artificialinteligence', 'artificial inteligence']: |
| 354 | + ply = int(input("Player 1 ply?\n")) |
| 355 | + p1 = Player('O', 'random', ply) |
| 356 | +else: |
| 357 | + print('Did not recognize that command!') |
| 358 | + time.sleep(1) |
| 359 | + quit() |
| 360 | +p2 = input("Player 2: Human or AI?\n") |
| 361 | +if p2.lower() in ['human', 'h', 'person']: |
| 362 | + p2 = 'human' |
| 363 | +elif p2.lower() in ['ai', 'a', 'artificialinteligence', 'artificial inteligence']: |
| 364 | + print("made it!") |
| 365 | + ply = int(input("Player 2 ply?\n")) |
| 366 | + p2 = Player('X', 'random', ply) |
| 367 | +else: |
| 368 | + print('Did not recognize that command!') |
| 369 | + time.sleep(1) |
| 370 | + quit() |
| 371 | +b = Board(int(input("Number of columns?\n")),int(input("Number of rows?\n"))) |
| 372 | +b.playGame(p1, p2) |
| 373 | + |
| 374 | + |
| 375 | + |
| 376 | + |
| 377 | + |
0 commit comments