Circuit Negma

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

Moving Square Game

Posted by Circuit Negma on February 11, 2006


 

Asn1.java
==========

/*
 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  =  Color.red;
private  static  final  Color  defaultMiddleColor  =  Color.yellow;
private  static  final  Color  defaultBottomColor  =  Color.blue;
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
 makeListsPanel();
 makeBoardPanel();
 makeMainPanel();
 addListeners();
}  

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);
 aList.add(RED);
 aList.add(YELLOW);
 aList.add(BLUE);
 aList.select(itemNumber);
 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));
 this.listsPanel.add(label1);
 this.listsPanel.add(label2);
 this.listsPanel.add(label3);
 this.listsPanel.add(this.topRowColorList);
 this.listsPanel.add(this.middleRowColorList);
 this.listsPanel.add(this.bottomRowColorList);
 add(listsPanel);
 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());
  this.boardPanel.add(this.board.getBoard());
  this.boardPanel.add(this.resetButton);
  add(boardPanel);
  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));
 this.mainPanel.add(this.boardPanel);
 this.mainPanel.add(this.listsPanel);
 add(mainPanel);
 return mainPanel;
}
private  void  addListeners() {
// adds the Applet as the ActionListener for the various components
  this.board.getSquare(“1”).addActionListener(this);
        this.board.getSquare(“2”).addActionListener(this);
        this.board.getSquare(“3”).addActionListener(this);
        this.board.getSquare(“4”).addActionListener(this);
        this.board.getSquare(“5”).addActionListener(this);
        this.board.getSquare(“6”).addActionListener(this);
        this.board.getSquare(“7”).addActionListener(this);
        this.board.getSquare(“8”).addActionListener(this);
        this.board.getSquare(“9”).addActionListener(this);
        this.resetButton.addActionListener(this);
        this.topRowColorList.addActionListener(this);
        this.middleRowColorList.addActionListener(this);
        this.bottomRowColorList.addActionListener(this);
}

private  Color  makeColor(String  colString) {
// Return a Color corresponding to the given String
 if(colString.equals(RED))
            return defaultTopColor;
        if(colString.equals(YELLOW))
            return defaultMiddleColor;
        if(colString.equals(BLUE))
            return defaultBottomColor;
        else
            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()));
            this.board.paintSquares();
        } else {
            board.move(result);
        }
 }
}// end of Asn1 class

____________________________________________________________________________________________

Board.java
===========

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;
  this.fillRows();
  this.assembleRows();
  this.paintSquares();
}

// 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.square1.setBackground(topRowColor);
 this.square2.setBackground(topRowColor);
 this.square3.setBackground(topRowColor);
 this.square4.setBackground(middleRowColor);
 this.square5.setBackground(emptySquareColor);
 this.square6.setBackground(middleRowColor);
 this.square7.setBackground(bottomRowColor);
 this.square8.setBackground(bottomRowColor);
 this.square9.setBackground(bottomRowColor);
 this.emptySquare = defaultEmpty; 
 System.out.println(“paintSquares”);
}

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.row1.add(this.square1);
 this.row1.add(this.square2);
 this.row1.add(this.square3);
 
 this.row2 = makeRowPanel();
 this.row2.add(this.square4);
 this.row2.add(this.square5);
 this.row2.add(this.square6);
 
 this.row3 = makeRowPanel();
 this.row3.add(this.square7);
 this.row3.add(this.square8);
 this.row3.add(this.square9);
 
}

private void assembleRows( ) {
// Arranges the rows into a single Panel (using GridLayout)
 this.boardPanel = new Panel(new GridLayout(3,1));
 this.boardPanel.add(this.row1);
 this.boardPanel.add(this.row2);
 this.boardPanel.add(this.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;
        if(number.equals(“9”))
         return this.square9;
        else
            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;
        else
        if(rowNumber == 2)
            this.middleRowColor = newColor;
        else
            this.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
 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.”);}
  else
   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:

WordPress.com Logo

You are commenting using your WordPress.com 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: