Advertisement
Ridwanul_Haque

Mancala Board

Dec 18th, 2021
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. import sys
  2.  
  3. alpha_beta_pruning = 1
  4. human = 2
  5. HEURISTIC1 = 3
  6. HEURISTIC2 = 4
  7. HEURISTIC3 = 5
  8. HEURISTIC4 = 6
  9. bin_quantity = 6
  10. initialStone = 4
  11. inf = 1e9
  12. win = 1e4
  13.  
  14. class Board(object):
  15.     def __init__(self):
  16.         self.storage = [0] * 3
  17.         self.bin = [[initialStone for j in range(bin_quantity + 1)] for i in range(3)]
  18.  
  19.     def printBoard(self):
  20.  
  21.         # player2 -> the row is in the upper row
  22.         # in player2's perspective his bin-1 == bin-6 of player1
  23.         print('\t', end='')
  24.         for i in range(bin_quantity, 0, - 1):
  25.             sys.stdout.write('%2d ' % self.bin[2][i])
  26.  
  27.         # player2's storage is in left, player1's in right
  28.         print()
  29.         sys.stdout.write('%2d ' % self.storage[2])
  30.         for i in range(2 + bin_quantity * 3):
  31.             print(' ', end='')
  32.         sys.stdout.write('%2d\n' % self.storage[1])
  33.  
  34.         # player1
  35.         print('\t', end='')
  36.         for i in range(1, bin_quantity + 1):
  37.             sys.stdout.write('%2d ' % self.bin[1][i])
  38.         print('\n')
  39.  
  40.     def updateBoard(self, playerNo, binNo):
  41.         turn = -1
  42.         tot = self.bin[playerNo][binNo]
  43.         self.bin[playerNo][binNo] = 0
  44.  
  45.         otherPlayer = 1
  46.         if playerNo == 1:
  47.             otherPlayer = 2
  48.  
  49.         div = bin_quantity * 2 + 1
  50.         q = tot // div
  51.         r = tot % div
  52.  
  53.         # ------------------------------------------------------
  54.         # each of them would get at least q(possibly 0)
  55.         for i in range(bin_quantity):
  56.             self.bin[playerNo][i] += q
  57.             self.bin[otherPlayer][i] += q
  58.  
  59.         self.storage[playerNo] += q
  60.  
  61.         '''
  62.        special case : if the number of marbles is 13
  63.        then the last piece would fall in the bin where we started
  64.        and we get that piece as well as the piece in the opposite pit
  65.        '''
  66.         if tot == 13:
  67.             self.storage[playerNo] += 1
  68.             self.bin[playerNo][binNo] = 0
  69.             self.storage[playerNo] += self.bin[otherPlayer][bin_quantity - binNo + 1]
  70.             self.bin[otherPlayer][bin_quantity - binNo + 1] = 0
  71.         # ------------------------------------------------------
  72.  
  73.         # ------------------------------------------------------
  74.         # fill own bins
  75.         for i in range(binNo + 1, bin_quantity + 1):
  76.             if r > 0:
  77.                 self.bin[playerNo][i] += 1
  78.                 r -= 1
  79.  
  80.                 # if the last piece ends up in an empty slot
  81.                 # then we get this one as well as opponents stones in the same slot
  82.                 # note that the opposite slot must not be empty
  83.                 if r == 0 and self.bin[playerNo][i] == 1 and \
  84.                                 self.bin[otherPlayer][bin_quantity - i + 1] > 0:
  85.                     self.storage[playerNo] += (self.bin[otherPlayer][bin_quantity - i + 1] + 1)
  86.                     self.bin[playerNo][i] = 0
  87.                     self.bin[otherPlayer][bin_quantity - i + 1] = 0
  88.  
  89.         # fill own storage
  90.         if r > 0:
  91.             self.storage[playerNo] += 1
  92.             r -= 1
  93.             if r == 0:
  94.                 turn = playerNo
  95.  
  96.         # fill the others
  97.         for i in range(1, bin_quantity + 1):
  98.             if r > 0:
  99.                 self.bin[otherPlayer][i] += 1
  100.                 r -= 1
  101.  
  102.         # if there are still some left, fill own storage from the beginning
  103.         # we started from binNo+1, so finish before it
  104.         for i in range(1, binNo + 1):
  105.             if r > 0:
  106.                 self.bin[playerNo][i] += 1
  107.                 r -= 1
  108.  
  109.         # if turn is not -1 then the current player has been given chance again
  110.         if turn == -1:
  111.             turn = otherPlayer
  112.  
  113.         return turn
  114.  
  115.     def gameOver(self, showResult):
  116.         # print(sum(self.bin[1][1:bin_quantity + 1]), sum(self.bin[2][1:bin_quantity + 1]), "sums")
  117.         if self.storage[1] + self.storage[2] == initialStone * bin_quantity * 2:
  118.             if showResult:
  119.                 self.showGameResult()
  120.             return True
  121.  
  122.         elif sum(self.bin[1][1:bin_quantity + 1]) == 0:
  123.             self.storage[2] += sum(self.bin[2][1:bin_quantity + 1])
  124.  
  125.             for k in range(bin_quantity + 1):
  126.                 self.bin[2][k] = 0
  127.  
  128.             if showResult:
  129.                 self.showGameResult()
  130.             return True
  131.  
  132.         elif sum(self.bin[2][1:bin_quantity + 1]) == 0:
  133.             self.storage[1] += sum(self.bin[1][1:bin_quantity + 1])
  134.  
  135.             for k in range(bin_quantity + 1):
  136.                 self.bin[1][k] = 0
  137.  
  138.             if showResult:
  139.                 self.showGameResult()
  140.             return True
  141.  
  142.         return False
  143.  
  144.     def checkWinner(self):
  145.         if self.storage[1] > self.storage[2]:
  146.             return 1
  147.         elif self.storage[2] > self.storage[1]:
  148.             return 2
  149.         else:
  150.             return 0
  151.  
  152.     def showGameResult(self):
  153.         if self.checkWinner() == 1:
  154.             print("player-1 wins")
  155.         elif self.checkWinner() == 2:
  156.             print("player-2 wins")
  157.         else:
  158.             print("tie")
  159.  
  160.         self.printBoard()
  161.  
  162.     def copyBoardDetail(self, board):
  163.         self.storage[1] = board.storage[1]
  164.         self.storage[2] = board.storage[2]
  165.  
  166.         for i in range(bin_quantity + 1):
  167.             self.bin[1][i] = board.bin[1][i]
  168.             self.bin[2][i] = board.bin[2][i]
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement