You are on page 1of 3

import java.util.concurrent.locks.

ReentrantLock;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.image.Image;
/**
* purpose: class to create evil robots for the cstman game(MainGame)
* this class will keep track of robot movements on the board & maintain threads
* @author Keith Tcom
* cst148
*
*/
public class RobotWorker extends Task<Void>
{
//constants
//holds the evil robot pic
private static final Image ROBOT_IMG = new Image("images/evil_robot.gif");
//holds the delay between movements of robots
private static final int ROBOT_DELAY = 1000;
private
private
private
private
private
private
//holds
private
private
private

MainGame parent;//instance of the MainGame object


int oldRow = 0; //holds the row moved from
int oldCol = 0; //holds the col moved from
int moveRow; //holds the current row of the robot
int moveCol; //holds the current col of the robot
boolean keepRunning = true; //keep running until thread stops
the image that previously occupied the spot the robot moved to
Image oldImage = MainGame.DOT_IMG;//first pic will be a dot
int var;
ReentrantLock lock = new ReentrantLock();

/**
* Purpose: holds a boolean value to stop the robots thread
*/
public void stopRobot()
{
keepRunning = false;
}
/**
* Constructor: sets the current & old Row/col for robots being created
* also sets the MainGame object to play on
* @param parent MainGame object
* @param startingRow holds the starting row
* @param startingCol holds the starting col
*/
public RobotWorker( MainGame parent, int startingRow, int startingCol)
{
this.parent = parent;
moveRow = startingRow;
oldRow = startingRow;
moveCol = startingCol;
oldCol = startingCol;
}
/**
* purpose: used to stop robots from spawning on top of each other
* @return column of the robot

*/
public int getCol()
{
return this.moveCol;
}
/**
*purpose:
* @return
*/
public int
{
return
}

used to stop robots from spawning on top of each other


row of the robot
getRow()
this.moveRow;

/**
* purpose: starts up when each robot thread starts on game start
* moves robots one spot at a time up, down, left, or right
* checks for obstacles and player for game end
*/
@Override
protected Void call() throws Exception
{
while( keepRunning )
{
//move robot randomly
boolean validMove = false;
while( !validMove )
{
int colOrRow = (int) ( Math.random() * 2 );
//0 = col, 1 = row
if( colOrRow == 0 )
{
int upOrDown = (int) ( Math.random() * 2 );
if( upOrDown == 0 )
{
moveCol -= 1;
}
else
{
moveCol += 1;
}
}
else
{
int leftOrRight = (int) ( Math.random() * 2 );
if( leftOrRight == 0 )
{
moveRow -= 1;
}
else
{
moveRow += 1;
}
}
lock.lock();

/*
* Check if move is valid:
*
row is in range of 0 to max
*
col is in range of 0 to max
*
space is not a block
*
not the same spot as previous
*/
if( moveRow >= 0 && moveRow < MainGame.NUM_ROWS &&
moveCol >= 0 && moveCol < MainGame.NUM_COLS &&
parent.isValidRoboMove(moveRow, moveCol) &&
( moveRow != oldRow || moveCol != oldCol) )
{
validMove = true;
}
else
{
moveRow = oldRow;
moveCol = oldCol;
}
lock.unlock();
}
//Move the evil robot
final int locRow = moveRow;
final int locCol = moveCol;
Platform.runLater( new Runnable() {
/**
* Purpose: all GUI updating goes in here for robot movements
*/
@Override
public void run()
{
//holds random wait time between 100 and 150 milisec
var = (int) (Math.random() * 51) + 100;
//Set the old cell to what it was originally
parent.setCellImage(oldRow, oldCol, oldImage);
//check to keep overlapping robots from leaving robot pics
if( !parent.getCellImage(locRow, locCol).equals(ROBOT_IMG) )
{
//Store what was originally in the new row
oldImage = parent.getCellImage(locRow, locCol);
}
//sets cell to robot image
parent.setCellImage(locRow, locCol, ROBOT_IMG);
parent.checkForCSTManCaught(locRow, locCol);
oldRow = locRow;
oldCol = locCol;
}
});
Thread.sleep(var);
}
return null;
}
}

You might also like