Files
tic_tac_toe/human_player.py
2024-06-11 22:05:06 +02:00

61 lines
1.4 KiB
Python

import numpy as np
from a_player import APlayer
from state 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 das \"{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(f"Okay, nächstes \"{self.mark}\" auf Feld {move+1}")
has_moved = True
break
state_next = self.to_state(state.copy(), move)
return state_next, has_moved
def end_game(self, state: np.array, 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.end_game(state, 1.0)