Created
December 20, 2017 23:31
-
-
Save Zeitsperre/bd7fc801664b3da64c4298fb256eeaf2 to your computer and use it in GitHub Desktop.
A hokey but functional TicTacToe game
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
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Dec 14 17:34:07 2017 | |
""" | |
import sys | |
import random | |
import time | |
def boardOut(board): | |
""" | |
This functions prints out the board. | |
""" | |
print("\n {6} | {7} | {8} \n\n------------------\n\n {3} | {4} | {5} \n\n------------------\n\n {0} | {1} | {2} \n".format(*board)) | |
def playerInput(player,playerx,board): | |
""" | |
This function accepts inputs from a player. | |
""" | |
if playerx == True: | |
marker = "X" | |
else: | |
marker = "O" | |
print("{}'s turn!").format(player) | |
print("Where would you like to place your {} \n\n").format(marker) | |
mark = [] | |
while mark != "x" or mark != "o": | |
mark = raw_input("Choose a space (1-9) and press enter\n") | |
if int(mark)-1 > 8 or int(mark)-1 < 0: | |
print("This is not a valid choice\n") | |
boardOut(board) | |
continue | |
elif board[int(mark)-1] == "x" or board[int(mark)-1] =="o": | |
print("This is not a valid choice\n") | |
boardOut(board) | |
continue | |
else: | |
board[int(mark)-1] = marker | |
return board | |
def winConditions(player,marker,board): | |
""" | |
This function checks the winning conditions of the game. | |
""" | |
if marker == True: | |
marker = "X" | |
else: | |
marker = "O" | |
if board[0] == board[1] == board[2] == marker or board[3] == board[4] == board[5]== marker or board[6] == board[7] == board[8] == marker: | |
boardOut(board) | |
print("{} has won the game horizontally!").format(player) | |
return True | |
elif board[0] == board[3] == board[6] == marker or board[1] == board[4] == board[7] == marker or board[2] == board[5] == board[8] == marker: | |
boardOut(board) | |
print("{} has won the game vertically!").format(player) | |
return True | |
elif board[0] == board[4] == board[8] == marker or board[2] == board[4] == board[6] == marker: | |
boardOut(board) | |
print("{} has won the game diagonally!").format(player) | |
return True | |
else: | |
tied = 0 | |
for move in board: | |
if move == "X" or move == 'O': | |
tied += 1 | |
if tied == 9: | |
boardOut(board) | |
print("The game is tied!") | |
return True | |
else: | |
return False | |
def goesFirst(p1,p2): | |
""" | |
Function to determine which player begins. | |
""" | |
time.sleep(1) | |
print("\nRolling the dice!") | |
time.sleep(2) | |
print("Looks like...") | |
time.sleep(1) | |
if random.random() > 0.5: | |
print("{} goes first!\n".format(p1)) | |
time.sleep(1) | |
return True | |
else: | |
print("{} goes first!\n".format(p2)) | |
time.sleep(1) | |
return False | |
def playAgain(): | |
""" | |
This function asks player if they want to play again | |
""" | |
while True: | |
playAgain = raw_input("Would you like to play again? (y/n): ") | |
if playAgain == "y": | |
print("Starting a new game. Have fun!") | |
setupGame() | |
elif playAgain == 'n': | |
sys.exit() | |
else: | |
print("That was not a valid answer. only 'y' or 'n' accepted.") | |
continue | |
def runGame(p1,p1x,p2,p2x): | |
""" | |
The main game function. Runs most functions that create the game. | |
""" | |
board=["1","2","3", | |
"4","5","6", | |
"7","8","9"] | |
while True: | |
boardOut(board) | |
playerInput(p1,p1x,board) | |
if winConditions(p1,p1x,board) == True: | |
break | |
boardOut(board) | |
playerInput(p2,p2x,board) | |
if winConditions(p2,p2x,board) == True: | |
break | |
playAgain() | |
def setupGame(): | |
print("Welcome to a really hokey tic tac toe game!") | |
p1 = ["",""] | |
p2 = ["",""] | |
p1[0] = raw_input("Player 1, Please Enter Your Name: ") | |
p2[0] = raw_input("Player 2, Please Enter Your Name: ") | |
first = goesFirst(p1[0],p2[0]) | |
while True: | |
if first: | |
x = raw_input("{}, Would you like to be X? (y/n): ".format(p1[0])) | |
if x.lower() == "y": | |
p1[1] = True | |
p2[1] = False | |
elif x.lower() == "n": | |
p1[1] = False | |
p2[1] = True | |
else: | |
print("That was not a valid choice") | |
continue | |
runGame(p1[0],p1[1],p2[0],p2[1]) | |
else: | |
x = raw_input("{}, Would you like to be X? (y/n): ".format(p2[0])) | |
if x.lower() == "y": | |
p1[1] = True | |
p2[1] = False | |
elif x.lower() == "n": | |
p1[1] = False | |
p2[1] = True | |
else: | |
print("That was not a valid choice") | |
continue | |
runGame(p2[0],p2[1],p1[0],p1[1]) | |
if __name__ == "__main__": | |
while True: | |
setupGame() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment