develop #2
29
main.py
29
main.py
@ -18,19 +18,27 @@ class TicTacToe:
|
|||||||
self.board = Board()
|
self.board = Board()
|
||||||
while True:
|
while True:
|
||||||
self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields))
|
self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields))
|
||||||
|
if self.board.is_win(self.player1.token):
|
||||||
self.board.print()
|
self.board.print()
|
||||||
if self.board.is_win(self.player1.token, self.player1.name):
|
if len(self.board.empty_fields) == 0:
|
||||||
|
print("It's a draw.")
|
||||||
|
else:
|
||||||
|
print(self.player1.name + " has won.")
|
||||||
break
|
break
|
||||||
|
|
||||||
self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields))
|
self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields))
|
||||||
|
if self.board.is_win(self.player2.token):
|
||||||
self.board.print()
|
self.board.print()
|
||||||
if self.board.is_win(self.player2.token, self.player2.name):
|
if len(self.board.empty_fields) == 0:
|
||||||
|
print("It's a draw.")
|
||||||
|
|||||||
|
else:
|
||||||
|
print(self.player2.name + " has won.")
|
||||||
break
|
break
|
||||||
|
|
||||||
class Board:
|
class Board:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.state = list(" "*9)
|
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)]
|
||||||
|
|
||||||
swarga
commented
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"
|
||||||
@ -43,7 +51,7 @@ class Board:
|
|||||||
else:
|
else:
|
||||||
print("This field's taken.")
|
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
|
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
|
||||||
@ -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[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
|
||||||
swarga
commented
No napisz to! No napisz to!
|
|||||||
print(name + " has won.")
|
len(self.empty_fields) == 0):
|
||||||
return True
|
|
||||||
if len(self.empty_fields) == 0:
|
|
||||||
print("It's a draw.")
|
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -74,8 +79,12 @@ class Player:
|
|||||||
return random.choice(empty_fields)
|
return random.choice(empty_fields)
|
||||||
|
|
||||||
|
|
||||||
opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="])
|
|
||||||
number_of_rounds = None
|
number_of_rounds = None
|
||||||
swarga
commented
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:
|
for opt, arg in opts:
|
||||||
if opt in ("-n", "--numberofrounds"):
|
if opt in ("-n", "--numberofrounds"):
|
||||||
number_of_rounds = arg
|
number_of_rounds = arg
|
||||||
|
Loading…
Reference in New Issue
Block a user
To możemy zastąpić generacją listy.