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:
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):
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:
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:
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
(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
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)