2020-12-28 22:39:37 +01:00
|
|
|
from Player import Player
|
2021-01-09 22:05:47 +01:00
|
|
|
from RandomPlayer import RandomPlayer
|
|
|
|
from HumanPlayer import HumanPlayer
|
2020-12-28 22:39:37 +01:00
|
|
|
from Board import Board
|
|
|
|
|
|
|
|
class TicTacToe:
|
2021-01-09 22:05:47 +01:00
|
|
|
def __init__(self, player_x, player_o):
|
2020-12-31 21:10:33 +01:00
|
|
|
self.players = []
|
2021-01-09 22:05:47 +01:00
|
|
|
self.player_x = player_x
|
|
|
|
self.player_o = player_o
|
|
|
|
|
|
|
|
if self.player_x == "random":
|
|
|
|
self.players.append(RandomPlayer("X", "Player X"))
|
|
|
|
else:
|
|
|
|
self.players.append(HumanPlayer("X", "Player X"))
|
|
|
|
|
|
|
|
if self.player_o == "random":
|
|
|
|
self.players.append(RandomPlayer("O", "Player O"))
|
|
|
|
else:
|
|
|
|
self.players.append(HumanPlayer("O", "Player O"))
|
2020-12-28 22:39:37 +01:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.board = Board()
|
2020-12-31 21:10:33 +01:00
|
|
|
player = 0
|
2020-12-28 22:39:37 +01:00
|
|
|
|
2021-01-09 22:05:47 +01:00
|
|
|
if self.player_x == "human":
|
|
|
|
self.board.print()
|
|
|
|
|
2020-12-31 21:10:33 +01:00
|
|
|
while len(self.board.empty_fields) != 0:
|
2021-01-09 22:05:47 +01:00
|
|
|
self.board.make_move(self.players[player].token, self.players[player].choose_field(self.board.empty_fields))
|
|
|
|
self.board.print()
|
|
|
|
|
2020-12-31 21:10:33 +01:00
|
|
|
if self.board.is_win(self.players[player].token):
|
|
|
|
print(self.players[player].name + " has won.\n")
|
2020-12-28 22:39:37 +01:00
|
|
|
break
|
2021-01-09 22:05:47 +01:00
|
|
|
|
|
|
|
if player == 0:
|
2020-12-31 21:10:33 +01:00
|
|
|
player = 1
|
|
|
|
else:
|
|
|
|
player = 0
|
|
|
|
else:
|
2021-01-09 22:05:47 +01:00
|
|
|
print("It's a draw.")
|