Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2007, 03:51 PM
Member
 
Join Date: Nov 2007
Posts: 8
clement1 is on a distinguished road
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
File Type: zip coursework4test.zip (10.2 KB, 2 views)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-21-2007, 04:57 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-21-2007, 05:08 PM
Member
 
Join Date: Nov 2007
Posts: 8
clement1 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2007, 12:10 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
dont know how to write these to the multi dimensional array
Here's an idea in pseudo code:
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; }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-22-2007, 11:35 AM
Member
 
Join Date: Nov 2007
Posts: 8
clement1 is on a distinguished road
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:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 12:11 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org