Java Ludo Game

By | February 3, 2017

Gaming is a challenge but simultaneously its a fun too. We at programming course work have made a game in swing using Java Swing Framework. The user has to first select the player thats is he can select human player or AI player. The virtual game make u feel playing real. The working of application is below

The various packages used are

  • awt.Color
  • awt.Dimension
  • awt.GridBagConstraints
  • awt.GridBagLayout
  • awt.Point
  • awt.image.BufferedImage
  • io.File
  • io.IOException
  • util.ArrayList
  • util.Arrays
  • imageio.ImageIO
  • swing.BorderFactory
  • swing.BoxLayout
  • swing.ImageIcon
  • swing.JButton
  • swing.JComboBox
  • swing.JFrame
  • swing.JLabel
  • swing.JLayeredPane
  • swing.JOptionPane
  • swing.JPanel

for the movement of dice we have various classes and java files that are

  1.  Dice.java
  2. BasicField.java
  3. Field.java
  4. GoalPosition.java
  5. HomePosition.java
  6. MakeMove.java
  7. Pawns.java
  8. Strategy.java

Now lets start explaining them

The various functions Dice.java has

  • getInstance()
  • roll()
  • lastRoll()
  • setImage()

getInstance function checks if the current instance is null if yes then it initializes it and returns it

roll function generates number between 1 to 6 for dice

lastRoll gives the value of last roll

setImage updates the image of dice as per the roll value

 

Now next java class in Field.java

The various function it is having is

  • hasNextField()- it checks if next filed is null or not
  • getNextField()- it returns the next field
  • setNextField(final Field nextField)- it sets the next field
  • hasPawn()- it checks if pawnOccupied instance is null or not
  • getPawn()- it returns the pawnOccupied instance
  • setPawn(final Pawns pawn)- It sets the pawn
  • Point getPoint()- It returns the point variable
  • setPoint(final Point point)- It sets the point variable

 

now next java class is BasicField.java

this class inherits the main Field class

the various functions it has

  • removePawn()- It sets the pawn to the null and then returns it
  • hasGoalField()- It checks if the basic filed has goal filed or not and returns the Boolean
  • getGoalField()-It returns the goal position of the current field
  • setGoalField()- It sets the goal position of the field

 

Now next class is GoalPosition.java

it has only one function that is setNextGoalField- so this function uses inherited setNextField Function for it. It uses the down casting for it.

 

Next class is HomePosition.java

The various functions are as follows

  • getPawn()- it removes the pawn from home field and returns it
  • peekThePawn()- it returns the pawn at the home field
  • hasPawn()- it checks if there is any pawn at home field
  • getPawnSize()- it returns the size of pawns at home field
  • ispawnhomeFull()- it returns true if pawnsathome size is 4
  • setPawns()- it sets all the pawns in array
  • setPawn()- it sets the particular pawn at home field
  • getPoint()- it returns the home field size of all pawns

Now next class is MakeMove.java

The various function of it are

  • getPawn()- it returns the pawn
  • getField()- It returns the field instance

 

Now next class is Pawns.java

  • getImgsrc()- it returns the image source of pawn
  • setPosition()-it sets the position of pawn image to the point
  • getPawnField()- it returns the field associated with the pawn
  • ispawnAtHome()- It returns true if pawn at home field
  • ispawnatBasic()- It returns true if pawn is at basic field
  • ispawnatGoal()- It returns true if pawn is at Goal field
  • movepawnToHome()- It moves pawn to home field
  • movepawnToField()- It moves pawn to the next field as specified

 

Now next class is strategy.java

Actually its an interface which is implemented by Human Player class and AIPlayer class

 

Next class is Player.java

The various functions are

  • doMove()- It helps in doing the move as per the dice roll.
  • takeMove()- It place the player to the new field.
  • getHomeField()- It returns the home field of the player
  • getEntryGoalField()- It returns the goal entry point of the player and is equal to the field size-1
  • getPawns()- It returns all the pawns of the player
  • checkMovePawnHome()- it checks if player’s pawn can be taken to home field otherwise it returns null
  • checkMovePawnBasic()- It checks if players pawn can be moved to the normal fields or not
  • checkMovePawnGoal()-It checks if the players pawns goal can be moved along the goal field or not
  • checkIfGoalFull()- It checks if the player’s goal are full or not
  • checkIfGoalOccupied()- Checks if goal position is occupied or not
  • getGoalOccupiedCount()- It returns the total pawns occupying the goal
  • setLabelNotTurn()-It changes the label color of player to indicate its not his turn
  • setLabelIsTurn()-It changes the label color of player to indicate its his turn
  • checkValidMove()- It checks if the move is valid. If already a pawn exist it throws an error
  • isOwnPawn()- It checks if the pawn belongs to the player or not

 

Now we have two different strategies which are governed by classes

HumanPlayer.java

AIPlayer.java

The various function of HumanPlayer are

chooseMove()– it is a overridden function. It chooses the move by adding an event mouse pressed.

sendMoveToPlayer()– By choosing the move now player takes the specified move

AIPlayer is also like the HumanPlayer, only no mouse event is required here

Download  the source to try it. some screen shots are below

Download: Java Ludo Game