Last active
July 28, 2018 03:21
-
-
Save topherPedersen/01f3b1bc579d581d4fa5141b8d4050d9 to your computer and use it in GitHub Desktop.
Tic Tac Toe by Snigdha Chalapalli, a rockstar programming student at theCoderSchool in Flower Mound, TX
This file contains hidden or 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
| # Tic Tac Toe by Snigdha Chalapalli, a rockstar programming student at | |
| # theCoderSchool in Flower Mound, TX <https://thecoderschool.com> | |
| # This is free and unencumbered software released into the public domain. | |
| # For more information, please refer to <http://unlicense.org/> | |
| # TODO: Need to add code that detects when a game ends in a tie/draw. | |
| from time import sleep | |
| import random | |
| print("Welcome to the game of Tic-Tac-Toe By: Snigdha Chalapalli") | |
| print("Instructions:") | |
| print("To win you must get three in a row with your letter.") | |
| print("Good Luck, and let the games begin!") | |
| print(" ") # add spacing (formatting) | |
| ''' | |
| Tic Tac Toe Board Mapping of Multi-Dimensional Array (list of lists) | |
| | 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] | | |
| board is a list which contains three rows. | |
| Each row is also a list, each containing three places | |
| The first row is board[0]. | |
| The second row is board[1]. | |
| And the third row is board[2]. | |
| Each row contains three places which will either contain an "X", an "O", or will be empty. | |
| To access each place on the board, simply enter the row number, and then the place number. | |
| For example: board[1][1] | |
| ''' | |
| # Create a Multi-Dimensional Array (a list of lists) | |
| board = [] | |
| board.append([]) | |
| board.append([]) | |
| board.append([]) | |
| # Row 1 | |
| board[0].append("1") | |
| board[0].append("2") | |
| board[0].append("3") | |
| # Row 2 | |
| board[1].append("4") | |
| board[1].append("5") | |
| board[1].append("6") | |
| # Row 3 | |
| board[2].append("7") | |
| board[2].append("8") | |
| board[2].append("9") | |
| def printBoard(): | |
| print("|" + board[0][0] + "|" + board[0][1] + "|" + board[0][2] + "|") | |
| print("|" + board[1][0] + "|" + board[1][1] + "|" + board[1][2] + "|") | |
| print("|" + board[2][0] + "|" + board[2][1] + "|" + board[2][2] + "|") | |
| printBoard() | |
| print(" ") # add spacing | |
| def resetGameBoard(): | |
| global board | |
| board[0][0] = "1" | |
| board[0][1] = "2" | |
| board[0][2] = "3" | |
| board[1][0] = "4" | |
| board[1][1] = "5" | |
| board[1][2] = "6" | |
| board[2][0] = "7" | |
| board[2][1] = "8" | |
| board[2][2] = "9" | |
| row_1_player_x_victory = False | |
| row_2_player_x_victory = False | |
| row_3_player_x_victory = False | |
| col_1_player_x_victory = False | |
| col_2_player_x_victory = False | |
| col_3_player_x_victory = False | |
| diag_159_player_x_victory = False | |
| diag_357_player_x_victory = False | |
| row_1_player_o_victory = False | |
| row_2_player_o_victory = False | |
| row_3_player_o_victory = False | |
| col_1_player_o_victory = False | |
| col_2_player_o_victory = False | |
| col_3_player_o_victory = False | |
| diag_159_player_o_victory = False | |
| diag_357_player_o_victory = False | |
| keepPlaying = True | |
| victory = False | |
| playAgain = "..." # declare global string variable | |
| def checkForVictory(): | |
| # //////////////////////////////////////////////////////////////////////////// | |
| # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
| # TODO: DETECT WHETHER THE GAME HAS BEEN WON | |
| # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
| # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| # \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
| # Detect If Game Has Been Won | |
| # TODO: Check row 1, row 2, row 3 | |
| # TODO: Check col 1, col 2, col 3 | |
| # TODO: Check diagonal victory (top left to bottom right, top left to bottom right) 1,5,9 : 3, 5, 7 | |
| global row_1_player_x_victory | |
| global row_2_player_x_victory | |
| global row_3_player_x_victory | |
| global col_1_player_x_victory | |
| global col_2_player_x_victory | |
| global col_3_player_x_victory | |
| global diag_159_player_x_victory | |
| global diag_357_player_x_victory | |
| global row_1_player_o_victory | |
| global row_2_player_o_victory | |
| global row_3_player_o_victory | |
| global col_1_player_o_victory | |
| global col_2_player_o_victory | |
| global col_3_player_o_victory | |
| global diag_159_player_o_victory | |
| global diag_357_player_o_victory | |
| global victory | |
| global player_x_victory | |
| global player_o_victory | |
| global keepPlaying | |
| global computersMove | |
| global firstGame | |
| global firstMoveOfNewGame | |
| lookForWinner = True | |
| while lookForWinner == True: | |
| # Detect Player X Row 1 Victory | |
| if board[0][0] == "X" and board[0][1] == "X" and board[0][2] == "X": | |
| row_1_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player X Row 2 Victory | |
| elif board[1][0] == "X" and board[1][1] == "X" and board[1][2] == "X": | |
| row_2_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player X Row 3 Victory | |
| elif board[2][0] == "X" and board[2][1] == "X" and board[2][2] == "X": | |
| row_3_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player X Col 1 Victory | |
| elif board[0][0] == "X" and board[1][0] == "X" and board[2][0] == "X": | |
| col_1_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player X Col 2 Victory | |
| elif board[0][1] == "X" and board[1][1] == "X" and board[2][1] == "X": | |
| col_2_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player X Col 3 Victory | |
| elif board[0][2] == "X" and board[1][2] == "X" and board[2][2] == "X": | |
| col_3_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player X Diagonal Victory (Positions 1, 5, 9) | |
| elif board[0][0] == "X" and board[1][1] == "X" and board[2][2] == "X": | |
| col_2_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player X Diagonal Victory (Positions 3, 5, 7) | |
| elif board[0][2] == "X" and board[1][1] == "X" and board[2][0] == "X": | |
| col_3_player_x_victory = True | |
| victory = True | |
| player_x_victory = True | |
| lookForWinner = False | |
| # Detect Player O Row 1 Victory | |
| elif board[0][0] == "O" and board[0][1] == "O" and board[0][2] == "O": | |
| row_1_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| # Detect Player O Row 2 Victory | |
| elif board[1][0] == "O" and board[1][1] == "O" and board[1][2] == "O": | |
| row_2_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| # Detect Player O Row 3 Victory | |
| elif board[2][0] == "O" and board[2][1] == "O" and board[2][2] == "O": | |
| row_3_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| # Detect Player O Col 1 Victory | |
| elif board[0][0] == "O" and board[1][0] == "O" and board[2][0] == "O": | |
| col_1_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| # Detect Player O Col 2 Victory | |
| elif board[0][1] == "O" and board[1][1] == "O" and board[2][1] == "O": | |
| col_2_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| # Detect Player O Col 3 Victory | |
| elif board[0][2] == "O" and board[1][2] == "O" and board[2][2] == "O": | |
| col_3_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| # Detect Player O Diagonal Victory (Positions 1, 5, 9) | |
| elif board[0][0] == "O" and board[1][1] == "O" and board[2][2] == "O": | |
| col_2_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| # Detect Player O Diagonal Victory (Positions 3, 5, 7) | |
| elif board[0][2] == "O" and board[1][1] == "O" and board[2][0] == "O": | |
| col_3_player_o_victory = True | |
| victory = True | |
| player_o_victory = True | |
| lookForWinner = False | |
| else: | |
| lookForWinner = False | |
| if victory == True: | |
| if player_x_victory == True: | |
| printBoard() | |
| print("Victory is yours! Player X Wins") | |
| else: | |
| printBoard() | |
| print("You Lose! Player 0 is Victorious") | |
| # Ask Player if He or She would like to play again. | |
| if victory == True: | |
| computersMove = False | |
| resetGameBoard() | |
| playAgain = raw_input("Would you like to play again? Enter 'y' for yes, 'n' for no.") | |
| if playAgain == "y": | |
| keepPlaying = True | |
| firstGame = False | |
| firstMoveOfNewGame = True | |
| else: | |
| keepPlaying = False | |
| print("Goodbye!") | |
| firstGame = True | |
| firstMoveOfNewGame = False | |
| computersMove = False | |
| # Main Game Loop | |
| while keepPlaying == True: | |
| if computersMove == False: | |
| if firstGame == False and firstMoveOfNewGame == True: | |
| printBoard() | |
| firstMoveOfNewGame = False | |
| # Prompt User to Select Next Move | |
| nextMove = raw_input("Make your next move by entering the number of the space where you would like to place your mark.") | |
| # Cast nextMove Variable as Integer | |
| nextMove = int(nextMove) | |
| # Make Move | |
| if nextMove == 1 and computersMove == False: | |
| board[0][0] = "X" | |
| elif nextMove == 2 and computersMove == False: | |
| board[0][1] = "X" | |
| elif nextMove == 3 and computersMove == False: | |
| board[0][2] = "X" | |
| elif nextMove == 4 and computersMove == False: | |
| board[1][0] = "X" | |
| elif nextMove == 5 and computersMove == False: | |
| board[1][1] = "X" | |
| elif nextMove == 6 and computersMove == False: | |
| board[1][2] = "X" | |
| elif nextMove == 7 and computersMove == False: | |
| board[2][0] = "X" | |
| elif nextMove == 8 and computersMove == False: | |
| board[2][1] = "X" | |
| elif nextMove == 9 and computersMove == False: | |
| board[2][2] = "X" | |
| checkForVictory() | |
| if victory == False: | |
| printBoard() | |
| else: | |
| # Run Loop until the computer has found an open space to make its next move | |
| openSpaceOnBoardFound = False | |
| while openSpaceOnBoardFound == False: | |
| randomNumber = random.randint(1, 9) | |
| if randomNumber == 1: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[0][0] != "X" and board[0][0] != "O": | |
| openSpaceOnBoardFound = True | |
| board[0][0] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 2: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[0][1] != "X" and board[0][1] != "O": | |
| openSpaceOnBoardFound = True | |
| board[0][1] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 3: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[0][2] != "X" and board[0][2] != "O": | |
| openSpaceOnBoardFound = True | |
| board[0][2] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 4: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[1][0] != "X" and board[1][0] != "O": | |
| openSpaceOnBoardFound = True | |
| board[1][0] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 5: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[1][1] != "X" and board[1][1] != "O": | |
| openSpaceOnBoardFound = True | |
| board[1][1] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 6: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[1][2] != "X" and board[1][2] != "O": | |
| openSpaceOnBoardFound = True | |
| board[1][2] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 7: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[2][0] != "X" and board[2][0] != "O": | |
| openSpaceOnBoardFound = True | |
| board[2][0] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 8: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[2][1] != "X" and board[2][1] != "O": | |
| openSpaceOnBoardFound = True | |
| board[2][1] = "O" | |
| print("Computer makes its move...") | |
| elif randomNumber == 9: | |
| # Check to make sure space hasn't already been checked with an 'X' or 'O' | |
| # If the space is open, mark the space with an "O" | |
| if board[2][2] != "X" and board[2][2] != "O": | |
| openSpaceOnBoardFound = True | |
| board[2][2] = "O" | |
| print("Computer makes its move...") | |
| # Check to See if Computer Opponent Has Won After Making its Move | |
| checkForVictory() | |
| if victory == False: | |
| printBoard() | |
| # Toggle Next Move Back and Forth Between the Player and the Computer | |
| if computersMove == False and victory == False: | |
| computersMove = True | |
| else: | |
| computersMove = False | |
| if victory == True: | |
| # Reset Game State Variables | |
| row_1_player_x_victory = False | |
| row_2_player_x_victory = False | |
| row_3_player_x_victory = False | |
| col_1_player_x_victory = False | |
| col_2_player_x_victory = False | |
| col_3_player_x_victory = False | |
| diag_159_player_x_victory = False | |
| diag_357_player_x_victory = False | |
| row_1_player_o_victory = False | |
| row_2_player_o_victory = False | |
| row_3_player_o_victory = False | |
| col_1_player_o_victory = False | |
| col_2_player_o_victory = False | |
| col_3_player_o_victory = False | |
| diag_159_player_o_victory = False | |
| diag_357_player_o_victory = False | |
| player_x_victory = False | |
| player_o_victory = False | |
| computersMove = False | |
| victory = False | |
| # ---------------------------------------------------------------------- | |
| # -----------------------END MAIN GAME LOOP ---------------------------- | |
| # ---------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment