Tech Off Post

Single Post Permalink

View Thread: Yet another C# dice question.
  • User profile image
    PeterF

    Random rndGen = new Random();
    int[] player1 = { 0, 0, 0, 0, 0, 0 };
    int[] player2 = { 0, 0, 0, 0, 0, 0 };
    for (int i = 0; i < 5; i++)
    {
       player1[rndGen.Next(0, 5)] += 1;
       player2[rndGen.Next(0, 5)] += 1;
    }
    //Let's assume in array entry 0 we store the value 6, in 1->5 etc.
    int iWinner = 0; // if iWinner == 0 we have a draw
    int iHowmany = 0; // here we store how many of a kind has won
    for (int i = 0; i < 5; i++)
    {
       
    if (player1[i] > player2[i] && player1[i] > iHowmany)
       {
          iWinner = 1;
          iHowmany = player1[i];
       }
       
    else if (player1[i] < player2[i] && player2[i] > iHowmany)
       {
          iWinner = 2;
          iHowmany = player2[i];
       }
    }

    // Look in iWinner who has won

    Note that this doesn't hold into account that 2 pairs of 3 and 4 could be valued more than 1 pair of 5 Smiley

    Good luck,
    Peter