Database Schema for a boat Programming Databases by playah … I could use some help. Situation: There are boats. A boat is build in a shipyard. A shipyard has an owner… owner, I still need to see from which shipyard the boat was bought. A fairly simple situation you might say, by… me if a shipyard is still the owner, or the boat has been sold.( of course it can be done with… Re: Database Schema for a boat Programming Databases by tesuji …and shipyards? [b]select owner.name, shipyard.name, boat.name from owner join shipyard on owner.onwerID = shipyard.…onwerID join boat on owner.onwerID = boat.onwerID;[/b] If you want to find…one: [b]select owner.name, boat.name from owner join boat on owner.ownerID = boat.ownerID where owner.category = 'shipyard… Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik … boats are missing in length (the length 4 boat becomes a length 3 boat). This doesn't happen only on borders, but… too. I've tried seeding with a single length 5 boat, and it still happens. The code: package potapanjebrodova; import java… Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik … mistake in line 49, the code still sometimes cuts some boat short. Re: Sink the boat game (complex conditions issue) Programming Software Development by bguild Just looking at the code I believe I see several problems. On line 48 I suspect you are demonstrating one of the major problems with copy-and-paste code. I think you really want that line to say `if(locationJ.toArray()[j] == positionJ)`. You should prefer using a private method instead of copying your code, because a private method gives a name to … Re: Sink the boat game (complex conditions issue) Programming Software Development by JamesCherrill Comments: I looked quickly at that code and decided I didn't have time to try to understand it. It's just too complex & nested. IMHO that's a result of not breaking the app up into enough classes. Read the rules of the game - the most common noun is "ship". So I would start with a Ship class class Ship final startRow, … Re: Sink the boat game (complex conditions issue) Programming Software Development by bguild > I believe you're wrong for 37-56: the table is 10x10 (100 cells), and the ships will occupy 16 in total (4+3+3+2+2+2). This means every ship will always have a free location to be seeded into. Your `table` array is 11x11, which causes your `printTable` method to show an 11x11 grid to the user. Your `locationI` and `locationJ` don't keep … Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik Alright, I've finally understood all of what you guys have said, and I'm reworking the code. I'll post the corrected version once I'm done correcting it. :D Thanks again! :) Re: Sink the boat game (complex conditions issue) Programming Software Development by kedxu As a newbie myself, I'm unsure whether this could be helpful, but I'll post it anyway: Instead of placing the ships one-by-one, you could try loop { randomlyArrangeShips(); //this ignores previously placed ships if(count_occupied_tiles == number_of_tiles_there_should_be) { //there is no intersection of ships… Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik There are 100 tiles, and a FIXED number of ship tiles (16). The amount of combinations is... large. 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.util.ArrayList; import java.util.List; import java.… Re: Sink the boat game (complex conditions issue) Programming Software Development by bguild > in order to eliminate that problem, it would require a lot of work anyway, It's actually not hard at all. With only a 10x10 grid and 7 ships to place, you can just do a simple exhaustive search. If you can produce a list of all places where each ship could be placed, then you can simply make one random decision for each ship with no danger of… Re: Sink the boat game (complex conditions issue) Programming Software Development by JamesCherrill This is all a premature optimisation. I tried the full game setup (battleshup, 2 destroyers ... 5 subs) by the brute force random approach, and on average there are only about 10 unused random ships. Not a problem. (code follows if you're interested - just a hack) import java.util.Random; public class Battleships { static … Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik I like the logic you used, James! Very smart stuff. :) However, the game requires all ships to be placed before the game actually begins, so no skipping. More about the game right over there -> [Battleship (game)](http://en.wikipedia.org/wiki/Battleship_(game)) As for my code, I know exactly what I have to do (in theory), but I just can't seem … Re: Sink the boat game (complex conditions issue) Programming Software Development by riahc3 Its a good excerise, Java or not. > I'd like to hear comments on the way I code, what are my strong points, what should I improve, am I using bad logic? The way someone code is very personal, for me. Makings lots of classes with no comments is the same as making one big entire class with lots and lots of comments. People are object obsessed … Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik I'm glad you like it, riahc3. :) Anyways, I'm still stuck, but I'm not giving up! I've reworked the conditioning part (lines ~ 35-67) completely. They now look like this: do { direction = this.R.nextInt(2); positionI = this.R.nextInt(9); positionJ = this.R.nextInt(9);… Re: Sink the boat game (complex conditions issue) Programming Software Development by JamesCherrill "No skipping"? My code adds all ships then displays them. Just try it! Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik Completed! package potapanjebrodova; import java.util.Random; public class PotapanjeBrodova { // 6, 4, 4, 3, 3, 3, 2, 2, 2, 2 int[] ships = {6, 4, 4, 3, 3, 3, 2, 2, 2, 2}; char[][] table = new char[10][10]; Random R = new Random(); public static void main(String[] args) {… Re: Sink the boat game (complex conditions issue) Programming Software Development by bguild > This is all a premature optimisation. It's not premature optimisation to find all the possible positions for a ship before making the random choice. It is more like doing it the slow way that guarantees correctness, which is far from optimisation. I've altered your elegant solution to turn it into an almost identical solution that doesn't … Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik 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! :D You've got 100 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… Re: Sink the boat game (complex conditions issue) Programming Software Development by JamesCherrill > 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.util.Random. That's OK, I just wish you had put that new code in its own method eg `List<Ship> getAllPossibleShips(...) { ...` :-) Re: Sink the boat game (complex conditions issue) Programming Software Development by JamesCherrill Now this is over, I'm still a bit unhappy with the code I posted earlier - in particular those almost-the-same loops inside the if (vertical) blocks. So here's a much cleaner/shorter version that has a width and height for each ship, so it doesn't need the vertical/horizontal boolean or the related if tests. It also opens up some possibilites such… Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik > yhou may want to hang on to this one. 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… Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik > - go through array, mark '_' as empty (boolean null), and everything else as a ship cell (boolean true) Had to correct a formating issue. Re: Sink the boat game (complex conditions issue) Programming Software Development by JamesCherrill 1. 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. 2. 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… Re: Sink the boat game (complex conditions issue) Programming Software Development by kedxu 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!", simply storing the empty/ship/hit status of each tile will not work--that means you can't just use a 2D boolean array. You'd need as James said a 'ship' class, with each ship storing its occupied … Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik My current code generates a char[][], with the cell's contents being either _, 6, 4, 3 or 2. In a day or two, I'll try to build a different class which goes through the char[][] and visualises something like this (by following James's advice): -if _, empty cell. -if number, ship (and assign color to cell according to number) Does this even make … Re: Sink the boat game (complex conditions issue) Programming Software Development by JamesCherrill > (by following James's advice) Not my advice. See my code for my best suggestion. Re: Sink the boat game (complex conditions issue) Programming Software Development by Nandomo Seems Like we are working on the same project my good sir. Re: Sink the boat game (complex conditions issue) Programming Software Development by Pobunjenik > Seems Like we are working on the same project my good sir. How so? Re: Sink the boat game (complex conditions issue) Programming Software Development by Nandomo 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.