0% found this document useful (0 votes)
45 views39 pages

Vinay Final Report

Uploaded by

vk6859190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views39 pages

Vinay Final Report

Uploaded by

vk6859190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Internship Report

on

LUDO GAME USING JAVA


JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY ANANTAPUR, ANANTHAPURAMU

In Partial Fulfillment of the Requirements for the Award of the degree of

BACHELOR OF TECHNOLOGY
In
COMPUTER SCIENCE & ENGINEERING
Submitted By

K.VINAYKUMAR - (22691A05P5)

MADANAPALLE INSTITUTE OF TECHNOLOGY &SCIENCE


(UGC – AUTONOMOUS)
(Affiliated to JNTUA, Ananthapuramu)
(Accredited by NBA, Approved by AICTE, New Delhi)
AN ISO 21001:2018 Certified Institution
P. B. No: 14, Angallu, Madanapalle – 517325

2024 - 25

I
2009-2013

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BONAFIDE CERTIFICATE

This is to certify that the internship work entitled “LUDO GAME USING JAVA” is a bonafide work carried
out by

K.VINAYKUMAR - (22691A05P5)

Submitted in partial fulfillment of the requirements for the award of degree Bachelor of Technology in

the stream of Computer Science & Engineering in Madanapalle Institute of Technology & Science,

Madanapalle, affiliated to Jawaharlal Nehru Technological University Anantapur, Ananthapuramu

during the academic year 2024-2025.

Internship Coordinator Head of the Department


Mr. Ch. Hemanand, M.Tech.,(Ph.D) Dr. M. Sreedevi, Ph.D
Assistant Professor, Professor & Head,
Department of CSE Department of CSE

Submitted for the University Examination held on:

Examiner - I Examiner - II

II
ACKNOWLEDGEMENT

We sincerely thank the MANAGEMENT of Madanapalle Institute of Technology &

Science for providing excellent infrastructure and lab facilities that helped me complete this

Internship.

We sincerely thank Dr. C. Yuvaraj, M.E., Ph.D., Principal, for guiding and providing

facilities for completing our Internship at Madanapalle Institute of Technology & Science,

Madanapalle.

We express our gratitude to Dr. M. Sreedevi, Ph.D., Professor andHead of the Department,

CSE for her continuous support in making necessary arrangements for the successful completion of

the Internship.

We express our sincere thanks to the Internship Coordinator, Mr. Ch.

Hemanand,M.Tech.,(Ph.D) Assistant Professor, Department of CSE for his tremendous support

for the successful completion of Internship.

We express our deep gratitude to our Internship In-Charge Mrs.M.Sangeetha M.E.,(Ph.D),

Assistant Professor, Department of CSE, for his guidance and encouragement that helped us to

complete this Internship.

We also wish to place on record my gratefulness to other Faculty of CSE Department and

our friends and our parents for their help and cooperation during our project work.

III
IV
DECLARATION

We hereby declare that the results embodied in this internship "LUDO GAME USING

JAVA" by us under the guidance of Mrs.M.Sangeetha,M.E.,(Ph.D) in partial fulfillment of the

award of Bachelor of Technology in Computer Science & Engineering from Jawaharlal Nehru

Technological University Anantapur, Ananthapuramu.

Date :

Place :

Internship Associate:

K.VINAYKUMAR

22691A05P5

V
ABSTRACT

This project focuses on the development of a Ludo game using Java, implementing a graphical user

interface with JavaFX for an interactive and engaging user experience. The game allows 2-4 players to

compete, with each player controlling four pieces that move around the board based on dice rolls. The

project integrates basic game logic such as turn management, player movement, and handling special rules

like capturing and safe zones. The code structure includes object-oriented principles such as classes and

methods to manage players, dice, and board elements. Event-driven programming is used to detect user

inputs, such as button clicks for dice rolls. The Ludo game also incorporates features like winning

conditions and a score tracker. The objective is to create an easy-to-use and fun version of Ludo while

practicing core Java programming concepts. The project demonstrates both technical proficiency in Java and

a solid understanding of game design fundamentals.

VI
CONTENTS

PAGE
S.NO. TOPIC
NO
1 INTRODUCTION 1
1.1 About Java 2

1.2 Importance and Applications of Java 2

1.3 Language used 2


1.4 Need for the model 3

2 TOOLS AND TECHNIQUES 4


2.1 Platform Used 5
2.2 Hardware & software Requirements 5
2.3 Development Tools 6

3 PROJECT WORK 7
3.1 Project overview 8
3.2 Algorithm 8

4 CODE AND OUTPUT SCREENSHOTS 28

5 CONCLUSION 50

REFERENCES 51

VII
CHAPTER-1

INTRODUCTION

1
1.1 ABOUT JAVA PROGRAMMING

Java is a versatile, high-level, object-oriented programming language designed for


portability,scalability, and ease of use. Developed by James Gosling and released by Sun
Microsystems in 1995, Java has become one of the most widely adopted programming languages,
powering applications across diverse platforms and industries. Its "write once, run anywhere"
(WORA) philosophy ensures that Java programs can run on any device with a Java Virtual Machine
(JVM),making it a cornerstone of cross-platform development.Java's syntax is concise and
straightforward, making it accessible to new learners, while its extensive standard libraries and
frameworks provide powerful tools for professional developers build robust and scalable
applications. Java's architecture-neutral and secure design, coupled with strong memory
management and built-in garbage collection, makes it a preferred choice for enterprise-level
development.

1.2 APPLICATIONS OF JAVA

Java is one of the most widely used programming languages in the world today. Its
versatility portability, and scalability make it an ideal choice for a wide range of applications across
various domains. Below are some key areas where Java plays a crucial role:

1. Web Development
Java is extensively used in building dynamic and interactive websites. Frameworks like
Spring, JavaServer Pages and Servlets allow developers to create web applications that are
scalable, maintainable, and secure. Java-based web applications typically run on servers and are
responsible for handling requests from clients, managing sessions, and interacting with databases.
2. Mobile Application Development
Java has been the backbone of mobile application development, particularly
for Android, the world's most popular mobile operating system. Android
applications are primarily written in Java (with support for Kotlin), which makes
Java essential for creating mobile apps.

2
1.3 LANGUAGE USED

The language used about Java often covers various aspects, such as its features, capabilities, and
how it is utilized in software development. Here are key points that are frequently mentioned:

1.Object-Oriented Programming : Java is a strongly object-oriented programming language,


meaning it organizes software design around objects, rather than functions or logic. This helps
in building modular, maintainable, and reusable code.

2.Platform Independence: A key feature of Java is its write Once, run anywhere capability,
enabled by the Java Virtual Machine . Java code is compiled into bytecode, which can be
executed on any platform that has a JVM, making Java platform-independent.

1.4 NEED FOR THE MODEL

The term "Java model" can refer to different aspects depending on the context. It might
imply the need for adopting Java as a programming language for various projects, systems, or
applications. Alternatively, it could refer to a Java-based model, such as an enterprise-level
application, a machine learning system, or a specific software architecture built using Java.

In both cases, Java has demonstrated a consistent demand due to its robustness, platform
independence, scalability, and an extensive ecosystem of libraries and tools. Below are several
key reasons for the need for the Java model in different fields:

1. Versatility and Broad Applicability

Java’s versatility and platform independence (via the Java Virtual Machine) make it highly
applicable across multiple domains:

2. Simplification of Complex Problems

Java’s object-oriented programming principles and strong type safety make it ideal for
structuring complex systems effectively:

3. Rapid Prototyping and Experimentation

While Java is not traditionally associated with rapid prototyping, modern tools and frameworks .

3
Chapter-2
TOOLS AND TECNNIQUES

4
2.1 PLATFORM USED

Java Development Kit and Java Compiler Overview

It is provided by Oracle Corporation and other vendors as the core package for Java
programmers. The JDK includes everything required to write, compile, and run Java programs,
making it the back The Java Development Kit is a crucial software development environment for
creating bone of Java development.

At the heart of the JDK is the Java Compiler, a tool responsible for converting human-
readable Java source code into bytecode. Bytecode is an intermediate, platform-independent
format understood by the Java Virtual Machine (JVM). This intermediate step ensures that Java
programs can run on any platform with a JVM, fulfilling Java’s “write once, run anywhere”
promise.

2.2 HARDWARE AND SOFTWARE REQUIREMENTS

Hardware Requirements for Java Development

1. Processor

The CPU is arguably the most important hardware component when developing Java applications,
as it is responsible for processing the code and executing instructions. The choice of CPU can
impact both development efficiency and application performance.

2. Memory

RAM is crucial for holding active processes and data in memory while running Java applications.
Java applications and itself all consume a significant amount of .The more R available, the better
your system can handle multiple tasks simultaneously.

Software Requirements for Java Development

1. Operating System

Java is a cross-platform language, and the Java runtime environment and development kit are
available for various operating systems, including Windows, macOS, and Linux. Your choice of
OS will depend on personal preference and the target deployment environment for your Java
application.

5
Minimum Requirement:

Windows: Windows 7 or later .

macOS: macOS 10.12 or late.

Linux: A modern Linux distribution such as Ubuntu, Fedora, or Debian.

Java Development Kit

The JDK is the core software suite required for Java development. It includes the Java compiler
the Java Runtime Environment , the Java Virtual Machine and other development tools. The JDK
is essential for compiling and running Java programs.

2.3 DEVELOPMENT TOOLS

Java development involves a wide range of tools designed to streamline coding, debugging,
testing, and deploying Java applications. These tools enhance productivity and help manage
complex projects. Here are some of the essential development tools for Java:

1. Integrated Development Environments

IDEs are crucial for Java developers as they provide an all-in-one interface for writing,
compiling, and debugging code. Popular IDEs include:

2. Java Development Kit

The JDK is the core toolkit required for compiling, running, and debugging Java programs.
It includes the Java Runtime Environment Java compiler and other essential tools like javadoc for
generating documentation and java for running Java applications. The JDK is essential for any
Java development.

3. Build Tools
Build tools automate the process of compiling, packaging, and deploying Java
applications. They are crucial for managing large codebases and dependencies:

6
CHAPTER-3
PROJECT WORK

7
3.1 PROJECT OVERVIEW
The To-Do List Project is a simple yet effective Java-based program designed to help users
manage tasks. This interactive, text-based application allows users to add, view, mark as
completed, and remove tasks. It focuses on providing a straightforward interface to enhance task
management efficiency. Below is an overview of the Java program’s key components,
functionality, and structure:

3.2 ALGORITHM

1.Initialization:

The Game Moves class initializes the layout and player positions.The paint method is
responsible for drawing the game board and pawns based on their positions.

2.Player Turns:

Each player takes turns. The current player is tracked using the current_player variable.The
dice variable stores the result of the dice roll.the program updates the graphical state after each
turn, showing the player's name and the rolled dice value.

3.Graphics Rendering:

The paint method in the Game Moves class handles the drawing of the board and players'
pawns. It also displays messages when a player wins or when it's their turn to roll the dice.

4.Dice Roll and Movement:

When the player presses "Enter," the game rolls the dice, updates the dice value, and allows
the player to move their pawn by clicking on it.

The path for each pawn is predefined in the Path class, and the pawn follows the path based on
the dice roll.

5.Winning Condition:A player wins if all of their pawns reach the end of their path (represented
by coin == 4).When a player wins, a congratulatory message is displayed, and the game resets
for a new round.

8
CHAPTER-4
CODE AND OUTPUT SCREEN SHOTS

9
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// Player class to store player information


class Player {
int height,width,status,coin;
Pawn[] pa=new Pawn[4]; // Array of 4 pawns for each player
public Player(int height,int width) {
status=-1;
coin=0;
for(int i=0;i<4;i++) { // Initializing pawns for each player
pa[i]=new Pawn(height,width);
}
}
public void draw(Graphics2D g) {
}
}

// Pawn class to store pawn information


class Pawn {
int x,y;
int current;
int height,width;
public Pawn(int h,int w){ // Constructor to initialize pawn
current=-1;
x=-1;
y=-1;

10
height=h;
width=w;
}
// Function to draw pawn on board
public void draw(Graphics2D g, int i, int j,int play) {
if(current==-1) { // If pawn is not on board
int temp1=80+(height/2),temp2=50+(width/2); // Initial position of pawn
x=i; // x coordinate of pawn
y=j; // y coordinate of pawn
if(play==0) { // If player 1
g.setColor(Color.RED); // Color of pawn
}
else if(play==1) { // If player 2
g.setColor(Color.GREEN); // Color of pawn
}
else if(play==2) { // If player 3
g.setColor(Color.YELLOW); // Color of pawn
}
else if(play==3) { // If player 4
g.setColor(Color.BLUE); // Color of pawn
}
g.fillOval(temp1+5+(i*width),temp2+5+(j*height),width-10,height-10); // Drawing pawn on board
g.setStroke(new BasicStroke(2)); // Thickness of border
g.setColor(Color.BLACK); // Color of border
g.drawOval(temp1+5+(i*width),temp2+5+(j*height),width-10,height-10); // Drawing border of
pawn
}
else
{ // If pawn is on board
int temp1=80,temp2=50; // Initial position of board
x=Path.ax[play][current]; // x coordinate of pawn
y=Path.ay[play][current]; // y coordinate of pawn
11
if(play==0) {
g.setColor(Color.RED);
}
else if(play==1) {
g.setColor(Color.GREEN);
}
else if(play==2) {
g.setColor(Color.YELLOW);
}
else if(play==3) {
g.setColor(Color.BLUE);
}
g.fillOval(temp1+5+(x*width),temp2+5+(y*height),width-10,height-10); // Drawing pawn on board
g.setStroke(new BasicStroke(2)); // Thickness of border
g.setColor(Color.BLACK);
g.drawOval(temp1+5+(x*width),temp2+5+(y*height),width-10,height-10); // Drawing border of
pawn
}
}
}

// Path class to store path information


class Path {
static int[][] ax= { // x coordinates of path for each player
{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,
4,3,2,1,0,0,1,2,3,4,5,6},
{8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,
6,6,6,6,6,7,7,7,7,7,7,7},
{13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,
13,14,14,13,12,11,10,9,8},
{6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,
8,8,8,8,8,7,7,7,7,7,7,7}
};

12
static int[][] ay= { // y coordinates of path for each player
{6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,
8,8,8,8,8,7,7,7,7,7,7,7},
{1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,
4,3,2,1,0,0,1,2,3,4,5,6},
{8,8,8,8,8,9,10,11,12,13,14,14,14,13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,
6,6,6,6,6,7,7,7,7,7,7,7},
{13,12,11,10,9,8,8,8,8,8,8,7,6,6,6,6,6,6,5,4,3,2,1,0,0,0,1,2,3,4,5,6,6,6,6,6,6,7,8,8,8,8,8,8,9,10,11,12,
13,14,14,13,12,11,10,9,8}
};
static int[][] initialx= { // x coordinates of initial position of each player
{1,1,3,3},
{10,10,12,12},
{10,10,12,12},
{1,1,3,3}
};
static int[][] initialy= { // y coordinates of initial position of each player
{1,3,1,3},
{1,3,1,3},
{10,12,10,12},
{10,12,10,12}
};
}

// Layout class to draw board


class Layout {
int x,y,width,height; // x,y coordinates and width and height of board
public Layout(int xi,int yi) {
x=xi;
y=yi;
width=30;
height=30;
}
13
// Method to draw board
public void draw(Graphics2D g){
g.setColor(Color.WHITE); // Color of background of board
g.fillRect(x,y,15*width,15*height); // Drawing background of board
for(int i=0;i<6;i++) { // drawing all the inner squares
g.setColor(Color.RED);
g.fillRect(x+(i*width),y, width, height);
g.fillRect(x, y+(i*height), width, height);
g.fillRect(x+(i*width),y+(5*height), width, height);
g.fillRect(x+(5*width), y+(i*height), width, height);
g.setColor(Color.GREEN);
g.fillRect(x+((i+9)*width),y, width, height);
g.fillRect(x+(9*width), y+(i*height), width, height);
g.fillRect(x+((i+9)*width),y+(5*height), width, height);
g.fillRect(x+(14*width), y+(i*height), width, height);
g.setColor(Color.YELLOW);
g.fillRect(x+((i+9)*width),y+(9*height), width, height);
g.fillRect(x+(9*width), y+((i+9)*height), width, height);
g.fillRect(x+((i+9)*width),y+(14*height), width, height);
g.fillRect(x+(14*width), y+((i+9)*height), width, height);
g.setColor(Color.BLUE);
g.fillRect(x+(i*width),y+(9*height), width, height);
g.fillRect(x, y+((i+9)*height), width, height);
g.fillRect(x+(i*width),y+(14*height), width, height);
g.fillRect(x+(5*width), y+((i+9)*height), width, height);
}
for(int i=1;i<6;i++) { // Drawing the inner boxes
g.setColor(Color.RED);
g.fillRect(x+(i*width),y+(7*height), width, height);
g.setColor(Color.YELLOW);

14
g.fillRect(x+((8+i)*width),y+(7*height), width, height);
g.setColor(Color.GREEN);
g.fillRect(x+(7*width),y+(i*height), width, height);
g.setColor(Color.BLUE);
g.fillRect(x+((7)*width),y+((8+i)*height), width, height);
}
g.setColor(Color.RED);
g.fillRect(x+(1*width),y+(6*height), width, height);
g.setColor(Color.YELLOW);
g.fillRect(x+((13)*width),y+(8*height), width, height);
g.setColor(Color.GREEN);
g.fillRect(x+(8*width),y+(1*height), width, height);
g.setColor(Color.BLUE);
g.fillRect(x+((6)*width),y+((13)*height), width, height);
int temp1=x+45,temp2=y+45;
for(int i=0;i<2;i++) { // Drawing the outer boxes
for(int j=0;j<2;j++) {
g.setColor(Color.RED);
g.fillRect(temp1+(2*i*width),temp2+(2*j*height), width, height);
g.setColor(Color.YELLOW);
g.fillRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+9*height, width, height);
g.setColor(Color.GREEN);
g.fillRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+0*height, width, height);
g.setColor(Color.BLUE);
g.fillRect(temp1+(2*i*width)+0*width,temp2+(2*j*height)+9*height, width, height);
}
}
g.setColor(Color.RED);
int xpoints0[] = {x+(6*width),x+(6*width), x+15+(7*width)};
int ypoints0[] = {y+(6*height),y+(9*height),y+15+(7*width)};
int npoints = 3;

15
g.fillPolygon(xpoints0, ypoints0, npoints); // Drawing the triangle in the center
g.setColor(Color.YELLOW);
int xpoints1[] = {x+(9*width),x+(9*width), x+15+(7*width)};
int ypoints1[] = {y+(6*height),y+(9*height),y+15+(7*width)};
int npoints1= 3;
g.fillPolygon(xpoints1, ypoints1, npoints1); // Drawing the triangle in the center
g.setColor(Color.GREEN);
int xpoints2[] = {x+(6*width),x+(9*width), x+15+(7*width)};
int ypoints2[] = {y+(6*height),y+(6*height),y+15+(7*width)};
int npoints2= 3;
g.fillPolygon(xpoints2, ypoints2, npoints2); // Drawing the triangle in the center
g.setColor(Color.BLUE);
int xpoints3[] = {x+(6*width),x+(9*width), x+15+(7*width)};
int ypoints3[] = {y+(9*height),y+(9*height),y+15+(7*width)};
int npoints3= 3;
g.fillPolygon(xpoints3, ypoints3, npoints3); // Drawing the triangle in the center
g.setStroke(new BasicStroke(2));
g.setColor(Color.BLACK);
for(int i=0;i<3;i++) { // Drawing the outer boxes
for(int j=0;j<6;j++) {
g.drawRect(x+((i+6)*width),y+(j*height), width, height);
g.drawRect(x+((j)*width),y+((i+6)*height), width, height);
g.drawRect(x+((i+6)*width),y+((j+9)*height), width, height);
g.drawRect(x+((j+9)*width),y+((i+6)*height), width, height);
}
}
g.drawRect(x+((1)*width),y+(1*height),4*width,4*height);
g.drawRect(x+((10)*width),y+(1*height),4*width,4*height);
g.drawRect(x+((1)*width),y+(10*height),4*width,4*height);
g.drawRect(x+((10)*width),y+(10*height),4*width,4*height);
g.drawRect(x,y,15*width,15*height);

16
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
g.drawRect(temp1+(2*i*width),temp2+(2*j*height), width, height);
g.drawRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+9*height, width, height);
g.drawRect(temp1+(2*i*width)+9*width,temp2+(2*j*height)+0*height, width, height);
g.drawRect(temp1+(2*i*width)+0*width,temp2+(2*j*height)+9*height, width, height);
}
}
g.drawPolygon(xpoints0, ypoints0, npoints);
g.drawPolygon(xpoints1, ypoints1, npoints1);
g.drawPolygon(xpoints2, ypoints2, npoints2);
g.drawPolygon(xpoints3, ypoints3, npoints3);
g.drawOval(x+5+(6*width),y+5+(2*height),width-10,height-10); // Drawing the circles in the
center
g.drawOval(x+5+(12*width),y+5+(6*height),width-10,height-10); // Drawing the circles in the
center
g.drawOval(x+5+(8*width),y+5+(12*height),width-10,height-10);
g.drawOval(x+5+(2*width),y+5+(8*height),width-10,height-10);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
g.drawString("RED", 90, 35); // Drawing the text on top of the boxes
g.drawString("GREEN", 370, 35);
g.drawString("Blue", 90, 540);
g.drawString("Yellow", 370, 540);
g.setFont(new Font("MV Boli", Font.BOLD, 30));
g.drawString("Press enter to roll dice", 550,150);
g.drawString("Click on the token to move", 550,200);
}
}

// This is the class for initializing the player positons


class Build_Player {
Player[] pl=new Player[4];

17
int[][] initialx= { // This is the array for the initial x positions of the players
{1,1,3,3},
{10,10,12,12},
{10,10,12,12},
{1,1,3,3}
};
int[][] initialy= { //This is the array for the initial y positions of the players
{1,3,1,3},
{1,3,1,3},
{10,12,10,12},
{10,12,10,12}
};
// This is the constructor for initializing the player positions
public Build_Player(int height, int width) {
for(int i=0;i<4;i++) {
pl[i]=new Player(height,width);
}
}
// This is the method for drawing the players
public void draw(Graphics2D g) {
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
pl[i].pa[j].draw(g,initialx[i][j],initialy[i][j],i);
}
}
}

// This is the class for the the game moves


class GameMoves extends JPanel implements KeyListener, ActionListener,MouseListener{

18
private static final long serialVersionUID = 1L;
Layout la; // This is the object for the layout class
Build_Player p; // This is the object for the build player class
Timer time; // This is the object for the timer class
int delay=10; // This is the delay for the timer
int current_player,dice; // This is the variable for the current player and the dice
int flag=0,roll,kill=0; // This is the flag for the dice roll and the kill variable

public GameMoves() {
setFocusTraversalKeysEnabled(false);
requestFocus();
current_player=0; // first player is set to red
la = new Layout(80,50);
p=new Build_Player(la.height,la.width);
dice=0;
flag=0;
roll=0;
kill=0;
}

// This is the method for drawing the game

public void paint(Graphics g) {


la.draw((Graphics2D)g);
p.draw((Graphics2D)g);
if(p.pl[current_player].coin==4) { // This is the condition for the winner
g.setColor(Color.WHITE);
g.fillRect(590, 100, 380,130);
if(current_player==0) {
g.setColor(Color.RED);
}

19
else if(current_player==1) {
g.setColor(Color.GREEN);
}
else if(current_player==2) {
g.setColor(Color.YELLOW);
}
else if(current_player==3) {
g.setColor(Color.BLUE);
}
g.setFont(new Font("serif", Font.BOLD, 40));
g.drawString("Player "+(current_player+1)+" wins.", 600, 350);
g.drawString("Congratulations.", 600, 400);
current_player=0;
la = new Layout(80,50);
p=new Build_Player(la.height,la.width);
dice=0;
flag=0;
roll=0;
kill=0;
}
else if(dice!=0) { // This is the condition for the dice roll

g.setColor(Color.pink);
g.fillRect(590, 300, 260,200);
if(current_player==0) {
g.setColor(Color.RED);
}
else if(current_player==1) {
g.setColor(Color.GREEN);
}
else if(current_player==2) {

20
g.setColor(Color.YELLOW);
}
else if(current_player==3) {
g.setColor(Color.BLUE);
}

g.setFont(new Font("MV Boli", Font.BOLD, 30));


if(current_player==0){
g.drawString("RED's turn:", 600, 350);
}
else if(current_player==1){
g.drawString("GREEN's turn:", 600, 350);
}
else if(current_player==2){
g.drawString("YELLOW's turn:", 600, 350);
}
else if(current_player==3){
g.drawString("BLUE's turn:", 600, 350);
}

// draw dice
g.setColor(Color.BLACK);
g.drawRect(670, 370, 100, 100);
g.setFont(new Font("MV Boli", Font.BOLD, 50));
g.drawString(""+dice, 700, 440);
}
if(flag==0&&dice!=0&&dice!=6&&kill==0) {
current_player=(current_player+1)%4;
}
kill=0;
}

21
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER&&flag==0) { // if enter is pressed the dice is rolled
roll=0;
dice=1 + (int)(Math.random() * 6); // random number is generated for the dice roll
repaint();
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].current!=-
1&&p.pl[current_player].pa[i].current!=56&&(p.pl[current_player].pa[i].current+dice)<=56) {
flag=1;
break;
}
}
if(flag==0&&dice==6) {
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].current==-1) {
flag=1;
break;
}
}
}
}
}

public void mouseClicked(MouseEvent e) {


if(flag==1) {
int x=e.getX();
int y=e.getY();
x=x-80;
y=y-50;
x=x/30;
22
y=y/30;
int value=-1;
//System.out.println(x+" "+y);
if(dice==6) { // if dice is 6
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].x==x&&p.pl[current_player].pa[i].y==y&&(p.pl[current_player].pa[i].
current+dice)<=56) {
value=i;
flag=0;
break;
}
}
if(value!=-1) { // if the player has a coin on the clicked position
p.pl[current_player].pa[value].current+=dice;
if(p.pl[current_player].pa[value].current==56) {
p.pl[current_player].coin++;
}
int k=0;
int hou=p.pl[current_player].pa[value].current;
if((hou%13)!=0&&(hou%13)!=8&&hou<51)
{
for(int i=0;i<4;i++) {
if(i!=current_player) {
for(int j=0;j<4;j++) {
int
tem1=Path.ax[current_player][p.pl[current_player].pa[value].current],tem2=Path.ay[current_player]
[p.pl[current_player].pa[value].current];
if(p.pl[i].pa[j].x==tem1&&p.pl[i].pa[j].y==tem2) {
p.pl[i].pa[j].current=-1;
kill=1;
k=1;
break;

23
}
}
}
if(k==1)
break;
}
}
}
else { // if the player does not have a coin on the clicked position
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].current==-1) {
p.pl[current_player].pa[i].current=0;
flag=0;
break;
}
}
}
}
else { // if dice is not 6
for(int i=0;i<4;i++) {
if(p.pl[current_player].pa[i].x==x&&p.pl[current_player].pa[i].y==y&&(p.pl[current_player].pa[i].
current+dice)<=56) {
value=i;
flag=0;
break;
}
}
if(value!=-1) { // if the player has a coin on the clicked position
p.pl[current_player].pa[value].current+=dice;
if(p.pl[current_player].pa[value].current==56) {
p.pl[current_player].coin++;
}
24
int k=0;
int hou=p.pl[current_player].pa[value].current;
if((hou%13)!=0&&(hou%13)!=8&&hou<51)
{
for(int i=0;i<4;i++) {
if(i!=current_player) {
for(int j=0;j<4;j++) {
int
tem1=Path.ax[current_player][p.pl[current_player].pa[value].current],tem2=Path.ay[current_player]
[p.pl[current_player].pa[value].current];
if(p.pl[i].pa[j].x==tem1&&p.pl[i].pa[j].y==tem2) {
p.pl[i].pa[j].current=-1;
kill=1;
k=1;
break;
}
}
}
if(k==1)
break;
}
}
}
}
repaint();
}
}

// these functions are not used but are compulsory to be defined as they are abstract functions of
MouseListener
public void actionPerformed(ActionEvent e) {
}

25
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent arg0) {
}
}
class GameScreen {
// Run the application from here
public static void main(String[] args) {
JFrame jframe = new JFrame();
jframe.setBounds(10,10,1000,600);
jframe.setBackground(new Color(0XB2A4FF));
jframe.setTitle("LUDO");
jframe.setResizable(false);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GameMoves gm = new GameMoves();
gm.setFocusable(true);
gm.addKeyListener(gm);
gm.addMouseListener(gm);
jframe.add(gm);
jframe.setVisible(true);
}
}

26
OUTPUT:

27
CHAPTER-5
CONCLUSION

28
CONCLUSION:

The Java-based Ludo game project offers an interactive and engaging way to play the
classic board game digitally. It uses Java's Swing library for the graphical user interface (GUI),
providing an intuitive and visually appealing experience for players. The game allows up to four
players to compete, each controlling four pawns, with dice rolls determining pawn movement.

The game logic ensures proper validation of pawn moves, checking for obstacles, and handling
player turns. Each player’s progress is tracked, and the game highlights pawn positions using
color-coded representations. The first player to move all their pawns to the finish area wins.

In addition, the project handles error cases such as invalid moves and includes a reset function to
restart the game. The Ludo game demonstrates core programming concepts such as object-
oriented design, event-driven programming, and GUI development. Overall, it serves as both a
fun, interactive game and a valuable learning project for understanding game mechanics and Java
programming.

29
CHAPTER-6
REFERENCES

30
REFERENCES
1.Java Official Documentation
https://docs.oracle.com/javase/

2. Swing Tutorial - Java Platform, Standard Edition (Java SE)


https://docs.oracle.com/javase/tutorial/uiswing/

3. Spring Framework Official Documentation


https://spring.io/projects/spring-framework

4. W3Schools Java Tutorials


https://www.w3schools.com/java/

5.GeeksforGeeks: Java Swing Tutorials


https://www.geeksforgeeks.org/java-swing/

31

You might also like