develop #2

Merged
swarga merged 10 commits from develop into master 2020-12-25 04:59:51 +01:00
Showing only changes of commit d6ce3d2579 - Show all commits

20
main.py
View File

@ -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):
Review

Ten cały blok kodu możnaby sprowadzić do:

  1. Gracz wykonuje ruch.
  2. Sprawdzamy, czy mamy wygranego (remis).
  3. Zmieniamy gracza.
  4. Wracamy do 1.
Ten cały blok kodu możnaby sprowadzić do: 1. Gracz wykonuje ruch. 2. Sprawdzamy, czy mamy wygranego (remis). 3. Zmieniamy gracza. 4. Wracamy do 1.
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)