My first Python project: Tic Tac Toe
I just finished coding a Tic Tac Toe game for my first Python project. Everything seems to be running correctly, but I wanted to get some advice on things I can improve. The main loop is pretty messy and I'm sure there is a way I can clean that up. Also I have many repetitive if statements that I think can be looped somehow. Just looking for tips, thanks!
import sys
import time
def check_position(the_position):
if the_position not in taken_positions:
return True
else:
return False
def get_position():
position = int(input("Enter a position : "))
return position
def draw_position_x(location):
if location == 1:
game_board[0][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def draw_position_o(location):
if location == 1:
game_board[0][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def win_check():
if game_board[0][0] == 'x' and game_board[1][0] == 'x' and game_board[2][0] == 'x' or
(game_board[0][0] == 'o' and game_board[1][0] == 'o' and game_board[2][0] == 'o'): # 1 HORIZONTAL
return True
if game_board[0][1] == 'x' and game_board[1][1] == 'x' and game_board[2][1] == 'x' or
(game_board[0][1] == 'o' and game_board[1][1] == 'o' and game_board[2][1] == 'o'): # 2 HORIZONTAL
return True
if game_board[0][2] == 'x' and game_board[1][2] == 'x' and game_board[2][2] == 'x' or
(game_board[0][2] == 'o' and game_board[1][2] == 'o' and game_board[2][2] == 'o'): # 3 HORIZONTAL
return True
if game_board[0][0] == 'x' and game_board[0][1] == 'x' and game_board[0][2] == 'x' or
(game_board[0][0] == 'o' and game_board[0][1] == 'o' and game_board[0][2] == 'o'): # 1 VERTICAL
return True
if game_board[1][0] == 'x' and game_board[1][1] == 'x' and game_board[1][2] == 'x' or
(game_board[1][0] == 'o' and game_board[1][1] == 'o' and game_board[1][2] == 'o'): # 2 VERTICAL
return True
if game_board[2][0] == 'x' and game_board[2][1] == 'x' and game_board[2][2] == 'x' or
(game_board[2][0] == 'o' and game_board[2][1] == 'o' and game_board[2][2] == 'o'): # 3 VERTICAL
return True
if game_board[0][0] == 'x' and game_board[1][1] == 'x' and game_board[2][2] == 'x' or
(game_board[0][0] == 'o' and game_board[1][1] == 'o' and game_board[2][2] == 'o'): # 1 DIAGONAL
return True
if game_board[0][2] == 'x' and game_board[1][1] == 'x' and game_board[2][0] == 'x' or
(game_board[0][2] == 'o' and game_board[1][1] == 'o' and game_board[2][0] == 'o'): # 2 DIAGONAL
return True
else:
return False
intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
print("Welcome to TIC TAC TOE!")
print("You can pick location by identifying the position on the board. (There are 9 positions)")
print("The player who plays first will be using 'x' and the second player will be using 'o'.")
print("|" + intro_board[0][0] + "|" + intro_board[1][0] + "|" + intro_board[2][0] + "|")
print("|" + intro_board[0][1] + "|" + intro_board[1][1] + "|" + intro_board[2][1] + "|")
print("|" + intro_board[0][2] + "|" + intro_board[1][2] + "|" + intro_board[2][2] + "|")
game_board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
taken_positions =
which_turn = 'P1'
win = False
num_moves = 0
running = True
isValid = True
while running:
while num_moves < 9 and not win:
which_position = get_position()
if which_turn == 'P1':
isValid = check_position(which_position)
if isValid:
which_turn = 'P2'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_x(which_position)
break
if not isValid:
print("Position taken, try again.")
break
if which_turn == 'P2':
isValid = check_position(which_position)
if isValid:
which_turn = 'P1'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_o(which_position)
break
if not isValid:
print("Position taken, try again.")
break
win = win_check()
if win:
print("We have a winner!!!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if num_moves == 9 and not win:
print("Draw!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if not running:
print("Exiting Game")
sys.exit()
python python-3.x
New contributor
add a comment |
I just finished coding a Tic Tac Toe game for my first Python project. Everything seems to be running correctly, but I wanted to get some advice on things I can improve. The main loop is pretty messy and I'm sure there is a way I can clean that up. Also I have many repetitive if statements that I think can be looped somehow. Just looking for tips, thanks!
import sys
import time
def check_position(the_position):
if the_position not in taken_positions:
return True
else:
return False
def get_position():
position = int(input("Enter a position : "))
return position
def draw_position_x(location):
if location == 1:
game_board[0][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def draw_position_o(location):
if location == 1:
game_board[0][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def win_check():
if game_board[0][0] == 'x' and game_board[1][0] == 'x' and game_board[2][0] == 'x' or
(game_board[0][0] == 'o' and game_board[1][0] == 'o' and game_board[2][0] == 'o'): # 1 HORIZONTAL
return True
if game_board[0][1] == 'x' and game_board[1][1] == 'x' and game_board[2][1] == 'x' or
(game_board[0][1] == 'o' and game_board[1][1] == 'o' and game_board[2][1] == 'o'): # 2 HORIZONTAL
return True
if game_board[0][2] == 'x' and game_board[1][2] == 'x' and game_board[2][2] == 'x' or
(game_board[0][2] == 'o' and game_board[1][2] == 'o' and game_board[2][2] == 'o'): # 3 HORIZONTAL
return True
if game_board[0][0] == 'x' and game_board[0][1] == 'x' and game_board[0][2] == 'x' or
(game_board[0][0] == 'o' and game_board[0][1] == 'o' and game_board[0][2] == 'o'): # 1 VERTICAL
return True
if game_board[1][0] == 'x' and game_board[1][1] == 'x' and game_board[1][2] == 'x' or
(game_board[1][0] == 'o' and game_board[1][1] == 'o' and game_board[1][2] == 'o'): # 2 VERTICAL
return True
if game_board[2][0] == 'x' and game_board[2][1] == 'x' and game_board[2][2] == 'x' or
(game_board[2][0] == 'o' and game_board[2][1] == 'o' and game_board[2][2] == 'o'): # 3 VERTICAL
return True
if game_board[0][0] == 'x' and game_board[1][1] == 'x' and game_board[2][2] == 'x' or
(game_board[0][0] == 'o' and game_board[1][1] == 'o' and game_board[2][2] == 'o'): # 1 DIAGONAL
return True
if game_board[0][2] == 'x' and game_board[1][1] == 'x' and game_board[2][0] == 'x' or
(game_board[0][2] == 'o' and game_board[1][1] == 'o' and game_board[2][0] == 'o'): # 2 DIAGONAL
return True
else:
return False
intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
print("Welcome to TIC TAC TOE!")
print("You can pick location by identifying the position on the board. (There are 9 positions)")
print("The player who plays first will be using 'x' and the second player will be using 'o'.")
print("|" + intro_board[0][0] + "|" + intro_board[1][0] + "|" + intro_board[2][0] + "|")
print("|" + intro_board[0][1] + "|" + intro_board[1][1] + "|" + intro_board[2][1] + "|")
print("|" + intro_board[0][2] + "|" + intro_board[1][2] + "|" + intro_board[2][2] + "|")
game_board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
taken_positions =
which_turn = 'P1'
win = False
num_moves = 0
running = True
isValid = True
while running:
while num_moves < 9 and not win:
which_position = get_position()
if which_turn == 'P1':
isValid = check_position(which_position)
if isValid:
which_turn = 'P2'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_x(which_position)
break
if not isValid:
print("Position taken, try again.")
break
if which_turn == 'P2':
isValid = check_position(which_position)
if isValid:
which_turn = 'P1'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_o(which_position)
break
if not isValid:
print("Position taken, try again.")
break
win = win_check()
if win:
print("We have a winner!!!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if num_moves == 9 and not win:
print("Draw!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if not running:
print("Exiting Game")
sys.exit()
python python-3.x
New contributor
add a comment |
I just finished coding a Tic Tac Toe game for my first Python project. Everything seems to be running correctly, but I wanted to get some advice on things I can improve. The main loop is pretty messy and I'm sure there is a way I can clean that up. Also I have many repetitive if statements that I think can be looped somehow. Just looking for tips, thanks!
import sys
import time
def check_position(the_position):
if the_position not in taken_positions:
return True
else:
return False
def get_position():
position = int(input("Enter a position : "))
return position
def draw_position_x(location):
if location == 1:
game_board[0][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def draw_position_o(location):
if location == 1:
game_board[0][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def win_check():
if game_board[0][0] == 'x' and game_board[1][0] == 'x' and game_board[2][0] == 'x' or
(game_board[0][0] == 'o' and game_board[1][0] == 'o' and game_board[2][0] == 'o'): # 1 HORIZONTAL
return True
if game_board[0][1] == 'x' and game_board[1][1] == 'x' and game_board[2][1] == 'x' or
(game_board[0][1] == 'o' and game_board[1][1] == 'o' and game_board[2][1] == 'o'): # 2 HORIZONTAL
return True
if game_board[0][2] == 'x' and game_board[1][2] == 'x' and game_board[2][2] == 'x' or
(game_board[0][2] == 'o' and game_board[1][2] == 'o' and game_board[2][2] == 'o'): # 3 HORIZONTAL
return True
if game_board[0][0] == 'x' and game_board[0][1] == 'x' and game_board[0][2] == 'x' or
(game_board[0][0] == 'o' and game_board[0][1] == 'o' and game_board[0][2] == 'o'): # 1 VERTICAL
return True
if game_board[1][0] == 'x' and game_board[1][1] == 'x' and game_board[1][2] == 'x' or
(game_board[1][0] == 'o' and game_board[1][1] == 'o' and game_board[1][2] == 'o'): # 2 VERTICAL
return True
if game_board[2][0] == 'x' and game_board[2][1] == 'x' and game_board[2][2] == 'x' or
(game_board[2][0] == 'o' and game_board[2][1] == 'o' and game_board[2][2] == 'o'): # 3 VERTICAL
return True
if game_board[0][0] == 'x' and game_board[1][1] == 'x' and game_board[2][2] == 'x' or
(game_board[0][0] == 'o' and game_board[1][1] == 'o' and game_board[2][2] == 'o'): # 1 DIAGONAL
return True
if game_board[0][2] == 'x' and game_board[1][1] == 'x' and game_board[2][0] == 'x' or
(game_board[0][2] == 'o' and game_board[1][1] == 'o' and game_board[2][0] == 'o'): # 2 DIAGONAL
return True
else:
return False
intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
print("Welcome to TIC TAC TOE!")
print("You can pick location by identifying the position on the board. (There are 9 positions)")
print("The player who plays first will be using 'x' and the second player will be using 'o'.")
print("|" + intro_board[0][0] + "|" + intro_board[1][0] + "|" + intro_board[2][0] + "|")
print("|" + intro_board[0][1] + "|" + intro_board[1][1] + "|" + intro_board[2][1] + "|")
print("|" + intro_board[0][2] + "|" + intro_board[1][2] + "|" + intro_board[2][2] + "|")
game_board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
taken_positions =
which_turn = 'P1'
win = False
num_moves = 0
running = True
isValid = True
while running:
while num_moves < 9 and not win:
which_position = get_position()
if which_turn == 'P1':
isValid = check_position(which_position)
if isValid:
which_turn = 'P2'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_x(which_position)
break
if not isValid:
print("Position taken, try again.")
break
if which_turn == 'P2':
isValid = check_position(which_position)
if isValid:
which_turn = 'P1'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_o(which_position)
break
if not isValid:
print("Position taken, try again.")
break
win = win_check()
if win:
print("We have a winner!!!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if num_moves == 9 and not win:
print("Draw!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if not running:
print("Exiting Game")
sys.exit()
python python-3.x
New contributor
I just finished coding a Tic Tac Toe game for my first Python project. Everything seems to be running correctly, but I wanted to get some advice on things I can improve. The main loop is pretty messy and I'm sure there is a way I can clean that up. Also I have many repetitive if statements that I think can be looped somehow. Just looking for tips, thanks!
import sys
import time
def check_position(the_position):
if the_position not in taken_positions:
return True
else:
return False
def get_position():
position = int(input("Enter a position : "))
return position
def draw_position_x(location):
if location == 1:
game_board[0][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'x'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def draw_position_o(location):
if location == 1:
game_board[0][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 2:
game_board[1][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 3:
game_board[2][0] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 4:
game_board[0][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 5:
game_board[1][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 6:
game_board[2][1] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 7:
game_board[0][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 8:
game_board[1][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
if location == 9:
game_board[2][2] = 'o'
print("|" + game_board[0][0] + "|" + game_board[1][0] + "|" + game_board[2][0] + "|")
print("|" + game_board[0][1] + "|" + game_board[1][1] + "|" + game_board[2][1] + "|")
print("|" + game_board[0][2] + "|" + game_board[1][2] + "|" + game_board[2][2] + "|")
def win_check():
if game_board[0][0] == 'x' and game_board[1][0] == 'x' and game_board[2][0] == 'x' or
(game_board[0][0] == 'o' and game_board[1][0] == 'o' and game_board[2][0] == 'o'): # 1 HORIZONTAL
return True
if game_board[0][1] == 'x' and game_board[1][1] == 'x' and game_board[2][1] == 'x' or
(game_board[0][1] == 'o' and game_board[1][1] == 'o' and game_board[2][1] == 'o'): # 2 HORIZONTAL
return True
if game_board[0][2] == 'x' and game_board[1][2] == 'x' and game_board[2][2] == 'x' or
(game_board[0][2] == 'o' and game_board[1][2] == 'o' and game_board[2][2] == 'o'): # 3 HORIZONTAL
return True
if game_board[0][0] == 'x' and game_board[0][1] == 'x' and game_board[0][2] == 'x' or
(game_board[0][0] == 'o' and game_board[0][1] == 'o' and game_board[0][2] == 'o'): # 1 VERTICAL
return True
if game_board[1][0] == 'x' and game_board[1][1] == 'x' and game_board[1][2] == 'x' or
(game_board[1][0] == 'o' and game_board[1][1] == 'o' and game_board[1][2] == 'o'): # 2 VERTICAL
return True
if game_board[2][0] == 'x' and game_board[2][1] == 'x' and game_board[2][2] == 'x' or
(game_board[2][0] == 'o' and game_board[2][1] == 'o' and game_board[2][2] == 'o'): # 3 VERTICAL
return True
if game_board[0][0] == 'x' and game_board[1][1] == 'x' and game_board[2][2] == 'x' or
(game_board[0][0] == 'o' and game_board[1][1] == 'o' and game_board[2][2] == 'o'): # 1 DIAGONAL
return True
if game_board[0][2] == 'x' and game_board[1][1] == 'x' and game_board[2][0] == 'x' or
(game_board[0][2] == 'o' and game_board[1][1] == 'o' and game_board[2][0] == 'o'): # 2 DIAGONAL
return True
else:
return False
intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
print("Welcome to TIC TAC TOE!")
print("You can pick location by identifying the position on the board. (There are 9 positions)")
print("The player who plays first will be using 'x' and the second player will be using 'o'.")
print("|" + intro_board[0][0] + "|" + intro_board[1][0] + "|" + intro_board[2][0] + "|")
print("|" + intro_board[0][1] + "|" + intro_board[1][1] + "|" + intro_board[2][1] + "|")
print("|" + intro_board[0][2] + "|" + intro_board[1][2] + "|" + intro_board[2][2] + "|")
game_board = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
taken_positions =
which_turn = 'P1'
win = False
num_moves = 0
running = True
isValid = True
while running:
while num_moves < 9 and not win:
which_position = get_position()
if which_turn == 'P1':
isValid = check_position(which_position)
if isValid:
which_turn = 'P2'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_x(which_position)
break
if not isValid:
print("Position taken, try again.")
break
if which_turn == 'P2':
isValid = check_position(which_position)
if isValid:
which_turn = 'P1'
num_moves = num_moves + 1
taken_positions.append(which_position)
draw_position_o(which_position)
break
if not isValid:
print("Position taken, try again.")
break
win = win_check()
if win:
print("We have a winner!!!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if num_moves == 9 and not win:
print("Draw!")
print("Exiting in 10 seconds")
time.sleep(10)
running = False
if not running:
print("Exiting Game")
sys.exit()
python python-3.x
python python-3.x
New contributor
New contributor
New contributor
asked 2 mins ago
BobPage
1
1
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
BobPage is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210958%2fmy-first-python-project-tic-tac-toe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
BobPage is a new contributor. Be nice, and check out our Code of Conduct.
BobPage is a new contributor. Be nice, and check out our Code of Conduct.
BobPage is a new contributor. Be nice, and check out our Code of Conduct.
BobPage is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210958%2fmy-first-python-project-tic-tac-toe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown