Tic-Tac-Toe/Board.py
2020-12-28 22:39:37 +01:00

31 lines
1.3 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 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)):
return True
return False
def get_state(field):
return self.state[field]