develop #2
20
main.py
20
main.py
@ -10,22 +10,20 @@ import random
|
|||||||
|
|
||||||
class TicTacToe:
|
class TicTacToe:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.player1 = Player("X")
|
self.player1 = Player("X", "Player 1")
|
||||||
self.player2 = Player("O")
|
self.player2 = Player("O", "Player 2")
|
||||||
self.board = Board()
|
self.board = Board()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
|||||||
while True:
|
while True:
|
||||||
self.board.make_move(self.player1.token, self.player1.make_random_move(self.board.emptyFields))
|
self.board.make_move(self.player1.token, self.player1.make_random_move(self.board.emptyFields))
|
||||||
self.board.print()
|
self.board.print()
|
||||||
if self.board.isWin(self.player1.token):
|
if self.board.isWin(self.player1.token, self.player1.name):
|
||||||
print("Player 1 has won.")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
self.board.make_move(self.player2.token, self.player2.make_random_move(self.board.emptyFields))
|
self.board.make_move(self.player2.token, self.player2.make_random_move(self.board.emptyFields))
|
||||||
self.board.print()
|
self.board.print()
|
||||||
if self.board.isWin(self.player2.token):
|
if self.board.isWin(self.player2.token, self.player2.name):
|
||||||
print("Player 2 has won.")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
@ -44,11 +42,7 @@ class Board:
|
|||||||
else:
|
else:
|
||||||
print("This field's taken.")
|
print("This field's taken.")
|
||||||
|
|
||||||
"""
|
def isWin(self, token, name):
|
||||||
Ta funkcja byłaby dużo bardziej użyteczna,
|
|
||||||
gdyby informowała nas który z graczy jest zwycięzcą
|
|
||||||
"""
|
|
||||||
def isWin(self, token):
|
|
||||||
if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or
|
if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or
|
||||||
(self.state[0] == token and self.state[4] == token and self.state[8] == token) or
|
(self.state[0] == token and self.state[4] == token and self.state[8] == token) or
|
||||||
(self.state[0] == token and self.state[3] == token and self.state[6] == token) or
|
(self.state[0] == token and self.state[3] == token and self.state[6] == token) or
|
||||||
@ -57,6 +51,7 @@ class Board:
|
|||||||
(self.state[2] == token and self.state[4] == token and self.state[6] == token) or
|
(self.state[2] == token and self.state[4] == token and self.state[6] == token) or
|
||||||
(self.state[3] == token and self.state[4] == token and self.state[5] == token) or
|
(self.state[3] == token and self.state[4] == token and self.state[5] == token) or
|
||||||
(self.state[6] == token and self.state[7] == token and self.state[8] == token)):
|
(self.state[6] == token and self.state[7] == token and self.state[8] == token)):
|
||||||
|
print(name + " has won.")
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -67,8 +62,9 @@ class Board:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
def __init__(self, token):
|
def __init__(self, token, name):
|
||||||
self.token = token
|
self.token = token
|
||||||
|
self.name = name
|
||||||
|
|
||||||
def make_random_move(self, emptyFields):
|
def make_random_move(self, emptyFields):
|
||||||
return random.choice(emptyFields)
|
return random.choice(emptyFields)
|
||||||
|
Loading…
Reference in New Issue
Block a user
Ten cały blok kodu możnaby sprowadzić do: