generated from mwc/lab_tic_tac_toe
Initial commit
This commit is contained in:
32
nim/view.py
Normal file
32
nim/view.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from nim.game_stub import NimGameStub
|
||||
|
||||
class NimView:
|
||||
def __init__(self, player0, player1):
|
||||
self.players = [player0, player1]
|
||||
self.game = NimGameStub()
|
||||
|
||||
def greet(self):
|
||||
print(f"{self.players[0].name} and {self.players[1].name}, welcome to Nim.")
|
||||
|
||||
def show_board(self, state):
|
||||
for lines_in_row in state["board"]:
|
||||
print("| " * lines_in_row)
|
||||
|
||||
def get_action(self, state):
|
||||
self.show_board(state)
|
||||
player = self.get_current_player(state)
|
||||
return player.choose_action(state)
|
||||
|
||||
def get_current_player(self, state):
|
||||
if state["first_player"]:
|
||||
return self.players[0]
|
||||
else:
|
||||
return self.players[1]
|
||||
|
||||
def conclude(self, state):
|
||||
self.show_board(state)
|
||||
if self.game.get_reward(state) > 0:
|
||||
winner = self.players[0]
|
||||
else:
|
||||
winner = self.players[1]
|
||||
print(f"Congratulations, {winner.name}!")
|
Reference in New Issue
Block a user