Circuit Negma

C++, C, VB.NET, PCB, Electronics, Circuit Design

Moving Square Game

Posted by Circuit Negma on February 11, 2006

 Trivial applet that displays a string

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Asn1 extends Applet implements ActionListener
// Constants
private  static  final  Color  defaultTopColor  =;
private  static  final  Color  defaultMiddleColor  =  Color.yellow;
private  static  final  Color  defaultBottomColor  =;
private  static  final  String  RED  =  “Red”;
private  static  final  String  YELLOW  =  “Yellow”;
private  static  final  String  BLUE  =  “Blue”;
private  static  final  String  RESET  =  “Reset”;
private  static  final  String  TOP_LIST_LABEL  =  “Top Row Colors”;
private  static  final  String  MIDDLE_LIST_LABEL  =  “Middle Row Colors”;
private  static  final  String  BOTTOM_LIST_LABEL  =  “Bottom Row Colors”;

// Variables
private Panel mainPanel, boardPanel, listsPanel;
private Board board;
private Button resetButton;
private List topRowColorList, middleRowColorList, bottomRowColorList;

public void init() {
// initializes applet

private List makeColorList(int itemNumber){
// Create a List with the 3 colour options; preselect the item with the given itemNumber
 List aList = new List(3, false);
 return aList;


private Panel makeListsPanel() {
// Make Labels and Lists for row colour choices, add them to a Panel
// The Panel uses GridLayout, 2 rows and 3 columns
 Label label1 = new Label(TOP_LIST_LABEL);
 Label label2 = new Label(MIDDLE_LIST_LABEL);
 Label label3 = new Label(BOTTOM_LIST_LABEL);
 this.topRowColorList = makeColorList(0);
 this.middleRowColorList = makeColorList(1);
 this.bottomRowColorList = makeColorList(2);
 this.listsPanel = new Panel(new GridLayout(2,3));
 return listsPanel;

private Panel makeBoardPanel() {
// Create a Board using the default row colours, create a reset Button;
// add this Board and Button to a Panel (which uses FlowLayout)
  this.board = new Board(defaultTopColor, defaultMiddleColor, defaultBottomColor);
  this.resetButton = new Button(RESET);
  this.boardPanel = new Panel(new FlowLayout());
  return boardPanel;

private Panel makeMainPanel() {
// create and return the Panel which contains the boardPanel and the ListsPanel
// The Panel uses GridLayout, 2 rows and 1 column
 this.mainPanel = new Panel(new GridLayout(2,1));
 return mainPanel;
private  void  addListeners() {
// adds the Applet as the ActionListener for the various components

private  Color  makeColor(String  colString) {
// Return a Color corresponding to the given String
            return defaultTopColor;
            return defaultMiddleColor;
            return defaultBottomColor;
            return null;

public  void  actionPerformed(ActionEvent  event) {
// Responds appropriately to whichever button has been clicked
// If Reset has been clicked, change row colours to the selected colours, else move the square that has been clicked
        String result = event.getActionCommand();
        if(result.equals(RESET)) {
            this.board.changeRowColor(1, makeColor(topRowColorList.getSelectedItem()));
            this.board.changeRowColor(2, makeColor(middleRowColorList.getSelectedItem()));
            this.board.changeRowColor(3, makeColor(bottomRowColorList.getSelectedItem()));
        } else {
}// end of Asn1 class


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Board {

// 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;

// static variables
private static Color emptySquareColor = Color.white;
private static final int defaultEmpty = 5;
private static final int boardSize = 3;

// define the class Board.
public Board(Color top, Color middle, Color bottom) {
  this.topRowColor = top;
  this.middleRowColor = middle;
  this.bottomRowColor = bottom;

// Instance methods
private Panel makeRowPanel( ) {
// Create a Panel. Set the LayoutManager to FlowLayout (centered). Return a reference to the Panel.
  Panel aPanel;
  aPanel = new Panel(new FlowLayout(FlowLayout.CENTER,0,0));
  return aPanel;

private Button makeSquare(String squareNumber) {
// Create and return a button with the given String as its Label
 Button aButton;
 aButton = new Button(squareNumber);
 return aButton;

public void paintSquares( ) {
// set the colours of the squares according to their row colours and set variable emptySquare to default

 this.emptySquare = defaultEmpty; 

private void fillRows( ) {
// Create the squares and their labels, add them to the appropriate rows
 this.square1 = makeSquare(“1”);
 this.square2 = makeSquare(“2”);
 this.square3 = makeSquare(“3”);
 this.square4 = makeSquare(“4”);
 this.square5 = makeSquare(String.valueOf(defaultEmpty));
 this.square6 = makeSquare(“6”);
 this.square7 = makeSquare(“7”);
 this.square8 = makeSquare(“8”);
 this.square9 = makeSquare(“9”);
 this.row1 = makeRowPanel();
 this.row2 = makeRowPanel();
 this.row3 = makeRowPanel();

private void assembleRows( ) {
// Arranges the rows into a single Panel (using GridLayout)
 this.boardPanel = new Panel(new GridLayout(3,1));

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
            return this.square1;
            return this.square2;
            return this.square3;
            return this.square4;
            return this.square5;
            return this.square6;
            return this.square7;
            return this.square8;
         return this.square9;
            return null;

public void changeRowColor(int rowNumber, Color newColor) {
// Change the current colour of the row with the given number
 if(rowNumber == 1)
            this.topRowColor = newColor;
        if(rowNumber == 2)
            this.middleRowColor = newColor;
            this.bottomRowColor = newColor;

private void changeSquares(Button coloredSquare, Button emptySquare) {
// Interchanges the colours of the given squares

public void move(String squareLabel) {
// If the move is valid, move the square with the given label into the empty space
 int number1 = Character.getNumericValue(squareLabel.charAt(0));
 int number2 = Math.abs(number1 – this.emptySquare);
 if (number2 == 1 || number2 == 3 ){
   this.changeSquares(getSquare(squareLabel), getSquare(String.valueOf(this.emptySquare)));
   this.emptySquare = number1;
   System.out.println(“the emptySquare is (” + this.emptySquare + “) so (” + this.emptySquare + “) is a valid move.”);}
   System.out.println(“this is invalid move (” + squareLabel + “)”);
} // end of Board class,

One Response to “Moving Square Game”

  1. My Home said

    This is very nice and informative post. I have bookmarked your site in order to find out your post in the future.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: