aspose file tools*
The moose likes Beginning Java and the fly likes [Head First Java] Sink a dot game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Head First Java] Sink a dot game" Watch "[Head First Java] Sink a dot game" New topic
Author

[Head First Java] Sink a dot game

Nazar Kotentsov
Greenhorn

Joined: Jul 20, 2011
Posts: 4
Hi everybody. Ive been reading Head First Java and found a kinda strange thing inside of a code. That probably is a mistake (though im not sure because there is no topic about that thing on this forum or or at list of errata at oreily.com) so i ask you guys to help me get it.
Here is a class which authors used without loads of comments (as they usually do). I marked the line which makes me doubt with a comment (it's number 16).

The problem is that in case we have got inappropriate value of location variable more than two hundred times, we skip the loop and DO NOT FIND appropriate values, just use an array coords[] as is. There can be any value there (e.g. the situation when coord[0] and coord[1] are filled with nice values and coord[2] is not), which may lead to crash.
Ofc you can say that to get bad values for 200 times in a row is a bit fantastic, but that's still possible is not it? We'd better remove that '200 times' limitation or at least throw an exception (or may be simply end the running of a program, or smth else) if that count of tries is reached.... What do you think about that?
I'm looking forward to your replies.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
The line about (i % 2) == 1 worries me more; that can potentially miss an odd number. I shall let you work out when it will miss an odd number.

Don't know yet; which page is this game on?
Nazar Kotentsov
Greenhorn

Joined: Jul 20, 2011
Posts: 4
Thank you for your reply, Campbell Ritchie.
that can potentially miss an odd number

Nope, that's just fine. That line tells us which cells to mark besides the generated one. In case this is an odd/even number, we've got to take two cells to the right or two cells to the bottom of that one which we got by Math.random()*gridSize (as long as ships may be located vertically or horizontally). To take two cells to the right is quite simple: we take cells with number+1 and number+2 positions. For a couple of cells to the bottom we've got to add a gridLength. Just like you've got a 2-dimensional array written in a single row: that's just about managing indexes. That's what the lines 11-15 are doing.

which page is this game on

chapter 6, pages 148-153. the code which i pasted above as a first one in this topic is located on pages 152-153.

So, the question remains unsolved. I know that (probably) it is not a question for a beginners' part of the forum but...
Guys, any ideas?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Now morning is here, and I can find my HFJ, and look at page 152, I can give a sensible answer.

The 200 doesn't really mean anything. It is partially there to demonstrate the && operator. It is partially there for somebody who is playing really really badly, trying the same square repeatedly. The idea of the 200 is so the game cannot go on for ever. If you get to 200 tries, the loop will terminate. It ought to terminate long before that because success will turn true and the !success test will turn false and the while will no longer be executed. I presume you have 100 squares, so you really ought to have finished the game long before 200, so attempts++ < 200 should always return true.

Remember some Boolean algebra: x && true = x
   and: x || false = x
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
The bit about attempts++ < 200 is regarded by some people as poor style, though I think it is a useful construct.
Nazar Kotentsov
Greenhorn

Joined: Jul 20, 2011
Posts: 4
Thank you for your reply, Campbell Ritchie.
I ask you to take your HFJ book again and read it with more attention, please. You misunderstood the code again.
It is partially there for somebody who is playing really really badly, trying the same square repeatedly.

The code where that string with constant 200 is used does not relate to a gameplay itself. It's a part of a game setting and the method

is just about placing a ship on a desk (returning the coordinates of that ship with comSize cells). So you are wrong with a supposition that if you cant win a game in 200 turns then it's time to stop. NO NO NO. The problem is inside INITIALISATION of a game.
If you get to 200 tries, the loop will terminate.

NO NO NO, you are not guessing in that part of the game, computer just generates RANDOM points for 200 times (so a number of cells (squares) does not matter in fact). In case the number which comes 200 will be unappropriate (and 199 times before too, so we got here), the code tells DO NOT TRY TO FIND A NICE VALUE ANY MORE, TAKE THAT WHICH IS GRANTED AND USE IT. So there's gonna be a crash. That BAD values will be RETURNED by the method and used later, so sometimes a ship can get only 2 cells placed together and one somewhere in a different place (or nowhere at all, or may be it will come out of boundaries and so on).
Remember some Boolean algebra

Yeah Ive got a bachelor's degree on math, so ofc i do.

Please think about that more carefully. The question is a bit more complicated then you thought it is.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
It says it is compressed code, not written for readability, so they have abbreviated the comments.

I see the grid is only 49 squares. I still think the 200 is to prevent the placing process going on for ever. It is possible that the program will "try" "randomly" and repeatedly hit the same cell, so the 200 is to prevent it from going on for ever.
By the way: how large is each dot-com on the grid? What is the probability of repeated collisions like this? There must be a finite probability that it will go on for >200 attempts, but that is presumably small. there is also the possibility that you will repeatedly try a corner or edge square whence it is not possible to fill all the dot-com squares.

I still think it is simply to prevent the program running for an inordinately long time. I downloaded the internet version of that class (in the downloads from www.headfirstlabs.com, for chapter 5) and the comments there were the same as in the paper version.

Anybody else got any more helpful suggestions? Bert, even?
Nazar Kotentsov
Greenhorn

Joined: Jul 20, 2011
Posts: 4
Thank you for your reply, Campbell Ritchie.
200 is to prevent it from going on for ever.

I thought about that. But that had to be an if-condition that in case we had 200 unappropriate positions to place a dotcom and all of them were bad, not to run a programm with that bad values so we wont get a runtime crash. I have already written about that in the first post of this topic: there could be something like an exception or returning a null-arraylist or smth like that so we could stop the program and tell a user that something goes wrong, not show him errors. BUT there is nothing like that in the code, and there is no excuse for this becouse that code was given "as is" with no comments, they did not have to explain all that exceptions, just PUT THEM DOWN there. So in this case its A BUG and it has to be fixed by book's authors, what do you think?
how large is each dot-com on the grid?

Each dotcom takes three cells, but the method which we discuss is able to return positions of dotcoms of any size (as long as there is a size parameter passed to function).
There must be a probability but that is presumably small

It can be an infinitesimal but the truth is that is exists. Untill there is a probabily (and theory of probability tells us that there really is) of crash, we cant use the code, and that's the basic principio of programming in a nice way. I hope nobody goes to argue about that.
 
wood burning stoves
 
subject: [Head First Java] Sink a dot game