Improved run function

This commit is contained in:
Paulina Nowak 2020-12-31 21:10:33 +01:00
parent 18b1305ad5
commit 4c0758aab6

View File

@ -1,57 +1,26 @@
from Player import Player
from Board import Board
"""
Gracz wykonuje ruch.
Sprawdzamy, czy mamy wygranego (remis).
Zmieniamy gracza.
Wracamy do 1.
`while noWinner():
player.make_move()
isWin()
swapPlayers()
`
Coś takiego
Twój kod dwa razy robi rzeczy
Zamiast zamienić gracza
I zrobić to raz
Linie 20-27 sa te same co 29-36
Tylko tam jest player 2
"""
class TicTacToe:
def __init__(self):
self.player1 = Player("X", "Player 1")
self.player2 = Player("O", "Player 2")
self.players = []
self.players.append( Player("X", "Player 1") )
self.players.append( Player("O", "Player 2") )
def run(self):
self.board = Board()
while True:
if len(self.board.empty_fields) == 0:
self.board.print()
print("It's a draw.")
break
player = 0
self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields))
if self.board.is_win(self.player1.token):
while len(self.board.empty_fields) != 0:
self.board.make_move(self.players[player].token, self.players[player].choose_random_field(self.board.empty_fields))
if self.board.is_win(self.players[player].token):
self.board.print()
print(self.player1.name + " has won.\n")
print(self.players[player].name + " has won.\n")
break
if len(self.board.empty_fields) == 0:
self.board.print()
print("It's a draw.")
break
self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields))
if self.board.is_win(self.player2.token):
self.board.print()
print(self.player2.name + " has won.\n")
break
if (player == 0):
player = 1
else:
player = 0
else:
self.board.print()
print("It's a draw.\n")