• Post Reply Bookmark Topic Watch Topic
  • New Topic

Headfirst Java, dotComBust Game?  RSS feed

 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So I've been working on this game for a few days now from the simple version right through to this fuller version, my problem is that while the game seems to compile fine and run it does not prompt me to "enter a guess" as it should, i have looked through other posts of a similar nature and while there was one post that was exactly the same problem, they seemed to solve their problem by finding some source code online for the GameHelper class, i am developing this in IntelliJ as opposed to say notepad and then run it in cmd as i believe the book intends, so the only real difference is that i have my own main class as well, in which of course i have the main method instead of the main method being in the DotComBust class as it is in the book, so to the meat of the problem, i will list them as bullet point to make for easier reading followed by my code, followed by my current output as it stands.

Problems:

* Game does not prompt me to enter a guess
* Even if i do enter a value the game returns nothing, as opposed to "hit", "miss" etc.
* I can see that the dot com objects have been added to the ArrayList by using my sout lines which i believe is showing the memory location (please correct me if I'm wrong), but then when it comes to them being assigned random locations from within the enhanced for loop its seems that only the first of the added objects get assigned 3 location values/cells (matching memory location id) of which the first of the three location cells seems to be random while the last two always seem to be a0, a0, (Hopefully this will make more sense once you see the code), after this the code then seems to begin to iterate once again through the enhanced for loop as it should however never reaches the sout line i have which tells me the locations that have just been set.

Here is the code:

Main Class.



DotCom Class.



DotComBust Class.



GameHelper Class



Hopefully someone is able to throw me a bone here, i by no means am asking for a definitive answer as it would be nice to figure at least some of this problem out myself, hence i have no real wish to just simply download some source code and then move on with the rest of the book, never really understanding why this particular program didn't function properly, thanks in advance to anyone and everyone who spends their precious time trying to help me, it is appreciated.

Thanks again.

K.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  it does not prompt me to "enter a guess" as it should,

Where is that question asked?  Is that code executed?

Try debugging the code by adding some print statements to show where the execution flow goes and what the values of variables are as they are changed and used.
The print out will show you what the compute is doing when it executes your code.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Where is that question asked?  Is that code executed?


DotComBust class: line 60.

Try debugging the code by adding some print statements to show where the execution flow goes and what the values of variables are as they are changed and used.
The print out will show you what the compute is doing when it executes your code.


DotComBust class: line 48 and line 51.

These are not part of the original intended code as per the book and are my debugging code print statements which do provide the output as i described in my third bullet point of problems listed, i meant to post the output of all this code (my debugging code included) in my OP, here it is now below:

Console output.

The dot com list is as follows: [com.krichard.DotCom@1540e19d, com.krichard.DotCom@677327b6, com.krichard.DotCom@14ae5a5]
Your goal is to sink three Dot Coms
They are:
BlowMeUp.Com.
ShootMe.Com.
Arrrrrrgh.Com.
Try to sink them in as few as guesses as possible.
setting com.krichard.DotCom@1540e19d
now set at: [e4, a0, a0]
setting com.krichard.DotCom@677327b6

Thanks again to anyone willing to help.

K.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DotComBust class: line 60. 

What would keep the code on line 60 from being executed?
What is the  value returned by:       dotComList.isEmpty() ?
Add a print statement after line 59 to see what value is returned.

Where does execution go after this line is printed:
setting com.krichard.DotCom@677327b6
Add some print statements to see what happens next.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is way before the while loop in the startGame method, as can been seen from the output code the setUpGame methods enhanced for loop is not functioning correctly, it stops iterating before it should and the game just seems to break, it doesn't throw an error, it doesn't exit with any code, its running but not doing anything, the main never gets to the startGame method which i guess answers one of my problems, where getting there haha.

Here is the output code again but I've commented it this time explaining what i believe to be happening.

Console output.

The dot com list is as follows: [com.krichard.DotCom@1540e19d, com.kricahrd.DotCom@677327b6, com.krichard.DotCom@14ae5a5]     // These i believe are the three memory allocations for the ArrayList dotCom objects.

Your goal is to sink three Dot Coms    // Basic user Instructions.
They are:
BlowMeUp.Com.
ShootMe.Com.
Arrrrrrgh.Com.
Try to sink them in as few as guesses as possible.

setting com.krichard.DotCom@1540e19d    // One of my debugging sout lines (first iteration of the enhanced for loop) which as can be seen matches the first allocated memory location above.
now set at: [c2, a0, a0]                                // Another one of my debugging sout lines which then displays the newly set location cells for the ArrayList object at the allocated memory location above.
setting com.krichard.DotCom@677327b6   // (This is now the second iteration of the enhanced for loop) which as can be seen matches the second allocated memory location above.


At this point for some unknown reason the enhanced for loop stops.

The program itself doesn't stop but it also doesn't do anything either.

Hope this helps, because I'm stumped.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does execution go after this line is printed:
setting com.krichard.DotCom@677327b6
Add some print statements to see what happens next.


There are print statements in there as stated in previous posts lines 48 and 50 are part of my debugging code and not part of the originally intended code

As far as my Java language programming experience tells me it should continue in its enhanced for loop until each object in the Arraylist has been assigned some random location cells, but as i can see from the output we don't even get as far as assigning the second object its random location cells before something breaks.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program itself doesn't stop 

That would say there is an infinite loop somewhere.  Add enough print statements to show where the infinite loop is
and then inside the loop add print statements to show why the condition to end the loop is never happening.  Print the values of the variables used to control the loop.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in your GameHelper class. Look at line 64 and work out what has to happen for that loop to exit and if it's possible for those conditions to never be satisfied (which would result in an infinite loop). You could try adding a println statement within that loop to see the values of the variables.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so ive found it!

Im stuck in a while loop in the GameHelper class, in the placeDotCom method which is of course the method that is invoked by the enhanced for loops code block to set the random location cells for each dotcom object, now as my java programming experience isn't all that great and as this class was part of the so called Ready Bake Code that we are told in the book to just copy and don't worry about trying to understand what it does as of yet, this is exactly what i did, i have proof read this now with the book numerous times as that would obviously be the first check, as it turns out my code is as per the book prints it, which leads me to believe that the code in the book is either incorrect or buggy at best.

Here is the method which contains the while loop I'm stuck in, i shall comment exactly where it is and where my debugging sout lines are, although they are probably obvious to anyone with eyes.



As stated id bet my life on it that my code is as per the book (not necessarily correct), which would lead me to believe why others with similar problems have downloaded source code and voila the game works, also due to my limited experience I'm not able to deduce exactly why I'm stuck here as we as learners were to take this entire class on faith because of its complexity for beginners and so we were to just copy, which is what is did.

Any pointers as to why I'm stuck here would be great, again I'm not looking for the answer but just some information that may help me find the answer for myself.

Thanks.

K.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think i found it, just changed the operator (<) less than, in the condition (success && x < comSize) to (>) greater than.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the game is at least working in that it assigns location cells and im able to guess them and have results hit or miss, im also able to finish the game as intended, the only problem now is that the programs assigns a0 as the cell location for every single random location for each of the three dotcom objects, new output code below:

The dot com list is as follows: [com.krichard.DotCom@1540e19d, com.krichard.DotCom@677327b6, com.krichard.DotCom@14ae5a5]
Your goal is to sink three Dot Coms
They are:
BlowMeUp.Com.
ShootMe.Com.
Arrrrrrgh.Com.
Try to sink them in as few as guesses as possible.
setting com.krichard.DotCom@1540e19d
now set at: [a0, a0, a0]
setting com.krichard.DotCom@677327b6
now set at: [a0, a0, a0]
setting com.krichard.DotCom@14ae5a5
now set at: [a0, a0, a0]
startPlaying method called
Enter a Guess


As you can see the three objects get allocated their random cell locations and then the game proceeds with the startGame method asking me for input (Enter a Guess), but as can also be seen and as mentioned above all the locations are a0, hahahahahahahah, this game is something else!
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K Richard wrote:Think i found it, just changed the operator (<) less than, in the condition (success && x < comSize) to (>) greater than.

This doesn't seem like a good change.  The variable x is an index into the coords array and needs to be less than its size.  BTW, "x" is a terrible name for a variable you're going to use over more than two lines.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ill change it back but that then just puts me back into the infinite loop, as for "x" being a terrible variable under these circumstances, i am doing as per instructed by the book, i.e. copy paste and don't ask questions, i have attached the offending code in two images for anyone to have a look and help, for the time being i shall change the operator back to less than (<), as is in the book.

Thanks.

K.
code1_small.jpg
[Thumbnail for code1_small.jpg]
Page 1 of the Ready Bake Code.
code2_small.jpg
[Thumbnail for code2_small.jpg]
Page 2 of the Ready Bake Code.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at this line.  What's wrong?
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Look carefully at this line.  What's wrong?


My lack of experience is showing as i have no idea whats wrong with that line of code as a stand alone piece of code or with respect to the greater whole, but in my defense that is part of the gameHelper class which the book explicitly states not to worry about trying to understand yet, and while i wish i did understand it and while i also wish i could see as you do what is wrong with that line of code, i just simply cant, it is at this stage beyond me, i can certainly play around with it and remove the not operator which then runs the code again as intended but as before assigns a0 as the random location to every objects elements, i can also change the ampersand to two of them instead of one but this doesn't really change the code, and as my understanding would have only changes what gets checked should something return false.

I also have no idea where the number 200 has been pulled from and what significance it has.

So unfortunately while i'd love to be able to answer you, I'm still none the wiser.

Thanks.

K.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also have no idea where the number 200 has been pulled from and what significance it has.

There are no comments in the code describing what it is trying to do and how it is going to do it.
That makes it very hard(maybe impossible) for a beginning programmer to try to get the code to work.


My suggestion at this point is for you to study the code and add comments to it that describes what it is doing.
Do a little at a time.  Reading and rereading the code may eventually allow you to understand what it is doing.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can also change the ampersand to two of them instead of one but this doesn't really change the code, and as my understanding would have only changes what gets checked should something return false. 

Ah, see!  You did have a clue to what was wrong.  Two ampersands (&&) is a logical "and" (this and that are true, for instance).  One ampersand (&) is a bitwise "and".  It's not used much by beginning programmers, except by mistake.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so after much searching found the answer online here:

http://forums.oreilly.com/topic/49225-chapter-6-dotcom-class-with-arraylist/
poster named Jugo. (Legend).

No need for me to post the original code as i had typed it or as the book has it printed but what i will post is the code as it should be, as i now have and have tested, works as originally intended by the authors, its a shame that beginners have to endure such errors and struggle along when all the while the all knowing all powerful authors had actually instructed the reader on page 112 that "Whenever you see the Ready Bake Code logo, you are seeing code that you have to type in as-is and take it on FAITH (i have placed emphasis, not the book), TRUST IT(again more emphasis). You'll learn how that code works later", well who would have thought that exactly 40 pages later that FAITH would be tested and fail miserably, the TRUST is officially gone, never to return, with regards to this boldly claiming book, here is the working code as it should be.

I shall just paste the offending method, all other code is correct.



While it seems obvious to me now when i see that of course without breaks how could one exit a loop, i still find rather sloppy the authors would leave out an entire else statement and have a Boolean wrong, anyway.

Hopefully that should clear this matter up somewhat quicker for any poor unfortunate souls that happen to buy this book and find themselves here.

Quick thanks to ALL who posted replies.

I'm now finally moving on and continuing my learning instead of marking time.

Thanks again.

K.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code should work without the break statements.  When the value of success is set to false, the while loop will exit.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it SHOULD, but it doesn't.

And this is the ONLY solution I've found in the last 2 days of searching, I'd rather not waste anymore of my time trying to break down and fully understand all the intricacies and finer syntactical details of a class that we as readers are specifically told NOT TO DO THAT, as we will understand it better the further progression is made into the book, I will be sure to revisit this part of the book once I've either:

A. Finished the book
B. Am more proficient and learned in the java language.

Maybe this was the original authors intent.

K.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maybe it SHOULD, but it doesn't.

OK, I see why now.  I assumed that the chain of if statements were if/else if/else.  They are not.  An earlier if being true would set success one way and a later if statement could set it another way.  The last setting wins even though an earlier setting was the other way.
Doesn't look to me like a good way to write the code.  I think it makes for confusion.
 
K Richard
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Totally agree, it most certainly has made for confusing code and for confusing the hell out of not just me but countless others spanning YEARS, anyway thanks for all your help, especially your last post which did actually help me understand it further, before reading statements like what you made I just can't seem to see the wood for the trees but then after I read your comment about successive if statements as opposed to if/else if/else it just becomes obvious, kinda the same as the pool puzzles in this book that have been frustrating me, while I feel at the moment being able to solve them is just too much, I can still however fully iterate them on paper and understand exactly why and where things are happening BUT ONLY ONCE I ALREADY LOOK AT THE ANSWER, I'm not sure if this is part of the learning curve of a programmer or not and maybe someday soon ill push past this stage and be able to just instinctively see what code goes where in these puzzles for them to make sense.

Thanks again.

K.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!