Advertisement
Guest User

PYTHIN

a guest
Nov 18th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. array = []
  2.  
  3. def init(array):
  4. for i in range(3):
  5. array.append([])
  6. for j in range(3):
  7. array[i].append(" ")
  8.  
  9. def writeArray(array):
  10. for i in range(3):
  11. if i != 0:
  12. print("-----------")
  13. for j in range(3):
  14. if j == 2:
  15. print(array[i][j])
  16. else:
  17. print(" ", array[i][j], " |", sep="", end="")
  18.  
  19. def integer(s):
  20. try:
  21. int(s)
  22. return True
  23. except ValueError:
  24. return False
  25.  
  26. def takeTurn(array):
  27. tileX = input("X: ")
  28. tileY = input("Y: ")
  29. if integer(tileX) == True and integer(tileY) == True:
  30. tileX = int(tileX)
  31. tileY = int(tileY)
  32. else:
  33. print("Those are not values.")
  34. takeTurn(array)
  35. return
  36. if tileX < 4 and tileX > 0 and tileY < 4 and tileY> 0:
  37. tileX = tileX - 1
  38. tileY = tileY - 1
  39. else:
  40. print("Those are not values.")
  41. takeTurn(array)
  42. return
  43. if array[tileY][tileX] == " ":
  44. array[tileY][tileX] = "X"
  45. else:
  46. print("Something already exists at that point.")
  47. takeTurn(array)
  48. return
  49.  
  50. def win(array):
  51. for i in range(3):
  52. if array[i][0] == array[i][1]and array[i][1] == array[i][2]:
  53. return array[i][0]
  54. if array[0][i] == array[1][i] and array[1][i] == array[2][i]:
  55. return array[0][i]
  56. if array[0][0] == array[1][1] and array[1][1] == array[2][2]:
  57. return array[0][0]
  58. if array[2][0] == array[1][1] and array[1][1] == array[0][2]:
  59. return array[0][2]
  60. return " "
  61.  
  62. def score(array, depth):
  63. if win(array) == "X":
  64. return 10 - depth
  65. elif win(array) == "O":
  66. return depth - 10
  67. else:
  68. return 0
  69.  
  70. def getMoves(array):
  71. moves = []
  72. for i in range(3):
  73. for j in range(3):
  74. if array[i][j] == " ":
  75. moves.append([i, j])
  76. return moves
  77.  
  78. def minmax(array, depth, player):
  79. if win(array) != " ":
  80. return
  81. scores = []
  82. depth = depth + 1
  83. moves = getMoves(array)
  84. if len(moves) > 0:
  85. newArray = array
  86. for i in range(len(moves)):
  87. if player == "O":
  88. newArray[moves[i][1]][moves[i][0]] = "O"
  89. scores.append(minmax(newArray, depth, "X"))
  90. elif player == "X":
  91. newArray[moves[i][1]][moves[i][0]] = "X"
  92. scores.append(minmax(newArray, depth, "O"))
  93. if player == "X":
  94. maxM = max(scores)
  95. choice = moves[scores.index(maxM)]
  96. return maxM
  97. elif player == "O":
  98. minM = min(scores)
  99. choice = moves[scores.index(minM)]
  100. return minM
  101.  
  102.  
  103. init(array)
  104. turns = 0
  105. choice = []
  106. while True:
  107. writeArray(array)
  108. takeTurn(array)
  109. turns = turns + 1
  110. print(win(array))
  111. minmax(array, turns, "O")
  112. array[choice[1]][choice[0]] = "O"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement