Results 1 to 15 of 15
Thread: Checkers Movement
- 07-02-2011, 01:44 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
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.
- 07-02-2011, 02:03 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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).
- 07-02-2011, 02:12 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
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];
}
}
- 07-02-2011, 02:12 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
Wow. Sorry didnt realize it would come out like that....
- 07-02-2011, 02:29 AM #5
Edit the post and wrap the code with code tags created by the # icon above the input area.
Also include the import statements.
- 07-02-2011, 02:34 AM #6
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
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]; } }
- 07-02-2011, 02:51 AM #7
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.
- 07-02-2011, 02:55 AM #8
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
It would be a human vs human game probably. So i have to add an action listener for all 64 buttons?
- 07-02-2011, 02:56 AM #9
No, just for the squares that are legal moves. The buttons for the non legal moves should be a different color and disabled.
- 07-02-2011, 02:59 AM #10
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
Okay, this helps tremendously thank you very much :)
- 08-28-2011, 07:27 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
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?
- 08-28-2011, 07:34 PM #12
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.
- 08-28-2011, 07:48 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
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.
- 08-28-2011, 08:02 PM #14
To set the "location" property for a button:
I did not read the API doc for these methods. I could have spelled them incorrectly.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
- 08-28-2011, 08:12 PM #15
Member
- Join Date
- Jul 2011
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
Our Checkers Game
By Doefat in forum Java AppletsReplies: 2Last Post: 01-21-2011, 11:30 AM -
Checkers Game problems
By dyelax in forum New To JavaReplies: 7Last Post: 12-29-2010, 02:57 AM -
Othello/Reversi flip checkers
By chielt in forum Java AppletsReplies: 10Last Post: 01-02-2010, 11:25 PM -
need advice for checkers game
By javanoob73 in forum New To JavaReplies: 13Last Post: 12-02-2009, 01:10 AM -
Checkers
By evan42781 in forum New To JavaReplies: 8Last Post: 05-08-2009, 04:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks