Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having trouble writing this in c++ 1.To play “Sorry”, you’ll need to create

ID: 3838791 • Letter: I

Question

I am having trouble writing this in c++

1.To play “Sorry”, you’ll need to create up to Four players.

Prompt the user for the number of players (2-4).

2. Create two die for the players to roll.

3. Create an Array to be used to track the player’s positions on the playing board.

4. The playing board has 50 spaces (with 50 being the winning space).

5. The dice have special conditions:

2 = Move two spaces

3 = Move three spaces

4 = Move back one space.

5 = Move five spaces.

6 = Move six spaces.

7 = Swap spots with the leading layer / or nothing if player is in lead.

8 = Move Eight spaces.

9 = Move nine spaces.

10 = Move ten spaces.

11 = Swap spots with the last player / or do nothing if player is last.

12 = Start Over

6. A player must roll a double to start.

7. If a player lands on the same space as another, the other player must return to the beginning.

Example: If P1 lands on a space where P3 is, P3 would go back to the start.

8. A player must roll an EXACT number to enter the winning space.

9. Use a random Generator to “roll” the dice, the user must press enter to roll.

10. Depict the players’ positions on the screen after each round.

11. Once a player finishes, create a winning message announcing the winner.

12. Then ask the user if they would like to play again.

13. You must use at least three functions. Some function examples could be:

Roll dice, check for other player (when moving), display board.

14. Display the status/location of the players between sets of rolls.

15. Depict a Playing Board on the screen and display the Players’ position on the board. (Extra Credit)

(maybe try using a method to create the board and screen each time a player moves)

16. If a player rolls a double, they’ll get another roll (Extra Credit).

17. If a player rolls two doubles in a row, they Start Over (Extra Credit).

Explanation / Answer

#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;
}