class WildPlay: def __init__(self, AI=("random", "random"), AI_iteration=(0, 0)): self.game = None self.turn = 0 self.AI = AI self.clf = None self.AI_iteration = AI_iteration def initialize(self): if self.AI[0] == "sklearn" or self.AI[1] == "sklearn": print("importing training_set_X") from training_set_X_10000_1_test import X as training_set_X from training_set_Y_10000_1_test import Y as training_set_Y self.clf = svm.SVR(kernel='linear') print("fitting sklearn") self.clf.fit(training_set_X, training_set_Y) def reset_game(self): self.game = MagicCardGame() self.game.initialize() self.turn = 0 def get_current_choice(self): if self.AI[self.game.get_current_player()] == "random": ## random choice available_set = () if len(self.game.available_attackers()) != 0: available_set += (2, ) elif len(self.game.available_play_cards()) != 0: available_set += (1, ) else: available_set += (3, ) inp = available_set[0] if inp == 1: card_id = random.choice(self.game.available_play_cards()) available_set += (card_id, ) target = None if cards[card_id][3] is not None: target = random.choice( self.game.available_play_card_targets(card_id)) available_set += (target, ) elif inp == 2: attacker_position = random.choice( self.game.available_attackers()) target_position = random.choice( self.game.available_attacking_targets(attacker_position)) available_set += ( attacker_position, target_position, ) elif inp == 3: available_set += (None, None) else: print("Invalid option!") return available_set, None elif self.AI[self.game.get_current_player()] == "minimax": ## minimax choice minimax = MinimaxWithSampling(self.game) current_choice = minimax.search() win_rate = minimax.max_utility return current_choice, win_rate elif self.AI_iteration[self.game.get_current_player()] > 0: ## sklearn AI choice return SklearnAI(self.game, self.clf).get_best_choice() else: raise Exception("Invalid AI!") def play_game(self, verbose=False): if verbose: self.game.show_state() while not self.game.is_end(): self.turn += 1 self.game.start_turn() if self.game.is_end(): break inp = 0 while inp != 3: ## input("Enter any key to continue: ") if verbose: self.game.show_state() self.game.show_available_play_cards() self.game.show_available_attackers() current_choice, win_rate = self.get_current_choice() inp = current_choice[0] if verbose: print("Action:", inp) if inp == 1: card_id = current_choice[1] if verbose: print("Playing card:", cards[card_id][0]) target = current_choice[2] if verbose: print("Target =", target) self.game.play_card(card_id, target=target) if self.game.is_end(): break elif inp == 2: attacker_position = current_choice[1] if verbose: print("Attacker: ", attacker_position) self.game.show_available_attacking_targets( attacker_position) target_position = current_choice[2] if verbose: print("Attacked target:", target_position) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() if verbose: self.game.show_state() self.game.end_game() print(" run_turns = %d, Winner is player %s!" % (self.turn, self.game.get_winner()))
def reset_game(self): self.game = MagicCardGame() self.game.initialize() self.turn = 0
class SklearnIterator: def __init__(self, AI = "minimax", AI_iteration = 0): self.game = None self.X = None # X is the state self.Y = None # Y is the play choice self.AI = AI self.clf = None self.AI_iteration = AI_iteration def initialize(self): if self.AI == "sklearn": print("importing training_set_X") from training_set_X0 import X as training_set_X from training_set_Y0 import Y as training_set_Y self.clf = svm.SVR(kernel = 'linear') print("fitting sklearn") self.clf.fit(training_set_X, training_set_Y) self.X = open("C:\\Users\\Zhi Li\\Desktop\\CIS 667 Introduction to AI\\Project\\codes\\gaming part\\20181122 game with coin\\training_data\\training_set_X 50 %d.py" % AI_iteration, "w+") self.Y = open("C:\\Users\\Zhi Li\\Desktop\\CIS 667 Introduction to AI\\Project\\codes\\gaming part\\20181122 game with coin\\training_data\\training_set_Y 50 %d.py" % AI_iteration, "w+") self.X.write("X = [\n") self.Y.write("Y = [\n") def reset_game(self): self.game = MagicCardGame() self.game.initialize() def get_current_choice(self): lst = [0] * (Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 4 + 3 + 1) ## hand0(134), target0(14), battlefield0(7),battlefield_target0(7) hero0,h1,h1, end_turn if self.AI == "minimax": minimax = MinimaxWithSampling(self.game, self.X, self.Y) current_choice = minimax.search() win_rate = minimax.max_utility return current_choice, win_rate elif self.AI_iteration > 0: return SklearnAI(self.game, self.clf, self.X, self.Y).get_best_choice() else: raise Exception ("Invalid AI!") def play_game(self, verbose = False): if verbose: self.game.show_state() while not self.game.is_end(): self.game.start_turn() if self.game.is_end(): break inp = 0 while inp != 3: ## input("Enter any key to continue: ") if verbose: self.game.show_state() self.game.show_available_play_cards() self.game.show_available_attackers() current_choice, win_rate = self.get_current_choice() inp = current_choice[0] if verbose: print("Action:", inp) if inp == 1: card_id = current_choice[1] if verbose: print("Playing card:", cards[card_id][0]) if cards[card_id][3] is not None: if verbose: self.game.show_available_play_card_targets(card_id) target = current_choice[2] else: target = None if verbose: print("Target =", target) self.game.play_card(card_id, target = target) if self.game.is_end(): break elif inp == 2: attacker_position = current_choice[1] if verbose: print("Attacker: ", attacker_position) self.game.show_available_attacking_targets(attacker_position) target_position = current_choice[2] if verbose: print("Attacked target:", target_position) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() if verbose: self.game.show_state() self.game.end_game() print("Winner is player %s!" % self.game.get_winner()) def end_game(self): self.X.write("\n]") self.Y.write("\n]") self.X.close() self.Y.close()
class SklearnIterator: def __init__(self, training_size, AI="minimax", AI_iteration=0): self.game = None self.X = None # X is the state self.Y = None # Y is the play choice self.AI = AI self.clf = None self.AI_iteration = AI_iteration self.training_size = training_size self.training_count = [0] def initialize(self): if self.AI == "sklearn": print("importing training_set_X") from training_set_X_5000_0_test import X as training_set_X from training_set_Y_5000_0_test import Y as training_set_Y self.clf = svm.SVR(kernel='linear') print("fitting sklearn") self.clf.fit(training_set_X, training_set_Y) self.X = open( "C:\\Users\\Zhi Li\\Desktop\\CIS 667 Introduction to AI\\Project\\codes\\gaming part\\20181122 game with coin\\training_data\\training_set_X_%d_%d_test.py" % (training_size, AI_iteration), "w+") self.Y = open( "C:\\Users\\Zhi Li\\Desktop\\CIS 667 Introduction to AI\\Project\\codes\\gaming part\\20181122 game with coin\\training_data\\training_set_Y_%d_%d_test.py" % (training_size, AI_iteration), "w+") self.X.write("X = [\n") self.Y.write("Y = [\n") def reset_game(self): self.game = MagicCardGame() self.game.initialize() def get_current_choice(self): if randint(0, 9) < 8: ## random choice available_set = () if len(self.game.available_attackers()) != 0: available_set += (2, ) elif len(self.game.available_play_cards()) != 0: available_set += (1, ) else: available_set += (3, ) inp = available_set[0] if inp == 1: card_id = random.choice(self.game.available_play_cards()) available_set += (card_id, ) target = None if cards[card_id][3] is not None: target = random.choice( self.game.available_play_card_targets(card_id)) available_set += (target, ) elif inp == 2: attacker_position = random.choice( self.game.available_attackers()) target_position = random.choice( self.game.available_attacking_targets(attacker_position)) available_set += ( attacker_position, target_position, ) elif inp == 3: available_set += (None, None) else: print("Invalid option!") return available_set, None elif self.AI == "minimax": ## minimax choice minimax = MinimaxWithSampling(self.game, self.X, self.Y, training_count=self.training_count) current_choice = minimax.search() win_rate = minimax.max_utility return current_choice, win_rate elif self.AI_iteration > 0: ## sklearn AI choice return SklearnAI( self.game, self.clf, self.X, self.Y, training_count=self.training_count).get_best_choice() else: raise Exception("Invalid AI!") def play_game(self, verbose=False): if verbose: self.game.show_state() while not self.game.is_end(): self.game.start_turn() if self.game.is_end(): break inp = 0 while inp != 3: ## input("Enter any key to continue: ") if verbose: self.game.show_state() self.game.show_available_play_cards() self.game.show_available_attackers() current_choice, win_rate = self.get_current_choice() inp = current_choice[0] if verbose: print("Action:", inp) if inp == 1: card_id = current_choice[1] if verbose: print("Playing card:", cards[card_id][0]) target = current_choice[2] if verbose: print("Target =", target) self.game.play_card(card_id, target=target) if self.game.is_end(): break elif inp == 2: attacker_position = current_choice[1] if verbose: print("Attacker: ", attacker_position) self.game.show_available_attacking_targets( attacker_position) target_position = current_choice[2] if verbose: print("Attacked target:", target_position) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() if verbose: self.game.show_state() self.game.end_game() print("Winner is player %s!" % self.game.get_winner()) print("training_count = %d" % self.training_count[0]) return self.training_count[0] def end_game(self): self.X.write("\n]") self.Y.write("\n]") self.X.close() self.Y.close()
class PlayMinimaxSamplingWithRandom: def __init__(self): self.game = MagicCardGame() def play(self, AI_player=0, verbose=False): self.game.initialize() if verbose: self.game.show_state() r = 0 while not self.game.is_end(): r += 1 self.game.start_turn() if self.game.is_end(): break inp = -10 while inp != 3: if verbose: ## input("Enter any key to continue: ") self.game.show_state() self.game.show_available_play_cards() self.game.show_available_attackers() available_set = () if len(self.game.available_play_cards()) != 0: available_set += (1, ) if len(self.game.available_attackers()) != 0: available_set += (2, ) available_set += (3, ) minimax_result = MinimaxWithSampling(self.game).search() inp = minimax_result[0] if verbose: print("Action:", inp) if inp == 1: card_id = minimax_result[1] if verbose: print("Playing card:", cards[card_id][0]) if cards[card_id][3] is not None: if verbose: self.game.show_available_play_card_targets(card_id) target = minimax_result[2] else: target = None if verbose: print("Target =", target) self.game.play_card(card_id, target=target) if self.game.is_end(): break elif inp == 2: attacker_position = minimax_result[1] if verbose: print("Attacker: ", attacker_position) self.game.show_available_attacking_targets( attacker_position) target_position = minimax_result[2] if verbose: print("Attacked target:", target_position) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() if verbose: self.game.show_state() self.game.end_game() print("round = %s" % r, end=" ") return self.game.get_winner()
def __init__(self): self.game = MagicCardGame()
def __init__(self, verbose=False): self.game = MagicCardGame() self.verbose = verbose self.winner = -10000
class RandomSamplingPlay: def __init__(self, verbose=False): self.game = MagicCardGame() self.verbose = verbose self.winner = -10000 def initialize(self): self.game.initialize() def set_state(self, new_game): self.game = new_game def get_winner(self): return self.winner def start_game(self): if self.verbose: self.game.show_state() while not self.game.is_end(): self.game.start_turn() if self.game.is_end(): break inp = 0 while inp != 3: ## input("Enter any key to continue: ") if self.verbose: self.game.show_state() self.game.show_available_play_cards() self.game.show_available_attackers() available_set = () if len(self.game.available_play_cards()) != 0: available_set += (1, ) if len(self.game.available_attackers()) != 0: available_set += (2, ) available_set += (3, ) inp = available_set[0] if self.verbose: print("Action:", inp) if inp == 1: card_id = random.choice(self.game.available_play_cards()) if self.verbose: print("Playing card:", cards[card_id][0]) if cards[card_id][3] is not None: if self.verbose: self.game.show_available_play_card_targets(card_id) target = random.choice( self.game.available_play_card_targets(card_id)) else: target = None if self.verbose: print("Target =", target) self.game.play_card(card_id, target=target) if self.game.is_end(): break elif inp == 2: attacker_position = random.choice( self.game.available_attackers()) if self.verbose: print("Attacker: ", attacker_position) self.game.show_available_attacking_targets( attacker_position) target_position = random.choice( self.game.available_attacking_targets( attacker_position)) if self.verbose: print("Attacked target:", target_position) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() if self.verbose: self.game.show_state() self.game.end_game() self.winner = self.game.get_winner()
class PlayMagicCard: def __init__(self): self.game = MagicCardGame() def initialize(self): self.game.initialize() def start_game(self): self.game.show_state() while not self.game.is_end(): self.game.start_turn() if self.game.is_end(): break inp = 0 while inp != 3: self.game.show_state() self.game.show_available_play_cards() self.game.show_available_attackers() available_set = (3, ) if len(self.game.available_play_cards()) != 0: available_set += (1, ) if len(self.game.available_attackers()) != 0: available_set += (2, ) inp = "x" while inp not in available_set: inp = int(input("Actions: 1.play 2.attack 3.end turn: ")) if inp == 1: card_id = "x" while card_id not in tuple( self.game.available_play_cards()): card_id = int(input("Choose a card to play: ")) if cards[card_id][3] is not None: self.game.show_available_play_card_targets(card_id) target = "x" while target not in self.game.available_play_card_targets( card_id): target = int(input("Choose a target: ")) else: target = None self.game.play_card(card_id, target=target) if self.game.is_end(): break elif inp == 2: attacker_position = "x" while attacker_position not in self.game.available_attackers( ): attacker_position = int( input("Choose an attacker minion: ")) self.game.show_available_attacking_targets( attacker_position) target_position = "x" while target_position not in self.game.available_attacking_targets( attacker_position): target_position = int(input("Choose a target: ")) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() self.game.show_state() self.game.end_game()
attacker_position) target_position = random.choice( self.game.available_attacking_targets( attacker_position)) if self.verbose: print("Attacked target:", target_position) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() if self.verbose: self.game.show_state() self.game.end_game() winner = self.game.get_winner() if winner < 0: ## dealing with tie, winner will become -10000 at that time winner = 0.5 return winner if __name__ == "__main__": game = MagicCardGame() game.initialize() run = RandomSamplingFromState(game) run.do_sampling()
class TrainingDataGenerator: def __init__(self, AI="minimax"): self.game = None self.X = None # X is the state self.Y = None # Y is the play choice self.AI = AI self.clf = None def initialize(self): if self.AI == "sklearn": self.clf = svm.SVR(kernel='linear') print("fitting sklearn") self.clf.fit(training_set_X, training_set_Y) self.X = open( "C:\\Users\\Zhi Li\\Desktop\\CIS 667 Introduction to AI\\Project\\codes\\gaming part\\20181122 game with coin\\training_data\\training_set_X150.py", "w+") self.Y = open( "C:\\Users\\Zhi Li\\Desktop\\CIS 667 Introduction to AI\\Project\\codes\\gaming part\\20181122 game with coin\\training_data\\training_set_Y150.py", "w+") self.X.write("X = [\n") self.Y.write("Y = [\n") def reset_game(self): self.game = MagicCardGame() self.game.initialize() def get_random_choice(self): lst = [0] * (Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 4 + 3 + 1) ## hand0(134), target0(14), battlefield0(7),battlefield_target0(7) hero0,h1,h1, end_turn new_game_state, random_choice = RandomChoiceGenerator( self.game).get_random_play() if random_choice[0] == 1: lst[random_choice[1] - 1] = 1 if random_choice[2] is not None: if random_choice[2] < 0: if random_choice[2] == Constants.PLAYER_NICKNAME[0]: lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 2 + 1 - 1] = 1 else: lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 2 + 2 - 1] = 1 elif random_choice[ 2] < self.game.state.battlefield.card0_length: lst[Constants.AVAILABLE_CARDS + random_choice[2]] = 1 elif random_choice[2] < len( self.game.state.battlefield.card_states): lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND + random_choice[2]] = 1 else: raise Exception("Wrong target! target = %s" % random_choice[2]) elif random_choice[0] == 2: if self.game.get_current_player() == 0: lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 2 + 2 + random_choice[1]] = 1 if random_choice[2] < 0: lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 4 + 3 - 1] = 1 else: target = random_choice[ 2] - self.game.state.battlefield.card0_length lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 3 + 2 + target] = 1 elif self.game.get_current_player() == 1: attacker = random_choice[ 2] - self.game.state.battlefield.card0_length lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 2 + 2 + attacker] = 1 if random_choice[2] < 0: lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 4 + 3 - 1] = 1 else: lst[Constants.AVAILABLE_CARDS + Constants.PLAYER_MINION_UPPERBOUND * 3 + 2 + random_choice[2]] = 1 else: raise Exception("Error!") elif random_choice[0] == 3: lst[-1] = 1 else: raise Exception("Wrong input type! type = %s" % random_choice[0]) if self.AI == "minimax": return lst, RandomSamplingFromState(new_game_state).do_sampling() elif self.AI == "sklearn": return lst, -1 else: raise Exception("Invalid AI!") def play_game(self, verbose=False): if verbose: self.game.show_state() while not self.game.is_end(): self.game.start_turn() if self.game.is_end(): break inp = 0 while inp != 3: ## input("Enter any key to continue: ") if verbose: self.game.show_state() self.game.show_available_play_cards() self.game.show_available_attackers() state_lst = list(self.game.get_state_tuple()) choice_list, win_rate = self.get_random_choice() training_X = state_lst + choice_list self.X.write(str(training_X)) self.X.write(",\n") if self.AI == "sklearn": win_rate = self.clf.predict([training_X]) self.Y.write(str(win_rate)) self.Y.write(",\n") available_set = () if len(self.game.available_play_cards()) != 0: available_set += (1, ) if len(self.game.available_attackers()) != 0: available_set += (2, ) available_set += (3, ) inp = available_set[0] if verbose: print("Action:", inp) if inp == 1: card_id = random.choice(self.game.available_play_cards()) if verbose: print("Playing card:", cards[card_id][0]) if cards[card_id][3] is not None: if verbose: self.game.show_available_play_card_targets(card_id) target = random.choice( self.game.available_play_card_targets(card_id)) else: target = None if verbose: print("Target =", target) self.game.play_card(card_id, target=target) if self.game.is_end(): break elif inp == 2: attacker_position = random.choice( self.game.available_attackers()) if verbose: print("Attacker: ", attacker_position) self.game.show_available_attacking_targets( attacker_position) target_position = random.choice( self.game.available_attacking_targets( attacker_position)) if verbose: print("Attacked target:", target_position) self.game.attack(attacker_position, target_position) if self.game.is_end(): break elif inp == 3: break else: print("Invalid option!") if self.game.is_end(): break self.game.end_turn() if verbose: self.game.show_state() self.game.end_game() print("Winner is player %s!" % self.game.get_winner()) def end_game(self): self.X.write("\n]") self.Y.write("\n]") self.X.close() self.Y.close()