• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help making multiplayer number guessing game?  RSS feed

 
fewfeqfqef fwqfqfeqwf
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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]
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • 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) ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!