Just looking for a review on my code, its currently only player vs player, but I'll be working on the AI soon enough. Functions edit_name, tutorial, and AI have been left out as I haven't started work on them.
class tic_tac_toe:
board = [" "," "," ",
" "," "," ",
" "," "," ",]
players = [["One", "O", True],["Two", "X", False]]
winning_combos = [[0,1,2], # Horizontal
[3,4,5],
[6,7,8],
[0,3,6], # Vertical
[1,4,7],
[2,5,8],
[0,4,8], # Diagonal
[2,4,6]]
grid_size = 3
def __init__(self):
pass
def edit_name(self):
pass
def occupied(self, position):
if self.board[self.translate_position(self.grid_size, position)] == " ":
return False
else:
return True
# Swaps the players turns
def swap_turns(self):
# if player 1 is turn is false
if self.players[0][2]:
self.players[0][2] = False
self.players[1][2] = True
else:
self.players[0][2] = True
self.players[1][2] = False
# Who's turn is it?
def players_turn(self):
if self.players[0][2]: return 0
if self.players[1][2]: return 1
# Update the board with new plays
def update_board(self, position, player):
if not self.occupied(position):
self.board[
self.translate_position(self.grid_size, position)
] = self.players[player][1]
else:
print("You cannot over lap plays!")
self.main()
# Translates [x,y] -> 0-8
@staticmethod
def translate_position(grid_size, position):
# y = (x_1 - 1) + n(x_2 - 1)
# 8 = [3,3][x_1,x_2] where the grid size(n) is 3
x1 = int(position[0])
x2 = int(position[1])
grid = int(grid_size)
return (x1 - 1) + grid * (x2 - 1)
# Displays the current board
def show_board(self):
print("/-----------------\\ \n"
"| {0} | {1} | {2} |\n"
"|-----------------|\n"
"| {3} | {4} | {5} |\n"
"|-----------------|\n"
"| {6} | {7} | {8} |\n"
"\-----------------/".format(self.board[0], self.board[1], self.board[2],
self.board[3], self.board[4], self.board[5],
self.board[6], self.board[7], self.board[8]))
# Checks if there is a winner
@property
def winner(self):
for player in range(0,len(self.players)):
for winning_combo in range(0,len(self.winning_combos)):
if self.players[player][1] in [self.board[self.winning_combos[winning_combo][0]]]:
if self.players[player][1] in [self.board[self.winning_combos[winning_combo][1]]]:
if self.players[player][1] in [self.board[self.winning_combos[winning_combo][2]]]:
return True
return False
def main(self):
while not self.winner:
player = self.players_turn()
print("Player {0}'s turn".format(self.players[player][0]))
play = []
x = input("Input the x-coordinate where you would like to place your piece: ")
y = input("Input the y-coordinate where you would like to place your piece: ")
try:
play.append(int(x))
play.append(int(y))
except:
print("Fill in Both values!")
self.main()
self.update_board(play, player)
self.show_board()
self.swap_turns()
z = self.players_turn()
if z == 0: name = self.players[1][0]
if z == 1: name = self.players[0][0]
print("player {0} wins!".format(name))
def tutorial(self):
pass
def AI(self):
pass
if __name__ == "__main__":
tic_tac_toe = tic_tac_toe()
tic_tac_toe.main()