A classic game for two players, X and O, who take turns marking spaces in a 3×3 grid. It's great for learning about 2D arrays and basic game logic in Python
The Code
Here's a sneak peek into the Python code that makes this game come to life. We'll use basic programming constructs like loops, conditionals, and functions to create an interactive Tic-Tac-Toe game.
import random
def draw_board(board):
print(' | |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
def input_player_letter():
letter = ''
while not (letter == 'X' or letter == 'O'):
print('Do you want to be X or O?')
letter = input().upper()
if letter == 'X':
return ['X', 'O']
else:
return ['O', 'X']
def who_goes_first():
if random.randint(0, 1) == 0:
return 'computer'
else:
return 'player'
def make_move(board, letter, move):
board[move] = letter
def is_winner(board, letter):
return ((board[7] == letter and board[8] == letter and board[9] == letter) or
(board[4] == letter and board[5] == letter and board[6] == letter) or
(board[1] == letter and board[2] == letter and board[3] == letter) or
(board[7] == letter and board[4] == letter and board[1] == letter) or
(board[8] == letter and board[5] == letter and board[2] == letter) or
(board[9] == letter and board[6] == letter and board[3] == letter) or
(board[7] == letter and board[5] == letter and board[3] == letter) or
(board[9] == letter and board[5] == letter and board[1] == letter))
def get_board_copy(board):
return board[:]
def is_space_free(board, move):
return board[move] == ' '
def get_player_move(board):
move = ' '
while move not in '1 2 3 4 5 6 7 8 9'.split() or not is_space_free(board, int(move)):
print('What is your next move? (1-9)')
move = input()
return int(move)
def choose_random_move_from_list(board, moves_list):
possible_moves = []
for move in moves_list:
if is_space_free(board, move):
possible_moves.append(move)
if possible_moves:
return random.choice(possible_moves)
else:
return None
def get_computer_move(board, computer_letter):
if computer_letter == 'X':
player_letter = 'O'
else:
player_letter = 'X'
for i in range(1, 10):
copy = get_board_copy(board)
if is_space_free(copy, i):
make_move(copy, computer_letter, i)
if is_winner(copy, computer_letter):
return i
for i in range(1, 10):
copy = get_board_copy(board)
if is_space_free(copy, i):
make_move(copy, player_letter, i)
if is_winner(copy, player_letter):
return i
move = choose_random_move_from_list(board, [1, 3, 7, 9])
if move:
return move
if is_space_free(board, 5):
return 5
return choose_random_move_from_list(board, [2, 4, 6, 8])
def is_board_full(board):
for i in range(1, 10):
if is_space_free(board, i):
return False
return True
print('Welcome to Tic Tac Toe!')
while True:
theBoard = [' '] * 10
playerLetter, computerLetter = input_player_letter()
turn = who_goes_first()
print('The ' + turn + ' will go first.')
game_is_playing = True
while game_is_playing:
if turn == 'player':
draw_board(theBoard)
move = get_player_move(theBoard)
make_move(theBoard, playerLetter, move)
if is_winner(theBoard, playerLetter):
draw_board(theBoard)
print('Hooray! You have won the game!')
game_is_playing = False
else:
if is_board_full(theBoard):
draw_board(theBoard)
print('The game is a tie!')
break
else:
turn = 'computer'
else:
move = get_computer_move(theBoard, computerLetter)
make_move(theBoard, computerLetter, move)
if is_winner(theBoard, computerLetter):
draw_board(theBoard)
print('The computer has beaten you! You lose.')
game_is_playing = False
else:
if is_board_full(theBoard):
draw_board(theBoard)
print('The game is a tie!')
break
else:
turn = 'player'
print('Do you want to play again? (yes or no)')
if not input().lower().startswith('y'):
break
Here is the screenshot of me playing tic-tac-toe:
Enjoy the game! Get the code from Github: https://github.com/IGNITE-Pathways/tic-tac-toe
Pygame Version
Explore the pygame version of the same game. tic-tac-toe-pygame.py file in Github contains the source code. NOTE: Player X starts first
Comments