ais against each other
This commit is contained in:
parent
006997f9d5
commit
d137e0149b
69
main.py
69
main.py
@ -96,9 +96,8 @@ class Board:
|
|||||||
print("#############")
|
print("#############")
|
||||||
|
|
||||||
START_WEIGHT = 20
|
START_WEIGHT = 20
|
||||||
WIN_REWARD = 3
|
WIN_REWARD = 1
|
||||||
DRAW_REWARD = 0
|
PUNUSHMENT = 1
|
||||||
PUNUSHMENT = 2
|
|
||||||
|
|
||||||
class AiPlayer:
|
class AiPlayer:
|
||||||
def __init__(self, ch: Ch) -> None:
|
def __init__(self, ch: Ch) -> None:
|
||||||
@ -113,10 +112,6 @@ class AiPlayer:
|
|||||||
for choice in self.current_choices:
|
for choice in self.current_choices:
|
||||||
self.choices[choice] += WIN_REWARD
|
self.choices[choice] += WIN_REWARD
|
||||||
|
|
||||||
def reward_draw(self):
|
|
||||||
for choice in self.current_choices:
|
|
||||||
self.choices[choice] += DRAW_REWARD
|
|
||||||
|
|
||||||
def punish_loss(self):
|
def punish_loss(self):
|
||||||
for choice in self.current_choices:
|
for choice in self.current_choices:
|
||||||
self.choices[choice] -= PUNUSHMENT
|
self.choices[choice] -= PUNUSHMENT
|
||||||
@ -141,7 +136,7 @@ class AiPlayer:
|
|||||||
for idx, choice in possible_choices:
|
for idx, choice in possible_choices:
|
||||||
choice = self.interned_choice(choice)
|
choice = self.interned_choice(choice)
|
||||||
key = choice.key()
|
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]
|
candiate_weigth = self.choices[key]
|
||||||
candidate_idcs = [(idx, key)]
|
candidate_idcs = [(idx, key)]
|
||||||
elif self.choices[key] == candiate_weigth:
|
elif self.choices[key] == candiate_weigth:
|
||||||
@ -163,46 +158,67 @@ class AiPlayer:
|
|||||||
for key in vs:
|
for key in vs:
|
||||||
self.choices[int(key)] = vs[key]
|
self.choices[int(key)] = vs[key]
|
||||||
|
|
||||||
class Game:
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.current_plays = []
|
|
||||||
self.weights = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
p0 = AiPlayer(Ch.Cross)
|
p1 = AiPlayer(Ch.Cross)
|
||||||
|
p2 = AiPlayer(Ch.Circle)
|
||||||
|
|
||||||
games = 0
|
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:
|
while True:
|
||||||
print(f"\n\nGame #{games}")
|
print("\nNew game")
|
||||||
board = Board()
|
board = Board()
|
||||||
p0.clear_choices()
|
p1.clear_choices()
|
||||||
while True:
|
while True:
|
||||||
print("AI's turn")
|
print("AI's turn")
|
||||||
p0.make_play(board)
|
p1.make_play(board)
|
||||||
board.print()
|
board.print()
|
||||||
if board.player_has_won(Ch.Cross):
|
if board.player_has_won(Ch.Cross):
|
||||||
print("AI won!")
|
print("AI won!")
|
||||||
print("Rewarding AI...")
|
|
||||||
p0.reward_win()
|
|
||||||
break
|
break
|
||||||
print("Your turn (0..8)")
|
print("Your turn (0..8)")
|
||||||
if board.is_draw():
|
if board.is_draw():
|
||||||
print("Draw!")
|
print("Draw!")
|
||||||
print("Rewarding AI...")
|
|
||||||
p0.reward_draw()
|
|
||||||
break
|
break
|
||||||
possible_choices = board.possible_plays()
|
possible_choices = board.possible_plays()
|
||||||
should_restart = False
|
should_restart = False
|
||||||
while True:
|
while True:
|
||||||
text = input("> ")
|
text = input("> ")
|
||||||
if text == ".save":
|
if text == ".save":
|
||||||
p0.save_to_file()
|
p1.save_to_file()
|
||||||
continue
|
continue
|
||||||
elif text == ".load":
|
elif text == ".load":
|
||||||
p0.load_from_file()
|
p1.load_from_file()
|
||||||
choice = 0
|
choice = 0
|
||||||
should_restart = True
|
should_restart = True
|
||||||
break
|
break
|
||||||
@ -223,11 +239,10 @@ def main():
|
|||||||
board.print()
|
board.print()
|
||||||
if board.player_has_won(Ch.Circle):
|
if board.player_has_won(Ch.Circle):
|
||||||
print("Player won!")
|
print("Player won!")
|
||||||
print("Punishing AI...")
|
|
||||||
p0.punish_loss()
|
|
||||||
break
|
break
|
||||||
games += 1
|
games += 1
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
main()
|
main()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
Loading…
Reference in New Issue
Block a user