This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

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

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"
else
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
java-boy
 
lowercase baba
Bartender
Posts: 12626
50
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!