Skip to content

Instantly share code, notes, and snippets.

@jackgolding
Last active August 29, 2015 14:11
Show Gist options
  • Save jackgolding/404cb65eee6d36920eb6 to your computer and use it in GitHub Desktop.
Save jackgolding/404cb65eee6d36920eb6 to your computer and use it in GitHub Desktop.
Tic-Tac-Toe Implementation in Python
#
# CONSTANTS
#
PLAYER_ICON = 'X'
BOARD_SIZE = 3
WELCOME_MESSAGE = ''
WIN_MESSAGE = ''
INVALID_MOVE_MESSAGE = ''
BOARD_PRINT_FORMAT = '''
{0}|{1}|{2}
-----
{3}|{4}|{5}
-----
{6}|{7}|{8}
'''
board = [[' '] * BOARD_SIZE] * BOARD_SIZE
#
# Check if a move is valid on a certain board
# Returns True or False
#
def check_valid_move(board, move):
return True
#
# Play a move and return the board, move must be valid
# Returns board
#
def play_move(board, player, move):
return board
#
# Given a board, check if player has won
# Values for player = 'X' or 'O'
# Returns True or False
#
def check_win(board, player):
return False
#
# Checks if the board is full
# Returns True if the board is full or False otherwise
#
def check_board_full(board):
return True
#
# Returns a random empty position in the board
#
def generate_random_move(board):
return
#
# Returns the icon opposite to player_icon
#
def choose_ai_icon(player_icon):
if player_icon == 'X':
return 'O'
if player_icon == 'O':
return 'X'
def format_board(board, print_format):
return print_format.format(board[0][0], board[0][1], board[0][2],
board[1][0], board[1][1], board[1][2],
board[2][0], board[2][1], board[2][2])
#
# Main
#
def main():
print(WELCOME_MESSAGE)
print format_board(board,BOARD_PRINT_FORMAT)
AI_ICON = choose_ai_icon(PLAYER_ICON)
return
if __name__ == '__main__':
main()
@jackgolding
Copy link
Author

Incomplete command-line tic-tac-toe in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment