• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic