View Single Post
Old Apr 21st, 2005, 4:36 PM   #1
Necronaut
Newbie
 
Join Date: Apr 2005
Posts: 11
Rep Power: 0 Necronaut is on a distinguished road
(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 5:12 PM.
Necronaut is offline   Reply With Quote