• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

What's this code doing?

 
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I'm very confused especially in the for loop.
I think it's not supposed to work that way.

PS- Source code. It's one of examples of head first java. Problem is given at page 98 of head first.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why you are confused? What part you don't understand? We will help you to understand that part..
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:why you are confused? What part you don't understand? We will help you to understand that part..



I'm not getting the point of traversing the array locationcells after our guess.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haven't run it, but the result I would expect from that sample code thus far is "hit"

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
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stefan
And as per code it should be a kill if the user guesses a "2" right ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

HIH

Winston
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Winston

I'm not doubting the book. Millions must have read it before me and so the code must be correct.
Now I'm just using my approach and comparing it to their's side by side.
 
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

--Scott
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Scott

I got it now.. infact its funny that now i see the code i can't even recall what was confusing me lol
everything makes sense now :p
 
BWA HA HA HA HA HA HA! Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic