Results 1 to 5 of 5

Thread: I need help!

  1. #1
    clement1 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default I need help!

    Ok, i need help with a problem. Its a program that generates a random number, allows 3 guesses and has 8 rounds. It stores the guesses and calculates how many rounds the user guessed right then gives a little rating. Ive done this part. the next part is:

    Finally, modify your program so that the game can be played by multiple players. When the program begins, a message is printed asking how many players will take part. A maximum of 6 players should be imposed. The computer generates ONE random number for each round. Each player plays 8 rounds, in which they take turns at guessing the generated random number. At the end of the 8 rounds, the program prints a message saying which player (or players) won the most games.

    I dont know how to structure my program to do this. from the previous version, it goes through all 8 rounds first. How do i store the scores in ym array and then sort them for the winning players?

    Any help is appreciated
    Attached Files Attached Files

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I wouldn't store the scores in an array. I would create Player objects and store the scores there. I would store the multiple player objects in an array. Then during your game, I would just loop through this array. One trip through the array = 1 round.

  3. #3
    clement1 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    What do you mean player objects? I am totally new to java only been doing it a few weeks and i havent touched on the object orientated side. I thought about a multi dimensional array to store the player number and their total win count but i dont know how to write these to the multi dimensional array.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    dont know how to write these to the multi dimensional array
    Here's an idea in pseudo code:
    Java Code:
    public static void main(String args[])
    {
        int[][] players_marks;
    
        no_players = getPlayers(MAX_PLAYERS);
    
        // The jvm initializes all array elements with the default value of zero.
        players_marks = new int [no_players][NO_ROUNDS];
    
        // If you count a win in any round as a 1 and a loss as
        // a zero this array will track the rounds won and lost
        // for each player:
        // any player = players_marks[playerIndex]
        // the rounds for any player = players_marks[playerIndex][roundIndex]
        // Each player will have an array of NO_ROUNDS length
        // that contains the score for each round of guesses.
    
        for (int times = 0; times < no_players; times++)
        {            
            for (int rounds = 0; rounds < NO_ROUNDS; rounds++)
            {
                //This displayes the current player
                currentPlayer = "Player " + (times + 1);
                    
                // print round number
               
                //Generates random number.
                               
                //get 3 guesses from user and calculate if they 
                // guess the number using checkAnswer method.
                for (int count = 0; count < GUESSES_SIZE; count++)
                {
                    compare = checkAnswer(usersGuess, randomNum); 
                    hint = giveHint(randomNum, usersGuess);
                   
                    if(compare)
                    {
                        winCount = (winCount + 1);
                        players_marks[times][rounds]++;
                        // or you could wait and set this equal to
                        // winCount after exiting the rounds loop.
                        break;
                    }
                    else
                    {
                        if(count == (GUESSES_SIZE - 1)) 
                        {
                            loseCount = (loseCount + 1); 
                        }
                    }
                }  
            }//end of rounds
       
            //Print the final win & lose count and display ranking.
            int totalWins = getPlayerWins(players_marks[times]);
            // winCount and totalWins should have the same value.
            // Print 8-round total of win data for this player.
            userRating = rating(totalWins);
        }//end of no players loop
    } //end 
    
    private int getPlayerWins(int[] playersRounds)
    {
        int total = 0;
        for(int j = 0; j < playersRounds.length; j++)
            total += playersRounds[j];
        return total;
    }

  5. #5
    clement1 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    dont know how to write these to the multi dimensional array
    Here's an idea in pseudo code:
    Java Code:
    public static void main(String args[])
    {
        int[][] players_marks;
    
        no_players = getPlayers(MAX_PLAYERS);
    
        // The jvm initializes all array elements with the default value of zero.
        players_marks = new int [no_players][NO_ROUNDS];
    
        // If you count a win in any round as a 1 and a loss as
        // a zero this array will track the rounds won and lost
        // for each player:
        // any player = players_marks[playerIndex]
        // the rounds for any player = players_marks[playerIndex][roundIndex]
        // Each player will have an array of NO_ROUNDS length
        // that contains the score for each round of guesses.
    
        for (int times = 0; times < no_players; times++)
        {            
            for (int rounds = 0; rounds < NO_ROUNDS; rounds++)
            {
                //This displayes the current player
                currentPlayer = "Player " + (times + 1);
                    
                // print round number
               
                //Generates random number.
                               
                //get 3 guesses from user and calculate if they 
                // guess the number using checkAnswer method.
                for (int count = 0; count < GUESSES_SIZE; count++)
                {
                    compare = checkAnswer(usersGuess, randomNum); 
                    hint = giveHint(randomNum, usersGuess);
                   
                    if(compare)
                    {
                        winCount = (winCount + 1);
                        players_marks[times][rounds]++;
                        // or you could wait and set this equal to
                        // winCount after exiting the rounds loop.
                        break;
                    }
                    else
                    {
                        if(count == (GUESSES_SIZE - 1)) 
                        {
                            loseCount = (loseCount + 1); 
                        }
                    }
                }  
            }//end of rounds
       
            //Print the final win & lose count and display ranking.
            int totalWins = getPlayerWins(players_marks[times]);
            // winCount and totalWins should have the same value.
            // Print 8-round total of win data for this player.
            userRating = rating(totalWins);
        }//end of no players loop
    } //end 
    
    private int getPlayerWins(int[] playersRounds)
    {
        int total = 0;
        for(int j = 0; j < playersRounds.length; j++)
            total += playersRounds[j];
        return total;
    }
    Thanks, i managed to write a method that stores the current highest score & player then compares it to the next score in the loop without using an array. It was a little assessment that i had to do so i didnt have time to sit and figure it out properly. I will try your suggestion tho and see what i can do with it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •