Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import numpy as np
  2. import random
  3.  
  4.  
  5. # Custom functions
  6. def menu():
  7. global computer
  8. end = False
  9. while not end:
  10. print "Tic Tac Toe\n1. New game PvP\n2. New game PvC\n0. Exit"
  11. option = input("Select an option: ")
  12. while not(option in range(0, 3)):
  13. option = input("Select a correct option: ")
  14. if option == 1:
  15. computer = False
  16. end = setup_game()
  17. if option == 2:
  18. computer = True
  19. end = setup_game()
  20. if option == 0:
  21. end = True
  22. return 0
  23.  
  24.  
  25. def setup_game():
  26. print "Setting up new game..."
  27. #grid = np.zeros((3, 3))
  28. grid = np.chararray((3, 3))
  29. grid[:] = '-'
  30. turn = random.randint(1, 2)
  31. while turn != 0:
  32. print "\nPlayer " + str(turn) + "' turn!"
  33. turn = turn_game(grid, turn)
  34. global winner
  35. if winner == 0:
  36. print "Draw :("
  37. else:
  38. draw_game(grid)
  39. print "\n///// Winner Player " + str(winner) + " /////\n"
  40. answer = raw_input("Play again? (y/n): ").lower()
  41. while answer != "y" and answer != 'n':
  42. print "Select a correct answer."
  43. answer = raw_input("Play again? (y/n): ").lower()
  44. if answer == 'n':
  45. return True
  46. return False
  47.  
  48.  
  49. def turn_game(grid, player):
  50. finish = False
  51. while not finish:
  52. draw_game(grid)
  53. if (player == 2 and not computer) or player == 1:
  54. column = input("Select a column: ") - 1
  55. row = input("Select a row: ") - 1
  56. if column in xrange(0, 3) and row in xrange(0, 3):
  57. # Check if position is available
  58. if grid[row, column] == '-':
  59. grid[row, column] = player_char(player)
  60. finish = True
  61. else:
  62. print "Move not possible, try again."
  63. else:
  64. print "Impossible move, try again."
  65. else:
  66. # Computer makes a move
  67. finish = computer_turn(grid)
  68. player = check_game(grid, player)
  69. return player
  70.  
  71.  
  72. def computer_turn(grid):
  73. move = False
  74. column = 0
  75. row = 0
  76. while not move:
  77. column = random.randint(0, 2)
  78. row = random.randint(0, 2)
  79. if grid[row, column] == "-":
  80. move = True
  81. print "Computer movement."
  82. grid[row, column] = "O"
  83. return True
  84.  
  85.  
  86. def check_game(grid, player):
  87. win = False
  88. # Get width and height
  89. w = grid.shape[0]
  90. h = grid.shape[1]
  91. # Horizontal check
  92. for i in xrange(h):
  93. count = 0
  94. for j in xrange(w):
  95. if grid[i, j] == player_char(player):
  96. count += 1
  97. if count >= 3:
  98. win = True
  99. # Vertical check
  100. for j in xrange(w):
  101. count = 0
  102. for i in xrange(h):
  103. if grid[i, j] == player_char(player):
  104. count += 1
  105. if count >= 3:
  106. win = True
  107. # Diagonal check
  108. count = 0
  109. for i in xrange(h):
  110. if grid[i, i] == player_char(player):
  111. count += 1
  112. if count >= 3:
  113. win = True
  114. count = 0
  115. for i in xrange(h):
  116. if grid[abs(i - 2), i] == player_char(player):
  117. count += 1
  118. if count >= 3:
  119. win = True
  120.  
  121. # Change player or set winner
  122. if not win:
  123. if player == 1:
  124. player = 2
  125. else:
  126. player = 1
  127. else:
  128. global winner
  129. winner = player
  130. player = 0
  131. return player
  132.  
  133.  
  134. def player_char(player):
  135. if player == 1:
  136. return "X"
  137. else:
  138. return "O"
  139.  
  140.  
  141. def draw_game(grid):
  142. # Get width and height
  143. w = grid.shape[0]
  144. h = grid.shape[1]
  145. # Draw grid
  146. for i in xrange(h):
  147. row = "\t"
  148. for j in xrange(w):
  149. row += grid[i, j] + "\t"
  150. print row
  151.  
  152. # Main
  153. winner = 0
  154. computer = False
  155. menu()
  156. print "Program ended."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement