• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Help making multiplayer number guessing game?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My number guessing game has two users. They both get one guess at a random number per round until one gets it right. At the end, it has to show their wrong answers and how many guesses it took them. Heres what I have so far

But I'm completely lost now. How do I get the answer to save as player 1's guess? And then how would I make it so its player 2's turn? Where do I go from here to get my end result basically. Any help appreciated

[Moderator edit: added code tags]
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Games are basically state machines, there are many phases that have to complete in the game as the game is played, not just player 1 through player k's turn, but as you are encountering saving data, checking for wins, and etc. You need an object that can hold the guesses. How many players and how many guesses--you don't know. So you need to make something that will hold an undefined number of records of an undefined number of players.

Look at collections for data stores, simply put: you need a collection of collections. I like ArrayLists, that is a personal preference, but look through the API and see what you want to use.

The other problem of keeping track, I would suggest looking at state machines and write the process on paper to see how many states you have to implement.

BTW: each turn could be a state, but if you treat it as such, you have the problem of a varying number of states, look at having each turn broken into states, and then over a loop to keep track of the player (state of which turn).  So you have a 2 dimensional state system.

Game programming rarely, if ever, has structure or progresses in linear fashion.
 
Marshal
Posts: 80763
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
You are right to use a Random object, but I think you are using the wrong version of nextInt.
Never use == false or == true. They are poor style and error‑prone.
Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
reply
    Bookmark Topic Watch Topic
  • New Topic