Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

I have a question for the following program:

#include <iostream>

#include <stdlib.h>

using namespace std;

 

int rolldice(){

int r1, r2, s = 0;

r1 = rand() % 6 + 1;

cout << "After rolling: " << r1 << endl;

r2 = rand() % 6 + 1;

cout << "After rolling: " << r2 << endl;

s = r1 + r2;

if(r1 == r2){

r1 = rand() % 6 + 1;

cout << "After rolling: " << r1 << endl;

r2 = rand() % 6 + 1;

cout << "After rolling: " << r2 << endl;

s += r1 + r2;

if(r1 == r2)

s = 1;

}

return s;

}

 

int findfirst(int pos[]){

int i, j = 0, m = pos[0];

for(i = 1; i < 4; i++){

if(m < pos[i]){

m = pos[i];

j = i;

}

}

return j;

}

 

int findlast(int pos[]){

int i, j = 0, m = pos[0];

for(i = 1; i < 4; i++){

if(m > pos[i]){

m = pos[i];

j = i;

}

}

return j;

}

 

void calposition(int pos[], int sum[]){

int i, p, temp, j;

for(i = 0; i < 4; i++){

if(sum[i] == 1 || sum[i] == 12)

pos[i] = 0;

else if(sum[i] == 4 && pos[i] > 1)

pos[i] -= 1;

else if(sum[i] == 7){

p = findfirst(pos);

if(pos[p] != pos[i]){

temp = pos[i];

pos[i] = pos[p];

pos[p] = temp;

}

}

else if(sum[i] == 11){

p = findlast(pos);

if(pos[p] != pos[i]){

temp = pos[i];

pos[i] = pos[p];

pos[p] = temp;

}

}

else{

if((pos[i] + sum[i]) <= 50)

pos[i] += sum[i];

}

for(j = 0; j < 4; j++){

if(i != j){

if(pos[i] == pos[j])

pos[j] = 0;

}

}

}

}

 

void display(int pos[]){

int i,j, k = 1, m;

for(i = 1; i <= 51; i++)

cout << "_";

cout << endl;

for(i = 1; i <= 5; i++){

for(j = 1; j <= 10; j++){

if(k == pos[0])

cout << "|_P" << 1 << "_";

else if(k == pos[1])

cout << "|_P" << 2 << "_";

else if(k == pos[2])

cout << "|_P" << 3 << "_";

else if(k == pos[3])

cout << "|_P" << 4 << "_";

else

cout << "|____";

k++;

}

cout << "|" << endl;

}

}

 

int findwinner(int pos[]){

int i;

for(i = 0; i < 4; i++){

if(pos[i] == 50){

break;  

}

}

if(i == 4)

return 0;

else

return (i + 1);

}

 

int main() {

   int pos[4];

   int sum[4];

   int i, temp, winner;

   char choice = 'y';

   cout << "Lets play, sorry!" << endl;

 

   while(choice == 'y' || choice == 'Y'){

   pos[0] = pos[1] = pos[2] = pos[3] = 0;

   while(true){

   for(i = 0; i < 4; i++){

   cout << "Player " << i + 1 << " is rollong." << endl;

   sum[i] = rolldice();

   }

   calposition(pos, sum);

   display(pos);

winner = findwinner(pos);

   if(winner > 0){

   cout << "Player " << winner << " is winner!!" << endl;

   break;

   }

   }

   cout << "Wanna play again? (Y/N)" << endl;

   cin >> choice;

   }

   return 0;

}

 

How did it display the player's position after every roll?

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education