Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7530547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:56:25+00:00 2026-05-30T04:56:25+00:00

It says that in my array that I have gone over the index. My

  • 0

It says that in my array that I have gone over the index. My program is a Number Guessing game played by 5 players (5 indexes). I have used arrays to create the object and player classes.
I have reached a stump where my program crashes within the second or third round of the game. I noticed that during my second round, the index did not loop property: the loop counts the index 1 to 5 in the first loop, then counts 2 to 5 in the second loop, then if I even get to the 3rd round of the loop, all the indexes are shuffled around meaning I can’t go from 1 to 5.

As each player gets 3 guesses, use those 3 guesses and your out of the game. I have taken the array of object I created for the player, created a temporary array smaller than the previous and referenced that to achieve the current array.

I looked over my references in the code and found as much code as I could fix, I cannot find the bug that is causing my System.IndexOutOfRangeException. It is being caused by my guessing game class.

Here is my GuessingGame Class:

using System;  // only this using statement is needed here.

namespace GuessingGame
{

class GuessingGame
{
  #region instance attributes
  private const int GUESSES_ALLOWED = 3;
  private const int NUMBER_OF_PLAYERS_TO_START = 5;
  private const int MIN_VALUE = 1;
  private const int MAX_VALUE = 15;
  private Player[] players;
  private Random randomSource;
  #endregion

  public GuessingGame()
  {
     Console.WriteLine("Starting Constructor of GuessingGame");
     players = new Player[NUMBER_OF_PLAYERS_TO_START];
     randomSource = new Random();

     string playerName = "";
     for (int index = 0; index < players.Length; index++)
     {
        Console.Write("What is the name for player #" 
              + (index +1) + "?\t");
        playerName = Console.ReadLine();
        players[index] = new Player(playerName, randomSource);
        Console.Write("\n");
     }
     Console.WriteLine("Ending GuessingGame Constructor");
  }

  public GuessingGame(string [] playerNames)
  {

     Console.WriteLine("Starting Constructor of GuessingGame");
     players = new Player[playerNames.Length];
     randomSource = new Random();
     for (int index = 0; index < playerNames.Length; index++)
     {
        players[index] = new Player(playerNames[index], randomSource);
     }
  }

  public void playGame()
  {
     int numberOfPlayersWhoHavePlayedThisRound = 0;
     int index = 0;

     bool[] playedThisRound = null;
     string playerGuessEntry = "";
     int playerGuessValue = -1;
     Player[] tempArray = new Player[players.Length - 1];
     bool roundOver = false; 

     Console.WriteLine(
           "Starting playGame - press any key to continue");
     //Console.Read()

     while (roundOver == false) // Is this the right condition?
     {

         playedThisRound = new bool[players.Length];


         while (playedThisRound[index] == false)
         {
             do
             {
                 Console.Write(players[index].getName()
                       + ", Enter a number between "
                       + MIN_VALUE.ToString()
                       + " and " + MAX_VALUE.ToString()
                       + " inclusive\t");
                 playerGuessEntry = Console.ReadLine();
                 Console.Write("\n");
             }
             while (!int.TryParse(playerGuessEntry,
                      out playerGuessValue)
                    || playerGuessValue < MIN_VALUE
                    || playerGuessValue > MAX_VALUE);
             if(playerGuessValue < MIN_VALUE || playerGuessValue > MAX_VALUE)
             {
                 Console.Write("Invalid guess- try again");
             }
             else
             {

                 Console.WriteLine("You entered "
                       + playerGuessValue.ToString());

                 players[index].makeAGuess(playerGuessValue);
                 playedThisRound[index] = true;
                if (index == players.Length)
                {
                    Console.WriteLine("End of Round");
                    index = 0; //edit?
                    numberOfPlayersWhoHavePlayedThisRound = 0;
                }

             }
             if (players[index].getGuessesUsed() == 3)
             {//creating a temp array
                 Console.WriteLine("Guesses MAXED");
                 tempArray = players[index].deletePlayerFromArray(players, index);
                 players = tempArray; // referencing
                 bool[] tempBooleanArray = new bool[playedThisRound.Length - 1];//reducing size of played this round array
                 Console.WriteLine("Playedthisround length: " + playedThisRound.Length + " \nThe Index: " + index.ToString());
                 tempBooleanArray = players[index].deletePlayerBool(playedThisRound, index);
                 playedThisRound = tempBooleanArray;
                 Console.WriteLine("New Player Array Size: " + players.Length);
                 Console.WriteLine("New Boolean Array Size: " + playedThisRound.Length);
             }
             if (index == players.Length - 1)
             {
                 index = 0;
                 numberOfPlayersWhoHavePlayedThisRound = 0;
             }
             if (players.Length == 1)
             {
                 roundOver = true;
             }
             index++;
             numberOfPlayersWhoHavePlayedThisRound++;
         }
            Console.WriteLine("WINNER:" + players[index].getName() + 
                "\nWins: " + players[index].getWins() + "\nArray Size: " + players.Length.ToString());

     }//end of while

     Console.WriteLine("Ending playGame - " 
           + "press any key to continue");
     Console.Read();
  }
       public bool playersAlreadyPlayed(bool[] thePlayer)
      {
          bool havePlayed = false;
          for (int plays = 0; plays < thePlayer.Length; plays++)
          {
              if (thePlayer[plays] == false)
              {
                  havePlayed = false;
              }
              else 
              {
                  havePlayed = true;
              }
          }
          return havePlayed;
      }

  static void Main(string[] args)
  {
     GuessingGame newGame = new GuessingGame();
     newGame.playGame();
  }
 }

}

And Here is the Player Class

using System;

namespace GuessingGame
{

   class Player
   {

      private String name;
      private int winningNumber;
      private int guessesUsed;
      private int wins;
      private Random myWinningNumberSource;

      public Player(string newName, Random random)
      {
         name = newName;
         guessesUsed = 0;
         wins = 0;
         myWinningNumberSource = random;
         winningNumber = myWinningNumberSource.Next(1, 16);
      }


      public bool makeAGuess(int guessValue)
      {
          bool isWinner = false;//edit
         if (guessValue == winningNumber)
         {
            wins++;

            Console.WriteLine("Congradulations, You have guessed correct number!\n");
            Console.WriteLine("You have a total of " + wins + " wins!");
            Console.WriteLine("You have " + (3 - guessesUsed) + " guesses left!\n");
            winningNumber = myWinningNumberSource.Next(1, 16);
            isWinner = true; //edit

         }
         else
         {
            guessesUsed++;

            Console.WriteLine("Oh no! You have guessed incorretly!");
            Console.WriteLine("You have used " + guessesUsed + " and have " + (3 - guessesUsed) + " guesses left!");
            Console.WriteLine("HINT: You should have guessed " + winningNumber);
            isWinner = false; 


            if (guessesUsed > 3)
            {
                Console.WriteLine("Sorry you have Lost, Game Over");

            }

         }
         return isWinner; 
      } 

      public int getGuessesUsed()
      {
         return guessesUsed;
      }

      public string getName()
      {
         return name;
      }
      public int getWins()
      {
          return wins;
      }
      public Player[] getWinner(Player[] nPlayers)
      {
          int maxScore = 0; //edit
          Player[] winningPlayers;
          winningPlayers = new Player[5];
          for (int i = 0; i < nPlayers.Length; i++)
          {
              if (nPlayers[i].wins >= maxScore)
              {
                  winningPlayers[i].wins = nPlayers[i].getWins();
                  winningPlayers[i].name = nPlayers[i].getName();
              }
          }
          return winningPlayers;
      }
      public bool[] deletePlayerBool(bool[] playedThisRound, int removeIndex)//edit
      {
          bool[] newArray = new bool[playedThisRound.Length - 1];
          int tempIndex = 0;
          for (int i = 0; i < playedThisRound.Length; i++)
          {
              if (i != removeIndex)
              {
                  newArray[tempIndex++] = playedThisRound[i];
              }
          }
          return newArray;
      }
      public Player[] deletePlayerFromArray(Player[] nPlayers, int removeIndex)
      {
          Player[] newArray = new Player[nPlayers.Length - 1];
          int tempIndex = 0;
          for (int i = 0; i < nPlayers.Length; i++)
          {
              if (i != removeIndex)
              {
                  newArray[tempIndex++] = nPlayers[i];
              }
          }
          return newArray;
      }

   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:56:26+00:00Added an answer on May 30, 2026 at 4:56 am

    i is within the bounds of nPlayer length not 0-4.

    public Player[] getWinner(Player[] nPlayers)
      {
          int maxScore = 0; //edit
          Player[] winningPlayers;
          winningPlayers = new Player[5];
          for (int i = 0; i < nPlayers.Length; i++)
          {
              if (nPlayers[i].wins >= maxScore)
              {
                  winningPlayers[i].wins = nPlayers[i].getWins();
                  winningPlayers[i].name = nPlayers[i].getName();
              }
          }
          return winningPlayers;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework question that says: Problem 1: Given the array [ 22
Everything I have read says that when making a managed stored procedure, to right
I have this array that I'm printing with this function in php: print_r($curriculos); Array
I have selected the database but for some weird reason it still says that
I have an array of objects in javascript and I know that I need
This is a situation where I have a function that receives the number of
I have an array in Javascript that i am trying to pass to php.
In my framework, I have a method that returns an array with objects. It
I have a trip model that contains an array of lat/lng pairs class Trip
Seam Solder documentation says that the @Requires annotation requires an array of Class objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.