Last active
August 29, 2015 14:17
-
-
Save lunacodes/8f63aaf0e0af82b95b83 to your computer and use it in GitHub Desktop.
Tic Tac Toe
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
"""Note: | |
There is currently one known bug in this game: | |
If the player prematurely presses Enter, without inputting a Move - the game will crash - due to receiving a literal instead of an int | |
""" | |
import random | |
#Board constructed from strings. Each board position contains the letter chosen by the player | |
def drawBoard(board): | |
print '\n' | |
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 ' | ' + ' | ' | |
print '\n' | |
#Random choice of which player goes first | |
def whoGoesFirst(): | |
first_move = random.randint(0,1) | |
if first_move == 0: | |
turn = "player 1" | |
elif first_move == 1: | |
turn = "player 2" | |
else: | |
print "There's been some sort of mistake" | |
return turn | |
#Runs through the free spaces on the board | |
def isBoardFull(board): | |
for space in range(1,10): | |
if isSpaceFree(board, space): #If any are free | |
return False #Return False | |
return True #Otherwise tell the game that the board's full | |
#Checks that space is a vaild move | |
def isSpaceFree(board, move): | |
if board[move] == ' ': | |
return True | |
else: | |
return False | |
#All of the possible different Win Conditions | |
def isWinner(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)) | |
#Gets the position to place the player's letter in | |
def getMove(turn): | |
move = 12 #non-specific number, outside of range just for assignment purposes | |
while move not in range(0,10): | |
if turn == "player 1": | |
move = input("Player 1: What is your move? (1-9): ") | |
else: #Player 2 turn | |
move = input("Player 2: What is your move? (1-9): ") | |
return move | |
#After Game is finished, Give the player the option to play again | |
def playAgain(): | |
playAgain = raw_input("Would you like to try again? (yes or no): ") | |
print "\n" | |
if playAgain.lower() == "yes": | |
main() | |
else: | |
print "Thanks for playing" | |
print "The End" | |
#Intro Screen using raw_input as a Start Button | |
def intro(): | |
print "HEYA - WELCOME TO TIC TAC TOE!!" | |
raw_input("Press any key to get started: ") | |
print "\n" | |
def main(): | |
run = True | |
while run: | |
theBoard = [' '] * 10 #Data structure for Board. Starts with 10 blank spaces | |
player_one_letter = '' | |
while not (player_one_letter.upper() == "X" or player_one_letter.upper() == "O"): #Makes sure the player one choice can only be X or O | |
player_one_letter = raw_input("Player 1 - Do you want to be X or O? ").upper() | |
if player_one_letter.upper() == "X": | |
player_two_letter = "O" | |
elif player_one_letter.upper() == "O": | |
player_two_letter = "X" | |
print "\n" | |
print "Player one is: ", player_one_letter | |
print "Player two is: ", player_two_letter | |
drawBoard(theBoard) | |
turn = whoGoesFirst() | |
print turn, "Goes First!!" | |
gameIsPlaying = True | |
while gameIsPlaying: | |
if turn == 'player 1': #Turn Sequence for Player One - The code for Player 1 sequence & Player 2 sequence is essentially the same. Perhaps there is a way to consolidate it? | |
move = getMove(turn) | |
if isSpaceFree(theBoard, move) == True: #Check if Space is free | |
theBoard[move] = player_one_letter #Alter Data Structure for the Board | |
drawBoard(theBoard) #Draw the Board | |
turn = 'player 2' | |
if isWinner(theBoard, player_one_letter) == True: | |
print "WE HAVE A WINNER!! - PLAYER 1 WINS!! \n" | |
gameIsPlaying = False # Finish round of game by turning gameIsPlaying loop off. | |
elif isBoardFull(theBoard) == True: #After Player Move, if they haven't won, check for Draw | |
print "Draw :/ " | |
print "\n" | |
gameIsPlaying = False | |
else: | |
print "THERE ISN'T A FREE SPACE THERE" | |
else: | |
move = getMove(turn) | |
if isSpaceFree(theBoard, move) == True: | |
theBoard[move] = player_two_letter | |
drawBoard(theBoard) | |
turn = 'player 1' | |
if isWinner(theBoard, player_two_letter) == True: | |
print "WE HAVE A WINNER!! PLAYER 2 WINS!!" | |
print "\n" | |
gameIsPlaying = False | |
elif isBoardFull(theBoard) == True: | |
print "Draw :/ " | |
print "\n" | |
gameIsPlaying = False | |
else: | |
print "THERE ISN'T A FREE SPACE THERE" | |
run = False #Turn the main Loop off so that playAgain() can run | |
intro() | |
main() | |
playAgain() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment