0 Replies - 517 Views - Last Post: 16 February 2010 - 10:54 PM Rate Topic: -----

#1 sub432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-October 09

Text-Based game implementation

Posted 16 February 2010 - 10:54 PM

Hi I've written code to implement a text-based version of Pacman but I'm having trouble with the lose condition. My current code is:
World.java
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Random;

public class World {
	private Cell [][] world;
	private int pRow, pCol;
	private int gRow, gCol;
	private int points = 0;
	private int size=10;
	private int pellets = size * size - 2;
//Constructor for World class.
	public World() {
		createWorld();
	}
	//Creates the Pacman World out of the Cell class. Forms a 10X10 2D array
	private void createWorld(){
		world = new Cell [size][size];
		for (int i=0; i<size; i++) {
			for (int j=0; j<size; j++){
				world [i][j] = new Cell();

			}
		}
		Random r = new Random();
		//Randomly finds a spot for Pacmon and the Ghost and places them on the board
		pRow = r.nextInt(size);
		pCol = r.nextInt(size);
		gRow = r.nextInt(size);
		gCol = r.nextInt(size);
		world[gRow][gCol].set('G');
		world[pRow][pCol].set('P');
	}
	//Prints the board so that the user can see it
	public void printBoard() {
		for (int i=0; i<size; i++) {
			for (int j=0; j<size; j++){
				System.out.print(world[i][j].get());
			}
			System.out.println();
			
		}
		System.out.println("Points: " + points);
	}
	//Input method so that the user can tell pacman what to do. This was taken from one of the modules
	private String input(){
		System.out.print ("Enter your next move: ");
		String input_line = "";
	    try {
	      InputStreamReader isr = new InputStreamReader (System.in);
	      LineNumberReader lr = new LineNumberReader (isr);

	      input_line = lr.readLine ();

	    }
	    catch (IOException e) {
	      // If there was a problem...
	      System.out.println (e);
	   

	}
	    return input_line;


	}
	//Checks if all the pellets were "eaten" and declares a victory if so.
	private boolean checkWin() {
		if (points == pellets){
			return true;
		}
		return false;
	}
	//Checks if a move is valid and not going off of the board.
	private boolean checkMove(int row, int col) {
		if (row<0 || row > size-1 || col < 0 || col > size-1) {
			return false;
		}
		return true;
	}
	//Randomly moves the ghost in a direction selected by the number generator. If the Ghost happens upon Pacman, the game is over.
	private boolean moveGhost() {
		int newMove;
		Random r = new Random();
		newMove = r.nextInt(7);
	
		if (newMove == 0) {
			if(checkMove(gRow-1, gCol)==false) {
			return false;
			}
			world[gRow-1][gCol].set('G');
			if (world[gRow-1][gCol].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gRow = gRow-1; 
			return true;
			
		}
		if (newMove == 1) {
			if(checkMove(gRow+1, gCol)==false) {
			return false;
			}
			world[gRow+1][gCol].set('G');
			if (world[gRow+1][gCol].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gRow = gRow+1; 
			return true;
			
		}
		if (newMove == 2) {
			if(checkMove(gRow, gCol-1)==false) {
			return false;
			}
			world[gRow][gCol-1].set('G');
			if (world[gRow][gCol-1].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gCol = gCol-1; 
			return true;
			
		}
		if (newMove == 3) {
			if(checkMove(gRow, gCol+1)==false) {
			return false;
			}
			world[gRow][gCol+1].set('G');
			if (world[gRow][gCol+1].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gCol = gCol+1; 
			return true;
			
		}
		if (newMove == 4) {
			if(checkMove(gRow-1, gCol-1)==false) {
			return false;
			}
			world[gRow-1][gCol-1].set('G');
			if (world[gRow-1][gCol-1].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gRow = gRow-1; 
			gCol = gCol-1;
			return true;
			
		}
		if (newMove == 5) {
			if(checkMove(gRow-1, gCol+1)==false) {
			return false;
			}
			world[gRow-1][gCol+1].set('G');
			if (world[gRow-1][gCol+1].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gRow = gRow-1; 
			gCol = gCol+1;
			return true;
			
		}
		if (newMove == 6) {
			if(checkMove(gRow+1, gCol-1)==false) {
			return false;
			}
			world[gRow+1][gCol-1].set('G');
			if (world[gRow+1][gCol-1].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gRow = gRow+1; 
			gCol = gCol-1;
			return true;
			
		}
		if (newMove == 7) {
			if(checkMove(gRow+1, gCol+1)==false) {
			return false;
			}
			world[gRow+1][gCol+1].set('G');
			if (world[gRow+1][gCol+1].get()=='P') {
				System.out.println("You Lose!");
				System.exit(0);
			}
			world[gRow][gCol].set(world[gRow][gCol].getOldValue());
			gRow = gRow+1; 
			gCol = gCol+1;
			return true;
			
		}
		
		return true;
		
		
	}
	
	//Up = W, Left = A, Right = D, Down = X, NorthW = Q, NorthE = E, SouthW= Z, SouthE = C
	//Directions to actually move pacman. Each if statement incorporates the checkmove() method to see if it can continue
	private boolean movePacman(){
		String m = input();
		
		if (m.equals("W")|| m.equals("w")) {
			if(checkMove(pRow-1, pCol)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow-1][pCol].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			}
			world[pRow-1][pCol].set('P');
			world[pRow][pCol].set('O');
			pRow = pRow -1;
			return true;
		}
		
		if (m.equals("X")|| m.equals("x")) {
			if(checkMove(pRow+1, pCol)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow+1][pCol].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			
			}
			world[pRow+1][pCol].set('P');
			world[pRow][pCol].set('O');
			pRow = pRow +1;
			return true;
		}
		if (m.equals("A")|| m.equals("a")) {
			if(checkMove(pRow, pCol-1)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow][pCol-1].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			}
			world[pRow][pCol-1].set('P');
			world[pRow][pCol].set('O');
			pCol = pCol -1;
			return true;
		}
		
		if (m.equals("D")|| m.equals("d")) {
			if(checkMove(pRow, pCol+1)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow][pCol+1].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			}
			world[pRow][pCol+1].set('P');
			world[pRow][pCol].set('O');
			pCol = pCol +1;
			return true;
		}
		
		if (m.equals("Q")|| m.equals("q")) {
			if(checkMove(pRow-1, pCol-1)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow-1][pCol-1].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			}
			world[pRow-1][pCol-1].set('P');
			world[pRow][pCol].set('O');
			pCol = pCol -1;
			pRow = pRow - 1;
			return true;
		}
		if (m.equals("e")|| m.equals("E")) {
			if(checkMove(pRow-1, pCol+1)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow-1][pCol+1].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			}
			world[pRow-1][pCol+1].set('P');
			world[pRow][pCol].set('O');
			pCol = pCol + 1;
			pRow = pRow - 1;
			return true;
		}
		if (m.equals("Z")|| m.equals("z")) {
			if(checkMove(pRow+1, pCol-1)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow+1][pCol-1].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			}
			world[pRow+1][pCol-1].set('P');
			world[pRow][pCol].set('O');
			pCol = pCol -1;
			pRow = pRow + 1;
			return true;
		}
		if (m.equals("C")|| m.equals("c")) {
			if(checkMove(pRow+1, pCol+1)==false) {
				System.out.println ("Move Invalid");
				return false;
				
			}
			if (world[pRow+1][pCol+1].get() == 'X'){
				points++;
				if(checkWin()) {
					System.out.println ("You Win!");
					System.exit(0);
				}
			
			}
			world[pRow+1][pCol+1].set('P');
			world[pRow][pCol].set('O');
			pCol = pCol +1;
			pRow = pRow + 1;
			return true;
		}
		
		
		else {
			System.out.println ("Wrong Input");
			return false;
		}
		
		
		
	}
	//The play method that begins the game. While the checkwin() method is false, the game continues to run.
	public void play() {
	
		boolean validMovePacman;
		boolean validMoveGhost;
		
	
		
		while (checkWin()==false) {
			printBoard();
			validMovePacman = movePacman();	
			while (!validMovePacman) {
				validMovePacman = movePacman();
			}
			validMoveGhost = moveGhost();
			while (!validMoveGhost) {
				validMoveGhost = moveGhost();
			}
			
		}
		
		
	}
	                   
	
}



I have another class that runs it. I guess my problem is in the moveGhost method somewhere but I'm not sure why it isn't running. When Pacman goes over a ghost, instead of ending the game it just turns into a Ghost. Can anyone help?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1