This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
  • Carey Brown
  • salvin francis
  • Claude Moore

Logic in Head First Dotcom game for creatinng new array page 130 question  RSS feed

Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In Head First Java page 310, it has mentioned about dot game following

if user guesses matches
remove cell from array
if array is empty then
return "kill"
return "hit"
else use guesses matches then
return miss
end if

Array = 0, 1, 2
Values = 4,5,6

When cell "5" is hit, we make a smaller array list

Array = 0, 1
Values = 4,6

But something I had not understood,

if player gives guess 2, which is correct but if we make new array then how code will find it is correct guess ? pl clear my logic.

thanks & regards
lowercase baba
Posts: 12738
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have the book in front of me...but...

I think they are talking about a "boat" that is three cells long somewhere in an array that is 10 cells long. The boat could be in position "0,1,2", "1,2,3", "2,3,4" etc., up to position "7,8,9". The trick here is you don't actually need all 10 cells, but only the three relevant ones where the boat "is".

You declare a three-cell array to hold the positions. You say you have this:

Array = 0, 1, 2
Values = 4,5,6

That means the boat is actually in position 4,5 and 6 in a 10-cell line.

When the user guesses "5", you search all the VALUES stored in the array, to see if a '5' is stored anywhere. It turns out there is, so you return a 'hit', and shrink your array. That would leave this:

Array = 0, 1
Values = 4,6

If the user then guesses "2", you again search the VALUES in the array. 2 is not there, so it's a miss.

I think you're getting the array index confused with the array values - and it's the latter we're searching for.
I found a beautiful pie. And a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!