Tic-Tac-Toe/Board.py

37 lines
1.5 KiB
Python

class Board:
def __init__(self):
self.state = list(" "*9)
self.empty_fields = [x for x in range(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
self.empty_fields.remove(field)
else:
print("This field's already taken. Choose another one.")
while field not in self.empty_fields:
field = int(input())
if field not in self.empty_fields:
print("This field's already taken. Choose another one.")
self.state[field] = token
self.empty_fields.remove(field)
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)):
return True
return False
def get_state(field):
return self.state[field]