Guest User

noughts & crosses [python]

a guest
Apr 10th, 2016
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #USER: TCG
  2. #Naughts & Crosses
  3.  
  4. import time
  5. import random
  6.  
  7. #Defines Grid
  8. #function is used so that the can be replaced into it's original form when user asks to replay
  9. def grid_function():
  10.     global g
  11.     g = ["",
  12.     "  ----------------------------------",
  13.     "  |          |          |          |",
  14.     "  |     1    |     2    |     3    |",
  15.     "  |          |          |          |",
  16.     "  ----------------------------------",
  17.     "  |          |          |          |",
  18.     "  |     4    |     5    |     6    |",
  19.     "  |          |          |          |",
  20.     "  ----------------------------------",
  21.     "  |          |          |          |",
  22.     "  |     7    |     8    |     9    |",
  23.     "  |          |          |          |",
  24.     "  ----------------------------------"]
  25.     print '\n'.join(g)
  26.  
  27. #REPLAY GAME / EXIT GAME
  28.  
  29. def replay():
  30.     global turn1l,turn2l
  31.     while True:
  32.         restart = raw_input("\nWould you like to replay (yes/no) : ")
  33.         if restart == "yes":
  34.             i = 0
  35.             turn1l = []
  36.             turn2l = []
  37.             grid_function()
  38.             return 1
  39.         elif restart == "no":
  40.             print '\n' "Thanks for playing!"
  41.             time.sleep(2)
  42.             raise SystemExit()
  43.         else:
  44.             print '\n' "Incorrect input! Please enter either \"yes\" or \"no\""
  45.                
  46. #CHECKS IF THERE IS A WINNER
  47.  
  48. def winner_checker():
  49.      
  50.     def winner_checker_2(x,x1,x2):
  51.            
  52.         #All possilble wins
  53.         winner = ("147", "123", "258", "369", "456", "789", "159", "357")
  54.         if any(all(y in x for y in numbers) for numbers in winner):            
  55.  
  56.             print '\n' * 5, x1, "YOU HAVE WON! GG TO", x2, '\n'
  57.             if replay() == 1:
  58.                 return 1  
  59.                
  60.     if winner_checker_2(turn1l,xplayer,name) == 1:
  61.         return 3
  62.     if winner_checker_2(turn2l,name,xplayer) == 1:
  63.         return 3
  64.  
  65.            
  66. #DISPLAY PLAYER'S MOVE
  67.  
  68. def print_grid_thing(n1,n2,x1,x2,x3):
  69.     if g[2] in n1 and g[3] in n2:
  70.         g[2] = x1
  71.         g[3] = x2
  72.         g[4] = x3
  73.     elif g[6] in n1 and g[7] in n2:
  74.         g[6] = x1
  75.         g[7] = x2
  76.         g[8] = x3
  77.     elif g[10] in n1 and g[11] in n2:
  78.         g[10] = x1
  79.         g[11] = x2
  80.         g[12] = x3
  81.     print '\n' * 40, '\n'.join(g)
  82.        
  83. def display(x,px1,px2,px3,x1,x2,x3,x4):
  84.     if x == move:
  85.         #Replace the spaces with 'x'
  86.         pxx1 = px1[:x1] + "\ /" + px1[x3:]
  87.         pxx2 = px2[:x2] + "x" + px2[x4:]
  88.         pxx3 = px3[:x1] + "/ \\" + px3[x3:]
  89.         print_grid_thing(px1,px2,pxx1,pxx2,pxx3)
  90.         return 2
  91.        
  92. def display2(x,px1,px2,px3,x1,x2,x3,x4):
  93.     if x == move:
  94.         #Replace the spaces with 'o'
  95.         pxx1 = px1[:x1] + "---" + px1[x3:]
  96.         pxx2 = px2[:x2] + "|   |" + px2[x4:]
  97.         pxx3 = px3[:x1] + "---" + px3[x3:]
  98.         print_grid_thing(px1,px2,pxx1,pxx2,pxx3)
  99.         return 2
  100.  
  101. #Header
  102. header = '''
  103. ------------------------------------------
  104. |                                        |
  105. |            KNOTS & CROSSES             |
  106. |                                        |
  107. ------------------------------------------'''
  108. print header, '\n'
  109.  
  110. #Asks for input
  111.  
  112. name1 = raw_input("Enter name of player 1 : ")
  113. name2 = raw_input("Enter name of player 1 : ")
  114.  
  115. print'\n' "CHOOSING PLAYERS..."
  116. time.sleep(1)
  117.  
  118. #xplayer is assigned from randomly choosing between name1 & name2
  119. xplayer = random.choice((name1,name2))
  120.  
  121. #Display's who's playing with either naughts and crosses
  122. print'\n', xplayer, "You are playing with Crosses"
  123. if name1 != xplayer:
  124.     name = name1
  125. elif name2 != xplayer:
  126.     name = name2
  127. print name,"You are playing with Noughts",'\n'
  128.  
  129. #print grid
  130. print "         HERE IS THE GRID :" '\n'
  131. grid_function()
  132. print
  133.  
  134. #Initiliaize lists
  135.  
  136. turn1l = []
  137. turn2l = []
  138.        
  139. #PLAYER 1/2 INPUTS
  140.  
  141. while True:
  142.     #assigned value to avoid NameError's & reset for replay
  143.     space_taken = 0
  144.     reloop = 0
  145.     loop_count = 0
  146.     for each in ((xplayer,name)*5):
  147.         if space_taken == 1:
  148.             move = raw_input("\n %s The space has already been taken \n \n"
  149.                              "Enter it again, using a number from the grid : " % each)
  150.             space_taken = 0
  151.        
  152.         else:
  153.             move = raw_input("%s It's your turn, enter a number from the grid : " % each)
  154.            
  155.         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":
  156.             print '\n' "Invalid Input, please enter an available a number from the grid"
  157.             continue
  158.         if move in turn2l or move in turn1l:
  159.                 space_taken
  160.                 continue
  161.         if each == xplayer:
  162.             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
  163.                 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
  164.                 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):
  165.            
  166.                 turn1l.append(move)
  167.         if each == name:
  168.             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
  169.                 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
  170.                 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):
  171.  
  172.                 turn2l.append(move)
  173.  
  174.         if winner_checker() == 3:
  175.             reloop = 1
  176.             break
  177.  
  178.         if loop_count == 8:
  179.             print '\n' * 5
  180.             print "******* The match was a draw *******" '\n'
  181.             if replay() == 1:
  182.                 reloop = 1
  183.                 break
  184.             break
  185.         loop_count += 1
  186.        
  187.     if reloop!= 1:
  188.         break
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×