Ludo Ladders Game (C++)Structural code is given below of the project.Few parts of the code is removed and comments are added, you need to complete the code. #pragma warning(disable:4996) #include #include #include #include using namespace std; struct Ludo { int Game[100]; // This array contains the position of ladders and snakes in the game // If the position contains a +ve number then there is a ladder at that position // If the position contains a -ve number then there is a snake at that position int Turn = 0; int position[2]; int state = -1; }; int Roll() { /** use rand() function from Math.h This function will roll a dice. The rules for dice roll are as follows. If the player roles a six then he gets an other turn. If he rolls three sixes then his score becomes zero but if he rolls more than three sixes then all rolls count. */ srand(time(0)); int i; int n = 6; for (i = 0; n == 6; i++) { if (i == 0) { n = rand() % 6 + 1; } else if (n == 18) { n = 0; } else { n += rand() % 6 + 1; } } return n; } int Move(Ludo* G) { /* This function will roll a dice using the Roll() function defined above. It will then update the game status (i.e change the turn, update player position etc. */ int score; score = Roll(); if (G->Turn == 0) { G->position[0] = G->position[0] + score; if (G->Game[G->position[0]] > 0) { G->position[0] = G->position[0] + G->Game[G->position[0]]; if (G->position[0] + G->Game[G->position[0]] >= 100) { G->position[0] = 99; } } if (G->Game[G->position[0]] < 0) { G->position[0] = G->position[0] - G->Game[G->position[0]]; if (G->position[0] + G->Game[G->position[0]] < 0) { G->position[0] = 0; } } } if (G->Turn == 1) { G->position[1] = G->position[1] + score; if (G->Game[G->position[1]] > 0) { G->position[1] = G->position[1] + G->Game[G->position[1]]; if (G->position[1] + G->Game[G->position[1]] >= 100) { G->position[1] = 99; } } if (G->Game[G->position[1]] < 0) { G->position[1] = G->position[1] - G->Game[G->position[1]]; if (G->position[0] + G->Game[G->position[0]] < 0) { G->position[0] = 0; } } } if (G->position[0] == 99) { G->state = 1; return 0; } if (G->position[1] == 99) { G->state = 2; return 0; } if (G->Turn == 0) { G->Turn = 1; } else { G->Turn = 0; } return 0; } int show(Ludo G) { system("CLS"); cout << "\t\t\t\t\tTable\n\n"; //This function will show the game status on screen. //It will clear the screen first //Show 10 Numbers on a Line //Show Position of Each player on the Game //Show the Position of snake and ladder //Show the players Turn int i; for (i = 0; i < 100; i++) { if (i == G.position[0]) { if (i % 10 == 0 && i != 0) { cout << endl; cout << "A\t"; } else { cout << "A\t"; } } else if (i == G.position[1]) { if (i % 10 == 0 && i != 0) { cout << endl; cout << "B\t"; } else { cout << "B\t"; } } else if (i % 10 == 0) { cout << endl << G.Game[i] << "\t"; } else { cout << G.Game[i] << "\t"; } } cout << "\n\nPosition of player 1: " << G.position[0]; cout << "\n\nPosition of player 2: " << G.position[1]; if (G.state == -1) { if (G.Turn == 0) cout << "\n\n\t\tPlayer 1 turn."; else cout << "\n\n\t\tPlayer 2 turn."; } return 0; } void init(Ludo* game) { for (int i = 0; i< 10; i++) { int position = rand() % 100; int change = rand() % 10 + 1; if (position + change < 100) { game->Game[position] = change; } } for (int i = 0; i< 10; i++) { int position = rand() % 100; int change = rand() % 10 + 1; if (position - change > 0) { if (game->Game[position] == 0) { game->Game[position] = -change; } } } } int main() { Ludo newGame; int i; for (i = 0; i < 100; i++) { newGame.Game[i] = 0; } newGame.position[0] = 0; newGame.position[1] = 0; init(&newGame); cout << "\n\nPlayer 1 Turn"; while (newGame.state == -1) { show(newGame); cout << "\n\n\t\tPress Enter To make your turn "; getchar(); Move(&newGame); } if (newGame.state == 1) { show(newGame); cout << "\n\n\t\tPlayer 1 Wins.......\n\n"; } else if (newGame.state == 2) { show(newGame); cout << "\n\n\t\tPlayer 2 Wins.......\n\n"; } cout << "\t\t"; return 0; }

Question

Ludo Ladders Game
(C++)Structural code is given below of the project.Few parts of the code is removed and comments are added, you need to complete the code.
#pragma warning(disable:4996)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct Ludo
{
int Game[100];
// This array contains the position of ladders and snakes in the game
// If the position contains a +ve number then there is a ladder at that position
// If the position contains a -ve number then there is a snake at that position
int Turn = 0;
int position[2];
int state = -1;
};
int Roll()
{
/**
use rand() function from Math.h
This function will roll a dice. The rules for dice roll are as follows.
If the player roles a six then he gets an other turn.
If he rolls three sixes then his score becomes zero but if he rolls more than three sixes then all rolls count.
*/
srand(time(0));
int i;
int n = 6;
for (i = 0; n == 6; i++)
{
if (i == 0)
{
n = rand() % 6 + 1;
}
else if (n == 18)
{
n = 0;
}
else
{
n += rand() % 6 + 1;
}
}
return n;
}
int Move(Ludo* G)
{
/*
This function will roll a dice using the Roll() function defined above.
It will then update the game status (i.e change the turn, update player position etc.
*/
int score;
score = Roll();
if (G->Turn == 0)
{
G->position[0] = G->position[0] + score;
if (G->Game[G->position[0]] > 0)
{
G->position[0] = G->position[0] + G->Game[G->position[0]];
if (G->position[0] + G->Game[G->position[0]] >= 100)
{
G->position[0] = 99;
}
}
if (G->Game[G->position[0]] < 0)
{
G->position[0] = G->position[0] - G->Game[G->position[0]];

if (G->position[0] + G->Game[G->position[0]] < 0)
{
G->position[0] = 0;
}
}
}
if (G->Turn == 1)
{
G->position[1] = G->position[1] + score;
if (G->Game[G->position[1]] > 0)
{
G->position[1] = G->position[1] + G->Game[G->position[1]];
if (G->position[1] + G->Game[G->position[1]] >= 100)
{
G->position[1] = 99;
}
}
if (G->Game[G->position[1]] < 0)
{
G->position[1] = G->position[1] - G->Game[G->position[1]];

if (G->position[0] + G->Game[G->position[0]] < 0)
{
G->position[0] = 0;
}
}
}
if (G->position[0] == 99)
{
G->state = 1;
return 0;
}
if (G->position[1] == 99)
{
G->state = 2;
return 0;
}
if (G->Turn == 0)
{
G->Turn = 1;
}
else
{
G->Turn = 0;
}
return 0;
}
int show(Ludo G)
{
system("CLS");
cout << "\t\t\t\t\tTable\n\n";
//This function will show the game status on screen.
//It will clear the screen first
//Show 10 Numbers on a Line
//Show Position of Each player on the Game
//Show the Position of snake and ladder
//Show the players Turn
int i;
for (i = 0; i < 100; i++)
{
if (i == G.position[0])
{
if (i % 10 == 0 && i != 0)
{
cout << endl;
cout << "A\t";
}
else
{
cout << "A\t";
}
}
else if (i == G.position[1])
{
if (i % 10 == 0 && i != 0)
{
cout << endl;
cout << "B\t";
}
else
{
cout << "B\t";
}
}
else if (i % 10 == 0)
{
cout << endl << G.Game[i] << "\t";
}
else
{
cout << G.Game[i] << "\t";
}
}
cout << "\n\nPosition of player 1: " << G.position[0];
cout << "\n\nPosition of player 2: " << G.position[1];
if (G.state == -1)
{
if (G.Turn == 0)
cout << "\n\n\t\tPlayer 1 turn.";
else
cout << "\n\n\t\tPlayer 2 turn.";
}
return 0;
}
void init(Ludo* game)
{

for (int i = 0; i< 10; i++)
{
int position = rand() % 100;
int change = rand() % 10 + 1;
if (position + change < 100)
{
game->Game[position] = change;
}
}
for (int i = 0; i< 10; i++)
{
int position = rand() % 100;
int change = rand() % 10 + 1;
if (position - change > 0)
{
if (game->Game[position] == 0)
{
game->Game[position] = -change;
}
}
}
}
int main()
{
Ludo newGame;
int i;
for (i = 0; i < 100; i++)
{
newGame.Game[i] = 0;
}
newGame.position[0] = 0;
newGame.position[1] = 0;
init(&newGame);
cout << "\n\nPlayer 1 Turn";
while (newGame.state == -1)
{
show(newGame);
cout << "\n\n\t\tPress Enter To make your turn ";
getchar();
Move(&newGame);
}
if (newGame.state == 1)
{
show(newGame);
cout << "\n\n\t\tPlayer 1 Wins.......\n\n";
}
else if (newGame.state == 2)
{
show(newGame);
cout << "\n\n\t\tPlayer 2 Wins.......\n\n";
}
cout << "\t\t";
return 0;
}

Expert Answer

1 Rating

Want to see the step-by-step answer?

Check out a sample Q&A here.

Want to see this answer and more?

Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Tagged in
Engineering
Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by students like you.

Q: Write a Java program to do the following.Ask the user to enter 5 integers (one at a time) using the ...

A: Program code: //import the required packages import java.util.Scanner; //define a class MinMax publ...

Q: Do -8 - 6 (decimal) in binary number system

A: Perform –8 – 6 in the binary number system. The given decimal number is –8 and –6 Convert these numb...

Q: What is the key distinction between homogeneous and federated distributed database systems?

A: Federated Distributed Database systems is a type of heterogeneous database system. These systems are...

Q: Evaluate the following expressions. (2) a. 4 * 7 == 74 / 3 b. 4 + 7 / 2 &lt;= 9 - 15 % 6 c. 'K' &lt;...

A: As per policy, only the first three parts are answered.  I am using Jupyter Notebook and Python 3 to...

Q: With a counter 10 lights will be controlled. At the first clock pulse all lights will be ON (1). at ...

A: Consider there are 10 lights with name 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. When the first clock pulse is ...

Q: Give the considerations for designing a system interface, pertaining to information onset display.

A: Solution: System Interface:Two or more  components of a computer system exchange information through...

Q: Ludo Ladders Game(C++)Structural code is given below of the project.Few parts of the code is removed...

A: Program defines a struct named Ludo with the following fields: Game an array of 100 and data type ...

Q: How does amethodology improve a process

A: Processes are evaluated by developers to increase scalability and productivity. The six most used me...

Q: Write a program named filereader.py that reads and displays the data in friends.txt. This program sh...

A: 1. Open the file  2. Read the file line by line and split each line by Space and store it into the l...

Q: What are two advantages of using iPad at the utilities department

A: Advantages of using “iPad” at the utilities department are as follows, The major reason the “iPad” ...

Q: What does a form’s Text property determine?

A: Explanation: The form's "Text" property determines the text that will be displayed in the form's ti...

Q: Develop a force field analysis that approximates the strength of the driving and restraining forces ...

A: Positive Train Control (PTC): Positive Train Control (PTC) is referred to as a system which is used...

Q: (True/False): The acronym PCI stands for programmable component interface

A: Peripheral Component Interconnect: Peripheral Component Interconnect (PCI), also known as Conventio...

Q: Explain how the SDLC serves as a framework for systems development

A: A System/Software Development Life Cycle is comprised of a set of phases which are clearly defined, ...

Q: Give Description of GetNumberOfConsoleMouseButtons function.

A: GetNumberOfConsoleMouseButtons function The GetNumberOfConsoleMouseButtons function is used to retr...

Q: What do you call a tiny “switch” that can be set to either on or off?

A: Tiny switch The tiny switch has two phases either on or off. The bits are similar to positive or ne...

Q: A program is required by a company to read an employee‘s number, pay rate and the number of hours wo...

A: Program: //include header file import java.util.Scanner;   //definition of main class public class M...

Q: How does strategic planning influence day-to-day business operations? Why is it important for system...

A: Strategic Planning: Strategic planning is an internal process which is important while developing ...

Q: What is the definition of Multimedia What does it mean to describe a multimedia project Linear and...

A: 1.Definition of Multimedia: A computer information that is in the form of text,audio,video,animation...

Q: What is the Alignment of REAL4?

A: alignment and storage are always given in bytes. alignment depends on the compiler platform and how ...

Q: One yottabyte is equal to?

A: One yottabyte is equal to 1,000 zettabytes. In other words, it consists of one trillion times one tr...

Q: Write a C program that acquires a sequence of characters from the keyboard. The sequence ends onceth...

A: Programming instructions:   Include necessary header files. Declare variables. Take input from the ...

Q: Which Win32 function lets you change the size of the cursor?

A: The SetConsoleCursorInfo lets us modify the size of the cursor. It sets the size visibility of the c...

Q: Why would a manager prefer a decision tree instead of a decision table?

A: Firstly, let’s understand the concept of decision table and decision tree. Decision table: It is a ...

Q: 2.  Problems 2 is based on the following query:   SELECT EMP_LNAME, EMP_FNAME, EMP_DOB, YEAR(EMP...

A: Data sparsity of EMP_DOB column The EMP_DOB column contains maximum number of unique values. It con...

Q: Write a program that prompts the user to enter the year and thefirst three letters of a month name (...

A: Since, here no programming language is specified.  So, we are providing the solution in Java. The be...

Q: I need help with Physical ER diagram with crows foot notation. Diagram needs entities/attributes/key...

A: The Entity-Relationship diagram for the given criteria is as follows,

Q: Write the following methodthat tests whether the array has four consecutive numbers with the same va...

A: To perform the given task, the coding will involve two parts - 1. main() method 2. isConsecutiveFour...

Q: (True/False): Linking OBJ modules is much faster than assembling ASM source files

A: The time taken to assemble large source files will be more and because of it the code is being broke...

Q: Write a C++ program to convert a hexadecimal number to binary.  Your program should read (from the k...

A: In the main the user is prompted to enter a hexadecimal number at least 4 digits long. The prompt is...

Q: What is the difference between a compiler and an interpreter?

A: S.No. Compiler Interpreter 1. It passes over whole program. It reads and executes one line at a ti...

Q: The GTUC Company wants to produce a product orders report from its product orders file. Each record ...

A: Pseudocode This is one of the approaches that could be used to represent a program's algorithm. This...

Q: Tuition($)   Applicant Pool   Applicant 950 76210 11040 1225 78000 10940 132...

A: (a). Determine the estimated multiple regression equation for the given data: Multiple linear regres...

Q: (True/False): A JMP instruction can only jump to a label inside the current procedure.

A: Explanation:   JMP instruction root an unconditional transfer to a destination, which can be identi...

Q: True or False When you call a string object’s Split method, the method divides the string into two s...

A: The given statement is false.   Split method: This method is used for splitting a string. It return...

Q: Describe data and process modeling concepts and tools.

A: Click to see the answer

Q: Write a calling statement that invokes an independent Sub procedure named CalcBonus, passing it the ...

A: The Sub procedure is basically a set of statements enclosed within the Sub and End Sub statements. I...

Q: Evaluate the following expression: 5 * 7 &gt; 6 ˆ 2. a. True b. False

A: Click to see the answer

Q: What happens if you attempt to compile and execute a program that contains syntax errors?

A: Syntax error: The mistake that happens due to the misspelled keyword, incorrect usage of punctuati...

Q: Consider the issue of interesting orders in optimization. Suppose you are given a query that compute...

A: The interesting order is every order on the subsets of the attributes which can potentially take par...