sdsd
This commit is contained in:
parent
24601be13c
commit
a3ad4bf7ac
29
main.py
29
main.py
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
import random
|
import random
|
||||||
|
import sys, getopt
|
||||||
|
|
||||||
class TicTacToe:
|
class TicTacToe:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -16,20 +17,20 @@ class TicTacToe:
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.emptyFields))
|
self.board.make_move(self.player1.token, self.player1.choose_random_field(self.board.empty_fields))
|
||||||
self.board.print()
|
self.board.print()
|
||||||
if self.board.isWin(self.player1.token, self.player1.name):
|
if self.board.is_win(self.player1.token, self.player1.name):
|
||||||
break
|
break
|
||||||
|
|
||||||
self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.emptyFields))
|
self.board.make_move(self.player2.token, self.player2.choose_random_field(self.board.empty_fields))
|
||||||
self.board.print()
|
self.board.print()
|
||||||
if self.board.isWin(self.player2.token, self.player2.name):
|
if self.board.is_win(self.player2.token, self.player2.name):
|
||||||
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 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
||||||
|
|
||||||
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"
|
||||||
@ -38,11 +39,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 isWin(self, token, name):
|
def is_win(self, token, name):
|
||||||
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
|
||||||
@ -66,12 +67,16 @@ class Player:
|
|||||||
self.token = token
|
self.token = token
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def choose_random_field(self, emptyFields):
|
def choose_random_field(self, empty_fields):
|
||||||
return random.choice(emptyFields)
|
return random.choice(empty_fields)
|
||||||
|
|
||||||
|
|
||||||
|
opts, args = getopt.getopt(sys.argv[1:], "n:", ["numberofrounds="])
|
||||||
|
number_of_rounds = None
|
||||||
|
for opt, arg in opts:
|
||||||
|
if opt in ("-n", "--numberofrounds"):
|
||||||
|
number_of_rounds = arg
|
||||||
|
|
||||||
game = TicTacToe()
|
game = TicTacToe()
|
||||||
game.run()
|
for x in range(0, int(number_of_rounds)):
|
||||||
|
game.run()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user