Skip to content

Instantly share code, notes, and snippets.

@codeboy101
Last active January 16, 2016 08:03
Show Gist options
  • Select an option

  • Save codeboy101/a066756b850e4613ec0e to your computer and use it in GitHub Desktop.

Select an option

Save codeboy101/a066756b850e4613ec0e to your computer and use it in GitHub Desktop.
import time
import random
game_map = [""*i for i in range(9)]
spots_for_comp = [int(x) for x in range(9)]
def checkWinner() :
if all(x==comp_sign for x in game_map[0:3]) :
print("comp wins")
if all(x==user_sign for x in game_map[0:3]) :
print("user wins")
if all(x==comp_sign for x in game_map[3:6]) :
print("comp wins")
if all(x==user_sign for x in game_map[3:6]) :
print("user wins")
if all(x==comp_sign for x in game_map[6:9]) :
print("comp wins")
if all(x==user_sign for x in game_map[6:9]) :
print("user wins")
if game_map[0] == game_map[4] == game_map[8] == comp_sign :
print("comp wins")
if game_map[0] == game_map[4] == game_map[8] == user_sign :
print("user wins")
if game_map[2] == game_map[4] == game_map[6] == comp_sign :
print("comp wins")
if game_map[2] == game_map[4] == game_map[6] == user_sign :
print("user wins")
if game_map[0] == game_map[3] == game_map[6] == comp_sign :
print("comp wins")
if game_map[0] == game_map[3] == game_map[6] == user_sign :
print("user wins")
if game_map[1] == game_map[4] == game_map[7] == comp_sign :
print("comp wins")
if game_map[1] == game_map[4] == game_map[7] == user_sign :
print("user wins")
if game_map[2] == game_map[6] == game_map[8] == comp_sign :
print("comp wins")
if game_map[2] == game_map[6] == game_map[8] == user_sign :
print("user wins")
print("the empty spaces are indexed 0-8 from left to right , top to bottom")
user_sign = input("choose x or o: ")
comp_sign = ""
if user_sign == "x":
comp_sign = "o"
else :
comp_sign = "x"
for i in range(0,3):
where_user_put = int(input("choose where to put {}".format(user_sign)))
if game_map[where_user_put] != "":
continue
game_map[where_user_put] = user_sign
comp_puts_here = random.choice(spots_for_comp)
if game_map[i] != "":
continue
game_map[comp_puts_here] = comp_sign
print([game_map[i:i+3] for i in range(0,len(game_map),3)])
checkWinner()
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment