def nextSituations(game, situation, player): """ returns the list of situations that can be reached from given situation by the player in the game :param game: the game :type game: a two players game :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(list<situtation>)* -- the list of situations that can be reached from given situation when player plays one round in the game """ sits = [] valid = getValidMoves(situation, player) for sit in valid: s = initSituation(game) for i in range(8): for j in range(8): s[i][j] = situation[i][j] x, y = sit[0], sit[1] l = (validMoves(s, player, x, y)) for k in l: x1, y1 = k[0], k[1] s[x1][y1] = (Player.get_spec(player)) s[x][y] = Player.get_spec(player) sits.append((s, (x, y))) return sits
def nextSituations(game, situation, player): """ returns the list of situations that can be reached from given situation by the player in the game :param game: the game :type game: a two players game :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(list<situtation>)* -- the list of situations that can be reached from given situation when player plays one round in the game """ sits = [] spec = Player.get_spec(player) for i in range(7): if isValidMove(situation, i): sit = initSituation(game) for x in range(6): for y in range(7): sit[x][y] = situation[x][y] for k in range(6): if sit[k][i] == ' ': if k == 5: sit[k][i] = Player.get_spec(player) sits.append((sit, i)) else: continue else: sit[k - 1][i] = Player.get_spec(player) sits.append((sit, i)) break return sits
def evalFunction(situation, player): """ the evaluation function for the min-max algorithm. It evaluates the given situation, the evaluation function increases with the quality of the situation for the player :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(number)* -- the score of the given situation for the given player. The better the situation for the minmax player, the higher the score. The opposite for human player. """ winner = getWinner([], situation, player) if winner != None: if Player.get_name(player) == Player.get_name(winner) == 'computer': return 100000 elif Player.get_name(player) != 'computer' and Player.get_name( winner) != 'computer': return -100000 else: return 1 counter = note = 0 spec = Player.get_spec(player) for i in range(6): for j in range(7): if situation[i][j] == Player.get_spec(player): for xdirection, ydirection in [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]: counter = 0 x, y = i, j x += xdirection y += ydirection if isOnBoard(x, y): if situation[x][y] == ' ': counter += 1 x += xdirection y += ydirection while isOnBoard(x, y) and situation[x][y] == " ": counter += 1 x += xdirection y += ydirection if counter == 3: note = 10000 else: note = -10000 else: if Player.get_name(player) == 'computer': return note elif Player.get_name(player) != 'computer': return -note
def changeValue(situation, valueToPlay, comp): """ """ x, y = valueToPlay[0], valueToPlay[1] l = (validMoves(situation, comp, x, y)) for k in l: x1, y1 = k[0], k[1] situation[x1][y1] = (Player.get_spec(comp)) situation[x][y] = Player.get_spec(comp) return situation
def handler(x, y, g, p1, p2, b, dif, eve): """ handles player's clicks and manage turns of the players :param x: the x-coordinate of the move :type x: int :param y: the y-coordinate of the move :type y: int :param g: the current situation :type g: situation :param p1: the first player :type p1: player :param p2: the second player :type p2: player :param b: buttons list :type b: list :param dif: the difficulty of the game(the depth of the IA recursivity) :type dif: int :param eve: the event :type eve: event """ global current if not othello.isFinished(g): if Player.get_name(current) != 'computer' and othello.playerCanPlay( g, g, current): g = othello.clean(g) if othello.isValidMove(g, current, x, y): valid = (othello.validMoves(g, current, x, y)) for k in valid: x1, y1 = k[0], k[1] g[x1][y1] = (Player.get_spec(current)) g[x][y] = Player.get_spec(current) current = switch_player(current, p1, p2) elif not othello.playerCanPlay(g, g, current): current = switch_player(current, p1, p2) if Player.get_name(current) == 'computer' and othello.playerCanPlay( g, g, current): g = othello.clean(g) g = AlphaBeta.IA(g, current, switch_player(current, p1, p2), dif, othello) current = switch_player(current, p1, p2) hints(g, b) __redraw(g, b) othello.clean(g) if othello.isFinished(g): winner = checkwinner(g, g, current, p1, p2, othello) _disable(g, b) msg = finalState(winner) msgbox.showinfo("finished", msg)
def handler(column, sit, p1, p2, b, eve): """ handles the click :param column: the column of the button :type column: int :param sit: the current situation :type sit: situation :param p1: the first player :type p1: player :param p2: the second player :type p2: player :param b: buttons list :type b: list :param eve: the event :type eve: event """ global current global dif spec = Player.get_spec(current) if not puissance.isFinished(sit): if Player.get_name(current) != 'computer': if puissance.isValidMove(sit, column): for i in range(6): if sit[i][column] == ' ': if i == 5: sit[i][column] = Player.get_spec(current) else: continue else: sit[i - 1][column] = Player.get_spec(current) break current = switch_player(current, p1, p2) if not puissance.isFinished(sit) and Player.get_name( current) == 'computer': print(dif) sit = AlphaBeta.IA(sit, current, switch_player(current, p1, p2), dif, puissance) if dif < 8: dif += 1 current = switch_player(current, p1, p2) __redraw(sit, b) if puissance.isFinished(sit): winner = twoplayergame.checkwinner(sit, sit, current, p1, p2, puissance) _disable(sit, b) msg = finalState(winner) msgbox.showinfo("finished", msg)
def getWinner(game, situation, player): """ Gives the winner of the game that end in situation :param game: the game :type game: game :param situation: the situation which is a final game situation :type situation: a game situation :param player: the player who should have played if situation was not final (then other player plays previous turn) :type player: player :returns: *(player)* -- the winner player or None in case of tie game :CU: situation is a final situation """ spec = Player.get_spec(player) compteur = adv = 0 for i in range(8): for j in range(8): if situation[i][j] == spec: compteur += 1 if situation[i][j] != spec and situation[i][j] != ' ': adv += 1 if compteur > adv: return player else: return None
def _input_coords(situation, player): """ takes the input value from the player :param situation: situation :param player: player :return: the value """ if Player.get_spec(player) == 'X': color = "n" else: color = 'b' print( Player.get_name(player) + '(' + colors[color] + ')' + " it's your turn") coords = input("coords of cell? ") coords = coords.split(',') x = int(coords[0]) y = int(coords[1]) try: if isCheck(situation, situation[x][y][1]): l = (checkMoves(situation, situation[x][y][1])[0]) if isOnBoard(x, y) and situation[x][y][1] == color and (x, y) in l: return (x, y) else: l = canPlay(situation, x, y) if situation[x][y][1] == color and len(l) != 0: return (x, y) except: return _input_coords(situation, player) print('illegal play') return _input_coords(situation, player)
def changeValue(situation, valueToPlay, comp): """ plays the value the computer has choosen after the minmax algorithm :param situation: situation :type situation: situation :param valueToPlay: the value :type valueToPlay: a tuple :param comp: The computer :type comp: Player """ if Player.get_spec(comp) == 'X': color = "n" else: color = "b" x, y = valueToPlay[0], valueToPlay[1] v = validMoves(situation, x, y) for i, j in v: if simulateCheck(situation, x, y, i, j): if (situation[x][y] == 'Pb' and i == 0) or (situation[x][y] == 'Pn' and i == 7): situation[i][j] = 'D' + situation[x][y][1] situation[x][y] = ' ' else: situation[i][j] = situation[x][y] situation[x][y] = ' ' #situation[k[0]][k[1]] = situation[x][y] #situation[x] [y] = ' ' return situation
def evalFunction(situation, player): """ the evaluation function for the min-max algorithm. It evaluates the given situation, the evaluation function increases with the quality of the situation for the player :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(number)* -- the score of the given situation for the given player. The better the situation for the minmax player, the higher the score. The opposite for human player. """ if Player.get_spec(player) == 'X': color = "n" else: color = "b" note = 0 if getWinner("", situation, player) != None: if getWinner('', situation, player).get_name() == 'computer': return 10000 else: return -10000 else: for i in range(8): for j in range(8): if situation[i][j][1] == color: note += -len(canPlay(situation, i, j)) * 10 + minmaxValues[ situation[i][j][0]] if Player.get_name(player) == "computer": return note else: return -note
def handler(x, y, g, p1, p2, b, eve): """ handles the click :param x: x-coordinate :param y: y-coordinate :param g: situation :param p1: player :param p2: player :param b: list of buttons :param eve: the event """ global current spec = Player.get_spec(current) if g[x][y] == '': g[x][y] = spec if Player.get_name(current) != 'computer' and Player.get_name( switch_player( current, p1, p2)) == "computer" and not tictactoe.isFinished(g): g = minmax.IA(g, switch_player(current, p1, p2), current, 10, tictactoe) else: current = switch_player(current, p1, p2) __redraw(g, b) if tictactoe.isFinished(g): winner = tictactoe.getWinner(g, g, switch_player(current, p1, p2)) _disable(g, b) msg = finalState(winner) msgbox.showinfo("finished", msg)
def nextSituations(game, situation, player): """ returns the list of situations that can be reached from given situation by the player in the game :param game: the game :type game: a two players game :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(list<situtation>)* -- the list of situations that can be reached from given situation when player plays one round in the game """ if Player.get_spec(player) == 'X': color = "n" else: color = "b" res = [] values = checkMoves(situation, color)[0] sits = checkMoves(situation, color)[1] pieces = canPlay(situation, getKing(situation, color)[0], getKing(situation, color)[1]) for k in range(len(sits)): res.append((sits[k], values[k])) return res
def evalFunction(situation, player): """ the evaluation function for the min-max algorithm. It evaluates the given situation, the evaluation function increases with the quality of the situation for the player :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(number)* -- the score of the given situation for the given player. The better the situation for the minmax player, the higher the score. The opposite for human player. """ spec = Player.get_spec(player) winner = getWinner([], situation, player) name = Player.get_name(player) edge = corner = compteur = 0 for i in range(8): for j in range(8): if isOnCorner(i, j) and situation[i][j] == spec: corner += 1 if situation[i][j] == spec: compteur += 1 if isOnEdge(i, j) and situation[i][j] == spec: edge += 1 if name == 'computer' and winner == player: return 1000 + compteur + 6 * corner + 2 * edge if name != 'computer' and winner == player: return -1000 - compteur - 6 * corner - 2 * edge if winner == None: return 0
def getWinner(game, situation, player): """ Gives the winner of the game that end in situation :param game: the game :type game: game :param situation: the situation which is a final game situation :type situation: a game situation :param player: the player who should have played if situation was not final (then other player plays previous turn) :type player: player :returns: *(player)* -- the winner player or None in case of tie game :CU: situation is a final situation """ spec = Player.get_spec(player) for i in range(len(situation)): if situation[i][0] == situation[i][1] == situation[i][2] == spec : return player if i == 0: for j in range(3): if situation[i][j] == situation[i+1][j] == situation[i+2][j] == spec : return player if j == 0 or j == 2 : if situation[i][j] == situation[i+1][abs(j-1)] == situation[i+2][abs(j-2)] == spec : return player return None
def humanPlayerPlays(game, player, situation): """ makes the human player plays for given situation in the game :param game: the game :type game: game :param player: the human player :type player: player :param situation: the current situation :type situation: a game situation :returns: *(game situtation)* -- the game situation reached afte the human player play """ x, y = _input_coords(situation, player) l = (validMoves(situation, player, x, y)) for k in l: x1, y1 = k[0], k[1] situation[x1][y1] = (Player.get_spec(player)) situation[x][y] = Player.get_spec(player) return situation
def changeValue(situation, valueToPlay, comp): """ plays the move the computer has chosen :param situation: the current situation :type situation: list :param valueToPlay: the value we want play :type valueToPlay: int :param comp: the computer player :type comp: player """ y = valueToPlay for i in range(6): if situation[i][y] == ' ': if i == 5: situation[i][y] = Player.get_spec(comp) else: continue else: situation[i - 1][y] = Player.get_spec(comp) break return situation
def changeValue(situation , valueToPlay, player): """ plays the move the computer has chosen :param situation: the current situation :type situation: list :param valueToPlay: the value we want play :type valueToPlay: int :param comp: the computer player :type comp: player """ situation[valueToPlay[0]][valueToPlay[1]] = Player.get_spec(player) return situation
def humanPlayerPlays(game, player, situation): """ makes the human player plays for given situation in the game :param game: the game :type game: game :param player: the human player :type player: player :param situation: the current situation :type situation: a game situation :returns: *(game situtation)* -- the game situation reached afte the human player play """ y = _input_coords(situation, player) for i in range(6): if situation[i][y] == ' ': if i == 5: situation[i][y] = Player.get_spec(player) else: continue else: situation[i - 1][y] = Player.get_spec(player) break return situation
def humanPlayerPlays(game, player, situation): """ makes the human player plays for given situation in the game :param game: the game :type game: game :param player: the human player :type player: player :param situation: the current situation :type situation: a game situation :returns: *(game situtation)* -- the game situation reached after the human player play """ game = situation x,y = _input_coords (situation, player) situation[x][y] = Player.get_spec(player) return situation
def getValidMoves(situation, player): """ gives the valid moves of the player given :param situation: the current situation : :type situation: list :param player: the player :type player: player """ spec = Player.get_spec(player) valid = [] for i in range(8): for j in range(8): if isValidMove(situation, player, i, j): valid.append((i, j)) return valid
def validMoves(board, player, xstart, ystart): """ gives the tiles to flip :param board: the current situation :type board: list :param player: the current player :type player: player :param xstart: the x-coordinate :type xstart: int :param ystart: the y-coordinate :type ystart: int """ tile = Player.get_spec(player) otherTile = otherTil(tile) tilesToFlip = [] if board[xstart][ystart] != ' ' or not isOnBoard(xstart, ystart): return tilesToFlip board[xstart][ystart] = tile for xdirection, ydirection in [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]: x, y = xstart, ystart x += xdirection y += ydirection if isOnBoard(x, y) and board[x][y] == otherTile: x += xdirection y += ydirection if not isOnBoard(x, y): continue while board[x][y] == otherTile: x += xdirection y += ydirection if not isOnBoard(x, y): break if not isOnBoard(x, y): continue if board[x][y] == tile: while True: x -= xdirection y -= ydirection if x == xstart and y == ystart: break tilesToFlip.append([x, y]) board[xstart][ystart] = ' ' return tilesToFlip
def fini(situation, p): """ tells whether the game is finished or not :param situation: situation :type situation: situation :param p: player :type p: Player :return: boolean """ if Player.get_spec(p) == 'X': color = "n" else: color = "b" if isCheck(situation, color): checkmate = checkMoves(situation, color)[1] if len(checkmate) == 0: return True return False
def getWinner(game, situation, player): """ Gives the winner of the game that end in situation :param game: the game :type game: game :param situation: the situation which is a final game situation :type situation: a game situation :param player: the player who should have played if situation was not final (then other player plays previous turn) :type player: player :returns: *(player)* -- the winner player or None in case of tie game :CU: situation is a final situation """ if Player.get_spec(player) == 'X': color = "b" else: color = "n" if fini(situation, player): return player return None
def rightClick(x, y, sit, b, eve): """ handles the right click :param x: x-coordinate :type x: int :param y: y-coordinate :type y: int :param sit: situation :type sit: situation :param b: list of buttons :type b: list :param eve: the event """ global res, current if Player.get_spec(current) == 'X': color = 'n' else: color = 'b' try: __redraw(sit, b) if chess.isCheck(sit, sit[x][y][1]): l = chess.checkMoves(sit, sit[x][y][1])[0] if sit[x][y][1] == color and (x, y) in l: res = (x, y) b[y][x].config(background='maroon') d = chess.canPlay(sit, x, y) v = chess.validMoves(sit, x, y) for i, j in v: if (i, j) in d and chess.simulateCheck(sit, x, y, i, j): b[j][i].config(background='maroon') else: l = chess.canPlay(sit, x, y) if sit[x][y][1] == color and len(l) != 0: res = (x, y) b[y][x].config(background='maroon') for i, j in l: b[j][i].config(background='maroon') except: pass
def _input_coords(situation, player): """ manage the interaction with the human player :param situation: the current situation :type situation: list :param player: player :type player: player """ print( Player.get_name(player) + '(' + Player.get_spec(player) + ')' + " it's your turn") coord = input("Enter number of a column ? ") try: y = int(coord) if isValidMove(situation, y): return (y) except: return _input_coords(situation, player) print('illegal play') return _input_coords(situation, player)
def _input_coords(game,player): """ manage the interaction with the human player :param situation: the current situation :type situation: list :param player: player :type player: player """ print(Player.get_name(player)+'('+Player.get_spec(player)+')'+" it's your turn") coords = input("coords of cell? ") coords = coords.split(',') try : x = int(coords[0]) y = int(coords[1]) if game[x][y] == '' : return (x,y) except : return _input_coords(game,player) print('illegal play, choose an empty cell') return _input_coords(game,player)
def _input_coords(situation, player): """ manage the interaction with the human player :param situation: the current situation :type situation: list :param player: player :type player: player """ print( Player.get_name(player) + '(' + Player.get_spec(player) + ')' + " it's your turn") coords = input("coords of cell? ") coords = coords.split(',') try: x = int(coords[0]) y = int(coords[1]) if isValidMove(situation, player, x, y): return (x, y) except: return _input_coords(situation, player) print('illegal play') return _input_coords(situation, player)
def getWinner(game, situation, player): """ Gives the winner of the game that end in situation :param game: the game :type game: game :param situation: the situation which is a final game situation :type situation: a game situation :param player: the player who should have played if situation was not final (then other player plays previous turn) :type player: player :returns: *(player)* -- the winner player or None in case of tie game :CU: situation is a final situation """ spec = Player.get_spec(player) for i in range(6): for j in range(7): if situation[i][j] == spec: for xdirection, ydirection in [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]: counter = 0 x, y = i, j x += xdirection y += ydirection if isOnBoard(x, y): if situation[x][y] == spec: counter += 1 x += xdirection y += ydirection while isOnBoard(x, y) and situation[x][y] == spec: counter += 1 x += xdirection y += ydirection if counter == 3: return player return None
def nextSituations(game, situation, player): """ returns the list of situations that can be reached from given situation by the player in the game :param game: the game :type game: a two players game :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(list<situtation>)* -- the list of situations that can be reached from given situation when player plays one round in the game """ spec = Player.get_spec(player) sits = [] for i in range(3): for j in range(3): if situation[i][j] == '': sit = initSituation([]) for x in range(3): for y in range(3): sit[x][y]= situation[x][y] sit[i][j] = spec sits.append((sit,(i,j))) return sits