develop #2

Merged
swarga merged 10 commits from develop into master 2020-12-25 04:59:51 +01:00

68
main.py
View File

@ -7,31 +7,38 @@
""" """
import random import random
import sys, getopt
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()
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.
self.board = Board()
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.choose_random_field(self.board.empty_fields))
self.board.print() if self.board.is_win(self.player1.token):
if self.board.isWin(self.player1.token): self.board.print()
print("Player 1 has won.") if len(self.board.empty_fields) == 0:
print("It's a draw.\n")
else:
print(self.player1.name + " has won.\n")
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.choose_random_field(self.board.empty_fields))
self.board.print() if self.board.is_win(self.player2.token):
if self.board.isWin(self.player2.token): self.board.print()
print("Player 2 has won.") if len(self.board.empty_fields) == 0:
print("It's a draw.\n")

To możemy zastąpić generacją listy.

To możemy zastąpić generacją listy.
else:
print(self.player2.name + " has won.\n")
break break
class Board: class Board:
def __init__(self): def __init__(self):
self.state = list(" "*9) self.state = list(" "*9)
self.emptyFields = [0, 1, 2, 3, 4, 5, 6, 7, 8] self.empty_fields = [x for x in range(9)]

Good.

Good.
def print(self): def print(self):
board = ""+ self.state[0] +"|"+self.state[1] +"|"+self.state[2] +" \n-|-|-\n"+self.state[3] +"|"+self.state[4] +"|"+self.state[5] +"\n-|-|-\n"+self.state[6] +"|"+self.state[7] +"|"+self.state[8] +"\n" board = ""+ self.state[0] +"|"+self.state[1] +"|"+self.state[2] +" \n-|-|-\n"+self.state[3] +"|"+self.state[4] +"|"+self.state[5] +"\n-|-|-\n"+self.state[6] +"|"+self.state[7] +"|"+self.state[8] +"\n"
@ -40,15 +47,11 @@ class Board:
def make_move(self, token, field): def make_move(self, token, field):
if self.state[field] == " ": if self.state[field] == " ":
self.state[field] = token self.state[field] = token
self.emptyFields.remove(field) self.empty_fields.remove(field)
else: else:
print("This field's taken.") print("This field's taken.")
""" def is_win(self, token):
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
@ -56,7 +59,8 @@ class Board:
(self.state[2] == token and self.state[5] == token and self.state[8] == token) or (self.state[2] == token and self.state[5] == token and self.state[8] == token) or
(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) or

No napisz to!

No napisz to!
len(self.empty_fields) == 0):
return True return True
return False return False
@ -67,15 +71,29 @@ class Board:
pass pass
class Player: class Player:
def __init__(self, token): def __init__(self, token, name):

Jeden tab za dużo?

Jeden tab za dużo?
self.token = token self.token = token
self.name = name
def make_random_move(self, emptyFields): def choose_random_field(self, empty_fields):
return random.choice(emptyFields) return random.choice(empty_fields)
number_of_rounds = None

Jeśli nie podam żadnego argumentu, to powinien mi się wyświetlić help (lub gra powinna ruszyć raz).

Jeśli nie podam żadnego argumentu, to powinien mi się wyświetlić help (lub gra powinna ruszyć raz).
try:
opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="])
except getopt.GetoptError:
print("main.py -n <numberofrounds>")
sys.exit(2)
if(len(sys.argv) != 1):
for opt, arg in opts:
if opt in ("-n", "--numberofrounds"):
number_of_rounds = arg
else:
print("main.py -n <numberofrounds>")
sys.exit()
game = TicTacToe() game = TicTacToe()
game.run() for x in range(0, int(number_of_rounds)):
game.run()