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
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.
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.
dont know how to write these to the multi dimensional array
Here's an idea in pseudo code:
public static void main(String args)
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);
winCount = (winCount + 1);
// or you could wait and set this equal to
// winCount after exiting the rounds loop.
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
private int getPlayerWins(int playersRounds)
int total = 0;
for(int j = 0; j < playersRounds.length; j++)
total += playersRounds[j];
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.
Originally Posted by hardwired