Boat Simulator WebGL. Sky Pirates WebGL. Sea Battles WebGL. Pirate of Ships Demo Unity 3D. Pirates Attack Flash. Sandcastle: Ancient Invasion Flash.
When Submarines Attack Flash. Sink My Treasure Flash. Raft Wars 2 Flash. Battleships Ready Go! Crashing Skies WebGL. Battlecoast HTML5. Pirate Mayhem Flash. Crush the Ships Flash. Cannonfodder Unity 3D. Fishing io HTML5. Nords Odyssey WebGL. Speedboat Racing WebGL. Feed Us 3 Flash. Sink or Spin Flash. Boat Rush Flash.
Warship vs. Submarines Unity 3D. Chimps Ahoy! Submarines Flash. Daring Ride Flash. Feed Us 5 Flash. The problem: My code seems to completely ignore the collision tests. If it finds even 1 slot occupied, the FOR loop should break and a new set of coordinates should be generated and tested in the same way. This code works!
I've tested it countless times. Here is an output example with line 65, which I used to debug :. I have to admit, lines did test my limits. Turns out the logic in my previous attempt was good, but the collision test was flawed FOR loop didn't hop from slot to slot as intended. I've altered your elegant solution to turn it into an almost identical solution that doesn't depend on luck or the implementation of java. I don't see why you'd ever test for all possible locations, but I see how the logic is correct.
Just find the first available slot and use it! You've got slots, and only 31 slots are occupied by ships according to the vanilla rules of the game. So, in every single iteration, even when all ships are seeded - you'll have a minimum of 69 empty slots. In other words, you'll get the job done fast and you'll never fall into an inifinte loop.
It also opens up some possibilites such as an aircraft carrier that's 6x2 cells. There are a few other tidy-ups as well. I still can't see how you will know when a ship has been sunk using your current data structures.
I see your concern, but this code's job was only to generate a valid layout. If I ever decide to make this into a game, I'd have another class pick up the generated layout and generate a neat table with each slot having 3 attributes empty, ship, hit. The logic: - pick up 2D array - go through array, mark ' ' as empty boolean null , and whatever! PPS I'd appreciate a hint Just a hint! What direction must I take to make a table similar to the one found here?
Just the structure, the mouseovers and such I'll find. All you need is a boolean[][] - has the player targetted this cell? You can then use the existing Ship[][] array to see if that's a hit, or what to display to the user for that cell. Draw a grid - subclass JPanel and in the paintComponent method 2 loops to draw horizontal and vertical lines, OR nested loop to draw little boxes - easier that way to draw the box's contents at the same time.
Just a note: if you want to know or might want to know later whether a ship has been sunk, perhaps to tell the player "You just sank a ship! You'd need as James said a 'ship' class, with each ship storing its occupied cells. Otherwise, you could never tell what is and isn't part of the same ship, especially when they're directly adjacent. I am also creating a battleship game, but I am stuck closing my GUI after I refresh with a new one, already have my ship placement logic.
I've reworked this code into what I consider to be correct. I've tested the new code by looping the seeding and printing methods times to look for bugs - none were found!
No ships collided, no ships were out of bounds and I've even built in a small method to fire at ships which returns correct values for both hits and misses. I've accomplished this by having 2 2D char arrays, one that contains the actual layout of the ships, and another empty one which displays hits and misses as the user fires.
GUI time. I've googled like a madman to find instructions on how to transfer data from a 2D array in this case, char[][] board to a jTable which is in a separate class called GUI. I would appreciate some tips so that I can get some friends to play the game on their PCs. We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge. You're trying to visit a URL that doesn't currently exist on the web.
Most likely, a member posted a link a long time ago to a web page that has since been removed. It's also possible that there was a typo when posting the URL. We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.
The code: package potapanjebrodova; import java. ArrayList; import java. List; import java. Thanks, Pob. Answered by JamesCherrill 4, in a post from 8 Years Ago. My code adds all ships then displays them. Just try it! Jump to Post. Answered by bguild in a post from 8 Years Ago.
I've altered your elegant solution to turn … Jump to Post. Your condition for the while on line 56 is confusing: this. Thank you for your lengthy response! I'll rework that test into a separate method. I didn't even know those existed. It's also going to make things lot easier when you want to know if a Ship has been sunk etc. Edited 8 Years Ago by kedxu. Anyways, I've reworked parts of the code mostly the conditions and probing for viable coordinates , and the newest code looks like so: package potapanjebrodova; import java.
Edited 8 Years Ago by Pobunjenik because: Formating that table. Edited 8 Years Ago by JamesCherrill. Pobunjenik commented: Very neat code, kudos! Here is a step by step of what I think has to be done to get flawless seeds on every iteration: - generate coordinates - generate direction - check for collision - check for out of bounds - in case of collison or out of bounds, return to step 1 PS The story behind the code: This is a pet project of mine.
This is part 1 - program seeds ships. Its a good excerise, Java or not. I'm glad you like it, riahc3. Edited 8 Years Ago by Pobunjenik because: typo.
Pobunjenik commented: Sorry, I must have missread your code. Pobunjenik commented: Foolproof code, risk eliminated! Pobunjenik commented: I like your style. PS The numbers could later be used to display different icons for each ship.
Wanna see? See my code for my best suggestion. Pobunjenik commented: I meant your style of coding. Seems Like we are working on the same project my good sir. How so? Facebook Like. Twitter Tweet. Be a part of the DaniWeb community. Sign Up — It's Free! Reply to this Topic. This question has already been solved! The person who asked this question has marked it as solved. Solved questions live forever in our knowledge base where they go on to help others facing the same issues for years to come.
Are you sure you have something valuable to add that has not already been mentioned? Consider starting a new topic instead. Otherwise, please be thoughtful, detailed and courteous, and adhere to our posting rules. Edit Preview. H1 H2. Post Reply. Broken Link.
Go Back to the Post Continue Anyways. Share Post. Permanent Link. Insert Code Block.
0コメント