Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tic Tac Toe for RC
- print("Welcome to a game of Tic Tac Toe!")
- print("-Coded in Python 3 by Ian Ross for The Recurse Center-\n")
- # Displays board in its current state
- def display_board(board):
- print(board[0], "|", board[1], "|", board[2])
- print("---------")
- print(board[3], "|", board[4], "|", board[5])
- print("---------")
- print(board[6], "|", board[7], "|", board[8])
- # Checks to see if there is a winning combo of squares
- def is_winner(letter):
- # Horizontal combos
- if board[0] == letter and board[1] == letter and board[2] == letter:
- return True
- if board[3] == letter and board[4] == letter and board[5] == letter:
- return True
- if board[6] == letter and board[7] == letter and board[8] == letter:
- return True
- # Vertical combos
- if board[0] == letter and board[3] == letter and board[6] == letter:
- return True
- if board[1] == letter and board[4] == letter and board[7] == letter:
- return True
- if board[2] == letter and board[5] == letter and board[8] == letter:
- return True
- # Diagonal combos
- if board[0] == letter and board[4] == letter and board[8] == letter:
- return True
- if board[6] == letter and board[4] == letter and board[2] == letter:
- return True
- # Checks to make sure any given square is taken
- def is_space_free(square):
- return board[square] == " "
- # Returns true if there is no longer a square without an X or O
- def is_board_full():
- for square in board:
- if square == " ":
- return False
- return True
- def get_move(letter):
- print("\nType which square you'd like to fill on the board")
- print("1 is the top left, 5 is the middle, and 9 is the bottom right \n")
- # Represented as 1 - 9 as it is easier for the user than 0 - 8
- square = int(input("\nWhich square do you choose for " + letter +"? (1 - 9) : "))
- return (square - 1)
- def failed_move():
- print("Sorry, that square is taken! You have lost your turn!\n")
- def make_move(square, letter):
- if is_space_free(square):
- board[square] = letter
- else:
- failed_move()
- # Main loop (besides the game loop) to potentially allow for resetting with an AI
- while True:
- begin = input("Would you like to play a game? (Yes/No) : ")
- if begin.lower() == "yes":
- playing_game = True
- elif begin.lower() == "no":
- print("That's too bad! Not sure why you ran this program, to be honest...")
- playing_game = False
- # Setting the board
- # Representation
- board = [" ", " ", " ", # [0, 1, 2,
- " ", " ", " ", # 3, 4, 5,
- " ", " ", " "] # 6, 7, 8]
- letter = "X"
- # Game loop
- while playing_game:
- # First Turn
- if letter == "X":
- # Display the board
- display_board(board)
- move = get_move(letter)
- make_move(move, letter)
- # Before the letter switches it goe sthrough a win/draw check
- if is_winner(letter):
- display_board(board)
- print("\nCongratulations! " + letter + " won the game!")
- break
- else:
- if is_board_full():
- display_board(board)
- print("\nThe game is a draw!")
- break
- else:
- letter = "O"
- elif letter == "O":
- # Display the board
- display_board(board)
- move = get_move(letter)
- make_move(move, letter)
- if is_winner(letter):
- display_board(board)
- print("\nCongratulations! " + letter + " won the game!")
- break
- else:
- letter = "X"
- break
- print("\nThanks for checking out my little project!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement