diff --git a/main.py b/main.py index 893fdb5..006b342 100644 --- a/main.py +++ b/main.py @@ -96,9 +96,8 @@ class Board: print("#############") START_WEIGHT = 20 -WIN_REWARD = 3 -DRAW_REWARD = 0 -PUNUSHMENT = 2 +WIN_REWARD = 1 +PUNUSHMENT = 1 class AiPlayer: def __init__(self, ch: Ch) -> None: @@ -113,10 +112,6 @@ class AiPlayer: for choice in self.current_choices: self.choices[choice] += WIN_REWARD - def reward_draw(self): - for choice in self.current_choices: - self.choices[choice] += DRAW_REWARD - def punish_loss(self): for choice in self.current_choices: self.choices[choice] -= PUNUSHMENT @@ -141,7 +136,7 @@ class AiPlayer: for idx, choice in possible_choices: choice = self.interned_choice(choice) key = choice.key() - if not candiate_weigth or self.choices[key] > candiate_weigth: + if not candiate_weigth or self.choices[key] > candiate_weigth + 4: candiate_weigth = self.choices[key] candidate_idcs = [(idx, key)] elif self.choices[key] == candiate_weigth: @@ -163,46 +158,67 @@ class AiPlayer: for key in vs: self.choices[int(key)] = vs[key] -class Game: - def __init__(self) -> None: - self.current_plays = [] - self.weights = {} - - - def main(): - p0 = AiPlayer(Ch.Cross) + p1 = AiPlayer(Ch.Cross) + p2 = AiPlayer(Ch.Circle) games = 0 + p1_wins = 0 + p2_wins = 0 + draws = 0 + + print("Training P1 against P2...") + + for _ in range(1000): + board = Board() + p1.clear_choices() + p2.clear_choices() + while True: + p1.make_play(board) + if board.player_has_won(Ch.Cross): + p1_wins += 1 + p1.reward_win() + p2.punish_loss() + break + if board.is_draw(): + draws += 1 + break + p2.make_play(board) + if board.player_has_won(Ch.Circle): + p2_wins += 1 + p2.reward_win() + p1.punish_loss() + break + if board.is_draw(): + break + games += 1 + + print(f"Games {games} Score: P1: {p1_wins}, P2: {p2_wins}, draws: {draws}") while True: - print(f"\n\nGame #{games}") + print("\nNew game") board = Board() - p0.clear_choices() + p1.clear_choices() while True: print("AI's turn") - p0.make_play(board) + p1.make_play(board) board.print() if board.player_has_won(Ch.Cross): print("AI won!") - print("Rewarding AI...") - p0.reward_win() break print("Your turn (0..8)") if board.is_draw(): print("Draw!") - print("Rewarding AI...") - p0.reward_draw() break possible_choices = board.possible_plays() should_restart = False while True: text = input("> ") if text == ".save": - p0.save_to_file() + p1.save_to_file() continue elif text == ".load": - p0.load_from_file() + p1.load_from_file() choice = 0 should_restart = True break @@ -223,11 +239,10 @@ def main(): board.print() if board.player_has_won(Ch.Circle): print("Player won!") - print("Punishing AI...") - p0.punish_loss() break games += 1 + try: main() except KeyboardInterrupt: