Tic-Tac-Toe/TicTacToe.py

42 lines
1.0 KiB
Python

from Player import Player
from RandomPlayer import RandomPlayer
from HumanPlayer import HumanPlayer
from Board import Board
class TicTacToe:
def __init__(self, player_x, player_o):
self.players = []
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"))
def run(self):
self.board = Board()
player = 0
if self.player_x == "human":
self.board.print()
while len(self.board.empty_fields) != 0:
self.board.make_move(self.players[player].token, self.players[player].choose_field(self.board.empty_fields))
self.board.print()
if self.board.is_win(self.players[player].token):
print(self.players[player].name + " has won.\n")
break
if player == 0:
player = 1
else:
player = 0
else:
print("It's a draw.")