Commit a1dcab0d by Adam Gerber

Merge branch 'lab04a' Conflicts: src/lec04/glab/greg/CalendarTestHaleys.java

parents e5ac69e6 35227ade
Showing with 2501 additions and 24 deletions
package lec04.glab.autoboxing;
import java.util.ArrayList;
public class AutoBoxing {
/**
......@@ -8,17 +10,24 @@ public class AutoBoxing {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> intNums = new ArrayList<>();
intNums.add(98);
Integer intNum1 = new Integer(5);
Integer intNum2 = 5; // autoboxing; released in Java5
if(intNum1.equals(intNum2))
System.out.println("Integer values are the same: " + intNum1 + " == " + intNum2);
System.out.println("Integer values are the same: " + intNum1 + " equals " + intNum2);
else
System.out.println("Integer values are NOT the same: " + intNum1 + " != " + intNum2);
System.out.println("Integer values are NOT the same: " + intNum1 + " not equals " + intNum2);
int nNum1 = -126;
int nNum1 = -271;
//nNum1 = intNum1; //auto-unboxing
int nNum2 = new Integer(81); // auto-unboxing; released in Java5
......
package lec04.glab.construct_me;
import java.util.ArrayList;
/**
* Created by ag on 10/20/2014.
*/
public class Driver {
public static void main(String[] args) {
ArrayList<Person> perPeeps = new ArrayList<>();
perPeeps.add(new Person("Dan"));
perPeeps.add(new Person("Jason"));
perPeeps.add(PersonFactory.getInstance("Liz"));
perPeeps.add(new Person("Charlie"));
perPeeps.add(new Person("Charlie", 150));
perPeeps.add(new Person("Charlie", 160));
perPeeps.add(new Person("Charlie", 160));
for (Person perPeep : perPeeps) {
System.out.println(perPeep.toString());
}
System.out.println(perPeeps.get(6).equals(perPeeps.get(5)));
System.out.println(perPeeps.get(0).equals(perPeeps.get(3)));
Person perDan = perPeeps.get(0);
System.out.println(perDan.getName() + "'s wieght in kilos is " + Metric.lbsToKilos(perDan.getWeight()));
}
}
package lec04.glab.construct_me;
/**
* Created by ag on 10/20/2014.
*/
public class Metric {
private static final double CONVERSION = 2.2046;
public static double lbsToKilos(double dLb){
return dLb / CONVERSION;
}
public static double kilosToLbs(double dKilo){
return dKilo * CONVERSION;
}
}
package lec04.glab.construct_me;
/**
* Created by ag on 10/20/2014.
*/
public class Person {
//fields
private String mName;
private double mWeight;
//constructors
public Person() {
//funnel constuction to the one-arg constructor
this("Adam", 100.0);
}
public Person(String name) {
this(name, 100.0);
}
public Person(String name, double weight) {
mName = name;
mWeight = weight;
}
//getters and setters
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public double getWeight() {
return mWeight;
}
public void setWeight(double weight) {
mWeight = weight;
}
@Override
public String toString() {
return super.toString() + ": Person{" +
"mName='" + mName + '\'' +
", mWeight=" + mWeight +
'}';
}
@Override
public boolean equals(Object obj) {
return this.getName().equals(((Person) obj).getName()) &&
this.getWeight() == ((Person) obj).getWeight();
}
}
package lec04.glab.construct_me;
/**
* Created by ag on 10/20/2014.
*/
public class PersonFactory {
//can not instantiate this now
private PersonFactory() {
}
//factory methods
public static Person getInstance() {
return new Person("Adam", 100.0);
}
public static Person getInstance(String name){
return new Person(name, 100.0);
}
public static Person getInstance(String name, double dLb){
return new Person(name, dLb);
}
}
......@@ -10,7 +10,7 @@ import java.util.GregorianCalendar;
// instantiate a date with today's date called datToday
// create a datCount set at some earlier date
// while datCount.year < some arbitrary end year, loop and increment datCount by one day
// datCount = datHaleys (year, month, 1) loop and increment datCount by one day
//if dayOfMonth == 1
//print out year plus month
//print out the days of the week Sun Mon Tue etc.
......@@ -23,12 +23,12 @@ import java.util.GregorianCalendar;
//print out the day of the month e.g. 18
//if startDate == today
//put an asterix
//add an asterix
//else
//just put another space
//just add another space
//if the day of week is saturday
//print line break
//put one day to the startDate
//add one day to the startDate
*/
......@@ -48,27 +48,29 @@ public class CalendarTestHaleys {
final int SUNDAY = 1;
final int SATURDAY = 7;
final String[] SHORT_NAMES = new DateFormatSymbols().getShortWeekdays();
final String[] MONTH_NAMES = new DateFormatSymbols().getMonths();
final String[] SHORT_NAMES = new DateFormatSymbols().getShortWeekdays(); //MON TUE WED, etc
final String[] MONTH_NAMES = new DateFormatSymbols().getMonths(); //January February etc.
//create a data structure to hold greg dates and put them manually
ArrayList<GregorianCalendar> greHaleyAppears = new ArrayList<GregorianCalendar>();
greHaleyAppears.add(new GregorianCalendar(1617, 5, 3));
greHaleyAppears.add(new GregorianCalendar(1852, 1, 2));
greHaleyAppears.add(new GregorianCalendar(1987, 11, 21));
greHaleyAppears.add(new GregorianCalendar(2061, 6, 18));
//create a data structure to hold greg dates and add them manually
ArrayList<GregorianCalendar> greAppears = new ArrayList<GregorianCalendar>();
greAppears.add(new GregorianCalendar(1617, 5, 3));
greAppears.add(new GregorianCalendar(1852, 1, 2));
greAppears.add(new GregorianCalendar(1987, 11, 21));
greAppears.add(new GregorianCalendar(2061, 6, 18));
GregorianCalendar greCount;
for (GregorianCalendar greAppear : greHaleyAppears) {
for (GregorianCalendar greAppear : greAppears) {
greCount = new GregorianCalendar(greAppear.get(Calendar.YEAR), 0, 1);
greCount = new GregorianCalendar(greAppear.get(Calendar.YEAR), greAppear.get(Calendar.MONTH), 1);
//while
do {
// day 1-31 etc.
int nDay = greCount.get(Calendar.DAY_OF_MONTH);
......@@ -77,7 +79,7 @@ public class CalendarTestHaleys {
System.out.println();
System.out.println();
// print out Year + Month
// print out Year + Month : "1617 June" + println
System.out.println(" " + greCount.get(Calendar.YEAR) + " "
+ MONTH_NAMES[greCount.get(Calendar.MONTH)]);
......@@ -114,11 +116,10 @@ public class CalendarTestHaleys {
if (greCount.get(Calendar.DAY_OF_WEEK) == SATURDAY)
System.out.println();
//put one day
//add one day
greCount.add(Calendar.DAY_OF_MONTH, 1);
} while (greCount.get(Calendar.YEAR) == greAppear
.get(Calendar.YEAR));
} while (greCount.get(Calendar.MONTH) == greAppear.get(Calendar.MONTH) );
}//end foreach
}//end main
......
package lec04.glab.override;
import java.util.ArrayList;
/**
* Created by ag on 10/20/2014.
*/
//this is the example with 2A
public class AbstractDriver {
public static void main(String[] args) {
ArrayList<Vehicle> vehVehicles = new ArrayList<>();
vehVehicles.add(new Mazarati("Mazarati 5400 LS"));
vehVehicles.add(new Trike("Green Machine"));
for (Vehicle vehVehicle : vehVehicles) {
System.out.println(vehVehicle.getModel() + " : " + vehVehicle.reportNumWheels());
}
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
public class Ambulance extends Vehicle implements Drawable {
public Ambulance(String model) {
super(model);
}
@Override
public String reportNumWheels() {
return " has four medium firestone tires";
}
@Override
public void draw() {
String strOut =
" _______\n" +
" /______/\"=,\n" +
" [ | \"=, \"=,,\n" +
" [-----+----\"=,* )\n" +
" (_---_____---_)/\n" +
" (O) (O)";
System.out.println(strOut);
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
public class Bike extends Vehicle implements Drawable {
public Bike(String model) {
super(model);
}
@Override
public String reportNumWheels() {
return " has two mountain-bike tires with spokes";
}
@Override
public void draw() {
String strOut =
" __\n" +
" ,--. <__)\n" +
" `- |________7\n" +
" |`. |\\\n" +
" .--|. \\ |.\\--.\n" +
" / j \\ `.7__j__\\ \\\n" +
" | o | (o)____O) |\n" +
" \\ / J \\ /\n" +
" `---' `---' ";
System.out.println(strOut);
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
public class Corsair extends Vehicle implements Drawable {
public Corsair(String model) {
super(model);
}
@Override
public String reportNumWheels() {
return " has three huge tires";
}
@Override
public void draw() {
String strOut =
" ___________ \n" +
" | \n" +
" _ _|_ _ \n" +
" (_)-/ \\-(_) \n" +
" _ /\\___/\\ _ \n" +
" (_)_______________________( ( . ) )_______________________(_) \n" +
" \\_____/ \n" +
" ";
System.out.println(strOut);
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
public interface Drawable {
//by default this is public so the 'public' is optional
public void draw();
}
package lec04.glab.override;
import java.util.ArrayList;
/**
* Created by ag on 10/20/2014.
*/
//this is the example with 2B
public class InterfaceDriver {
public static void main(String[] args) {
ArrayList<Drawable> draVehicles = new ArrayList<>();
draVehicles.add(new Corsair("Corsair 89000 Luxury"));
draVehicles.add(new Bike("Swhinn magna"));
draVehicles.add(new Ambulance("GM 1200"));
for (Drawable draVehicle : draVehicles) {
System.out.println(draVehicle.getClass().getName() + " : " + ((Vehicle)draVehicle).getModel() + " : " + ((Vehicle)draVehicle).reportNumWheels() + "\n");
draVehicle.draw();
}
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
public class Mazarati extends Vehicle {
public Mazarati(String model) {
super(model);
}
@Override
public String reportNumWheels() {
return " has 4 21-inch Perelli all-weather tires";
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
public class Trike extends Vehicle {
public Trike(String model) {
super(model);
}
@Override
public String reportNumWheels() {
return " has three plastic wheels";
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
//an abstract class has zero or more abstract methods which MUST be overriden in subclasses
public abstract class Vehicle {
protected Vehicle(String model) {
mModel = model;
}
private String mModel;
public String getModel() {
return mModel;
}
public void setModel(String model) {
mModel = model;
}
//an abstract method
public abstract String reportNumWheels();
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
//this shows example 1 from exaplanation. We are overriding the toString method of Object because we want to.
public class Water {
//no point storing this state in each instance, it can be calculated based on temp.
public static enum State {solid, liquid, gas}
private double mTemp;
public Water(double temp) {
mTemp = temp;
}
public Water() {
this(67.5);
}
public double getTemp() {
return mTemp;
}
public void setTemp(double temp) {
mTemp = temp;
}
public State getState(){
if (mTemp <= 0){
return State.solid;
} else if (mTemp > 0 && mTemp < 100){
return State.liquid;
} else {
return State.gas;
}
}
@Override
public String toString() {
return getState() + " : Water{" +
"mTemp=" + mTemp +
'}';
}
}
package lec04.glab.override;
/**
* Created by ag on 10/20/2014.
*/
public class WaterDriver {
public static void main(String[] args) {
Water water = new Water(55.88);
System.out.println(water);
System.out.println(water.getState() == Water.State.solid ? "ice" : "not ice");
}
}
Overriding facilitates polymorphism. it allows you to express subclasses in different ways. For example, let's say we have a game with
a Sprite class. Each subclass of sprite has a draw method that expresses itself itself differently.
Here are the conditions for potentially overriding a method
1/ you want to override
2/ you must override. What do you mean, you must override? What are the conditions in which you must override?
A/ abstract classes
B/ interfaces.
\ No newline at end of file
......@@ -96,7 +96,7 @@ public class House implements Cloneable {
//no need to clone string because it is immutable
// comment out below line and see if clone works.
// comment out below line and see if clone works.
houClone.setDatInstantiated((Date)getDatInstantiated().clone());
return houClone;
......
package lec04.glab.tetris;
import java.awt.*;
public class Block
{
boolean oc; //occupied
Color color;
int row;
int col;
public Block ( boolean oc, Color color, int row, int col )
{
this.oc = oc;
this.color = color;
this.row = row;
this.col = col;
}
public int points()
{
return 100;
}
}
package lec04.glab.tetris;
import java.awt.*;
public class CitizenSnips extends Widget
{
public CitizenSnips ()
{
super();
initMe();
color = Color.cyan;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.tetris;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame extends JFrame
{
private JPanel contentPane;
private ImageIcon image1;
private ImageIcon image2;
private ImageIcon image3;
private BorderLayout borderLayout1 = new BorderLayout();
//Construct the frame
public Frame()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
image1 = new ImageIcon(Frame.class.getResource("openFile.png"));
image2 = new ImageIcon(Frame.class.getResource("closeFile.png"));
image3 = new ImageIcon(Frame.class.getResource("help.png"));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("rolling dice");
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e)
{
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e)
{
Frame_AboutBox dlg = new Frame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
jMenuFileExit_actionPerformed(null);
}
}
}
package lec04.glab.tetris;
import java.awt.*;
import java.awt.Frame;
import java.awt.event.*;
import javax.swing.*;
public class Frame_AboutBox extends JDialog implements ActionListener
{
private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel insetsPanel1 = new JPanel();
private JPanel insetsPanel2 = new JPanel();
private JPanel insetsPanel3 = new JPanel();
private JButton button1 = new JButton();
private JLabel imageLabel = new JLabel();
private JLabel label1 = new JLabel();
private JLabel label2 = new JLabel();
private JLabel label3 = new JLabel();
private JLabel label4 = new JLabel();
private ImageIcon image1 = new ImageIcon();
private BorderLayout borderLayout1 = new BorderLayout();
private BorderLayout borderLayout2 = new BorderLayout();
private FlowLayout flowLayout1 = new FlowLayout();
private GridLayout gridLayout1 = new GridLayout();
private String product = "";
private String version = "1.0";
private String copyright = "Copyright (c) 2004";
private String comments = "";
public Frame_AboutBox(java.awt.Frame parent)
{
super(parent);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Frame_AboutBox()
{
this(null);
}
//Component initialization
private void jbInit() throws Exception
{
image1 = new ImageIcon(Frame.class.getResource("about.png"));
imageLabel.setIcon(image1);
this.setTitle("About");
panel1.setLayout(borderLayout1);
panel2.setLayout(borderLayout2);
insetsPanel1.setLayout(flowLayout1);
insetsPanel2.setLayout(flowLayout1);
insetsPanel2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
gridLayout1.setRows(4);
gridLayout1.setColumns(1);
label1.setText(product);
label2.setText(version);
label3.setText(copyright);
label4.setText(comments);
insetsPanel3.setLayout(gridLayout1);
insetsPanel3.setBorder(BorderFactory.createEmptyBorder(10, 60, 10, 10));
button1.setText("Ok");
button1.addActionListener(this);
insetsPanel2.add(imageLabel, null);
panel2.add(insetsPanel2, BorderLayout.WEST);
this.getContentPane().add(panel1, null);
insetsPanel3.add(label1, null);
insetsPanel3.add(label2, null);
insetsPanel3.add(label3, null);
insetsPanel3.add(label4, null);
panel2.add(insetsPanel3, BorderLayout.CENTER);
insetsPanel1.add(button1, null);
panel1.add(insetsPanel1, BorderLayout.SOUTH);
panel1.add(panel2, BorderLayout.NORTH);
setResizable(true);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
cancel();
}
super.processWindowEvent(e);
}
//Close the dialog
void cancel()
{
dispose();
}
//Close the dialog on a button event
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
cancel();
}
}
}
package lec04.glab.tetris;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.applet.Applet;
import java.applet.AudioClip;
class GameSprite
{
// Fields:
//once set...these are global to the all objects of the class, since they are static
static int width; // Dimensions of the graphics area.
static int height;
Polygon shape; // Initial sprite shape, centered at the origin (0,0).
boolean active; // Active flag.
double angle; // Current angle of rotation.
double deltaAngle; // Amount to change the rotation angle.
double currentX, currentY; // Current position on screen.
double deltaX, deltaY; // Amount to change the screen position.
Polygon sprite; // Final location and shape of sprite after applying rotation
Color color;
// moving to screen position. Used for drawing on the screen
// in detecting collisions.
// Constructors:
public GameSprite ()
{
this.shape = new Polygon();
this.active = false;
this.angle = 0.0;
this.deltaAngle = 0.0;
this.currentX = 0.0;
this.currentY = 0.0;
this.deltaX = 0.0;
this.deltaY = 0.0;
this.sprite = new Polygon();
this.color = Color.white;
}
// Methods:
public void advance ()
{
// Update the rotation and position of the sprite based on the delta values. If the sprite
// moves off the edge of the screen, it is wrapped around to the other side.
this.angle += this.deltaAngle;
/*************************
if ( this.angle < 0 )
{
this.angle += 2 * Math.PI;
}
if ( this.angle > 2 * Math.PI )
{
this.angle -= 2 * Math.PI;
}
*************************/
this.currentX += this.deltaX;
if ( this.currentX < -width / 2 )
{
this.currentX += width;
}
if ( this.currentX > width / 2 )
{
this.currentX -= width;
}
this.currentY -= this.deltaY;
if ( this.currentY < -height / 2 )
{
this.currentY += height;
}
if ( this.currentY > height / 2 )
{
this.currentY -= height;
}
}
public void render ()
{
// Render the sprite's shape and location by rotating it's base shape and moving it to
// it's proper screen position.
this.sprite = new Polygon();
for ( int i = 0; i < this.shape.npoints; i++ )
{
this.sprite.addPoint( ( int )Math.round( this.shape.xpoints[i] *
Math.cos( this.angle ) +
this.shape.ypoints[i] *
Math.sin( this.angle ) ) +
( int )Math.round( this.currentX ) + width / 2,
( int )Math.round( this.shape.ypoints[i] *
Math.cos( this.angle ) -
this.shape.xpoints[i] *
Math.sin( this.angle ) ) +
( int )Math.round( this.currentY ) + height / 2 );
}
}
public boolean isColliding ( GameSprite s )
{
// Determine if one sprite overlaps with another, i.e., if any vertice
// of one sprite lands inside the other.
for ( int i = 0; i < s.sprite.npoints; i++ )
{
if ( this.sprite.inside( s.sprite.xpoints[i], s.sprite.ypoints[i] ) )
{
return true;
}
}
for ( int k = 0; k < this.sprite.npoints; k++ )
{
if ( s.sprite.inside( this.sprite.xpoints[k], this.sprite.ypoints[k] ) )
{
return true;
}
}
return false;
}
}
package lec04.glab.tetris;
import java.awt.*;
import java.util.*;
public class Grid
{
static final int ROWS = 20;
static final int COLS = 12;
//I could use Game.DIM here but I decided to give Grid its own constant since
//I use it alot
static final int DIM = 4;
//the 2-dimensional array of blocks
Block[][] block;
//an arrayList used to store accumulated blocks
ArrayList arOccupied;
//score object for keeping the score
Score score;
public Grid ()
{
//instantiate my members upon construction
block = new Block[ROWS][COLS];
initBlocks();
arOccupied = new ArrayList();
score = new Score();
}
//used extensively
synchronized public boolean requestLateral ( Widget w )
{
boolean[][] lt = new boolean[ROWS][COLS];
lt = w.getLights( w.or );
for ( int m = w.col; m < w.col + DIM; m++ )
{
for ( int k = w.row; k < w.row + DIM; k++ )
{
//if it tries to move beyond the bounds..no
if ( lt[k - w.row][m - w.col] &&
( m < 0 || m >= Grid.COLS || k >= Grid.ROWS ) )
{
return false;
}
//if it tries to move into a block that's occupied...no
if ( lt[k - w.row][m - w.col] && block[k][m].oc )
{
return false;
}
}
}
//ok
return true;
}
//used extensively
synchronized public boolean requestDown ( Widget w )
{
boolean[][] lt = new boolean[ROWS][COLS];
lt = w.getLights( w.or );
for ( int m = w.col; m < w.col + DIM; m++ )
{
for ( int k = w.row; k < w.row + DIM; k++ )
{
//if it goes out of bounds..no
if ( lt[k - w.row][m - w.col] &&
k >= Grid.ROWS )
{
return false;
}
//if it tries to move into a block that's occupied...no
if ( lt[k - w.row][m - w.col] && block[k][m].oc )
{
return false;
}
}
}
//is ok
return true;
}
//this method sets blocks to the grid
synchronized public void setBlocks ( Widget w )
{
boolean[][] lt = new boolean[ROWS][COLS];
lt = w.getLights( w.or );
Color c = w.color;
//first set all the blocks to black and unoccupied
for ( int k = 0; k < ROWS; k++ )
{
for ( int m = 0; m < COLS; m++ )
{
block[k][m] = new Block( false, Color.black, k, m );
}
}
//then draw the widget beginning with it's upperLeft coord
for ( int m = w.col; m < w.col + DIM; m++ )
{
for ( int k = w.row; k < w.row + DIM; k++ )
{
if ( lt[k - w.row][m - w.col] )
{
//occupied, color, row, column
block[k][m] = new Block( false, c, k - w.row, m - w.col );
}
}
}
//then the arrayList of occupied blocks
for ( int k = 0; k < arOccupied.size(); k++ )
{
//draw the array of blocks
Block b = ( Block )arOccupied.get( k );
block[b.row][b.col] = new Block( true, b.color, b.row, b.col );
}
}
synchronized public void checkFullRow ()
{
//to check for full row
LinkedList llRow = new LinkedList();
//to reposition blocks down
LinkedList llReposit = new LinkedList();
//start at the bottom of the arrayList until the top row is reached
//iterate from the back to the beginning...otherwise removing will
//not work properly
int m = Grid.ROWS - 1;
while ( m >= 0 )
{
//iterate through the arrayList
for ( int k = arOccupied.size() - 1; k >= 0; k-- )
{
Block b = ( Block )arOccupied.get( k );
if ( b.row == m )
{
//the integer value is the key
llRow.add( new Integer( k ) );
}
}
//if the size of the queue is equal to Grid.COLS
//then there is a complete row
if ( llRow.size() == Grid.COLS )
{
//this picks off the values of llRow from the beginning... removeFirst returns an 0bj
//uses that integer values stored in llRow to do it. Notice the FIFO or queue nature
//of the linked-list
while ( llRow.size() > 0 )
{
Block blk = ( Block )arOccupied.remove( ( ( Integer )llRow.
removeFirst() ).intValue() );
score.addScore( blk.points() );
}
//check for highScore
if ( score.getScore() > score.getHighScore() )
{
score.setHighScore( score.getScore() );
}
//this would be an appropriate time to check to increase difficulty,
//since I just increased the score.
score.checkThresh();
//ok, time to reposition the blocks
//I've used a seperate data structure called llReposit
for ( int p = arOccupied.size() - 1; p >= 0; p-- )
{
Block b = ( Block )arOccupied.get( p );
//if its above the row that is being deleted
if ( b.row < m )
{
arOccupied.remove( p ); //use the index number to delete it
b.row++; //increment the row (move it down)
llReposit.add( b ); //add this block to the temp linkedList
}
}
//now put the blocks back where they belong
//and clear llReposit
while ( llReposit.size() > 0 )
{
arOccupied.add( llReposit.removeLast() ); //removeLast both removes the obj and returns a ref to it.
}
//clear both linkedLists
llRow.clear();
llReposit.clear();
//no need to decrement the counter m, since we are going to check the next
//highest row (which has just been moved down into the current row). The only time the
//counter moves up then is when there is an incomplete row
}
//stop this method if there are no blocks in a row
else if ( llRow.size() == 0 )
{
return;
}
else //there is an incomplete row (neither empty nor full)
{
//reset the linkedList that tests for row
llRow.clear();
//decrement the row (move up one)
m--;
}
} //end while loop that tests iterates through rows
} //end meth checkFullRow
//this method checks for game over condition
synchronized public void topRow ()
{
for ( int k = 0; k < arOccupied.size(); k++ )
{
Block b = ( Block )arOccupied.get( k );
if ( b.row <= Widget.DIM )
{
//setting this flag to false... ends the game.
Game.bPlaying = false;
purgeGrid();
}
}
}//end meth topRow
synchronized public void addOccupied ( Widget w )
{
boolean[][] lt = new boolean[ROWS][COLS];
lt = w.getLights( w.or );
Color c = w.color;
// int row = w.row;
//int col = w.col;
for ( int m = w.col; m < w.col + DIM; m++ )
{
for ( int k = w.row; k < w.row + DIM; k++ )
{
if ( lt[k - w.row][m - w.col] )
{
arOccupied.add( new Block( true, c, k, m ) );
}
}
}
}
synchronized private void purgeGrid ()
{
initBlocks();
arOccupied.clear();
}
//just inits the blocks
synchronized public void initBlocks ()
{
//first set all the blocks to black and not occupied
for ( int k = 0; k < ROWS; k++ )
{
for ( int m = 0; m < COLS; m++ )
{
block[k][m] = new Block( false, Color.black, k, m );
}
}
} //end initBlocks
synchronized public Block[][] getBlocks ()
{
//return the grid blocks
return block;
}
} //end class
package lec04.glab.tetris;
import java.awt.*;
public class Long extends Widget
{
public Long ()
{
super();
initMe();
color = Color.red;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.tetris;
import java.awt.*;
public class LongOne extends Widget
{
public LongOne ()
{
super();
initMe();
color = Color.red;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = true;
lights[k][0][1] = true;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = true;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.tetris;
import java.awt.*;
public class Luigi extends Widget
{
public Luigi ()
{
super();
initMe();
color = Color.orange;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = true;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = true;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = true;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = false;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.tetris;
import javax.swing.UIManager;
import java.awt.*;
import java.awt.Frame;
public class MyApp
{
private boolean packFrame = false;
//Construct the application
public MyApp()
{
java.awt.Frame frame = new Frame();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
}
else
{
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
new MyApp();
}
}
package lec04.glab.tetris;
import java.awt.*;
public class Peanut extends Widget
{
public Peanut ()
{
super();
initMe();
color = Color.green;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = false;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = false;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = true;
}
}
}
}
package lec04.glab.tetris;
import java.awt.*;
public class Sancho extends Widget
{
public Sancho ()
{
super();
initMe();
color = Color.magenta;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = false;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.tetris;
public class Score
{
//score data
private int nScore;
private int nHighScore;
private int nThresh;
//constructor
public Score ()
{
nThresh = 2400;
nScore = 0;
nHighScore = 0;
}
public void checkThresh ()
{
if ( nScore > nThresh && Game.nAutoDelay > 30)
{
nThresh += Game.PNT; //add another 2400 to the threshold amount
Game.nAutoDelay -= 15; //make the game more difficult
}
}
//methods for access to scoring data
public int getScore ()
{
return nScore;
}
public void addScore ( int aNScore )
{
nScore += aNScore;
}
public void setScore ( int aNScore )
{
nScore = aNScore;
}
public int getHighScore ()
{
return nHighScore;
}
public void setHighScore ( int aNHighScore )
{
nHighScore = aNHighScore;
}
public int getThresh ()
{
return nThresh;
}
public void setThresh ( int aNThresh )
{
nThresh = aNThresh;
}
}
package lec04.glab.tetris;
import java.awt.*;
public class Slim extends Widget
{
public Slim ()
{
super();
initMe();
color = Color.red;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = true;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = true;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = false;
lights[k][1][3] = false;
lights[k][2][0] = true;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = true;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = true;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = true;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.tetris;
import java.awt.*;
public class Squared extends Widget
{
public Squared ()
{
super();
initMe();
color = Color.blue;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.tetris;
import java.awt.*;
public class Widget
{
final static int ORN = 4; //number of orientations
final static int DIM = 4; //dimension of bounding box
//all nonstatic members set to protected
int row;
int col;
int or; //orientation
Color color;
boolean[][][] lights; //every 4x4 widget has 4 orientations
public Widget ()
{
//set the col to random int upon instantiation
col = Game.r.nextInt(Grid.COLS - DIM);
//set the orientation to random int upon instantiation
or = Game.r.nextInt(ORN);
//row will be automatically set to zero (top)
lights = new boolean[ORN][DIM][DIM];
}//end constructor
public void moveDown ()
{
row++;
} //end moveDown
public void moveLeft ()
{
col--;
}//end moveLeft
public void moveRight ()
{
col++;
}//end moveRight
public void rotateMe ()
{
if ( or < 3 )
{
or++;
}
else
{
or = 0;
}
}//end rotateMe
//get the lights, given a particular orientation
public boolean[][] getLights ( int nOrientation )
{
boolean[][] b = new boolean[DIM][DIM];
for ( int j = 0; j < DIM; j++ )
{
for ( int k = 0; k < DIM; k++ )
{
b[j][k] = lights[nOrientation][j][k];
}
}
return b;
}//end getLights
//this mehtod does what it says.
public Widget duplicateWidget ()
{
Widget w = new Widget();
w.row = this.row;
w.col = this.col;
w.or = this.or;
w.color = this.color;
w.lights = this.lights;
return w;
}//end dup
}//end class
package lec04.glab.tetris;
import java.awt.*;
public class Zorro extends Widget
{
public Zorro ()
{
super();
initMe();
color = Color.yellow;
}
//initialize the boolean lights
public void initMe ()
{
for ( int k = 0; k < DIM; k++ ) //the lenght of the orientations
{
if ( k == 0 )
{
lights[k][0][0] = false;
lights[k][0][1] = true;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 1 )
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else if ( k == 2 )
{
lights[k][0][0] = false;
lights[k][0][1] = true;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = true;
lights[k][1][2] = true;
lights[k][1][3] = false;
lights[k][2][0] = false;
lights[k][2][1] = false;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
else
{
lights[k][0][0] = false;
lights[k][0][1] = false;
lights[k][0][2] = false;
lights[k][0][3] = false;
lights[k][1][0] = false;
lights[k][1][1] = false;
lights[k][1][2] = true;
lights[k][1][3] = true;
lights[k][2][0] = false;
lights[k][2][1] = true;
lights[k][2][2] = true;
lights[k][2][3] = false;
lights[k][3][0] = false;
lights[k][3][1] = false;
lights[k][3][2] = false;
lights[k][3][3] = false;
}
}
}
}
package lec04.glab.uml;
/**
* Created by ag on 10/20/2014.
*/
public class Condo {
}
package lec04.glab.uml;
/**
* Created by ag on 10/20/2014.
*/
public class Driver {
public void main() {
}
}
package lec04.glab.uml;
/**
* Created by ag on 10/20/2014.
*/
public class House {
private String mAddress;
public void display() {
}
}
package lec04.glab.uml;
/**
* Created by ag on 10/20/2014.
*/
public class RealEstateDriver {
public void main() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment