#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Posts
    1
    Rep Power
    0

    Creating a Checkers game using NetBeans


    Hello,

    I'm currently studying Netbeans for an assignment, i have been given the task to create a multiplayer game that can be played across two mobiles. We have been given three options of different games we can create, i have chosen the easiest option of creating a 'Checkers' game

    Unfortunately my Java skills are not to a high standard and for the past three days i've become stuck on the same problem.

    I've created a board and have managed to create two different coloured checker pieces using 'sprites', my main concern at the moment is how to move on from having just one checker each to having three rows of checkers for each player. At the moment i can only think of creating multiple amounts of the checker pieces by having 24 sprites (bad practice?) and then moving on by coding the movement of the pieces. Is there another way of generating 24 pieces onto the board without having 24sprite images?

    Many thanks for any feedback, if i'm in the wrong place i apologize.

    James
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2008
    Location
    Fort Meade, MD
    Posts
    170
    Rep Power
    50
    The good thing about Java, is that objects in Java behave a little like pointers in C++. For example:
    Code:
    obj2 = obj1;
    This makes obj2 and obj1 handles to the same object (in other words, changing one will change the other).

    SO what this means for you, is you can pass the handle to a single "sprite" on to multiple checkers.
    For example, you could first create a Checker class:
    Code:
    import java.awt.image.BufferedImage;
    
    public class Checker
    {
        private BufferedImage mySprite;
    
        // Constructor: gets passed a handle to the sprite:
        public Checker( BufferedImage s )
        {
            // Point "mySprite" to the given handle:
            mySprite = s;
        }
    }
    Then, create a Checker Manager class, which loads the sprite once, then creates the individual Checkers and passes them a handle to the loaded sprite:
    Code:
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.IOException;
    import java.lang.System;
    
    public class CheckerManager
    {
        private BufferedImage redCheckerSprite;
        private BufferedImage blackCheckerSprite;
        private Checker[] redCheckers;
        private Checker[] blackCheckers;
    
        // Constructor: load the sprites, and create the checkers
        public CheckerManager()
        {
            String redCheckerFilename = "redchecker.jpg";
            String blackCheckerFilename = "blackchecker.jpg";
    
            // Use whatever way you want to load your sprites:
            try
            {
                redCheckerSprite = ImageIO.read( getClass().getClassLoader().getResourceAsStream( redCheckerFilename ) );
                blackCheckerSprite = ImageIO.read( getClass().getClassLoader().getResourceAsStream( blackCheckerFilename ) );
            }
            catch( IOException e )
            {
                System.err.println( "Error loading a sprite" );
            }
    
            // Create 12 of each type of checkers:
            redCheckers = new Checker[12];
            blackCheckers = new Checker[12];
            for( int c = 0; c < 12; c++ )
            {
                // Create the individual checkers, each gets a handle to one of the two loaded sprites:
                redCheckers[c] = new Checker( redCheckerSprite );
                blackCheckers[c] = new Checker( blackCheckerSprite );
            }
        }    
    }
    Hope this helps!

IMN logo majestic logo threadwatch logo seochat tools logo