Advertisement
-TCG-

Noughts & Crosses/ Tic Tac Toe

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