Files
tic_tac_toe/human_player.py
T
2024-06-10 20:48:50 +02:00

61 lines
1.4 KiB
Python

import numpy as np
from a_player import APlayer
from helper import to_state_string, create_empty_state, create_test_state
from state import get_potential_moves
class HumanPlayer(APlayer):
def __init__(self, mark, name: str):
APlayer.__init__(self, mark)
self.name = name
def move(self, state: np.array):
move = 0
has_moved = False
while True:
print(f"{self.name}, Du bist dran. Das Spielfeld sieht so aus:")
print(to_state_string(state, create_test_state()))
print(f"{self.name}, in welches Feld willst Du Dein \"{self.mark}\" setzen?")
choice = input("Feldnummer: ")
if choice in "Qq":
print("Abbruch")
break
if not choice in "123456789" or len(choice) != 1:
print("Ungültiges Feld!")
print("Versuche es nochmal")
continue
move = int(choice) - 1
if not move in get_potential_moves(state):
print("Feld is bereits belegt!")
print("Versuche es nochmal")
continue
print("So soll es geschehen")
has_moved = True
break
state_next = self.to_state(state.copy(), move)
return state_next, has_moved
def reward(self, value):
if value > 0:
print(f"{self.name}, Du hast gewonnen, super!")
else:
print(f"{self.name}, Du hast leider verloren!")
if __name__ == '__main__':
p = HumanPlayer("X", "Jens")
state = create_empty_state()
while True:
state, has_moved = p.move(state)
if not has_moved:
break
p.reward(1.0)