0

I have an assignment to make the sorry board game and in my code, I keep getting an automatic loop but I'd like for it to say hit enter key to roll your dice as I need to for the assignment but I have no clue on how to do that and couldn't find anything, also it repeats the same number a bunch of times before it switches numbers which each player should be getting a different number each time instead of the same number 20 times then a different number. How do I go at fixing both of these?

[enter image description here][1]

https://gyazo.com/9f0d48e035cf19bbb7a64662d0acf346

idk if I did the image thing correctly so there's the gyazo link to

#include <iostream>
#include <string>
#include <ctime>
#include <iomanip>

using namespace std;
int RandomNumber()
{
  srand(time(NULL));
  return rand() % 11+2;
}

bool CheckWinner(int i)
{
  return (i == 50);
}

int main() {


switch (RandomNumber())
{



}

int NumOfPlayer;
cout << "Enter Number of Players (2-4): ";
cin >> NumOfPlayer;
while (NumOfPlayer < 2 && NumOfPlayer > 4){
cout << "Please insert a number of players with a minimum of 2 or a max of 4: ";
cin >> NumOfPlayer;
}

int Spaces[50];
int *playerPos = new int[NumOfPlayer];

for (int i = 0; i<NumOfPlayer; i++)
playerPos[i] = 0;

bool win = false;
int winner;

while (!win)
{
  
  for (int j = 0; j<NumOfPlayer; j++)
  {
    cout << "Player "<< j + 1 << " turn " << endl;
    

    int num = RandomNumber();
    cout << "You rolled: " << num << endl;
    
    switch (num)
    {
      case 2:
      {
        if (playerPos[j] + 2 <= 50)
        playerPos[j] += 2;

        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;

        }
        
        break;
      }

      case 3:
      {
        if (playerPos[j] + 3 <= 50)
        playerPos[j] += 3;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 4:
      {
        if (playerPos[j] + 4 <= 50)
        playerPos -= 1;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }
      case 5:
      {
        if (playerPos[j] + 5 <= 50)
        playerPos += 5;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 6:
      {
        if (playerPos[j] + 6 <= 50)
        playerPos += 6;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 7:
      {
        int k = 0;
        while (k < NumOfPlayer)
        {
          if (k != j && playerPos[k] > playerPos[j])
          playerPos[j] = playerPos[k];
          k++;
          cout << "You swapped with the lead player!" << endl;
        }
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 8: 
      {
        if (playerPos[j] + 8 <= 50)
        playerPos[j] += 8;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 9:
      {
        if (playerPos[j] + 9 <= 50)
        playerPos[j] += 9;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 10:
      {
        if (playerPos[j] + 10 <= 50)
        playerPos[j] += 10;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 11:
      {
        int k = 0;
        while (k < NumOfPlayer)
        {
          if (k != j && playerPos[k] < playerPos[j])
          playerPos[j] = playerPos[k];
          k++;
        }
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

      case 12:
      {
        playerPos[j] = 0;
        if (CheckWinner(playerPos[j]))
        {
          win = true;

          winner = j + 1;
          
        }
        break;
      }

    }

  }



}
cout << "Winner is Player: " << winner;

return 0;
}```


  [1]: https://i.stack.imgur.com/ign0e.png
Cyson
  • 1
  • 2
    `srand()` should be called once at the start of the program and never again. It seeds the random generator. You don't need to re-seed it. If you re-seed it with the current time (in seconds), you'll get a re-start of the sequence every sacond. [`srand()` - why only call it once](https://www.xspdf.com/resolution/50552146.html) – Ted Lyngmo Dec 19 '20 at 09:18
  • 1
    Does this answer your question? [rand() returns same values when called within a single function](https://stackoverflow.com/questions/6729214/rand-returns-same-values-when-called-within-a-single-function) – kaios Dec 19 '20 at 09:22
  • Check your code for places where you increment or decrement `playerPos` instead of `playerPos[j]`. – jxh Dec 19 '20 at 09:23
  • 1
    Btw, you shouldn't ask two questions in the same post, please make another post for your second question – kaios Dec 19 '20 at 09:24
  • 1
    Think about this loop `while (NumOfPlayer < 2 && NumOfPlayer > 4){`.if a number is less than 2 AND more than four. That is **never** true, no number is both less than two and more than four. Presumably you meant OR not AND. – john Dec 19 '20 at 09:24

0 Answers0