Jaypee Hernandez wrote:I have a pdf copy of Head First Java. Based on what is in the book, it is a game (Simple Dot Game), first of all it has 2 classes, a Game class and a DotCom class. First is you initialized an integer array, which will be the placeholder of the dots. There are 3 dots to guessed on which "place" it is. Then the options will be integers like 1,2,3,4,5 you will select one of these and goes the loop. The objective of the game is to guess the location of three cells in the integer array as efficient as possible with fewer guesses i think. In your DotCom class you should initialize the location of the three dots. Then in your code above, make a method but you didn't used it, maybe it will be used for your Game class. The For loop is used whenever a user throws a guess on which array position [from 0-6 i guess] the dots are located. Then if the user guess correctly a place where a dot is, a hit will be counted, until it reaches 3, and then when you guess all the positions of the dots correctly, the "kill" message will come out and then it will print the number of guess you've made. Maybe in a 2v2 game, the winner will be the one with the fewer guess..
This is what the book says but the code they've given isn't doing that.
Here's the complete code given by the book :
And here's main method :
Now either the main is for testing purposes only and I'm not getting correct flow of work or there's some flaw in logic(which I doubt). You can run and see the outcomes.
From what I can see, it has a list of "locations"
The user enters a number
If that number is in the "locations" array. that is a hit.
Once you have "hit" all the locations in the array, that is a kill.
Obviously the user would have to "guess" several numbers along the way.
Shubham Semwal wrote:I'm not getting the point of traversing the array locationcells after our guess.
I suspect that this is because you don't understand the game, rather than because the code is wrong (HFJ is a pretty good book).
There is probably something in the solution (perhaps that if statement involving locationcells.length) that you can't 'connect' with your understanding of the game.
But this is the nature of programming.
It is NOT simple; and for complex problems there are not only many different solutions, but many different ways of approaching them.
I suspect, right now, you're going down one path to a solution, because it's the way you're thinking about it; but the HFJ bods have found another.
My advice (assuming the code works): Write down the rules of this game in English (or your native language) as you understand them. And include as much detail as possible. I suspect that exercise alone will point out where your fallacy is.
If you're anything like me, you'll get halfway through it and go "DOH-H-H...of course!" - but you must start it to get to that point.
My suspicion is that right now you're too close to the code, and you're not thinking about the problem (or game) in general.
Shubham Semwal wrote:@Stefan
And as per code it should be a kill if the user guesses a "2" right ?
Hi, Shubham --
From reading the code, that's not the result I would expect.
But back to Tushar and Winston's comments: what is it that makes you say you're confused, or you think the code isn't supposed to work the way it's working?