Tic-Tac-Toe/TicTacToe.py

42 lines
1.0 KiB
Python
Raw Normal View History

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):
2020-12-31 21:10:33 +01:00
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()
2020-12-31 21:10:33 +01:00
player = 0
if self.player_x == "human":
self.board.print()
2020-12-31 21:10:33 +01:00
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()
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")
break
if player == 0:
2020-12-31 21:10:33 +01:00
player = 1
else:
player = 0
else:
print("It's a draw.")