Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #USER: TCG
- #Naughts & Crosses
- import time
- import random
- #Defines Grid
- #function is used so that the can be replaced into it's original form when user asks to replay
- def grid_function():
- global g
- g = ["",
- " ----------------------------------",
- " | | | |",
- " | 1 | 2 | 3 |",
- " | | | |",
- " ----------------------------------",
- " | | | |",
- " | 4 | 5 | 6 |",
- " | | | |",
- " ----------------------------------",
- " | | | |",
- " | 7 | 8 | 9 |",
- " | | | |",
- " ----------------------------------"]
- print '\n'.join(g)
- #REPLAY GAME / EXIT GAME
- def replay():
- global turn1l,turn2l
- while True:
- restart = raw_input("\nWould you like to replay (yes/no) : ")
- if restart == "yes":
- i = 0
- turn1l = []
- turn2l = []
- grid_function()
- return 1
- elif restart == "no":
- print '\n' "Thanks for playing!"
- time.sleep(2)
- raise SystemExit()
- else:
- print '\n' "Incorrect input! Please enter either \"yes\" or \"no\""
- #CHECKS IF THERE IS A WINNER
- def winner_checker():
- def winner_checker_2(x,x1,x2):
- #All possilble wins
- winner = ("147", "123", "258", "369", "456", "789", "159", "357")
- if any(all(y in x for y in numbers) for numbers in winner):
- print '\n' * 5, x1, "YOU HAVE WON! GG TO", x2, '\n'
- if replay() == 1:
- return 1
- if winner_checker_2(turn1l,xplayer,name) == 1:
- return 3
- if winner_checker_2(turn2l,name,xplayer) == 1:
- return 3
- #DISPLAY PLAYER'S MOVE
- def print_grid_thing(n1,n2,x1,x2,x3):
- if g[2] in n1 and g[3] in n2:
- g[2] = x1
- g[3] = x2
- g[4] = x3
- elif g[6] in n1 and g[7] in n2:
- g[6] = x1
- g[7] = x2
- g[8] = x3
- elif g[10] in n1 and g[11] in n2:
- g[10] = x1
- g[11] = x2
- g[12] = x3
- print '\n' * 40, '\n'.join(g)
- def display(x,px1,px2,px3,x1,x2,x3,x4):
- if x == move:
- #Replace the spaces with 'x'
- pxx1 = px1[:x1] + "\ /" + px1[x3:]
- pxx2 = px2[:x2] + "x" + px2[x4:]
- pxx3 = px3[:x1] + "/ \\" + px3[x3:]
- print_grid_thing(px1,px2,pxx1,pxx2,pxx3)
- return 2
- def display2(x,px1,px2,px3,x1,x2,x3,x4):
- if x == move:
- #Replace the spaces with 'o'
- pxx1 = px1[:x1] + "---" + px1[x3:]
- pxx2 = px2[:x2] + "| |" + px2[x4:]
- pxx3 = px3[:x1] + "---" + px3[x3:]
- print_grid_thing(px1,px2,pxx1,pxx2,pxx3)
- return 2
- #Header
- header = '''
- ------------------------------------------
- | |
- | KNOTS & CROSSES |
- | |
- ------------------------------------------'''
- print header, '\n'
- #Asks for input
- name1 = raw_input("Enter name of player 1 : ")
- name2 = raw_input("Enter name of player 1 : ")
- print'\n' "CHOOSING PLAYERS..."
- time.sleep(1)
- #xplayer is assigned from randomly choosing between name1 & name2
- xplayer = random.choice((name1,name2))
- #Display's who's playing with either naughts and crosses
- print'\n', xplayer, "You are playing with Crosses"
- if name1 != xplayer:
- name = name1
- elif name2 != xplayer:
- name = name2
- print name,"You are playing with Noughts",'\n'
- #print grid
- print " HERE IS THE GRID :" '\n'
- grid_function()
- print
- #Initiliaize lists
- turn1l = []
- turn2l = []
- #PLAYER 1/2 INPUTS
- while True:
- #assigned value to avoid NameError's & reset for replay
- space_taken = 0
- reloop = 0
- loop_count = 0
- for each in ((xplayer,name)*5):
- if space_taken == 1:
- move = raw_input("\n %s The space has already been taken \n \n"
- "Enter it again, using a number from the grid : " % each)
- space_taken = 0
- else:
- move = raw_input("%s It's your turn, enter a number from the grid : " % each)
- if move !="1"and move !="2"and move !="3"and move !="4"and move !="5"and move !="6"and move !="7"and move !="8"and move !="9":
- print '\n' "Invalid Input, please enter an available a number from the grid"
- continue
- if move in turn2l or move in turn1l:
- space_taken
- continue
- if each == xplayer:
- if (display("4",g[6],g[7],g[8],7,8,10,9) == 2 or display("1",g[2],g[3],g[4],7,8,10,9) == 2 or display("7",g[11],g[12],g[13],7,8,10,9) == 2 or
- display("2",g[2],g[3],g[4],18,19,21,20) == 2 or display("5",g[6],g[7],g[8],18,19,21,20) == 2 or display("8",g[10],g[11],g[12],18,19,21,20) == 2 or
- display("3",g[2],g[3],g[4],29,30,32,31) == 2 or display("6",g[6],g[7],g[8],29,30,32,31) == 2 or display("9",g[10],g[11],g[12],29,30,32,31) == 2):
- turn1l.append(move)
- if each == name:
- if (display2("4",g[6],g[7],g[8],7,6,10,11) == 2 or display2("1",g[2],g[3],g[4],7,6,10,11) == 2 or display2("7",g[10],g[11],g[12],7,6,10,11) == 2 or
- display2("2",g[2],g[3],g[4],18,17,21,22) == 2 or display2("5",g[6],g[7],g[8],18,17,21,22) == 2 or display2("8",g[10],g[11],g[12],18,17,21,22) == 2 or
- display2("3",g[2],g[3],g[4],29,28,32,33) == 2 or display2("6",g[6],g[7],g[8],29,28,32,33) == 2 or display2("9",g[10],g[11],g[12],29,28,32,33) == 2):
- turn2l.append(move)
- if winner_checker() == 3:
- reloop = 1
- break
- if loop_count == 8:
- print '\n' * 5
- print "******* The match was a draw *******" '\n'
- if replay() == 1:
- reloop = 1
- break
- break
- loop_count += 1
- if reloop!= 1:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement