Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. import pygame, sys, random
  2. from pygame.locals import *
  3.  
  4. BACKGROUNDCOLOR = (255, 255, 255)
  5. BLACK = (255, 255, 255)
  6. BLUE = (0, 0, 255)
  7. CELLWIDTH = 50
  8. CELLHEIGHT = 50
  9. PIECEWIDTH = 47
  10. PIECEHEIGHT = 47
  11. BOARDX = 35
  12. BOARDY = 35
  13. FPS = 40
  14.  
  15. def terminate():
  16. pygame.quit()
  17. sys.exit()
  18.  
  19.  
  20. def resetBoard(board):
  21. for x in range(8):
  22. for y in range(8):
  23. board[x][y] = 'none'
  24.  
  25. board[3][3] = 'black'
  26. board[3][4] = 'white'
  27. board[4][3] = 'white'
  28. board[4][4] = 'black'
  29.  
  30.  
  31. def getNewBoard():
  32. board = []
  33. for i in range(8):
  34. board.append(['none'] * 8)
  35.  
  36. return board
  37.  
  38.  
  39. def isValidMove(board, tile, xstart, ystart):
  40. if not isOnBoard(xstart, ystart) or board[xstart][ystart] != 'none':
  41. return False
  42.  
  43. board[xstart][ystart] = tile
  44.  
  45. if tile == 'black':
  46. otherTile = 'white'
  47. else:
  48. otherTile = 'black'
  49.  
  50. tilesToFlip = []
  51. for xdirection, ydirection in [ [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1] ]:
  52. x, y = xstart, ystart
  53. x += xdirection
  54. y += ydirection
  55. if isOnBoard(x, y) and board[x][y] == otherTile:
  56. x += xdirection
  57. y += ydirection
  58. if not isOnBoard(x, y):
  59. continue
  60. while board[x][y] == otherTile:
  61. x += xdirection
  62. y += ydirection
  63. if not isOnBoard(x, y):
  64. break
  65. if not isOnBoard(x, y):
  66. continue
  67. if board[x][y] == tile:
  68. while True:
  69. x -= xdirection
  70. y -= ydirection
  71. if x == xstart and y == ystart:
  72. break
  73. tilesToFlip.append([x, y])
  74.  
  75. board[xstart][ystart] = 'none' # restore the empty space
  76.  
  77. if len(tilesToFlip) == 0: # If no tiles were flipped, this is not a valid move.
  78. return False
  79. return tilesToFlip
  80.  
  81.  
  82. def isOnBoard(x, y):
  83. return x >= 0 and x <= 7 and y >= 0 and y <=7
  84.  
  85. def getValidMoves(board, tile):
  86. validMoves = []
  87.  
  88. for x in range(8):
  89. for y in range(8):
  90. if isValidMove(board, tile, x, y) != False:
  91. validMoves.append([x, y])
  92. return validMoves
  93.  
  94.  
  95. def getScoreOfBoard(board):
  96. xscore = 0
  97. oscore = 0
  98. for x in range(8):
  99. for y in range(8):
  100. if board[x][y] == 'black':
  101. xscore += 1
  102. if board[x][y] == 'white':
  103. oscore += 1
  104. return {'black':xscore, 'white':oscore}
  105.  
  106.  
  107. def whoGoesFirst():
  108. if random.randint(0, 1) == 0:
  109. return 'computer'
  110. else:
  111. return 'player'
  112.  
  113.  
  114. def makeMove(board, tile, xstart, ystart):
  115. tilesToFlip = isValidMove(board, tile, xstart, ystart)
  116.  
  117. if tilesToFlip == False:
  118. return False
  119.  
  120. board[xstart][ystart] = tile
  121. for x, y in tilesToFlip:
  122. board[x][y] = tile
  123. return True
  124.  
  125. def getBoardCopy(board):
  126. dupeBoard = getNewBoard()
  127.  
  128. for x in range(8):
  129. for y in range(8):
  130. dupeBoard[x][y] = board[x][y]
  131.  
  132. return dupeBoard
  133.  
  134. def isOnCorner(x, y):
  135. return (x == 0 and y == 0) or (x == 7 and y == 0) or (x == 0 and y == 7) or (x == 7 and y == 7)
  136.  
  137.  
  138. def getComputerMove(board, computerTile):
  139. possibleMoves = getValidMoves(board, computerTile)
  140.  
  141. random.shuffle(possibleMoves)
  142.  
  143. for x, y in possibleMoves:
  144. if isOnCorner(x, y):
  145. return [x, y]
  146.  
  147. bestScore = -1
  148. for x, y in possibleMoves:
  149. dupeBoard = getBoardCopy(board)
  150. makeMove(dupeBoard, computerTile, x, y)
  151. score = getScoreOfBoard(dupeBoard)[computerTile]
  152. if score > bestScore:
  153. bestMove = [x, y]
  154. bestScore = score
  155. return bestMove
  156.  
  157. def isGameOver(board):
  158. for x in range(8):
  159. for y in range(8):
  160. if board[x][y] == 'none':
  161. return False
  162. return True
  163.  
  164.  
  165. pygame.init()
  166. mainClock = pygame.time.Clock()
  167.  
  168. boardImage = pygame.image.load('board.png')
  169. boardRect = boardImage.get_rect()
  170. blackImage = pygame.image.load('black.png')
  171. blackRect = blackImage.get_rect()
  172. whiteImage = pygame.image.load('white.png')
  173. whiteRect = whiteImage.get_rect()
  174.  
  175. basicFont = pygame.font.SysFont(None, 48)
  176. gameoverStr = 'Game Over Score '
  177.  
  178. mainBoard = getNewBoard()
  179. resetBoard(mainBoard)
  180.  
  181. turn = whoGoesFirst()
  182. if turn == 'player':
  183. playerTile = 'black'
  184. computerTile = 'white'
  185. else:
  186. playerTile = 'white'
  187. computerTile = 'black'
  188.  
  189. print(turn)
  190.  
  191. windowSurface = pygame.display.set_mode((boardRect.width, boardRect.height))
  192. pygame.display.set_caption('Othello')
  193.  
  194.  
  195. gameOver = False
  196.  
  197.  
  198. while True:
  199. for event in pygame.event.get():
  200. if event.type == QUIT:
  201. terminate()
  202. if gameOver == False and turn == 'player' and event.type == MOUSEBUTTONDOWN and event.button == 1:
  203. x, y = pygame.mouse.get_pos()
  204. col = int((x-BOARDX)/CELLWIDTH)
  205. row = int((y-BOARDY)/CELLHEIGHT)
  206. if makeMove(mainBoard, playerTile, col, row) == True:
  207. if getValidMoves(mainBoard, computerTile) != []:
  208. turn = 'computer'
  209.  
  210. windowSurface.fill(BACKGROUNDCOLOR)
  211. windowSurface.blit(boardImage, boardRect, boardRect)
  212.  
  213. if (gameOver == False and turn == 'computer'):
  214. x, y = getComputerMove(mainBoard, computerTile)
  215. makeMove(mainBoard, computerTile, x, y)
  216. savex, savey = x, y
  217.  
  218. if getValidMoves(mainBoard, playerTile) != []:
  219. turn = 'player'
  220.  
  221. windowSurface.fill(BACKGROUNDCOLOR)
  222. windowSurface.blit(boardImage, boardRect, boardRect)
  223.  
  224. for x in range(8):
  225. for y in range(8):
  226. rectDst = pygame.Rect(BOARDX+x*CELLWIDTH+2, BOARDY+y*CELLHEIGHT+2, PIECEWIDTH, PIECEHEIGHT)
  227. if mainBoard[x][y] == 'black':
  228. windowSurface.blit(blackImage, rectDst, blackRect)
  229. elif mainBoard[x][y] == 'white':
  230. windowSurface.blit(whiteImage, rectDst, whiteRect)
  231.  
  232. if isGameOver(mainBoard):
  233. scorePlayer = getScoreOfBoard(mainBoard)[playerTile]
  234. scoreComputer = getScoreOfBoard(mainBoard)[computerTile]
  235. outputStr = gameoverStr + str(scorePlayer) + ":" + str(scoreComputer)
  236. text = basicFont.render(outputStr, True, BLACK, BLUE)
  237. textRect = text.get_rect()
  238. textRect.centerx = windowSurface.get_rect().centerx
  239. textRect.centery = windowSurface.get_rect().centery
  240. windowSurface.blit(text, textRect)
  241.  
  242. pygame.display.update()
  243. mainClock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement