develop #2

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

35
main.py
View File

@ -18,19 +18,27 @@ class TicTacToe:
self.board = Board()
while True:
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, self.player1.name):
if self.board.is_win(self.player1.token):
self.board.print()
if len(self.board.empty_fields) == 0:
print("It's a draw.")
else:
print(self.player1.name + " has won.")
break
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, self.player2.name):
if self.board.is_win(self.player2.token):
self.board.print()
if len(self.board.empty_fields) == 0:
print("It's a draw.")

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

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

Good.

Good.
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"
@ -43,7 +51,7 @@ class Board:
else:
print("This field's taken.")
def is_win(self, token, name):
def is_win(self, token):
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
@ -51,11 +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[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
if len(self.empty_fields) == 0:
print("It's a draw.")
(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 False
@ -71,11 +76,15 @@ class Player:
self.name = name
def choose_random_field(self, empty_fields):
return random.choice(empty_fields)
return random.choice(empty_fields)
opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="])
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()
for opt, arg in opts:
if opt in ("-n", "--numberofrounds"):
number_of_rounds = arg