@@ -60,6 +60,12 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
* The number of microseconds between Brain moves
*/
privatestaticfinallongBRAIN_WAIT_US=1000000;
/**
* The amount of time to wait between games, to allow for existing elements to settle.
*/
privatestaticfinallongTIME_BETWEEN_GAMES_MS=1000;
/**
* The number of empty rows on the top of the game.
*/
@@ -105,7 +111,16 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
privateSelfAwareCircle[][]circles;
privateStringhighScoreName=null;// the name to use for the high score
staticfinalRandomrandGen=newRandom();// package-private random number generator used for the entire package (i.e., repeatedly seeding with the same value produces the same randomness).
staticprivateintnumClicks=1;// Must be a positive integer; counts the number of clicks on circles since the last time the board was reset.
/**
* Counts the number of clicks on circles since the last time the board was reset.
* @requires must be a positive integer; c
*/
staticprivateintnumClicks=1;
/**
* true iff the game is over (i.e., not running)
*/
booleangameOver=false;
/**
* An instance of the SGE GUI.
*/
@@ -254,6 +269,9 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
* (i.e. every circle is surrounded by cleared circles or circles of a different color)
*/
publicbooleangameOverQ(){
if(gameOver)
returntrue;
for(intxx=0;xx<width;xx++)
for(intyy=0;yy<height;yy++){
if(!circles[xx][yy].isCleared()){
@@ -275,7 +293,6 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
*/
@Override
publicvoidmakeMove(intxx,intyy){
System.out.println(newDate()+": Moving to "+xx+", "+yy);
circles[xx][yy].mouseEntered(null);// pretend the mouse was pressed at location (xx,yy)
Timert=newTimer();
TimerTasktt=newTimerTask(){
@@ -452,7 +469,6 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
* As {@link #numClicks} gets larger, circles come more often and drop faster.
*/
publicvoidrestartTetrising(){
System.out.println("Restarting tetrising: ");
// cancel the old schedules
cancelTetrisingSchedules();
@@ -492,6 +508,9 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
// cancel tetrising schedules
numClicks=1;// reset the number of circle clicks in the system
cancelTetrisingSchedules();
// reset gameOver
gameOver=false;
}
/*
@@ -519,11 +538,9 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
/*
* Called to request a reshifting of the board (as necessary).
* This should happen if some circles are rendered "clear"ed
* @return true iff the shift succeeded without the game ending,
* and false if the game ended as a result of the shift.
*/
finalbooleanshiftCircles(){
finalvoidshiftCircles(){
/* start at the bottom and move up ... all cleared circles are
* removed, with upper circles falling into their positions;
* if a column is totally empty, then its rightmost columns
@@ -533,40 +550,40 @@ public class SameGameTris extends GUI implements ActionListener, ItemListener {
GUIlock.lock();// acquire a lock
try{
// 1. SHIFT VERTICALLY
for(intxx=0;xx<width;xx++){
intfirstClr=height-1;// the lowest cleared entry in the column
intfirstFull=
height-1;// the lowest uncleared entry in the column that has not yet been processed
booleanmoveOn=false;// set to true in order to move on to the next column
while(!moveOn){
// find the lowest clear entry in the column (if it exists)
try{
while(!circles[xx][firstClr].isCleared())
firstClr--;
}catch(ArrayIndexOutOfBoundsExceptione){
moveOn=true;
continue;// i.e. no cleared circle found in this column --- go to the next column
}
if(firstFull>firstClr)
firstFull=firstClr;// only move items "down" the column
// find the lowest non-cleared entry in the column (if it exists)
try{
while(circles[xx][firstFull].isCleared())
firstFull--;
}catch(ArrayIndexOutOfBoundsExceptione){
moveOn=true;
continue;// i.e. the whole column is clear --- for now, go to the next column