This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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 ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 12627
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!