All pastes #753276 Raw Edit

Board.java

public java v1 · immutable
#753276 ·published 2007-10-29 02:45 UTC
rendered paste body
import java.awt.*;public class Board {			// Constants	private static final Color emptySquareColor = Color.white;	private static final int defaultEmpty = 5;	private static final int boardSize = 3;		// 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) {		// Initialize me with the given top, middle and bottom colors. 		//Fill my rows (i.e. Label my buttons with a number, color them and put them into rows.) 		//Make a panel out of each of my rows and then make a single panel out of all of my rows.		topRowColor = top;		middleRowColor = middle;		bottomRowColor = bottom;		row1 = makeRowPanel();		row2 = makeRowPanel();		row3 = makeRowPanel();		fillRows();		paintSquares();		assembleRows();	}		// Instance methods	private Panel makeRowPanel() {		// Create a Panel. Set the LayoutManager to FlowLayout (centered). Return a reference to the Panel.		Panel panel = new Panel();		panel.setLayout(new FlowLayout(1, 0, 0));		return panel;	}		private Button makeSquare(String squareNumber) {		// Create and return a button with the given String as its Label		Button button = new Button(squareNumber);		return button;	}		public void paintSquares() {		// Set the colours of the squares according to their row colours, set empty square to white, and set variable emptySquare to default		square1.setBackground(topRowColor);		square2.setBackground(topRowColor);		square3.setBackground(topRowColor);		square4.setBackground(middleRowColor);		square6.setBackground(middleRowColor);		square7.setBackground(bottomRowColor);		square8.setBackground(bottomRowColor);		square9.setBackground(bottomRowColor);		square5.setBackground(emptySquareColor);		emptySquare = defaultEmpty;	}		private void fillRows() {		// Create the squares and their labels, add them to the appropriate rows 1, 2 or 3		square1 = makeSquare("1");		square2 = makeSquare("2");		square3 = makeSquare("3");		square4 = makeSquare("4");		square5 = makeSquare("5");		square6 = makeSquare("6");		square7 = makeSquare("7");		square8 = makeSquare("8");		square9 = makeSquare("9");		row1.add(square1);		row1.add(square2);		row1.add(square3);		row2.add(square4);		row2.add(square5);		row2.add(square6);		row3.add(square7);		row3.add(square8);		row3.add(square9);	}		private void assembleRows() {		// Arranges the rows into a single Panel (using GridLayout)		boardPanel = new Panel();		boardPanel.setLayout(new GridLayout(boardSize, 1, 0, 0));		boardPanel.add(row1);		boardPanel.add(row2);		boardPanel.add(row3);	}		public Panel getBoard(){		// Return a reference to the Panel into which all 3 rows have been assembled.		return this.boardPanel;	}			public Button getSquare(String number) {		// Returns the square (i.e. Button) with the given label		if(number.equals("1"))            return this.square1;        if(number.equals("2"))            return this.square2;        if(number.equals("3"))            return this.square3;        if(number.equals("4"))            return this.square4;        if(number.equals("5"))            return this.square5;        if(number.equals("6"))            return this.square6;        if(number.equals("7"))            return this.square7;        if(number.equals("8"))            return this.square8;        else            return this.square9;	}		public void changeRowColor(int rowNumber, Color newColor) {		// Change the current colour of the row with the given number 1, 2, 3... to the given colour		if(rowNumber == 1)            topRowColor = newColor;        else        if(rowNumber == 2)            middleRowColor = newColor;        else            bottomRowColor = newColor;	}		private void changeSquares(Button coloredSquare, Button emptySquare) {		// Interchanges the colours of the given squares		emptySquare.setBackground(coloredSquare.getBackground());		coloredSquare.setBackground(emptySquareColor);	}		public void move(String squareLabel) {		// If the move is valid, move the square with the given label into the empty space		Button colorToEmpty = getSquare(squareLabel);        Button emptyToColor = getSquare(String.valueOf(emptySquare));        int squareNumber = Character.digit(squareLabel.charAt(0), 10);        int j = Math.abs(squareNumber - emptySquare);        if(j == 3 || j == 1 && (squareNumber + emptySquare) % 3 != 1)        {            emptySquare = squareNumber;            changeSquares(colorToEmpty, emptyToColor);        }	}} // end of Board class