Improved run function
This commit is contained in:
parent
18b1305ad5
commit
4c0758aab6
61
TicTacToe.py
61
TicTacToe.py
@ -1,57 +1,26 @@
|
|||||||
from Player import Player
|
from Player import Player
|
||||||
from Board import Board
|
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:
|
class TicTacToe:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.player1 = Player("X", "Player 1")
|
self.players = []
|
||||||
self.player2 = Player("O", "Player 2")
|
self.players.append( Player("X", "Player 1") )
|
||||||
|
self.players.append( Player("O", "Player 2") )
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.board = Board()
|
self.board = Board()
|
||||||
while True:
|
player = 0
|
||||||
if len(self.board.empty_fields) == 0:
|
|
||||||
self.board.print()
|
|
||||||
print("It's a draw.")
|
|
||||||
break
|
|
||||||
|
|
||||||
self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields))
|
while len(self.board.empty_fields) != 0:
|
||||||
if self.board.is_win(self.player1.token):
|
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()
|
self.board.print()
|
||||||
print(self.player1.name + " has won.\n")
|
print(self.players[player].name + " has won.\n")
|
||||||
break
|
break
|
||||||
|
if (player == 0):
|
||||||
if len(self.board.empty_fields) == 0:
|
player = 1
|
||||||
self.board.print()
|
else:
|
||||||
print("It's a draw.")
|
player = 0
|
||||||
break
|
else:
|
||||||
|
self.board.print()
|
||||||
self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields))
|
print("It's a draw.\n")
|
||||||
if self.board.is_win(self.player2.token):
|
|
||||||
self.board.print()
|
|
||||||
print(self.player2.name + " has won.\n")
|
|
||||||
break
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user