def find_chdrn(self, token, ind): # find children of the next move of token ind = list(ind) def find_chdrn2(token, state): # find children of the next move of token children = [] for pos in state['P']: new_state = copy_dict(state) # copy the original dict new_state['P'].remove(pos) new_state[token].append(pos) children.append(new_state) return children children = [] children_mv_pos = [] state = self.state[ind[0]][ind[1]] bd = TTT_Board(3) bd.state2board(state) """ if bd.check_finish(): bkup_score = float('-inf') for newind in self.index: if newind[0] != ind[0] and newind[1] != ind[1]: new_state = self.state[newind[0]][newind[1]] new_score = score2(new_state,token,float('-inf'),float('inf')) print(new_score) if token == self.token and new_score>bkup_score: state = new_state bkup_score = new_score if token == self.anothertoken and new_score<bkup_score: state = new_state bkup_score = new_score """ while bd.check_finish(): ind[0] = random.randint(0, 2) ind[1] = random.randint(0, 2) state = self.state[ind[0]][ind[1]] bd = TTT_Board(3) bd.state2board(state) sub_ch = find_chdrn2(token, state) for ch in sub_ch: new_mv = list(set(ch[token]) - set(state[token])) state_cp = copy_state(self.state) state_cp[ind[0]][ind[1]] = ch new_nd = TTT3_Node() new_nd.state = state_cp children.append(new_nd) children_mv_pos.append(new_mv[0]) self.children = children return children, children_mv_pos #should return the new ind
def check_finish2(self): # check to win as many as possible finish_score = [['P' for i in range(3)] for j in range(3)] for inde in self.index: s = self.state[inde[0]][inde[1]] bd = TTT_Board(self.len) bd.state2board(s) another_token = 'X' if self.token == 'O' else 'O' if bd.check_finish() == self.token: finish_score[inde[0]][inde[1]] = self.token elif bd.check_finish() == another_token: finish_score[inde[0]][inde[1]] = another_token elif s['P'] == []: finish_score[inde[0]][inde[1]] = 'D' big_bd = TTT_Board(3) big_bd.board = finish_score return big_bd.check_finish()
def start_game(self, n): self.bd = TTT_Board(n) # build the board self.bd.show() print(self.bd.check_finish()) # setup AI AI = TTT_Node() while not self.bd.check_finish(): pos = input("What position do you want to play?:\n" ) # parse the players play self.bd.board[int(pos[1])][int( pos[3])] = 'O' # need someway to alowe change role self.bd.show() # ai's move current_state = self.bd.board2state() AI.state = current_state AI.children = AI.find_chdrn('X') update_state = AI.move() print(update_state) self.bd.state2board(update_state) self.bd.show()
def find_chdrn(self, token, ind): # find children of the next move of token ind = list(ind) def find_chdrn2(token, state): # find children of the next move of token children = [] for pos in state['P']: new_state = copy_dict(state) # copy the original dict new_state['P'].remove(pos) new_state[token].append(pos) children.append(new_state) return children children = [] children_mv_pos = [] state = self.state[ind[0]][ind[1]] bd = TTT_Board(3) bd.state2board(state) while bd.check_finish(): ind[0] = random.randint(0, 2) ind[1] = random.randint(0, 2) state = self.state[ind[0]][ind[1]] bd = TTT_Board(3) bd.state2board(state) sub_ch = find_chdrn2(token, state) for ch in sub_ch: new_mv = list(set(ch[token]) - set(state[token])) state_cp = copy_state(self.state) state_cp[ind[0]][ind[1]] = ch new_nd = TTT3_Node() new_nd.state = state_cp children.append(new_nd) children_mv_pos.append(new_mv[0]) self.children = children return children, children_mv_pos
def check_finish(self): bd = TTT_Board(self.len) bd.state2board(self.state) another_token = 'X' if self.token == 'O' else 'O' if bd.check_finish() == self.token: return 2 elif bd.check_finish() == another_token: return -2 if self.state['P'] == []: return 1 return 0
def check_finish(self): # check to win single for inde in self.index: s = self.state[inde[0]][inde[1]] bd = TTT_Board(self.len) bd.state2board(s) another_token = 'X' if self.token == 'O' else 'O' if bd.check_finish() == self.token: return 1 elif bd.check_finish() == another_token: return -1 if s['P'] == []: return 0.001 return 0
def state2board(self): # b is a board object, converting a state to a board object? but there should be only one board object per game bd = TTT_Board(self.len) bd.board = [['P' for i in range(self.len)] for j in range(self.len)] bd.len = self.len for pos in self.state['X']: bd.board[pos[0]][pos[1]] = 'X' for pos in self.state['O']: bd.board[pos[0]][pos[1]] = 'O' return bd
def check_finish2(self): # check to win as many as possible finish_score = 0 for inde in self.index: s = self.state[inde[0]][inde[1]] bd = TTT_Board(self.len) bd.state2board(s) another_token = 'X' if self.token == 'O' else 'O' if not bd.check_finish(): return 0 else: if bd.check_finish() == self.token: finish_score += 1 elif bd.check_finish() == another_token: finish_score -= 1 elif s['P'] == []: finish_score += 0.0000001 return finish_score