def evaluate(self, game_state: GameState, player_now): if (game_state.hasWon(player_now)): return 10000 if (game_state.hasWon(1 - player_now)): return -10000 sign = 1 if (game_state.player == 1 - player_now): sign = -1 value = 0 #Tinggal memiliki satu tangan, kondisi buruk if (game_state.values[game_state.player].__contains__(0)): value -= sign * 100 if (game_state.values[1 - game_state.player].__contains__(0)): value += sign * 100 #mempunyai tangan dengan jari 4 lebih beresiko daripada 1 jari (5 point value) for i in range(2): value -= sign * game_state.values[game_state.player][i] * 5 value += sign * game_state.values[1 - game_state.player][i] * 5 #player memiliki state unggul ketika salah satu tangan dapat membunuh musuh 20 point value numMakeDead = 0 for i in range(2): player_tmp1 = game_state.values[game_state.player][i] for y in range(2): player_tmp2 = game_state.values[1 - game_state.player][y] if (player_tmp1 + player_tmp2 >= 5): numMakeDead += 1 value += sign * numMakeDead * 20 return value
def stateResultCallback(self, game_state: GameState): print("SLOT STATE ") game_state.print() self.changePlayerLeftHandTo(game_state.values[0][0]) self.changePlayerRightHandTo(game_state.values[0][1]) # self.player_left.setText(str(game_state.values[0][0])) # self.player_right.setText(str(game_state.values[0][1])) self.ai_left.setText(str(game_state.values[1][0])) self.ai_right.setText(str(game_state.values[1][1]))
def stateResultCallbackPlayer(self, game_state: GameState): print("SLOT STATE ") # time.sleep(1) game_state.print() self.changeAiLeftHandTo(game_state.values[1][0]) self.changeAiRightHandTo(game_state.values[1][1]) self.changePlayerLeftHandTo(game_state.values[0][0]) self.changePlayerRightHandTo(game_state.values[0][1]) self.turnStatusShow(1)
def testFourBoards(self): self.evalutor_state = Evaluator() self.game_states = [ # GameState(1, 0, 1, 1, 2) # GameState(1, 0, 1, 4, 2) GameState(1, 1, 2, 4, 1), # GameState(0, 0, 1, 2, 4), # # GameState(1, 3, 3, 0, 4), # GameState(1, 3, 3, 0, 2), # # GameState(1, 0, 3, 0, 4), # GameState(1, 0, 3, 0, 2), # # GameState(1, 0, 1, 3, 4), # GameState(1, 0, 1, 0, 2), ] for i in self.game_states: gs_rating = self.evalutor_state.evaluate(i, 1) print("utility : ", gs_rating, end='\t state: ') i.print() Ai = AiMove2() Ai.predictMove(self.game_states[0])
def playGame(self, val): self.evalutor_state = Evaluator() print("------------------PLAY PALY------YOU CHOOSE : ") self.Ai = AiMove() self.finger = None self.util = None self.state = None self.game_states.print() # while self.is_game_running: if self.game_states.player == 0: player_left = self.game_states.values[0][0] player_right = self.game_states.values[0][1] ai_left = self.game_states.values[1][0] ai_right = self.game_states.values[1][1] # val = input("masukan") self.game_mtx.acquire() if val == 0: # print("Masuk") self.game_states = (GameState(1, player_left, player_right, (player_left + ai_left) % 5, ai_right)) if val == 1: self.game_states = (GameState(1, player_left, player_right, ai_left, (player_left + ai_right) % 5)) if val == 2: self.game_states = (GameState(1, player_left, player_right, ai_left, (player_right + ai_right) % 5)) if val == 3: self.game_states = (GameState(1, player_left, player_right, (player_right + ai_left) % 5, ai_right)) if val == 4: if ((player_left + player_right) % 2 == 0): self.game_states = (GameState( 1, int((player_left + player_right) / 2), int((player_left + player_right) / 2), ai_left, ai_right)) self.game_mtx.release() print("\n------------------------YOU CHOOSE : ") self.game_states.print() self.resultPlayerSignal.emit(self.game_states)
def playGameLoop(self): self.evalutor_state = Evaluator() self.game_states = GameState(0, 1, 1, 1, 1) self.Ai = AiMove() self.finger = None self.util = None self.state = None self.game_states.print() while True: if self.game_states.player == 0: player_left = self.game_states.values[0][0] player_right = self.game_states.values[0][1] ai_left = self.game_states.values[1][0] ai_right = self.game_states.values[1][1] val = input("PERINTAH : ") print(val) val = int(val) if val == 0: # print("Masuk") self.game_states = (GameState(1, player_left, player_right, (player_left + ai_left) % 5, ai_right)) if val == 1: self.game_states = (GameState( 1, player_left, player_right, ai_left, (player_left + ai_right) % 5)) if val == 2: self.game_states = (GameState( 1, player_left, player_right, ai_left, (player_right + ai_right) % 5)) if val == 3: self.game_states = (GameState(1, player_left, player_right, (player_right + ai_left) % 5, ai_right)) if val == 4: if ((player_left + player_right) % 2 == 0): self.game_states = (GameState( 1, int((player_left + player_right) / 2), int((player_left + player_right) / 2), ai_left, ai_right)) print("\n------------------------YOU CHOOSE : ") self.game_states.print() else: print("AI TURN") self.finger, self.util, self.state = self.Ai.predictMove( self.game_states) self.state.print() self.game_states = self.state if self.util >= 10000: print("AI WIN, YOU LOOSE!") break # if __name__ == '__main__': # test_game = TheGame() # test_game.testFourBoards()
def tapToAll(self, game_state: GameState): self.eval = Evaluator() player_left = game_state.values[0][0] player_right = game_state.values[0][1] ai_left = game_state.values[1][0] ai_right = game_state.values[1][1] probability_move = [] utility_value = [0, 0, 0, 0, 0] if (game_state.player == 1): #Ai turn probability_move.append( GameState(0, (player_left + ai_left) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_left) % 5, ai_left, ai_right)) probability_move.append( GameState(0, (player_left + ai_right) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_right) % 5, ai_left, ai_right)) if ((ai_left + ai_right) % 2 == 0): probability_move.append( GameState(0, player_left, player_right, int((ai_left + ai_right) / 2), int((ai_left + ai_right) / 2))) else: #Player turn probability_move.append( GameState(1, player_left, player_right, (player_left + ai_left) % 5, ai_right)) probability_move.append( GameState(1, player_left, player_right, (player_right + ai_left) % 5, ai_right)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_left + ai_right) % 5)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_right + ai_right) % 5)) if ((player_left + player_right) % 2 == 0): probability_move.append( GameState(1, int((player_left + player_right) / 2), int((player_left + player_right) / 2), ai_left, ai_right)) for i in range(len(probability_move)): print("i:", i, end=' \t') probability_move[i].print() if ai_left == 0: utility_value[0] = utility_value[1] = None if ai_right == 0: utility_value[2] = utility_value[3] = None if player_left == 0: utility_value[0] = utility_value[2] = None if player_right == 0: utility_value[1] = utility_value[3] = None if ((ai_left == ai_right) or (((ai_left + ai_right) % 2) != 0) ) and probability_move[i].player == 0: utility_value[4] = None if ((player_left == player_right) or ((player_left + player_right) % 2) != 0 ) and probability_move[i].player == 1: utility_value[4] = None if utility_value[i] is not None: utility_value[i] = self.eval.evaluate(probability_move[i], 1) print("UTILITY ", utility_value) return utility_value, probability_move
def findMove(self, game_state: GameState, iter_i, result_util, result_finger): self.eval = Evaluator() player_left = game_state.values[0][0] player_right = game_state.values[0][1] ai_left = game_state.values[1][0] ai_right = game_state.values[1][1] probability_move = [] utility_value = [0, 0, 0, 0, 0] if (game_state.player == 1): # Ai turn probability_move.append( GameState(0, (player_left + ai_left) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_left) % 5, ai_left, ai_right)) probability_move.append( GameState(0, (player_left + ai_right) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_right) % 5, ai_left, ai_right)) if ((ai_left + ai_right) % 2 == 0): probability_move.append( GameState(0, player_left, player_right, int((ai_left + ai_right) / 2), int((ai_left + ai_right) / 2))) else: # Player turn probability_move.append( GameState(1, player_left, player_right, (player_left + ai_left) % 5, ai_right)) probability_move.append( GameState(1, player_left, player_right, (player_right + ai_left) % 5, ai_right)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_left + ai_right) % 5)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_right + ai_right) % 5)) if ((player_left + player_right) % 2 == 0): probability_move.append( GameState(1, int((player_left + player_right) / 2), int((player_left + player_right) / 2), ai_left, ai_right)) pass if ai_left == 0: utility_value[0] = utility_value[1] = None if ai_right == 0: utility_value[2] = utility_value[3] = None if player_left == 0: utility_value[0] = utility_value[2] = None if player_right == 0: utility_value[1] = utility_value[3] = None for i in range(len(probability_move)): print("i:",i ,end=' \t') probability_move[i].print() if ((ai_left == ai_right) or (((ai_left+ai_right) %2)!=0)) and probability_move[i].player == 0: utility_value[4] = None if ((player_left == player_right) or ((player_left+player_right) %2) != 0) and probability_move[i].player == 1: utility_value[4] = None if utility_value[i] is not None: utility_value[i] = self.eval.evaluate(probability_move[i],1) print("UTILITY ", utility_value) for i in range(len(utility_value)): if utility_value[i] is not None: if result_util is None: result_util = utility_value[i] result_finger = i elif probability_move[i].player == 0: if result_util < utility_value[i]: result_util = utility_value[i] result_finger = i else: if result_util > utility_value[i]: result_util = utility_value[i] result_finger = i print("MAX UTIL ",result_util, " iter:", iter_i) # return result_util, probability_move[result_finger], result_finger if iter_i == 0: return result_util, probability_move, result_finger, utility_value elif iter_i == 1: print("REUSL UTIL : ",result_util) return result_util else: for i in range(len(utility_value)): if utility_value[i] is not None: self.findAllMove(probability_move[i],iter_i-1)
def tapToAll(self, game_state: GameState): probability_move = [] # print(" PROB 2-------") player_left = game_state.values[0][0] player_right = game_state.values[0][1] ai_left = game_state.values[1][0] ai_right = game_state.values[1][1] if (game_state.player == 1): #Ai turn probability_move.append( GameState(0, (player_left + ai_left) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_left) % 5, ai_left, ai_right)) probability_move.append( GameState(0, (player_left + ai_right) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_right) % 5, ai_left, ai_right)) if ((ai_left + ai_right) % 2 == 0): probability_move.append( GameState(0, player_left, player_right, int((ai_left + ai_right) / 2), int((ai_left + ai_right) / 2))) else: #Player turn probability_move.append( GameState(1, player_left, player_right, (player_left + ai_left) % 5, ai_right)) probability_move.append( GameState(1, player_left, player_right, (player_right + ai_left) % 5, ai_right)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_left + ai_right) % 5)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_right + ai_right) % 5)) if ((player_left + player_right) % 2 == 0): probability_move.append( GameState(1, int((player_left + player_right) / 2), int((player_left + player_right) / 2), ai_left, ai_right)) # for i in range(len(probability_move)): # probability_move[i].print() # pass # for i in range(len(probability_move)): rating = self.eval.evaluate(probability_move[i], 1) if i == 0: AiMove.best_utility = rating AiMove.best_state = probability_move[i] if probability_move[i].player == 0: if rating > AiMove.best_utility: AiMove.best_utility = rating AiMove.best_state = probability_move[i] AiMove.which_finger = i else: if rating < AiMove.best_utility: AiMove.best_utility = rating AiMove.best_state = probability_move[i] AiMove.which_finger = i # print("utility", rating, end='\t State : ') # probability_move[i].print() # print(" PROB 2-------") # if(AiMove.layer_tree <= 3): # AiMove.layer_tree+=1 # for i in range(len(probability_move)): # print(" PROB222 -------") # probability_move[i].print() # print("utility", self.eval.evaluate(probability_move[i],probability_move[i].player)) # self.tapToAll(probability_move[i]) # print("best from serc: ", AiMove.best_utility) return probability_move, AiMove.best_utility, AiMove.best_state
def tapToAll(self, game_state: GameState): probability_move = [] #print(" PROB 2-------") player_left = game_state.values[0][0] player_right = game_state.values[0][1] ai_left = game_state.values[1][0] ai_right = game_state.values[1][1] blocked_finger = [0,0,0,0,0] blocked_finger_pl = [0, 0, 0, 0,0] if player_left == 0: blocked_finger_pl[0] = 1 blocked_finger_pl[1] = 1 if player_right == 0: blocked_finger_pl[2] = 1 blocked_finger_pl[3] = 1 if ai_left == 0: blocked_finger[0] = 1 blocked_finger[1] = 1 if ai_right == 0: blocked_finger[2] = 1 blocked_finger[3] = 1 if(game_state.player == 1): #Ai turn probability_move.append( GameState(0, (player_left + ai_left) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_left) % 5, ai_left, ai_right)) probability_move.append( GameState(0, (player_left + ai_right) % 5, player_right, ai_left, ai_right)) probability_move.append( GameState(0, player_left, (player_right + ai_right) % 5, ai_left, ai_right)) if ((ai_left + ai_right) % 2 ==0): probability_move.append( GameState(0, player_left, player_right, int((ai_left + ai_right)/2), int((ai_left + ai_right)/2))) else: #Player turn probability_move.append( GameState(1, player_left, player_right, (player_left + ai_left) % 5,ai_right)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_left + ai_right) % 5)) probability_move.append( GameState(1, player_left, player_right, (player_right + ai_left) % 5, ai_right)) probability_move.append( GameState(1, player_left, player_right, ai_left, (player_right + ai_right) % 5)) if ((player_left + player_right) % 2 ==0): probability_move.append( GameState(1, int((player_left + player_right)/2), int((player_left + player_right)/2), ai_left, ai_right)) # for i in range(len(probability_move)): # probability_move[i].#print() # pass # for i in range(len(probability_move)): rating = self.eval.evaluate(probability_move[i],1) # if i == 0: # AiMove.best_utility = rating # AiMove.best_state = probability_move[i] if probability_move[i].player == 0: if i == 0: AiMove.best_utility = -11111 if rating > AiMove.best_utility and (blocked_finger[i] != 1 and blocked_finger_pl[i] != 1): #print("INI MASUK ai", i) AiMove.best_utility = rating AiMove.best_state = probability_move[i] AiMove.which_finger = i else: if i == 0: AiMove.best_utility = 11111 #print("i : ", i, " block ", blocked_finger_pl[i], "block ai ", blocked_finger[i]) if rating < AiMove.best_utility and (blocked_finger_pl[i] != 1 and blocked_finger[i] != 1): #print("INI MASUK ",i) AiMove.best_utility = rating AiMove.best_state = probability_move[i] AiMove.which_finger = i #print("utility", rating, end='\t State : ') probability_move[i].print() #print("best from serc: ", AiMove.best_utility) return probability_move, AiMove.best_utility, AiMove.best_state
def resetState(self): self.game_mtx.acquire() self.game_states = GameState(0, 1, 1, 1, 1) self.game_mtx.release()
def __init__(self): super().__init__() self.is_game_running = False self.game_mtx = Lock() self.game_states = GameState(0, 1, 1, 1, 1)
class TheGame(QObject): resultGameSignal = Signal(int) resultStateSignal = Signal(object) resultAiSignal = Signal(object) resultPlayerSignal = Signal(object) resultLoseSignal = Signal(object) animateAiSignal = Signal(int) def __init__(self): super().__init__() self.is_game_running = False self.game_mtx = Lock() self.game_states = GameState(0, 1, 1, 1, 1) def resetState(self): self.game_mtx.acquire() self.game_states = GameState(0, 1, 1, 1, 1) self.game_mtx.release() @Slot(int) def playGame(self, val): self.evalutor_state = Evaluator() print("------------------PLAY PALY------YOU CHOOSE : ") self.Ai = AiMove() self.finger = None self.util = None self.state = None self.game_states.print() # while self.is_game_running: if self.game_states.player == 0: player_left = self.game_states.values[0][0] player_right = self.game_states.values[0][1] ai_left = self.game_states.values[1][0] ai_right = self.game_states.values[1][1] # val = input("masukan") self.game_mtx.acquire() if val == 0: # print("Masuk") self.game_states = (GameState(1, player_left, player_right, (player_left + ai_left) % 5, ai_right)) if val == 1: self.game_states = (GameState(1, player_left, player_right, ai_left, (player_left + ai_right) % 5)) if val == 2: self.game_states = (GameState(1, player_left, player_right, ai_left, (player_right + ai_right) % 5)) if val == 3: self.game_states = (GameState(1, player_left, player_right, (player_right + ai_left) % 5, ai_right)) if val == 4: if ((player_left + player_right) % 2 == 0): self.game_states = (GameState( 1, int((player_left + player_right) / 2), int((player_left + player_right) / 2), ai_left, ai_right)) self.game_mtx.release() print("\n------------------------YOU CHOOSE : ") self.game_states.print() self.resultPlayerSignal.emit(self.game_states) def playGameAi(self): print("AI TURN") self.finger, self.util, self.state = self.Ai.predictMove( self.game_states) self.state.print() self.game_states = self.state print("--------FINGER AI ", self.finger) if self.util >= 10000: print("AI WIN, YOU LOOSE!") self.game_states.values[2].append(self.finger) self.resultAiSignal.emit(self.game_states) self.resultLoseSignal.emit(self.game_states) else: # self.animateAiSignal.emit(self.finger) self.game_states.values[2].append(self.finger) self.resultAiSignal.emit(self.game_states) # if __name__ == '__main__': # test_game = TheGame() # test_game.testFourBoards()