LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2003, 08:31 PM   #1
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
OOP Modeling Question


This is a question for you OO/design pattern gurus.

I'm writing a Battleship game that two players can play over a TCP/IP network. Using the model-view-controller design pattern, my model is obviously the game board with the ships on it. So I have a GameBoard class, and an abstract class for Ship with concrete classes AircraftCarrier, Battleship, Submarine, Cruiser, and Destroyer all inheriting from Ship. The GameBoard aggregates the Ships. When the player attacks a position on the game board, someone must keep track of whether the result is a hit or a miss.

Should the each Ship or the GameBoard (or both, or neither) be responsible for keeping track of the hits and misses? I go back and forth. It makes sense for the GameBoard to keep track of the misses, since that info is unrelated to a Ship. That sort of implies that the Model would also keep track of the hits. However, a Ship should be able to tell you if it is sunken which implies that the Ship should keep track of its hits. But, if the Ship keeps track of its hits then the Ship must somehow correlate an <x,y> position on the GameBoard with the points on the Ship, which doesn't seem like a good separation of responsibilities.

Any suggestions on a good way to set this up? Any design patterns out there to help solve this problem?
 
Old 10-05-2003, 02:55 AM   #2
Robert0380
LQ Guru
 
Registered: Apr 2002
Location: Atlanta
Distribution: Gentoo
Posts: 1,280

Rep: Reputation: 47
suggestion:

the board could keep track of the ship's location, if someone hits a ship (an x,y location on the board) then the board could notify the ship that it has been hit and also tell the player "you hit his ship". If the board just knows "there is a ship in this spot" then the ship could keep track of weather or not it has been sunk and the board could ask the ship if it has sunk (like maybe there could be a method in the Ship class isSunken that checks each square for a hit or not and if each squre has been hit then the ship is no long floating. This way, the board knows if a ship is hit, but doesnt have to keep track of where the ship actually ends and begins, each square on the board can just hold a value of true for ship here or false for water. then on a "hit" the board then asks the ship (are you sunken?) and if the ship replies with a "yes" (is Sunked returns true) then the board can inform the other player of the situation.

to me this would make the board easier to write because it doesnt have to know where ships begins and ends but just if a particular square contains a section of a ship.

you'd need a way to ask the correct ship if it has been sunk or not though, like a handle to the ship that's occpying the square.

i hope my suggestions help.

to sum it up: have the ship keep track of it's hits but have the board know if a ship is in a square or not.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
3D Modeling? computergee Linux - Software 9 02-09-2005 03:00 AM
Any database modeling tool? alantang Linux - Software 0 11-02-2004 04:50 AM
OOP / Python inheritance question usernamenumber Programming 1 12-02-2003 10:21 PM
3D Animation and Modeling packages bkeating Linux - Software 2 08-21-2003 05:54 AM
3d modeling program bjwbell Linux - Software 2 09-12-2002 04:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration