Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: If you had a chess-like boardgame....

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default If you had a chess-like boardgame....

    ....how exactly would you build an object-oriented program for it?

    Suppose the game has the following basic rules:
    1.) Two players
    2.) Each player has 10 units that are placed on a grid-like board
    3.) Players alternate turns by moving one unit at a time
    4.) Units can attack each other (if within range) and deal damage to the units health (unlike in chess where everything is a one-hit kill).
    5.) Each unit moves in a unique manner (some can move three spaces, some can move 4, etc.)


    What would your object-oriented program look like?

    I'm thinking there needs to be the following classes:

    Board class, Player class, Game class, Unit class

    Where the Game class contains one board and two players. Each Player has ten units.


    But, where I get confused, is where do you put the "move" method??????

    It seems that the move method should be a part of the Unit class, since each unit possesses the ability to move. But, in order to move, the Unit must have access to the board (to determine where it can move). But I don't see why it would make sense to have every Unit have a board data member. That seems wasteful...or is it?

    I really don't know....I'm not much of a programmer, and I know you guys are much more advanced than me. So how exactly would you deal with the move method here?

  2. The Following User Says Thank You to integer9 For This Useful Post:

    GregBrannon (January 2nd, 2014)


  3. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,751
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: If you had a chess-like boardgame....

    Thanks for thinking your design through before writing code. Even with a thorough design in mind and then written down, you can run into unexpected requirements later that may require design changes. The more thought up front, the fewer changes along the way, the less time spent rewriting code. You may need additional classes to present the game's interface to the players, so you might want to give that some thought.

    As for where the move() method belongs, I'm not even sure there is a move() method. Don't the users or players decide which Unit objects to move and where to move them? Aren't the users/players responsible for knowing the rules, how their Unit objects move, and where each can move from their current locations? Or is there some AI involved so that Unit objects move themselves?

    I can imagine a Unit.move() method similar to move( Location newLocation) that is commanded by the user/player. That method would physically move the Unit's graphical representation from its current location to the new location and then update that Unit object's state to the new location. There could be some validity checking as to whether the move is legal, the new location is available, etc., but some of that (all?) should be the player's responsibility. As in a real chess game, the pieces have specific moves, but it's up to the players to move the pieces correctly and to approve the other player's moves. That's part of the game.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    integer9 (January 2nd, 2014)

  5. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: If you had a chess-like boardgame....

    That's an interesting point.

    But what I always thought was this:

    when you play chess, if you click on a piece, all the panels on the board to which you can move light up. You then click one of those panels, and the piece moves there. Moving to an illegal panel isn't even an option.

    In addition, my game is a bit more complicated than chess for a reason I failed to mention in the original post: you can move through friendly units (they will "step aside" to let you pass") but you cannot move through enemy units (they will block your path).

    As an example, suppose one of your units can move three panels in any direction. Suppose that there is an enemy unit right in front of him. This means that your unit would have to move around that enemy unit. If you visualize it, you'd see that your unit cannot move behind the enemy unit with just three moves, despite the fact that the panel behind the enemy unit is just two panels away and your unit can move three.

    This makes things a little complicated. I also think the game would be better and easier to play if all the panels on which you could move lit up every time you clicked on a unit.....but determining where each unit can move is somewhat difficult. Since it's possible for inexperienced players to be playing this game, I think it'd make more sense for the game to determine whether each move is valid. What if a player just calls another player's move invalid, even when it isn't? What if a player is new to the game and has not yet learned all the rules (some units, for example, may have special abilities that allow them to move through or fly over enemy units)?

    In addition, I need to remember the path each unit took to move because ultimately the game will show the unit walking (in animation) as he moves. It's also worth noting that this needs to be the shortest path.

    This function seems complicated enough to need its own method, though I'm still unsure of where it would go.

  6. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: If you had a chess-like boardgame....

    It seems like you are trying to provide several distinct actions in a single method. Checking where a unit can move should probably be a distinct method from actually moving the unit. Both of these functions seem to best fit into the Unit class.

    As far as whether including board as a member of Unit is wasteful, I wouldn't worry about it. You are debating over a few hundred bytes at most when even phones have several millions if not billions of them to spare. Consider the interface it offers you: it allows you to have the unit operate on the board without having to pass which board to operate on to every method. Is this beneficial? Well, maybe. I don't see it as detrimental either way. The one part I might worry about is having a unit track its own location. Depending on how you track your board this may make it possible to easily have the unit think it is in one location while the board (and consequently other units) think it is somewhere else. Keeping track of where the unit currently is in my opinion seems to fit best with the board itself.

    class Board
    {
        // simple example: just use a 2D array to track units at some (row,col)
        Unit[][] data;
    }
     
    class Unit
    {
        // Asks what is a possible (or even optimal) move path from a starting location curr to dest, or the empty list if no possible path.
        public ArrayList<Location> getMovePath(Board b, Location curr, Location dest);
     
        // asks where this unit can move given a Board and its current location. Simplest brute force implementation given in pseudo-code
       // You could do something smarter here if you really wanted to, but for a chess-size board this should be fast on even quite old hardware
        public ArrayList<Location> availableMoveSpots(Board b, Location curr)
        {
            ArrayList<Location> locations;
            for(all possible Locations dest in b)
            {
                if(getMovePath(b, curr, dest) is not empty)
                {
                    locations.add(dest);
                }
            }
            return locations;
        }
     
        // actually moves the unit from curr to dest. Can call getMovePath() to do the path finding part again (code reuse: yay!)
        // alternative, just as valid interface: pass the path as a parameter to move(). You probably have it saved from earlier checking if a move is valid.
        public void move(Board b, Location curr, Location dest);
    }

    Like with many programming designs there's not generally one right answer, but you usually can spot when you have a wrong one pretty quick.

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    integer9 (January 2nd, 2014)

  8. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,751
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: If you had a chess-like boardgame....

    if you click on a piece, all the panels on the board to which you can move light up
    That's a valid option but may not be always turned on. Sounds like a bit of a crutch.

    As you've described the rules of Unit movement, each piece or unit should know what move(s) it is capable of making but may not know which of those possible moves are allowed by the game board's current state. I would expect the allowed moves to be defined by some game controller above the Unit level. From that, I imagine the user selects a unit, the unit reports the moves possible according to that unit's movement rules, and those possible moves are fed to a game controller to process and indicate which of those possible moves are allowed by the current game state, indicating the possible end points and possibly the effect of the movement along the paths to those end points once selected by the player, depending on how much detail you can/want to show.

    You've thought most of this through. You seem to have a good idea of how you want the game play to occur and to appear to the players. Programming the mechanics of realizing Unit movement doesn't seem like it would be that hard. Don't give each unit object more responsibility for its existence and movement than it should have. This will keep responsibility for game play where it belongs and will require you to reduce each element of the game into its simplest form, reducing the overall complexity of the game's design as as much as possible.

    Start by writing out everything the game should do - its functions. Then define the classes you think are necessary to do those things and assign each of the game's functions to the classes you've defined. Write a plain language description of each class, the game's functions it is responsible for, and how each class accomplishes those functions. Ultimately, you'll want to define each class' inputs/outputs and the data dependencies between classes. If you have functions left over or create new ones as you go through this process, define additional classes to handle them, as many as are needed. If the plain language descriptions of what the classes do begin to sound too complex or inappropriate for that class, create additional classes to share the load.

    Don't start coding until you have all of the game's functions defined and assigned to classes, and you've written class descriptions in as much detail as necessary to ensure all game functions can and should be accomplished by that class. Once you've thought it through and documented the design to this level of detail, coding is much easier.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    integer9 (January 2nd, 2014)

  10. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    3
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: If you had a chess-like boardgame....

    Thanks a ton for the help guys!

    I have a lot of great advice to look through. I'll read through everything you guys said carefully. I think it helps a lot to hear advice from advanced programmers. Thanks again!

Similar Threads

  1. Fixing my Chess Program setup
    By Totara in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 1st, 2013, 10:02 PM
  2. Chess Program
    By gokuball in forum Java Theory & Questions
    Replies: 218
    Last Post: August 19th, 2013, 04:47 PM
  3. Chess game help
    By that_guy in forum Java Theory & Questions
    Replies: 3
    Last Post: December 4th, 2011, 07:42 PM
  4. Chess Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 11:07 AM
  5. Simple Chess program
    By x3rubiachica3x in forum What's Wrong With My Code?
    Replies: 23
    Last Post: September 22nd, 2010, 11:12 AM