Hello I am working on a java packman for my programing class. The next step in t
ID: 3813166 • Letter: H
Question
Hello I am working on a java packman for my programing class.
The next step in this project is to add save states to the pacman game.
This is what the instructor wants
Finally, I want you to be able to save the current state of your program. So, if the user decides to save their current progress, they can and then resume at a later date. You should save the following:
a. Current location of the player
b. Current location of the monsters.
c. How many dots are left.
d. Current score.5
I think it needs to be done in my maze class, so I am posting just that but if you need more please tell me.
Maze.java
package game.packman;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Maze {
ArrayList lines;
// int row, column;
int rows, columns;
int width, height;
public Position packmanPos;
public Position ghostPos;
public ArrayList pills, powerPills;
public Maze(int m) {
//maze txt. file load
try {
pills = new ArrayList();
powerPills = new ArrayList();
lines = new ArrayList();
Scanner s = new Scanner(new File("mazes/"+m));
int r = 0;
while (s.hasNextLine()) {
String line = s.nextLine();
lines.add(line);
if (line.contains("4")) {
ghostPos = new Position(r, line.indexOf('4'));
}
if (line.contains("5")) {
packmanPos = new Position(r, line.indexOf('5'));
}
for (int i=0; i if (line.charAt(i) == '2') {
pills.add(new Position(r, i));
} else if (line.charAt(i) == '3') {
powerPills.add(new Position(r, i));
}
}
r++;
}
s.close();
rows = lines.size();
columns = lines.get(0).length();
width = columns*2;
height = rows*2;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
public char charAt(int r, int c) {
return lines.get(r).charAt(c);
}
public char[][] getCells() {
char[][] cells = new char[rows][columns];
for (int r=0; r System.arraycopy(lines.get(r).toCharArray(), 0, cells[r], 0, columns);
}
return cells;
}
}
Explanation / Answer
import java.util.List;
import java.util.Scanner;
public class Maze {
ArrayList lines;
// int row, column;
int rows, columns;
int width, height;
public Position packmanPos;
public Position ghostPos;
public ArrayList pills, powerPills;
public Maze(int m) {
//maze txt. file load
try {
pills = new ArrayList();
powerPills = new ArrayList();
lines = new ArrayList();
Scanner s = new Scanner(new File("mazes/"+m));
int r = 0;
while (s.hasNextLine()) {
String line = s.nextLine();
lines.add(line);
if (line.contains("4")) {
ghostPos = new Position(r, line.indexOf('4'));
}
if (line.contains("5")) {
packmanPos = new Position(r, line.indexOf('5'));
}
for (int i=0; i if (line.charAt(i) == '2') {
pills.add(new Position(r, i));
} else if (line.charAt(i) == '3') {
powerPills.add(new Position(r, i));
}
}
r++;
}
s.close();
rows = lines.size();
columns = lines.get(0).length();
width = columns*2;
height = rows*2;
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
public char charAt(int r, int c) {
return lines.get(r).charAt(c);
}
public char[][] getCells() {
char[][] cells = new char[rows][columns];
for (int r=0; r System.arraycopy(lines.get(r).toCharArray(), 0, cells[r], 0, columns);
}
return cells;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.