A tic-tac-toe game, (almost) implemented by GitHub Copilot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Prompt: | |
"""A tic-tac-toe game.""" | |
# Everything down below is autocompleted: | |
def print_board(board): | |
"""Prints the board.""" | |
print(board[0] + ' | ' + board[1] + ' | ' + board[2]) | |
print('---------') | |
print(board[3] + ' | ' + board[4] + ' | ' + board[5]) | |
print('---------') | |
print(board[6] + ' | ' + board[7] + ' | ' + board[8]) | |
def player_input(): | |
"""Asks the player for their input.""" | |
marker = '' | |
while not (marker == 'X' or marker == 'O'): | |
marker = input('Player 1: Do you want to be X or O? ').upper() | |
if marker == 'X': | |
return ('X', 'O') | |
else: | |
return ('O', 'X') | |
def place_marker(board, marker, position): | |
"""Places the marker on the board.""" | |
board[position] = marker | |
def win_check(board, mark): | |
"""Checks if the player has won.""" | |
return ((board[0] == mark and board[1] == mark and board[2] == mark) or | |
(board[3] == mark and board[4] == mark and board[5] == mark) or | |
(board[6] == mark and board[7] == mark and board[8] == mark) or | |
(board[0] == mark and board[3] == mark and board[6] == mark) or | |
(board[1] == mark and board[4] == mark and board[7] == mark) or | |
(board[2] == mark and board[5] == mark and board[8] == mark) or | |
(board[0] == mark and board[4] == mark and board[8] == mark) or | |
(board[2] == mark and board[4] == mark and board[6] == mark)) | |
def choose_first(): | |
"""Chooses which player goes first.""" | |
if random.randint(0, 1) == 0: | |
return 'Player 1' | |
else: | |
return 'Player 2' | |
def space_check(board, position): | |
"""Checks if the space is empty.""" | |
return board[position] == ' ' | |
def full_board_check(board): | |
"""Checks if the board is full.""" | |
for i in range(len(board)): | |
if space_check(board, i): | |
return False | |
return True | |
def player_choice(board): | |
"""Asks the player for their choice.""" | |
position = 0 | |
while position not in [1, 2, 3, 4, 5, 6, 7, 8, 9] or not space_check(board, position): | |
position = int(input('Choose your next position: (1-9) ')) | |
return position | |
def replay(): | |
"""Asks the player if they want to play again.""" | |
return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y') | |
print('Welcome to Tic Tac Toe!') | |
while True: | |
# Set the game up here | |
the_board = [' '] * 10 | |
player1_marker, player2_marker = player_input() | |
turn = choose_first() | |
print(turn + ' will go first.') | |
play_game = input('Are you ready to play? Enter Yes or No: ') | |
if play_game.lower()[0] == 'y': | |
game_on = True | |
else: | |
game_on = False | |
while game_on: | |
# Player 1's turn. | |
if turn == 'Player 1': | |
# Show the board. | |
print_board(the_board) | |
# Choose a position. | |
position = player_choice(the_board) | |
# Place the marker on the board. | |
place_marker(the_board, player1_marker, position) | |
# Check if they won. | |
if win_check(the_board, player1_marker): | |
print_board(the_board) | |
print('Congratulations! You have won the game!') | |
game_on = False | |
else: | |
# If they didn't win, it's now Player 2's turn. | |
turn = 'Player 2' | |
else: | |
# Player 2's turn. | |
# Show the board. | |
print_board(the_board) | |
# Choose a position. | |
position = player_choice(the_board) | |
# Place the marker on the board. | |
place_marker(the_board, player2_marker, position) | |
# Check if they won. | |
if win_check(the_board, player2_marker): | |
print_board(the_board) | |
print('Congratulations! You have won the game!') | |
game_on = False | |
else: | |
# If they didn't win, it's now Player 1's turn. | |
turn = 'Player 1' | |
# If the game is over, ask if they want to play again. | |
if not replay(): | |
break | |
# Clear the board. | |
the_board = [' '] * 10 | |
# Reset the markers. | |
player1_marker, player2_marker = player_input() | |
# Reset the turn. | |
turn = choose_first() | |
# Reset the game_on variable. | |
game_on = True | |
# Reset the board. | |
the_board = [' '] * 10 | |
# Reset the markers. | |
player1_marker, player2_marker = player_input() | |
# Reset the turn. | |
turn = choose_first() | |
# Reset the game_on variable. | |
game_on = True | |
# Reset the board. | |
the_board = [' '] * 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment