Compare commits
3 Commits
9644a51f9f
...
master
Author | SHA1 | Date | |
---|---|---|---|
0b973804e7 | |||
7d59652863 | |||
|
5944ed9a4e |
102
main.py
102
main.py
@ -6,58 +6,94 @@
|
|||||||
| |
|
| |
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from random import randint
|
import random
|
||||||
from random import choice
|
import sys, getopt
|
||||||
|
|
||||||
|
|
||||||
class TicTacToe:
|
class TicTacToe:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.state = list(" "*9)
|
self.player1 = Player("X", "Player 1")
|
||||||
|
self.player2 = Player("O", "Player 2")
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.board = Board()
|
||||||
|
while True:
|
||||||
|
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()
|
||||||
|
if len(self.board.empty_fields) == 0:
|
||||||
|
print("It's a draw.\n")
|
||||||
|
else:
|
||||||
|
print(self.player1.name + " has won.\n")
|
||||||
|
break
|
||||||
|
|
||||||
|
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()
|
||||||
|
if len(self.board.empty_fields) == 0:
|
||||||
|
print("It's a draw.\n")
|
||||||
|
else:
|
||||||
|
print(self.player2.name + " has won.\n")
|
||||||
|
break
|
||||||
|
|
||||||
|
class Board:
|
||||||
|
def __init__(self):
|
||||||
|
self.state = list(" "*9)
|
||||||
|
self.empty_fields = [x for x in range(9)]
|
||||||
|
|
||||||
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"
|
||||||
|
|
||||||
print(board)
|
print(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.empty_fields.remove(field)
|
||||||
else:
|
else:
|
||||||
print("This field's taken.")
|
print("This field's taken.")
|
||||||
|
|
||||||
|
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
|
||||||
|
(self.state[1] == token and self.state[4] == token and self.state[7] == 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[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) or
|
||||||
|
len(self.empty_fields) == 0):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_state():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def get_action():
|
||||||
|
pass
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
def __init__(self, token):
|
def __init__(self, token, name):
|
||||||
self.token = token
|
self.token = token
|
||||||
|
self.name = name
|
||||||
def make_random_move(self, state):
|
|
||||||
sequence = ["X", "O"]
|
|
||||||
|
|
||||||
emptyField = True
|
def choose_random_field(self, empty_fields):
|
||||||
while emptyField:
|
return random.choice(empty_fields)
|
||||||
field = state[randint(0,8)]
|
|
||||||
if field == " ":
|
|
||||||
state[field] = choice(sequence)
|
|
||||||
emptyField = False
|
|
||||||
|
|
||||||
|
|
||||||
player = Player("X")
|
number_of_rounds = None
|
||||||
player1 = Player("O")
|
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.print()
|
for x in range(0, int(number_of_rounds)):
|
||||||
|
game.run()
|
||||||
for x in range(1,10):
|
|
||||||
player.make_random_move(game.state)
|
|
||||||
game.print()
|
|
||||||
player1.make_random_move(game.state)
|
|
||||||
game.print()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# make_move(state, "X", 0)
|
|
||||||
# print_board(state)
|
|
||||||
# make_move(state, "O", 3)
|
|
||||||
# print_board(state)
|
|
||||||
# make_move(state, "X", 0)
|
|
||||||
# print_board(state)
|
|
Loading…
Reference in New Issue
Block a user