Tic-Tac-Toe/main.py
2020-12-20 21:46:22 +01:00

63 lines
1.1 KiB
Python

"""
| |
-|-|-
| |
-|-|-
| |
"""
from random import randint
from random import choice
class TicTacToe:
def __init__(self):
self.state = list(" "*9)
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"
print(board)
def make_move(self, token, field):
if self.state[field] == " ":
self.state[field] = token
else:
print("This field's taken.")
class Player:
def __init__(self, token):
self.token = token
def make_random_move(self, state):
sequence = ["X", "O"]
emptyField = True
while emptyField:
field = state[randint(0,8)]
if field == " ":
state[field] = choice(sequence)
emptyField = False
player = Player("X")
player1 = Player("O")
game = TicTacToe()
game.print()
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)