Results 1 to 15 of 15
  1. #1
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Checkers Movement

    Hey forum um I wasn't sure where to post this so i guess here will do. I am attempting to make a 2 player checkers game. This isn't a school project or anything just kind of a personal project. I am stuck though when i came to the actual movement of the checkers. I have an array of JButtons and an 2 arrays of values. One for the actual values the other as kind of an area to change the values. I don't know where to start even when it comes to the movement and any help would be appreciated thank you.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    You should define precisely what you mean by 'movement'. Do you want to drag something? If so, what (JButton, jpanel, drawing in JPanel)? Further, the answers are very context dependent (upon the code written).

  3. #3
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks for answering :) this is what i have so far.


    class Runner extends JFrame {
    JMenuBar menuBar;
    JMenu file, help;
    JMenuItem exit, about, reset;
    JButton[][] boardButtons = new JButton[8][8];
    int x, y;
    Dimension move = new Dimension();
    public Runner() {
    setupGUI();
    JMenu FileMenu= new JMenu("File");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setSize(700, 700);
    this.setVisible(true);
    this.setTitle("Checkers");
    this.setResizable(false);
    }
    public void setupGUI() {
    setLayout(new GridLayout(8,8));
    //Creates Overhead Menu
    menuBar=new JMenuBar();
    add(menuBar);

    file = new JMenu("File");
    menuBar.add(file);
    reset = new JMenuItem("New");
    file.add(reset);
    exit = new JMenuItem("Exit");
    file.add(exit);

    help = new JMenu("Help");
    menuBar.add(help);
    about = new JMenuItem("About");
    help.add(about);

    setJMenuBar(menuBar);
    //Creates buttons
    for (int i=0; i<8; i++)
    {
    for (int j=0; j<8; j++)
    {
    boardButtons[i][j] = new JButton("");
    add(boardButtons[i][j]);
    }
    }
    }
    public static void main(String args[]) {
    Board board = new Board();
    }
    }


    class Board extends Arrays {
    Runner run = new Runner();
    Icon red = new ImageIcon(getClass().getResource("checkerRed.png") );
    Icon black = new ImageIcon(getClass().getResource("checkerBlack.png "));

    public Board() {
    setBoard();
    for(int i=0;i<8;i++) {
    for(int k=0;k<8;k++) {
    if(arry[i][k]==BLANK) {
    } else if(arry[i][k]==RED) {
    run.boardButtons[i][k].setIcon(red);
    } else if(arry[i][k]==BLACK) {
    run.boardButtons[i][k].setIcon(black);
    }
    }
    }
    }
    public void setBoard()
    {
    for(int i=0; i<8; i++)
    {
    for(int j=0; j<8; j++)
    {
    arry[i][j] = BLANK;
    tempArray[i][j] = BLANK;
    }
    }
    for(int j=0; j<8; j+=2)
    {
    arry[7][j] = RED;
    tempArray[7][j] = RED;
    arry[6][j+1] = RED;
    tempArray[6][j+1] = RED;
    arry[5][j] = RED;
    tempArray[5][j] = RED;
    arry[0][j+1] = BLACK;
    tempArray[0][j+1] = BLACK;
    arry[1][j] = BLACK;
    tempArray[1][j] = BLACK;
    arry[2][j+1] = BLACK;
    tempArray[2][j+1] = BLACK;
    }
    }
    public void move() {

    }
    }



    class Arrays
    {

    /*
    *Main Array That Contains The Actual Values
    */
    public int[][] arry = new int[8][8];

    /*
    *Array That is Used To change the values in arry
    */
    public int[][] tempArray = new int[8][8];

    /*
    *Int that represents the BlankSpace
    */
    public static final int BLANK = 0;

    /*
    *Int that represents the RedChecker
    */
    public static final int RED = 1;

    /*
    *Int that represents the BlackChecker
    */
    public static final int BLACK = 2;

    /*
    *Int that represents the BlackKing
    */
    public static final int BLACKKING = 3;

    /*
    *Int that represents the RedKing
    */
    public static final int REDKING = 4;

    /*
    *Gets Array
    *@return arry
    */
    public int[][] getArray() {
    return arry;
    }

    /*
    *Gets the selected piece type
    */
    public int pieceSelected;

    public int getPieceSelected()
    {
    return pieceSelected;
    }

    /*
    *Gets the specific point in the array
    */
    public int getLocation(int x, int y) {
    return arry[x][y];
    }


    }

  4. #4
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Wow. Sorry didnt realize it would come out like that....

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Edit the post and wrap the code with code tags created by the # icon above the input area.

    Also include the import statements.

  6. #6
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    class Runner extends JFrame {
    	JMenuBar menuBar;
    	JMenu file, help;
    	JMenuItem exit, about, reset;
    	JButton[][] boardButtons = new JButton[8][8];
    	int x, y;
    	Dimension move = new Dimension();
    	public Runner() {
    		setupGUI();
    		JMenu FileMenu= new JMenu("File");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(700, 700);
    		this.setVisible(true);
    		this.setTitle("Checkers");
    		this.setResizable(false);
    	}
    	public void setupGUI() {
    		setLayout(new GridLayout(8,8));
    		//Creates Overhead Menu
    			menuBar=new JMenuBar();
    			add(menuBar);
    
    			file = new JMenu("File");
    			menuBar.add(file);
    			reset = new JMenuItem("New");
    			file.add(reset);
    			exit = new JMenuItem("Exit");
    			file.add(exit);
    
    			help = new JMenu("Help");
    			menuBar.add(help);
    			about = new JMenuItem("About");
    			help.add(about);
    
    			setJMenuBar(menuBar);
    			//Creates buttons
    		for (int i=0; i<8; i++)
    		{
    			for (int j=0; j<8; j++)
    			{
    				boardButtons[i][j] = new JButton("");
    				add(boardButtons[i][j]);
    			}
    		}
    	}
    	public static void main(String args[]) {
    		Board board = new Board();
    	}
    }
    
    
    class Board extends Arrays {
    	Runner run = new Runner();
    	Icon red = new ImageIcon(getClass().getResource("checkerRed.png"));
    	Icon black = new ImageIcon(getClass().getResource("checkerBlack.png"));
    
    	public Board() {
    		setBoard();
    		for(int i=0;i<8;i++) {
    			for(int k=0;k<8;k++) {
    				if(arry[i][k]==BLANK) {
    				} else if(arry[i][k]==RED) {
    					run.boardButtons[i][k].setIcon(red);
    				} else if(arry[i][k]==BLACK) {
    					run.boardButtons[i][k].setIcon(black);
    				}
    			}
    		}
    	}
    	public void setBoard()
    	{
    		for(int i=0; i<8; i++)
    		{
    			for(int j=0; j<8; j++)
    			{
    				arry[i][j] = BLANK;
    				tempArray[i][j] = BLANK;
    			}
    		}
    		for(int j=0; j<8; j+=2)
    		{
    			arry[7][j] = RED;
    			tempArray[7][j] = RED;
    			arry[6][j+1] = RED;
    			tempArray[6][j+1] = RED;
    			arry[5][j] = RED;
    			tempArray[5][j] = RED;
    			arry[0][j+1] = BLACK;
    			tempArray[0][j+1] = BLACK;
    			arry[1][j] = BLACK;
    			tempArray[1][j] = BLACK;
    			arry[2][j+1] = BLACK;
    			tempArray[2][j+1] = BLACK;
    		}
    	}
    	public void move() {
    
    	}
    }
    
    
    
    class Arrays
    {
    
    		/*
    		 *Main Array That Contains The Actual Values
    		 */
    		public int[][] arry = new int[8][8];
    
    		/*
    		 *Array That is Used To change the values in arry
    		 */
    		public int[][] tempArray = new int[8][8];
    
    		/*
    		 *Int that represents the BlankSpace
    		 */
    		public static final int BLANK = 0;
    
    		/*
    		 *Int that represents the RedChecker
    		 */
    		public static final int RED = 1;
    
    		/*
    		 *Int that represents the BlackChecker
    		 */
    		public static final int BLACK = 2;
    
    		/*
    		 *Int that represents the BlackKing
    		 */
    		public static final int BLACKKING = 3;
    
    		/*
    		 *Int that represents the RedKing
    		 */
    		public static final int REDKING = 4;
    
    		/*
    		 *Gets Array
    		 *@return arry
    		 */
    		public int[][] getArray() {
    			return arry;
    		}
    
    		/*
    		 *Gets the selected piece type
    		 */
    		public int pieceSelected;
    
    		public int getPieceSelected()
    		{
    			return pieceSelected;
    		}
    
    		/*
    		 *Gets the specific point in the array
    		 */
    		public int getLocation(int x, int y) {
    			return arry[x][y];
    		}
    
    
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    How do will the game work? A human on one side and the computer on the other?
    For the human to select his move, you will need to add listeners to the buttons so you know which one was selected to be move and to select the target square.
    For each piece you could have an arraylist of the possible legal squares it can move to. Then a method to test if a square is in that list to test for legal moves.

  8. #8
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    It would be a human vs human game probably. So i have to add an action listener for all 64 buttons?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    No, just for the squares that are legal moves. The buttons for the non legal moves should be a different color and disabled.

  10. #10
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Okay, this helps tremendously thank you very much :)

  11. #11
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    Quick question. I have changed the code quite a bit and am now using an arraylist of JToggleButtons. My question is. Would it be possible to have one button listener and have that button listener get the index of the button that is selected?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Yes the button could use the JComponent's ClientProperties methods to save info about the button that could be gotten as required. Save each button's index number in the properties and then you could get it in the listener.
    The properties work like a hashmap.

  13. #13
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    How would i go about doing this? As in how would i get the button selected. I'm not entirely sure how to use the ClientProperties. I was just looking at them in the API and im a little fuzzy on how i would use them.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    To set the "location" property for a button:
    Java Code:
    final String TheLoc = "location";  // define constant that is used below
    
    button.setClientProperty(TheLoc, <HERE  THE VALUE YOU WANT SAVED>);  // save the value for this button
    
    ...
    aVariable = button.getClientProperty(TheLoc);  // get the value saved for this button
    I did not read the API doc for these methods. I could have spelled them incorrectly.

  15. #15
    Hollowsoul is offline Member
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

Similar Threads

  1. Our Checkers Game
    By Doefat in forum Java Applets
    Replies: 2
    Last Post: 01-21-2011, 11:30 AM
  2. Checkers Game problems
    By dyelax in forum New To Java
    Replies: 7
    Last Post: 12-29-2010, 02:57 AM
  3. Othello/Reversi flip checkers
    By chielt in forum Java Applets
    Replies: 10
    Last Post: 01-02-2010, 11:25 PM
  4. need advice for checkers game
    By javanoob73 in forum New To Java
    Replies: 13
    Last Post: 12-02-2009, 01:10 AM
  5. Checkers
    By evan42781 in forum New To Java
    Replies: 8
    Last Post: 05-08-2009, 04:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •