Files
GTSchoolShit/CS1322/p6/PacmanEngine.java
2025-06-07 01:59:34 -04:00

304 lines
8.1 KiB
Java

/**
* <PRE>
* PacmanEngine.java
*
* Revisions: 1.0 Nov. 25, 2002
* Created the PacmanEngine class
* 1.1 Nov. 26, 2002
* Compiled, Finished
* 1.2 Nov. 26, 2002
* Corrected Movement problem, AI still seems wacky, but can't
* find the problem
*
* </PRE>
*
* Collaboration statement:
* I worked on this assignment with Roland Krystian Alberciak
*
* @author
*<A HREF="mailto:gtg142g@mail.gatech.edu">Roland Krystian Alberciak</A>
*@author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
* @version Version 1.2, Nov. 26, 2002
*/
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class PacmanEngine implements ActionListener, KeyListener{
/**
*Timers to fire every x.ms and do whatever
*/
private Timer GhostTimer;//fire at P6Constants.GHOST_INTERVAL ms
private Timer PacmanTimer;//fire at P6Constants.PACMAN_INTERVAL ms
/**
*globals to hold data for engine use
*/
private GameBoard board;
private PacmanFrame pf;
private Ghost[] ghosts;
private Pacman pacman;
/**
*Constructor
*@param pf, the PacmanFrame instance
*@param gb, the GameBoard instance
*/
public PacmanEngine(PacmanFrame pf, GameBoard gb){
board = gb;
this.pf = pf;
//gb = pf.getGB();
pf.addKeyListener(this);
//PacmanFileReader pfr = new PacmanFileReader(P6Constants.BOARD_FILE);
GhostTimer = new Timer(P6Constants.GHOST_INTERVAL,this);
PacmanTimer = new Timer(P6Constants.PACMAN_INTERVAL,this);
ghosts = board.getGhosts();
pacman = board.getPacman();
}
/**
*@param ae, the action performed
*Handles what ae asks for
*/
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == PacmanTimer){
isLegalMove(pacman,pacman.getDirection());
}
else if(ae.getSource() == GhostTimer){
for(int i=0; i<4; i++){
int[] array = ghosts[i].getMove(pacman.getX(),pacman.getY());
boolean legal = false;
int z=0;
do{
legal = isLegalMove(ghosts[z],array[z]);
z++;
}while(!legal && z<4);
}//end for()
}//end else if()
GamePiece[][] temp = board.getPieceArray();
//check pacman eat nibble
if(temp[pacman.getX()][pacman.getY()] instanceof FloorPiece){
((FloorPiece)temp[pacman.getX()][pacman.getY()]).setNibble(false);
}
if(temp[pacman.getX()][pacman.getY()] instanceof FloorPiece){
if(((FloorPiece)temp[pacman.getX()][pacman.getY()]).getSpecial()){
((FloorPiece)temp[pacman.getX()][pacman.getY()]).setSpecial(false);
for(int i=0; i<4; i++){
ghosts[i].runAway();
}
}
}
//check for game over
boolean isDone = true;
for(int x=0; x<temp.length; x++){
for(int y=0; y<temp[x].length; y++){
if(temp[x][y] instanceof FloorPiece
&& ((FloorPiece)temp[x][y]).getNibble()){
isDone=false;
}
}
}
if(isDone){
GhostTimer.stop();
PacmanTimer.stop();
JOptionPane.showMessageDialog(pf,new String("You win!"),
new String("Congrats"),1);
}
//check ghost and pacman same box
for(int i=0; i<4; i++){
if(pacman.getX() == ghosts[i].getX()
&& pacman.getY() == ghosts[i].getY()){
if(ghosts[i].getRunAwayFlag()){
ghosts[i].resetGhost();
}
else{//game over time
GhostTimer.stop();
PacmanTimer.stop();
JOptionPane.showMessageDialog(pf,new String("You Lose!"),
new String("Game Over"),1);
}
}
}
board.repaint();
}//end actionPerformed()
/**
*check legality of a move and make the move
*@return true if the move is possible
*@param mp, the piece to check
*@param direction, the direction to move
*/
private boolean isLegalMove(MoveablePiece mp, int direction){
try{
switch(direction){
case P6Constants.NORTH:
if(board.getPieceArray()[mp.getX()][mp.getY()-1]
instanceof FloorPiece){
mp.move(P6Constants.NORTH);
return true;
}
break;
case P6Constants.SOUTH:
if(board.getPieceArray()[mp.getX()][mp.getY()+1]
instanceof FloorPiece){
mp.move(P6Constants.SOUTH);
return true;
}
break;
case P6Constants.EAST:
if(board.getPieceArray()[mp.getX()+1][mp.getY()]
instanceof FloorPiece){
mp.move(P6Constants.EAST);
return true;
}
break;
case P6Constants.WEST:
if(board.getPieceArray()[mp.getX()-1][mp.getY()]
instanceof FloorPiece){
mp.move(P6Constants.WEST);
return true;
}
break;
};//end switch()
return false;
}catch(ArrayIndexOutOfBoundsException aiobe){return false;}
catch(NullPointerException npe){return false;}
}
/**
*Java's Pathetic Handler class
*@param ke, the key pressed
*/
public void keyPressed(KeyEvent ke){
switch(ke.getKeyCode()){
case KeyEvent.VK_S:
PacmanTimer.start();
GhostTimer.start();
System.out.println("Pressed s");
break;
case KeyEvent.VK_P:
GhostTimer.stop();
PacmanTimer.stop();
System.out.println("Pressed p");
break;
case KeyEvent.VK_UP:
pacman.setDirection(P6Constants.NORTH);
System.out.println("Pressed up");
break;
case KeyEvent.VK_DOWN:
pacman.setDirection(P6Constants.SOUTH);
System.out.println("Pressed down");
break;
case KeyEvent.VK_LEFT:
pacman.setDirection(P6Constants.WEST);
System.out.println("Pressed left");
break;
case KeyEvent.VK_RIGHT:
pacman.setDirection(P6Constants.EAST);
System.out.println("Pressed right");
break;
}
}//end keyPressed
/**
*Added to satisfy the requirements of the KeyListener
*/
public void keyTyped(KeyEvent ke){
/*switch(ke.getKeyCode()){
case KeyEvent.VK_S:
PacmanTimer.start();
GhostTimer.start();
System.out.println("Pressed s");
break;
case KeyEvent.VK_P:
GhostTimer.stop();
PacmanTimer.stop();
System.out.println("Pressed p");
break;
case KeyEvent.VK_UP:
pacman.setDirection(P6Constants.NORTH);
System.out.println("Pressed up");
break;
case KeyEvent.VK_DOWN:
pacman.setDirection(P6Constants.SOUTH);
System.out.println("Pressed down");
break;
case KeyEvent.VK_LEFT:
pacman.setDirection(P6Constants.WEST);
System.out.println("Pressed left");
break;
case KeyEvent.VK_RIGHT:
pacman.setDirection(P6Constants.EAST);
System.out.println("Pressed right");
break;
}*/
}
/**
*Added to satisfy the requirements of the KeyListener
*/
public void keyReleased(KeyEvent ke){
/*switch(ke.getKeyCode()){
case KeyEvent.VK_S:
PacmanTimer.start();
GhostTimer.start();
System.out.println("Pressed s");
break;
case KeyEvent.VK_P:
GhostTimer.stop();
PacmanTimer.stop();
System.out.println("Pressed p");
break;
case KeyEvent.VK_UP:
pacman.setDirection(P6Constants.NORTH);
System.out.println("Pressed up");
break;
case KeyEvent.VK_DOWN:
pacman.setDirection(P6Constants.SOUTH);
System.out.println("Pressed down");
break;
case KeyEvent.VK_LEFT:
pacman.setDirection(P6Constants.WEST);
System.out.println("Pressed left");
break;
case KeyEvent.VK_RIGHT:
pacman.setDirection(P6Constants.EAST);
System.out.println("Pressed right");
break;
}*/
}
/**
* Debugging main for class PacmanEngine.
* This method will rigorously test my code.
*
* <br><br>
* @param args a String array of command line arguments.
*/
public static void main(String[] args) {
PacmanFrame fs = new PacmanFrame();
PacmanEngine pe =
//new PacmanEngine(new PacmanFrame(),new GameBoard("board_1.txt"));
new PacmanEngine(fs,fs.getGB());
}// end of main(String[] args)
}// end of class PacmanEngine