• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Head First Java sink a dot com game, where am I going wrong?

 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,

Im trying to learn Java through HeadFirst and tried out some of their code on my own to build a sink-a -dot-com game. The game is simple, its supposed to take three user inputs and compare them as to whether they match those of the cells alloted to the dotcoms randomly.
Here's my source code

DotComBust.java


and DotCom.java



When I run DotComBust.java this is what happens

Im pretty sure the problem is because of the element not being present in the list, then how am I being able to print hit? Any insight would be helpful, thanks!!
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The if/else block that prints "hit" or "kill" should be nested inside the if block that updates the list. That way it will only execute if a hit has occurred. As it stands, this code is executed whether or not it's a hit or a miss.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Dennis.

I moved the if/else block into the if statement, however the program still doesnt work as expected.For instance, even if an element is actually present in the 'dotcom' , the program still results in a miss ,I some how feel that Im not being able to set the locationCells correctly in DotCom.java. Any help on that would be great.

method that sets up the Game in DotComBust


Result
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help folks?!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

i think it is because


locationCells is not empty so it is always printing hit. in the first code

after changing the if(index>=0) position ,the execution never entered into if(index>=0){ block

so the default value got printed "miss"
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's time to learn the art of debugging. You need to test your assumptions by printing things to the console (or using the debugging tool if you're using an IDE). You already know that locationCells does NOT contain userGuess. That's your starting point to focus your debugging efforts. How do things get added to the list? What are the contents of the list when you call indexOf? This is the real blood sweat and tears of programming, and only you can do it.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dennis and Athira!

The problem was with not looking at the code from the right perspective. Thank you for the time!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic