develop #2
20
main.py
20
main.py
@ -10,22 +10,20 @@ import random
|
||||
|
||||
class TicTacToe:
|
||||
def __init__(self):
|
||||
self.player1 = Player("X")
|
||||
self.player2 = Player("O")
|
||||
self.player1 = Player("X", "Player 1")
|
||||
self.player2 = Player("O", "Player 2")
|
||||
self.board = Board()
|
||||
|
||||
def run(self):
|
||||
|
||||
while True:
|
||||
self.board.make_move(self.player1.token, self.player1.make_random_move(self.board.emptyFields))
|
||||
self.board.print()
|
||||
if self.board.isWin(self.player1.token):
|
||||
print("Player 1 has won.")
|
||||
if self.board.isWin(self.player1.token, self.player1.name):
|
||||
break
|
||||
|
||||
self.board.make_move(self.player2.token, self.player2.make_random_move(self.board.emptyFields))
|
||||
self.board.print()
|
||||
if self.board.isWin(self.player2.token):
|
||||
print("Player 2 has won.")
|
||||
if self.board.isWin(self.player2.token, self.player2.name):
|
||||
break
|
||||
|
||||
class Board:
|
||||
@ -44,11 +42,7 @@ class Board:
|
||||
else:
|
||||
swarga
commented
Good. Good.
|
||||
print("This field's taken.")
|
||||
|
||||
"""
|
||||
Ta funkcja byłaby dużo bardziej użyteczna,
|
||||
gdyby informowała nas który z graczy jest zwycięzcą
|
||||
"""
|
||||
def isWin(self, token):
|
||||
def isWin(self, token, name):
|
||||
if((self.state[0] == token and self.state[1] == token and self.state[2] == token) or
|
||||
swarga
commented
Poczytaj o enumie. Poczytaj o enumie.
Plasznę możemy reprezentować jako listę intów: 0, -1 oraz +1.
Pomyśl w jaki sposób może to ułatwić znajdywanie wygranej (modulo is your friend).
|
||||
(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
|
||||
@ -57,6 +51,7 @@ class Board:
|
||||
(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[6] == token and self.state[7] == token and self.state[8] == token)):
|
||||
print(name + " has won.")
|
||||
return True
|
||||
return False
|
||||
|
||||
@ -67,8 +62,9 @@ class Board:
|
||||
pass
|
||||
swarga
commented
No napisz to! No napisz to!
|
||||
|
||||
class Player:
|
||||
def __init__(self, token):
|
||||
def __init__(self, token, name):
|
||||
self.token = token
|
||||
self.name = name
|
||||
|
||||
def make_random_move(self, emptyFields):
|
||||
return random.choice(emptyFields)
|
||||
|
Loading…
Reference in New Issue
Block a user
Ten cały blok kodu możnaby sprowadzić do: