SHARE
TWEET

OX Game file1

Babhero10 May 31st, 2020 (edited) 902 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ############### Game mechanics functions ###############
  2.  
  3. from OX_Game_file2 import PlayWithAi
  4.  
  5. # Return 0 if Draw ,
  6. # 1 first play win ,
  7. # -1 the second player win
  8. # 3 continue the Game
  9. def TerminalState(Game, playerTurn):
  10.    
  11.     if playerTurn == 1:
  12.         utility = -1
  13.     else:
  14.         utility = 1
  15.    
  16.     # End of the Game              
  17.     for i in range(3):
  18.         if Game[i*3] == Game[(i*3)+1] == Game[(i*3)+2]:
  19.             return utility
  20.            
  21.         elif Game[i] == Game[i+3] == Game[i+6]:
  22.             return utility
  23.            
  24.     if Game[0] == Game[4] == Game[8]:
  25.         return utility
  26.            
  27.     elif Game[2] == Game[4] == Game[6]:
  28.         return utility
  29.            
  30.            
  31.     possibleActions = 0
  32.    
  33.     for i in range(9):
  34.        
  35.         if Game[i] != "O" and Game[i] != "X":
  36.             possibleActions += 1            
  37.            
  38.     if possibleActions == 0:
  39.         return 0        
  40.        
  41.     return 3
  42.    
  43. ############### Game display functions ###############
  44. def ShowGame(Game):
  45.    
  46.     print()
  47.    
  48.     for x in range(9):
  49.        
  50.         if (x+1)%1 == 0:
  51.             print(" ", end = "")
  52.        
  53.         print(Game[x], end = "  ")
  54.        
  55.         if (x+1)%3 == 0:
  56.             print("\n")
  57.            
  58. # Check the possibility of player choose
  59. def CheckAction(number, Game, OX):
  60.    
  61.     while True:
  62.        
  63.         if number.isnumeric():
  64.             number = int(number)
  65.         else:
  66.             number = input("Try Again: ")
  67.             continue
  68.            
  69.         if number == Game[number - 1]:
  70.             Game[number-1] = OX
  71.             return
  72.         else:
  73.             number = input("Try Again: ")
  74.  
  75. # Two Players Mode
  76. def TwoPlayers(players, OX, Game):
  77.    
  78.     playerTurn = 0
  79.    
  80.     while True:
  81.        
  82.         ShowGame(Game)
  83.        
  84.         print(players[playerTurn], ": ", end = "")
  85.      
  86.         CheckAction (input(), Game, OX)
  87.        
  88.         Terminal = TerminalState(Game, playerTurn)
  89.        
  90.         if Terminal == 1 or Terminal == -1:
  91.             ShowGame(Game)
  92.             print("\n", players[playerTurn], "Win!\n")
  93.             return
  94.            
  95.         elif Terminal == 0:
  96.             ShowGame(Game)
  97.             print("\nDraw!\n")
  98.             return
  99.        
  100.         # Change players
  101.         if playerTurn == 1:
  102.             playerTurn = 0
  103.         else:
  104.             playerTurn += 1
  105.        
  106.         # Change OX
  107.         if OX == "O":
  108.             OX = "X"
  109.         else:
  110.             OX = "O"
  111.        
  112.     return
  113.  
  114. ############### Game Menu functions ###############
  115.  
  116. def start(players, OX):
  117.    
  118.     while True:
  119.         print("\n\t----- New Game -----\n",
  120.                  "1. Play with AI\n",
  121.                  "2. Two players\n",
  122.                  "3. Return\n")
  123.    
  124.         Game = []
  125.         for i in range(9):
  126.             Game.append(i+1)
  127.        
  128.         select = input(" Select: ")
  129.        
  130.         if select == "1":
  131.            
  132.             PlayWithAi(Game, OX)
  133.            
  134.             Game = []
  135.             for i in range(9):
  136.                 Game.append(i+1)
  137.        
  138.         elif select == "2":
  139.             TwoPlayers(players, OX, Game)
  140.             Game = []
  141.             for i in range(9):
  142.                 Game.append(i+1)
  143.        
  144.         elif select == "3":
  145.             return
  146.        
  147.         else:
  148.             select = input(" Try Again: ")
  149.        
  150.        
  151. def playerName():
  152.    
  153.     print("\n\t----- Players' Name -----\n")
  154.     player = []
  155.        
  156.     player.append(input(" Player1: "))
  157.     player.append(input(" Player2: "))
  158.        
  159.     return player
  160.        
  161.        
  162. def selectOX():
  163.     print("\n\t----- select either O or X -----\n")
  164.     select = input(" Select: ")
  165.    
  166.     while True:
  167.         if select.lower() == "o":
  168.             return "O"
  169.                
  170.         elif select.lower() == "x":
  171.             return "X"
  172.            
  173.         else:
  174.             select = input(" Try Again: ")
  175.        
  176.        
  177. def menu():
  178.    
  179.     players = ["Player1", "Player2"]
  180.     OX = "O"
  181.    
  182.     while True:
  183.         print("\n\t----- O/X Game -----\n",
  184.                  "1. Start new game\n",
  185.                  "2. Player name\n",
  186.                  "3. Select O or X\n",
  187.                  "4. Exit\n")
  188.        
  189.         select = input(" Select: ")
  190.        
  191.         while True:
  192.        
  193.             if select == "1":
  194.                 start(players, OX)
  195.                
  196.             elif select == "2":
  197.                 players = playerName()
  198.                
  199.             elif select == "3":
  200.                 OX = selectOX()
  201.                
  202.             elif select == "4":
  203.                 print("\n\n Thank you for playing\n")
  204.                 return
  205.                
  206.             else:
  207.                 select = input(" Try Again: ")
  208.                 continue
  209.            
  210.             break
  211.  
  212.  
  213. menu()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top