abbarnes

Solitaire Moncala [codeskulptor]

Dec 12th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.71 KB | None | 0 0
  1. """
  2. Student facing implement of solitaire version of Mancala - Tchoukaillon
  3.  
  4. Goal: Move as many seeds from given houses into the store
  5.  
  6. In GUI, you make ask computer AI to make move or click to attempt a legal move
  7. """
  8.  
  9.  
  10. class SolitaireMancala:
  11.     """
  12.    Simple class that implements Solitaire Mancala
  13.    """
  14.    
  15.     def __init__(self):
  16.         """
  17.        Create Mancala game with empty store and no houses
  18.        """
  19.         self.configuration = [0]
  20.    
  21.     def set_board(self, configuration):
  22.         """
  23.        Take the list configuration of initial number of seeds for given houses
  24.        house zero corresponds to the store and is on right
  25.        houses are number in ascending order from right to left
  26.        """
  27.         self.configuration = configuration
  28.         print "Setting Board: "
  29.         print self.configuration
  30.    
  31.     def __str__(self):
  32.         """
  33.        Return string representation for Mancala board
  34.        """
  35.         view_config = []
  36.         for index in self.configuration:
  37.             view_config.append(index)
  38.         view_config.reverse()
  39.         return str(view_config)
  40.    
  41.     def get_num_seeds(self, house_num):
  42.         """
  43.        Return the number of seeds in given house on board
  44.        """
  45.         return self.configuration[house_num]
  46.  
  47.     def is_game_won(self):
  48.         """
  49.        Check to see if all houses but house zero are empty
  50.        """
  51.         return self.configuration[1:].count(0) == len(self.configuration[1:])
  52.    
  53.     def is_legal_move(self, house_num):
  54.         """
  55.        Check whether a given move is legal
  56.        """
  57.         return self.configuration[house_num] == house_num
  58.  
  59.    
  60.     def apply_move(self, house_num):
  61.         """
  62.        Move all of the stones from house to lower/left houses
  63.        Last seed must be played in the store (house zero)
  64.        """
  65.         i = 0
  66.         while i < house_num:
  67.             self.configuration[i] += 1
  68.             i += 1
  69.         self.configuration[house_num] = 0
  70.  
  71.     def choose_move(self):
  72.         """
  73.        Return the house for the next shortest legal move
  74.        Shortest means legal move from house closest to store
  75.        Note that using a longer legal move would make smaller illegal
  76.        If no legal move, return house zero
  77.        """
  78.         seed_index = 1
  79.         for seeds in self.configuration[1:]:
  80.             if seed_index == seeds:
  81.                 return seed_index
  82.             else:
  83.                 seed_index += 1
  84.         return 0
  85.    
  86.     def plan_moves(self):
  87.         """
  88.        Return a sequence (list) of legal moves based on the following heuristic:
  89.        After each move, move the seeds in the house closest to the store
  90.        when given a choice of legal moves
  91.        Not used in GUI version, only for machine testing
  92.        """
  93.         move_list = []
  94.         test_game = SolitaireMancala()
  95.         test_config = []
  96.         for seeds in self.configuration:
  97.             test_config.append(seeds)
  98.         test_game.set_board(test_config)
  99.         while not test_game.is_game_won() and test_game.choose_move() != 0:
  100.             move_list.append(test_game.choose_move())
  101.             test_game.apply_move(move_list[-1])
  102.         return move_list
  103.  
  104.  
  105. # Create tests to check the correctness of your code
  106.  
  107. def test_mancala():
  108.     """
  109.    Test code for Solitaire Mancala
  110.    """
  111.    
  112.     my_game = SolitaireMancala()
  113.     print "Testing init - Computed:", my_game, "Expected: [0]"
  114.    
  115.     config1 = [0, 1, 1, 1, 3, 5, 0]
  116.     my_game.set_board(config1)  
  117.    
  118. #    print "Testing set_board - Computed:", str(my_game), "Expected:", str([0, 5, 3, 1, 1, 0, 0])
  119. #    print "Testing get_num_seeds - Computed:", my_game.get_num_seeds(1), "Expected:", config1[1]
  120. #    print "Testing get_num_seeds - Computed:", my_game.get_num_seeds(3), "Expected:", config1[3]
  121. #    print "Testing get_num_seeds - Computed:", my_game.get_num_seeds(5), "Expected:", config1[5]
  122. #    # add more tests here
  123. #    print "Testing is_legal_move - Computed:", my_game.is_legal_move(5), "Expected True"
  124. #    print "Testing is_legal_move - Computed:", my_game.is_legal_move(1), "Expected False"
  125. #    print "Testing is_game_won   - Computed:", my_game.is_game_won(),    "Expected False"
  126. #    print "Testing choose_move   - Computed:", my_game.choose_move(),    "Expected: 1"
  127. #    print "Testing plan_moves     - Computed:", my_game.plan_moves(),     "Expected: [1,5]"
  128.  
  129.     print str(my_game) + "<< Store"
  130.     my_moves = my_game.plan_moves()
  131.     print "Move Plan: " + str(my_moves)
  132.     my_game.apply_move(my_moves[1])
  133.     print my_game
  134.    
  135.  
  136. test_mancala()
  137.  
  138.  
  139. #Import GUI code once you feel your code is correct
  140. import poc_mancala_gui
  141. poc_mancala_gui.run_gui(SolitaireMancala())
Add Comment
Please, Sign In to add comment