Here is the actual code which i will be using, the move() method is where i need to carry ou this task. An idea where to start would be great. Havent used the % either, does it mean 2/3 remainder 2 or 3/2 remainder 1. Not really sure, the output is not illegal move but know move will take place. Thanks for the help.
When a legal move is made the square switch color.
thans again
import java.util.*;
import java.io.*;
import java.awt.*;
import java.applet.Applet;
public class Board extends Applet {
// Constants
private static final Color emptySquareColor = Color.white;
private static final int defaultEmpty = 5;
private static final int boardSize = 3;
private static final String int1 = "1";
private static final String int2 = "2";
private static final String int3 = "3";
private static final String int4 = "4";
private static final String int5 = "5";
private static final String int6 = "6";
private static final String int7 = "7";
private static final String int8 = "8";
private static final String int9 = "9";
private static final int intNumber2 = 2;
private static final int intNumber4 = 4;
// instance variables
private Panel row1, row2, row3, boardPanel;
private Button square1, square2, square3, square4, square5;
private Button square6, square7, square8, square9;
private Color topRowColor, middleRowColor, bottomRowColor;
private int emptySquare;
// Constructor
public Board(Color top, Color middle, Color bottom) {
this.topRowColor = top;
this.middleRowColor = middle;
this.bottomRowColor = bottom;
}
// Instance methods
private Panel makeRowPanel( LayoutManager lm) {
//Create a Panel. Set the LayoutManager to FlowLayout (centered). Return a reference to the Panel.
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
return p;
}
private Button makeSquare(String squareNumber) {
// Create and return a button with the given String as its Label
Button buttons = new Button(squareNumber);
buttons.setFont(new Font("Courier",Font.BOLD,15));
return buttons;
}
public void paintSquares( ) {
// Set the colours of the squares according to their row colours and set variable emptySquare to default
this.square1.setBackground(this.topRowColor);
this.square2.setBackground(this.topRowColor);
this.square3.setBackground(this.topRowColor);
this.square4.setBackground(this.middleRowColor);
this.square5.setBackground(this.emptySquareColor);
this.square6.setBackground(this.middleRowColor);
this.square7.setBackground(this.bottomRowColor);
this.square8.setBackground(this.bottomRowColor);
this.square9.setBackground(this.bottomRowColor);
}
private void fillRows( ) {
// Create the squares and their labels, add them to the appropriate rows 1, 2 or 3
this.row1 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row1.add(this.square1 = makeSquare(Board.int1));
this.row1.add(this.square2 = makeSquare(Board.int2));
this.row1.add(this.square3 = makeSquare(Board.int3));
this.row2 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row2.add(this.square4 = makeSquare(Board.int4));
this.row2.add(this.square5 = makeSquare(Board.int5));
this.row2.add(this.square6 = makeSquare(Board.int6));
this.row3 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row3.add(this.square7 = makeSquare(Board.int7));
this.row3.add(this.square8 = makeSquare(Board.int8));
this.row3.add(this.square9 = makeSquare(Board.int9));
paintSquares();
}
private void assembleRows( ) {
// Arranges the rows into a single Panel (using GridLayout)
fillRows();
this.boardPanel = new Panel(new GridLayout(3,1));
this.boardPanel.add(this.row1);
this.boardPanel.add(this.row2);
this.boardPanel.add(this.row3);
}
public Button getSquare(String number) {
// Returns the square (i.e. Button) with the given label
Button getButton = new Button(number);
return getButton;
}
public void changeRowColor(int rowNumber, Color newColor) {
// Change the current colour of the row with the given number 1, 2, 3....
if(rowNumber == 1){
this.square1.setBackground(newColor);
this.square2.setBackground(newColor);
this.square3.setBackground(newColor);
}
if(rowNumber == 2){
this.square4.setBackground(newColor);
this.square5.setBackground(emptySquareColor);
this.square6.setBackground(newColor);
}
if(rowNumber == 3){
this.square7.setBackground(newColor);
this.square8.setBackground(newColor);
this.square9.setBackground(newColor);
}
}
private void changeSquares(Button coloredSquare, Button emptySquare) {
// Interchanges the colours of the given squares
Color storage = coloredSquare.getBackground();
coloredSquare.setBackground(emptySquare.getBackground());
emptySquare.setBackground(storage);
}
public void move(String squareLabel) {
// If the move is valid, move the square with the given label into the empty space
int intString = Integer.parseInt(squareLabel.trim());
THIS IS WHERE I WOULD NEED TO PUT IT PLEASE AN IDEA WHERE TO START WOULD BE GREAT
}
public Panel getBoard( ){
// Return a reference to the Panel into which all 3 rows have been assembled.
assembleRows();
return this.boardPanel;
}
} // end of Board class