Computer Vs Human Luddo Game

 

077-BSCS-2010

 

Introduction

This article explains the code and design of a ” human verses computer Luddo Game” . In which multiple human players can play against each other as well as against multiple computer players. Luddo as we all know is a multiplayer game, so I have designed my application in a way that if more than one persons are not available at a time then a single person can play it with multiple computer players.

Background

Luddo is a commonly played game. When we were asked to develop an artificially intelligent application as the final project of A.I in semester five, I decided to go for Luddo. This game follows the all the traditional rules of classic Luddo and gives the player a real time essence of actual Game.

The requirements

  • A graphical representation of Luddo, using Windows forms.
  • The users should be able to choose if they would like to play against 1, 2 or 3 computer players.
  • The users can chose to play against 3 human players also.
  • The application should allow only valid moves – as defined by rules.

The DESIGNE

UI Elements

There is a clear separation between the UI and the backend. I have used two windows form in the application , the one of them is named as “StartUpPage” and also serve as the start page. On the start of application this form gives the user option to choose the number of computer players against him. As shown in following fig, he can choose to play against 1,2 or 3 computer players.

 

 

Start Page

After the start page farm, the farm1.cs contains all the graphical components and servers as the driver for the application. In this application I have used picture boxes to show the pieces of every player in the main form , on the complete farm , there is a picture of Luddo’s design and then after suitable locations of that farm, I have placed picture boxes showing the pieces of each player. As shown in the following picture:

luddomain
On the right hand side of this Luddo image you can see the Dice. When button Roll Dice is clicked, the background image of above picture box I changed randomly between 1 to 6. And player has to perform his turn according to that number.
Again on the right hand side beneath the button roll Dice, the label “Steps to move ” is of very importance. It calculates the legal steps that a player can move to , after he has finished to role the dice.
And the radio buttons are the indicators that which player’s move is this. Once a player has finished his move, his radio button will go unchecked and the next player’s radio button will become checked indicating that this player is having hi turn and so on.

The Code
The coding of this game application, as I would explain it, is very simple and easy to understand.
There are four main classes in the applications, and those are as following

  1. Pieces.cs
  2. player.cs
  3. possitions.cs
  4. Dice.cs

Following is the explanation of each class separately for sake of understanding
Pieces.cs
class pieces.cs is the most simple class of the application . it determines the position of each and every piece of every player on the game.


class Pieces
{
int PieceSteps = -1;
Point InitPiecesPoint;
PictureBox PieceLocation;

public Pieces(PictureBox PBox)
{
InitPiecesPoint.x = PBox.Location.X;
InitPiecesPoint.y = PBox.Location.Y;
PieceLocation = PBox;

}

……..
}
The position of pieces is determined by the get set functions as following:

public Point InitPoints
{
get
{
return InitPiecesPoint;
}

public PictureBox PiecePosition
get
{
return PieceLocation;
}
set
{
PieceLocation = value;
}

…………..
}

Player.cs
Player.cs is most lengthy class. Following is the constructor of class player.cs

class Player
{
Pieces[] Piece;
Position Location;
string Mode;
string color;

public Player(PictureBox[] Pbox, string UserType,string Color)
{
Location = new Position();
Mode = UserType;
Piece = new Pieces[4];
color = Color;
for (int i = 0; i < 4; i++)
{

Piece[i] = new Pieces(Pbox[i]);
}

AS we can see in the constructor this class takes a picture box, the type of user and color in order to determine that which player’s turn is this.

 

If during game a piece is got killed by opposite player , this class is responsible of getting that piece reset using the following method


public void ResetPiece(int PieceIndex)
{

..................................

}

Postions.cs
In order to get every possible position in the main game farm, I have made a class named position.cs. This class simply have information of every single location in the main farm and we can get or set thedesired location of any piece using the get set methods of this class


{
const int MaxMove = 45;

Point[] BlackSteps = new Point[MaxMove]
{
new Point(275,651), new Point(274,584), new Point(273,520), new Point(274,456), new
}


Get and set methods of this class are as following

public Point[] Red
{
get
{
return RedSteps;
}

}

………………..
}


Dice.cs

The last and perhaps the most important class of the game is of Dice. As this class defines all the rules to be obeyed in the game. The rolling of dice is performed in class and also the number of valid moves for a specific player. The dice class also determines that which player’s move is this and allows that player to move a piece to the valid numbers according to the value of dice on that turn.


class Dice
{
PictureBox PbDice;
Random random = new Random();
ImageList DImages;
public Dice(PictureBox pbD)
{
………….
}

Following method rolls the dice and returns the number that will be used by the player class to perform the turn of player.

public int RollDice()
{
int ran=1;
for (int i = 0; i < 15; i++)
{
ran = random.Next(1,7);
PbDice.Image = DImages.Images[ran-1];
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
ran = random.Next(1,7);
PbDice.Image = DImages.Images[ran-1];
System.Threading.Thread.Sleep(100);
return ran;

}