def test_is_valid_move(): print("Testing is_valid_move...") gb = GameBoard() gb.place_piece([1,1]) if (not(gb.is_valid_move([1,1]))): print(" Success") if (gb.is_valid_move([1, 2])): print(" Success") if (not(gb.is_valid_move([1,3]))): print(" Success")
# --- Game logic mx = pygame.mouse.get_pos()[0] my = pygame.mouse.get_pos()[1] mbd = pygame.mouse.get_pressed()[0] # print(get_cell(mx, my)) loc = get_cell(mx, my) p1.update(gb, loc) turn = gb.turnNumber # print(gb.turnNumber) if mbd and not mbdPrev or False: if gb.is_valid_move(players[turn].make_move(gb)): gb.place_piece(players[turn].make_move(gb)) results = gb.get_results() if results != None: print(results) # p2.end_game(gb, results) # p1.end_game(gb, results) gb = GameBoard() gb.turnNumber = random.choice([0, 1]) if count == 1000: done = True else: gb.switch_turn()
class Persona: def copy_board(self, board): tempBoard = GameBoard() for i in range(3): for j in range(3): tempBoard.board[i][j] = board.board[i][j] return tempBoard.board def rev_board(self, board): d = {"-": "-", "O": "X", "X": "O"} s = "" for c in board: s = s + d[c] return s # this is where end of game / memory file stuff is def read_history(self, fileName): mem = dict() with open(fileName, "r") as file: split = [] # print(file.read()) for line in file.readlines(): # print(line) split = line.split(" ") split[2] = split[2][:-1] # print(split) mem[split[0]] = (float(split[1]), float(split[2])) file.close() # print(mem) return mem def write_history(self, fileName): file = open(fileName, "w") for brd in self.memory: file.write(brd) file.write(" ") file.write(str(self.memory[brd][0])[:7]) file.write(" ") file.write(str(self.memory[brd][1])) file.write("\n") file.close() def add_board_to_memory(self, board, w): if board in self.memory: self.memory[board] = ((self.memory[board][0] * self.memory[board][1] + w) / (self.memory[board][1] + 1), self.memory[board][1] + 1) else: self.memory[board] = (w, 1) rBoard = self.rev_board(board) d = {0: 1, 1: 0, self.TIEWORTH: self.TIEWORTH} if rBoard in self.memory: # self.memory[rBoard] = ((self.memory[rBoard][0] * self.memory[rBoard][1] + d[w]) / (self.memory[rBoard][1] + 1), self.memory[rBoard][1] + 1) t = True else: self.memory[rBoard] = (d[w], 1) # this is where in game memory is def add_board(self, brdStrng): if brdStrng not in self.boardStates: self.boardStates.append(brdStrng) def __init__(self, t): self.memory = self.read_history("history.txt") self.currBoard = GameBoard() self.boardStates = [GameBoard().to_string_one_line()] self.piece = t self.UNRESEARCHEDBOARDVALUE = .5 self.TIEWORTH = .7 # default operations of a "player" def Q(self, board, move): posBonus = 0 if board in self.memory: print("IN MEMORY") prevQ = self.memory[board][0] * math.log(self.memory[board][1], 10) posBonus = posBonus / self.memory[board][1] else: print("NEW MOVE") prevQ = self.UNRESEARCHEDBOARDVALUE return prevQ + posBonus def evaluate_option(self, board, move, temp): top = math.e ** (self.Q(board, move) / temp) return top def evaluate_options(self): Temperature = 5 currWinner = None editBoard = GameBoard() editBoard.board = self.copy_board(self.currBoard) score = 0 strng = "" prob = list() self.currBoard.print_board() for i in range(3): for j in range(3): if (self.currBoard.is_valid_move((i, j))): editBoard.board[i][j] = self.piece strng = editBoard.to_string_one_line() score = 0 prob.append(((i, j), self.evaluate_option(strng, (i, j), Temperature))) editBoard.board = self.copy_board(self.currBoard) bot = 0 for dub in prob: bot += dub[1] for dub in range(len(prob)): # print(prob[dub]) if dub == 0: prob[dub] = (prob[dub][0], prob[dub][1] / bot) else : prob[dub] = (prob[dub][0], prob[dub][1] / bot + prob[dub - 1][1]) rand = random() # print(prob) for dub in prob: if (rand < dub[1]): return dub[0] print("F**K") return (0, 0) def make_move(self): return self.evaluate_options() def update(self, board, pos): self.add_board(board.to_string_one_line()) if (board.to_string_one_line() != self.currBoard.to_string_one_line()): self.currBoard.board = self.copy_board(board) def end_game(self, board, w): d = {"-": self.TIEWORTH, "O": 1 if self.piece == w else 0, "X": 1 if self.piece == w else 0} # print(self.boardStates) # print("") self.add_board(board.to_string_one_line()) [self.add_board_to_memory(b, d[w]) for b in self.boardStates] self.boardStates = [GameBoard().to_string_one_line()]