Popular Posts

Saturday, July 6, 2013

Simple Peg Solitaire Game in Python



Description:

   It is a Simple Peg Solitaire command line game in python. It has two classes Board and Play.
When create Object for Board class it initialize game board like the above picture. Board class contain methods to display board and check any valid move have in this board.

        Player Class methods get the move positions from the user and validate positions, play with the game with user entered date it valid.

Main function plays the game with the Board and Player classes.

 




 """Pige Solatire  Game"""

PEG = 'O'
BLANK = '#'
HOLE = ' '

class Board:
    """ This class is responsible to create board and checking board status"""

    def __init__(self):
        """ Initialize Board and it's attributes"""

        self.move = 0
        self.max_row = 7
        self.max_col = 7
        self.max_move = 31
        self.board = []
        self.board.append([BLANK, BLANK, PEG, PEG, PEG, BLANK, BLANK])
        self.board.append([BLANK, BLANK, PEG, PEG, PEG, BLANK, BLANK])
        self.board.append([PEG, PEG, PEG, PEG, PEG, PEG, PEG])
        self.board.append([PEG, PEG, PEG, HOLE, PEG, PEG, PEG])
        self.board.append([PEG, PEG, PEG, PEG, PEG, PEG, PEG])
        self.board.append([BLANK, BLANK, PEG, PEG, PEG, BLANK, BLANK])
        self.board.append([BLANK, BLANK , PEG, PEG, PEG, BLANK, BLANK])
      
    def print_board(self):
        """ Display current status of board """

        print '\nBoard:\n'
        print "  0 1 2 3 4 5 6"
        print "---------------"
        count = 0
        for row in self.board:
            print ("%d") % (count),
            count += 1
            for position in row:
                print position,
            print ''
  
    def check_move_possible(self):
        """ Check for further valid move in the board"""

        for i in range(self.max_row):
            if(i < 2 or i > 4):
                ret = self.three_row_check(i)
            else:
                ret = self.seven_row_check(i)
            if(ret == True):
                return True

        for i in range(self.max_col):
            if(i < 2 or i > 4):
                ret = self.three_col_check(i)
            else:
                ret = self.seven_col_check(i)
                if(ret == True):
                    return True
        return False
                          
    def three_row_check(self, row):
        """ Checking valid move in three column rows"""
      
        if(self.board[row][2] == PEG and self.board[row][3] == PEG
           and self.board[row][4] == HOLE):
            return True

        if(self.board[row][2] == HOLE and self.board[row][3] == PEG
           and self.board[row][4] == PEG):
            return True
        return False

    def seven_row_check(self, row):
        """ Checking valid move in seven column rows"""
      
        for i in range(self.max_col):
            if(i == 0):
                if(self.board[row][0] == PEG and self.board[row][1] == PEG \
                       and self.board[row][2] == HOLE):
                    return True
            elif(i == 6 or i == 5):  
                if(self.board[row][6] == PEG and self.board[row][5] == PEG \
                       and self.board[row][4] == HOLE):
                    return True
            else:
                if(self.board[row][i] == PEG and self.board[row][i + 1] == PEG):
                    if(self.board[row][i - 1] == HOLE or \
                           self.board[row][i + 2] == HOLE):
                        return True
        return False

    def three_col_check(self, col):
        """ Checking valid move in three row cols"""
      
        if(self.board[2][col] == PEG and self.board[3][col] == PEG \
               and self.board[4][col] == HOLE):
            return True

        if(self.board[2][col] == HOLE and self.board[3][col] == PEG \
               and self.board[4][col] == PEG):
            return True
        return False

    def seven_col_check(self, col):
        """ Checking valied move in seven row cols"""
      
        for i in range(self.max_row):
            if(i == 0):
                if(self.board[0][col] == PEG and self.board[1][col] == PEG \
                       and self.board[2][col] == HOLE):
                    return True
            elif(i == 6 or i == 5):  
                if(self.board[6][col] == PEG and self.board[5][col] == PEG \
                       and self.board[4][col] == HOLE):
                    return True
            else:
                if(self.board[i][col] == PEG and self.board[i + 1][col] == PEG):
                    if(self.board[i - 1][col] == HOLE or \
                           self.board[i + 2][col] == HOLE):
                        return True
        return False
  

  
class Play:
    """ This class is responsible to get move info \
    from player and play peg solitaire game."""

    def __init__(self):

        "initialize instance variables"
        self.get_row = 0
        self.get_col = 0
        self.put_row = 0
        self.put_col = 0
        self.midr = 0
        self.midc = 0

    def get_pos(self):
        """ Get move position from user"""

        self.get_row = int (raw_input("Enter row no to get peg : "))
        self.get_col = int (raw_input("Enter col no to get peg : "))
        self.put_row = int (raw_input("Enter row no to put peg : "))
        self.put_col = int (raw_input("Enter col no to put peg : "))
      
    def validate_pos_board(self):
        """ Validate user entered position inputs with board layout"""

        if(self.get_row < 0 or self.get_row > 6):
            return False

        if(self.get_col < 0 or self.get_col > 6):
            return False

        if(self.put_row < 0 or self.put_row > 6):
            return False
      
        if(self.put_col < 0 or self.put_col > 6):
            return False
        return True

    def validate_valied_pos(self):
        """ validate user entered positions """

        if(self.get_row == self.put_row):
            val = self.get_col - self.put_col
            if(val == -2 or val == 2):
                self.midr = self.get_row
                self.midc = (self.get_col + self.put_col) / 2
                return True
        if(self.get_col == self.put_col):
            val = self.get_row - self.put_row
            if(val == -2 or val == 2):
                self.midr = (self.get_row + self.put_row) / 2
                self.midc = self.get_col
                return True
        return False
          
    def validate_pos_with_peg_and_play(self, board):
        """ Valid date user entered position with valid move"""

        if(board.board[self.get_row][self.get_col] == PEG):
            if(board.board[self.put_row][self.put_col] == HOLE):
                if(board.board[self.midr][self.midc] == PEG):
                    board.board[self.get_row][self.get_col] = HOLE
                    board.board[self.put_row][self.put_col] = PEG
                    board.board[self.midr][self.midc] = HOLE
                    board.move += 1
                    return True
                return False
            return False

def main():
    """ Function for playing Peg Solitaire """
    play_board = Board()
    player = Play()
  
    while(True):
        play_board.print_board()
        player.get_pos()
      
        ret = player.validate_pos_board()
        if(ret == False):
            print "Wrong Position"
            continue
      
        ret = player.validate_valied_pos()
        if(ret == False):
            print "Wrong Position"
            continue
      
        ret = player.validate_pos_with_peg_and_play(play_board)
        if(ret == False):
            print "Wrong Position"
            continue
      
        ret = play_board.check_move_possible()
        if(ret == False):
            print "No more legal Moves"
            play_board.print_board()
            break
      
        if(play_board.move == play_board.max_move):
            print "Congratulations!! you have solved the puzzle!"
            play_board.print_board()
            break

if __name__ == '__main__':
    main()
  

3 comments:

  1. Really appreciate this post. It’s hard to sort the good from the bad sometimes, but I think you’ve nailed it!

    Moving Display Board

    ReplyDelete
  2. Are you not playing Solitaire on your mobile? Download The Coolest Version (Available for iOS and Android)

    ReplyDelete
  3. Are you playing Solitaire Games online ? Go to one of the best online gaming site playtournamentgames.com

    ReplyDelete