![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 11
Rep Power: 0
![]() |
(Nearly) a draughts program
Hello peoples
I'm working on a draughts program on a really tight schedule so i'm trying to keep everything fairly simple. I just can't decide what the best way would be to assign places to the pieces. I'm thinking about implementing the pieces as Labels with ImagIcons. Is that a good way to go? I want to create a simple, almost 2 player, sort of interface and then worry about implementing the AI at a slightly later stage. MyBoard.java import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyBoard extends JFrame implements ActionListener
{
private static int BOARD_SIZE;
private BoardButton[][] boardSquare = new BoardButton[BOARD_SIZE][BOARD_SIZE];
//private int[][] p1Places = { {0, 1}, {0, 3}, {0, 5}, {0, 7}, {1, 0}, {1, 2}, {1, 4}, {1, 6}, {2, 1}, {2, 3}, {2, 5}, {2, 7} };
//private int[][] p2Places = { {5, 0}, {5 ,2}, {5, 4}, {5, 6}, {6, 1}, {6, 3}, {6, 5}, {6, 7}, {7, 0}, {7, 2}, {7, 4}, {7, 6} };
//OK ignore the above, I was just experimenting with assigning places
//Arrays in java suck really bad!
//private static boolean[] p1Places = {false, true, false, true, false, true, false, true}
public MyBoard()
{
setTitle("Draughts");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cntnr = this.getContentPane();
cntnr.setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
for (int row = 0; row < BOARD_SIZE; ++row)
{
for (int col = 0; col < BOARD_SIZE; ++col)
{
boardSquare[row][col] = new BoardButton(row, col);
boardSquare[row][col].setPreferredSize(new Dimension(50,50));
boardSquare[row][col].addActionListener(this);
boardSquare[row][col].setBackground(setColour(row, col));
cntnr.add(boardSquare[row][col]);
resetPieces();
}
}
this.setSize(400, 450);
this.setVisible(true);
}
private Color setColour(int x, int y)
{
Color c;
if ((x + y) % 2 == 1)
{ c = Color.black; }
else
{ c = Color.red; }
return c;
}
private void resetBoard()
{
for (int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{
boardSquare[x][y].setBackground(setColour(x, y));
}
}
}
private void resetPieces()
{
for (int x = 0; x < BOARD_SIZE; x++)
{
for (int y = 0; y < BOARD_SIZE; y++)
{/*
if (x != boardSquare[x][y].getRow()) //What to do????
{
System.out.println("Ello");
}
*/}
}
}
BoardButton lastButton;
public void actionPerformed(ActionEvent evt)
{
resetBoard();
lastButton = (BoardButton)evt.getSource();
if ((lastButton.getRow() + lastButton.getCol()) % 2 == 1)
{
lastButton.setBackground(Color.magenta);
}
System.out.println("\nButton clicked properties: \n" + evt.getSource().toString());
}
public static void main(String[] args)
{
BOARD_SIZE = 8;
MyBoard DBoard = new MyBoard();
}
}BoardButton.java import javax.swing.JButton;
public class BoardButton extends JButton
{
int row, col;
public BoardButton()
{
super();
}
public BoardButton(int row, int col)
{
super();
setButtonCoords(row, col);
}
public void setButtonCoords(int row, int col)
{
this.row = row;
this.col = col;
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
public String toString()
{
return "Row=" + row + ",Col=" + col + " \n" + super.toString();
}
}Its not written particularly well here (nor very consistently) but i'm hoping for some feedback on how I can clean this up. I'm going to stick with keeping the baord at 8*8 :-) The deadline is fairly close so i'm gonna just keep it nice and straightforward. Any suggestions will be very much appreciated Last edited by Necronaut; Apr 21st, 2005 at 6:12 PM. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
well, you can do it with a label, or a image. i think in my (uncompleted) chess game i used labels. what happened then was that when i click on the piece i want to move, i'd draw a border around the jpanel(i used jpanels instead of buttons), get what's on the panel and the put that on the place i want it to go. then all you do is replace the the label you don't need with an empty label.
you'll also need error control, to make sure the user doesn't make any illegal moves, but worry about that when you get the program to make moves. it should go easier with draughts than with chess, because all the pieces only move diagonally.hope i gave you some usefull advice. if not, come back and tune me. good luck.
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2005
Posts: 11
Rep Power: 0
![]() |
OK.
I've changed my mind. I'm going for a separate Peice class and have a piece painted onto the button. Looking at the API it doesnt seem that JButton has a paintComponent method - Is there a way to draw a circle on a button? |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
you mean something like this?
import java.awt.Dimension;
import javax.swing.*;
public class ImageButton extends JButton
{
public static void main( String[] args )
{
JFrame frame = new JFrame();
ImageIcon image = new ImageIcon( "c:\\pic.gif" );
JPanel panel = new JPanel();
panel.add( new ImageButton( image ) );
frame.getContentPane().add( panel );
frame.setSize( 200, 200 );
frame.setVisible( true );
}
public ImageButton( ImageIcon im )
{
super( im );
setPreferredSize( new Dimension( 50, 50 ) );
}
}that's the first thing that jumped to mind. if you compile and run that, then you must obviously have a "pic.gif" in your c:. ![]()
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Apr 2005
Posts: 11
Rep Power: 0
![]() |
I've scrapped the idea of using images, now i've painted pieces on as icons. The problem i'm having now is coming up with an algorithm that will put the pieces in the right squares. I tried using an array of booleans but I kept getting ArrayOutOfBounds Exceptions (or something of the sort)
Here is my code now. It works as it is but its kinda yucky :-p Piece.java import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Piece implements Icon
{
public int player;
protected Dimension dim = new Dimension(45, 45);
public boolean isKing;
public Piece(int player)
{
this.player = player;
this.isKing = false;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
if (player == 1)
{
g.setColor(Color.red);
}
else
{
g.setColor(Color.white);
}
g.setColor(Color.white);
g.fillOval(x, x, dim.width, dim.height);
}
public int getIconWidth()
{
return dim.width;
}
public int getIconHeight()
{
return dim.height;
}
public void setKing()
{
this.isKing = true;
}
}MyBoard.java import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyBoard extends JFrame implements ActionListener
{
private static int BOARD_SIZE;
private BoardButton[][] boardSquare = new BoardButton[BOARD_SIZE][BOARD_SIZE];
//private int[][] p1Places = { {0, 1}, {0, 3}, {0, 5}, {0, 7}, {1, 0}, {1, 2}, {1, 4}, {1, 6}, {2, 1}, {2, 3}, {2, 5}, {2, 7} };
//private int[][] p2Places = { {5, 0}, {5 ,2}, {5, 4}, {5, 6}, {6, 1}, {6, 3}, {6, 5}, {6, 7}, {7, 0}, {7, 2}, {7, 4}, {7, 6} };
// col*row (3*8)
private static boolean[][] p1Places = { { false, true, false, true, false, true, false, true },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true } };
public MyBoard()
{
setTitle("Draughts");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cntnr = this.getContentPane();
cntnr.setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
for (int col = 0; col < BOARD_SIZE; ++col)
{
for (int row = 0; row < BOARD_SIZE; ++row)
{
boardSquare[row][col] = new BoardButton(row, col);
boardSquare[row][col].setPreferredSize(new Dimension(50,50));
boardSquare[row][col].addActionListener(this);
boardSquare[row][col].setBackground(setColour(row, col));
cntnr.add(boardSquare[row][col]);
}
}
resetPieces();
this.setSize(400, 450);
this.setVisible(true);
}
private Color setColour(int x, int y)
{
Color c;
// Stu Rules
if ((x + y) % 2 == 1)
{ c = Color.black; }
else
{ c = Color.red; }
return c;
}
private void resetBoard()
{
for (int y = 0; y < BOARD_SIZE; y++)
{
for (int x = 0; x < BOARD_SIZE; x++)
{
boardSquare[x][y].setBackground(setColour(x, y));
}
}
}
private void resetPieces()// Dont put resetBoard and resetPieces together - see actionPerformed
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < BOARD_SIZE; x++) // For the time being
{
if ((x + y) % 2 == 1) // This works for now
// but i'm trying to get this to work: if (p1Places[x][y])
{
putPiece(boardSquare[x][y], 1); //put pieces in right places
}
}
}
}
public void putPiece(BoardButton button, int player)
{
button.setIcon(new Piece(player));
}
//clearPiece
private BoardButton buttonClicked;
public void actionPerformed(ActionEvent evt)
{
resetBoard();
buttonClicked = (BoardButton)evt.getSource();
if ((buttonClicked.getRow() + buttonClicked.getCol()) % 2 == 1)
{
buttonClicked.setBackground(Color.magenta);
}
System.out.println("\nButton clicked properties: \n" + evt.getSource().toString());
}
public static void main(String[] args)
{
BOARD_SIZE = 8;
MyBoard DBoard = new MyBoard();
}
}BoardButton.java import javax.swing.JButton;
public class BoardButton extends JButton
{
protected int row, col;
protected boolean occupied;
public BoardButton()
{
super();
}
public BoardButton(int row, int col)
{
super();
this.row = row;
this.col = col;
}
public boolean isOccupied()
{
return occupied;
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
public String toString()
{
return "Row=" + row + ",Col=" + col + " \n" + super.toString();
}
}I'm trying to use the "p1Places"" array to determine whether a piece should be placed in a particualar square. is there a way to do this that is independant of the baord size? if not, i'm happy with keeping the board 8*8 My project supervisor wants to see good java code so i'd love some suggestions on how i can make it look a bit prettier Thanks ![]() |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
just keeping it neat and tabbed should be proof enough of pretty code, and isnt draughts 8*8 anyways?
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Apr 2005
Posts: 11
Rep Power: 0
![]() |
What I meant by pretty code was to make sure everything was kept 'object orientated'. so all methods would belong to the correct classes and all the different behaviours would be encapsulated by their own classes :-)
As for making the board anything other than 8*8 would just be a novelty in the program, giving the user the opportunity to play a more difficult game by having a greater number of pieces. By the looks of things i'm going to have to scrap that :-( |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
um...
if (p1Places[y][x]) note the x and y have swapped around. the reason you were going out of bounds, was because the loop using x as a variable is running quicker than the one using y. x was going up to 7 and y to 2. the x loop was causing the error. ![]() have you started moving pieces around?
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2005
Posts: 11
Rep Power: 0
![]() |
Hey guys
Thanks for all the help and suggestions. But sadly this project has to be laid to rest for a while now because i've got exams now for the next few weeks so I just won't have the time to finish this. Hopefully, after they're out of the way I'll get back to this just for the sake of finishing it off. Thanks again :-) |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|