Add second year

This commit is contained in:
2023-12-07 01:19:12 +00:00
parent 3291e5c79e
commit 3d12031ab8
1168 changed files with 431409 additions and 0 deletions

View File

@ -0,0 +1,273 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
public class AStarMaze extends JFrame implements Runnable, MouseListener, MouseMotionListener, KeyListener {
// member data
private static final Dimension WindowSize = new Dimension(800, 800);
private boolean isInitialised = false;
private BufferStrategy strategy;
private Graphics offscreenBuffer;
private static boolean map[][] = new boolean[40][40];
private boolean isGameRunning = false;
private BadGuy badguy;
private Player player;
private String FilePath;
// constructor
public AStarMaze() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window, centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
this.setTitle("A* Pathfinding Demo");
FilePath = System.getProperty("user.dir") + "/";
// load raster graphics and instantiate game objects
ImageIcon icon = new ImageIcon(FilePath+"badguy.png");
Image img = icon.getImage();
badguy = new BadGuy(img);
icon = new ImageIcon(FilePath+"player.png");
img = icon.getImage();
player = new Player(img);
// initialise double-buffering
createBufferStrategy(2);
strategy = getBufferStrategy();
// create and start our animation thread
Thread t = new Thread(this);
t.start();
// register the Jframe itself to receive mouse and keyboard events
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
// initialise the map state
for (x=0;x<40;x++) {
for (y=0;y<40;y++) {
map[x][y]=false;
}
}
isInitialised = true;
}
// thread's entry point
public void run() {
long loops = 0;
while (true) {
// 1: sleep for 1/5 sec
try {
Thread.sleep(200);
} catch (InterruptedException e) { }
// 2: animate game objects
if (isGameRunning) {
loops++;
player.move(map); // player moves every frame
// recalculating the badguys path every move, highly inefficient but easier to program lol
badguy.reCalcPath(map, player.x, player.y);
if (loops%10==0) // badguy moves once every 3 frames
badguy.move(map,player.x,player.y);
}
// 3: force an application repaint
this.repaint();
}
}
private void loadMaze() {
String filename = FilePath+"maze.txt";
String textinput = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
textinput = reader.readLine();
reader.close();
}
catch (IOException e) { }
if (textinput!=null) {
for (int x=0;x<40;x++) {
for (int y=0;y<40;y++) {
map[x][y] = (textinput.charAt(x*40+y)=='1');
}
}
}
}
private void saveMaze() {
// pack maze into a string
String outputtext="";
for (int x=0;x<40;x++) {
for (int y=0;y<40;y++) {
if (map[x][y])
outputtext+="1";
else
outputtext+="0";
}
}
try {
String filename = FilePath+"maze.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(outputtext);
writer.close();
}
catch (IOException e) { }
}
// mouse events which must be implemented for MouseListener
public void mousePressed(MouseEvent e) {
if (!isGameRunning) {
// was the click on the 'start button'?
int x = e.getX();
int y = e.getY();
if (x>=15 && x<=85 && y>=40 && y<=70) {
isGameRunning=true;
return;
}
// or the 'load' button?
if (x>=315 && x<=385 && y>=40 && y<=70) {
loadMaze();
return;
}
// or the 'save' button?
if (x>=415 && x<=485 && y>=40 && y<=70) {
saveMaze();
return;
}
}
// determine which cell of the gameState array was clicked on
int x = e.getX()/20;
int y = e.getY()/20;
// toggle the state of the cell
map[x][y] = !map[x][y];
// throw an extra repaint, to get immediate visual feedback
this.repaint();
// store mouse position so that each tiny drag doesn't toggle the cell
// (see mouseDragged method below)
prevx=x;
prevy=y;
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
//
// mouse events which must be implemented for MouseMotionListener
public void mouseMoved(MouseEvent e) {}
// mouse position on previous mouseDragged event
// must be member variables for lifetime reasons
int prevx=-1, prevy=-1;
public void mouseDragged(MouseEvent e) {
// determine which cell of the gameState array was clicked on
// and make sure it has changed since the last mouseDragged event
if (e.getX() < 800 && e.getX() >= 0 && e.getY() < 800 && e.getY() >= 0) {
int x = e.getX()/20;
int y = e.getY()/20;
if (x!=prevx || y!=prevy) {
// toggle the state of the cell
map[x][y] = !map[x][y];
// throw an extra repaint, to get immediate visual feedback
this.repaint();
// store mouse position so that each tiny drag doesn't toggle the cell
prevx=x;
prevy=y;
}
}
badguy.reCalcPath(map, player.x, player.y);
}
// Keyboard events
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_LEFT)
player.setXSpeed(-1);
else if (e.getKeyCode()==KeyEvent.VK_RIGHT)
player.setXSpeed(1);
else if (e.getKeyCode()==KeyEvent.VK_UP)
player.setYSpeed(-1);
else if (e.getKeyCode()==KeyEvent.VK_DOWN)
player.setYSpeed(1);
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_LEFT || e.getKeyCode()==KeyEvent.VK_RIGHT)
player.setXSpeed(0);
else if (e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN)
player.setYSpeed(0);
}
public void keyTyped(KeyEvent e) { }
// application's paint method
public void paint(Graphics g) {
if (!isInitialised)
return;
// g = offscreenBuffer; // draw to offscreen buffer
g = strategy.getDrawGraphics();
// clear the canvas with a big black rectangle
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 800);
// redraw the map
g.setColor(Color.WHITE);
for (int x=0;x<40;x++) {
for (int y=0;y<40;y++) {
if (map[x][y]) {
g.fillRect(x*20, y*20, 20, 20);
}
}
}
// redraw the player and badguy
// paint the game objects
player.paint(g);
badguy.paint(g);
if (!isGameRunning) {
// game is not running..
// draw a 'start button' as a rectangle with text on top
// also draw 'load' and 'save' buttons
g.setColor(Color.GREEN);
g.fillRect(15, 40, 70, 30);
g.fillRect(315, 40, 70, 30);
g.fillRect(415, 40, 70, 30);
g.setFont(new Font("Times", Font.PLAIN, 24));
g.setColor(Color.BLACK);
g.drawString("Start", 22, 62);
g.drawString("Load", 322, 62);
g.drawString("Save", 422, 62);
}
// flip the buffers
strategy.show();
}
// application entry point
public static void main(String[] args) {
AStarMaze w = new AStarMaze();
}
}

View File

@ -0,0 +1,298 @@
import java.awt.*;
import java.util.*;
public class BadGuy {
/* list of nodes to which the algorithm has already found a route (i.e., one of its conencted neighbours has been expanded)
* but have not themselves been expanded */
LinkedList<Node> openlist = new LinkedList<Node>();
// list of nodes that have been expanded and which therefore should not be revisited
LinkedList<Node> closedlist = new LinkedList<Node>();
Node[][] allnodes = new Node[40][40] ; // array of all the nodes
Stack<Node> finalpath = new Stack<Node>();
Image myImage;
int x=0,y=0;
boolean hasPath=false;
public BadGuy( Image i ) {
myImage=i;
x = 30;
y = 10;
}
public void reCalcPath(boolean map[][],int targx, int targy) {
System.out.println();
System.out.println("recalculating path");
hasPath = false;
openlist.clear();
closedlist.clear();
finalpath.clear();
// looping through map[][], generating each node, and marking each wall node as closed
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
allnodes[i][j] = new Node(i, j); // generating node
if (map[i][j]) {
allnodes[i][j].closed = true;
allnodes[i][j].open = false;
closedlist.add(allnodes[i][j]);
}
}
}
// calculate f,g,h for the starting node and set to open
Node starting = allnodes[x][y];
starting.g = 0;
starting.h = targx - x + targy - y; // manhattan distance
starting.f = starting.g + starting.h;
starting.open = true;
starting.closed = false;
openlist.add(starting);
// looping while a path has not been found
// end condition: if a neighbour is the target, or if there are no open nodes
while (!hasPath) {
// breaking if there are no open nodes
if (openlist.size() == 0) {
break;
}
/* progress is made by identifying the most promising node in the open list (i.e., the one with lowest f value) and
* expanding it by adding each of its connected neighbours to the open list, unless they are already closed. */
// looping through open list to find most promising node
Node mostpromising = openlist.get(0);
for (int i = 1; i < openlist.size(); i++) {
if (openlist.get(i).f < mostpromising.f) {
mostpromising = openlist.get(i);
}
}
int mx = mostpromising.x;
int my = mostpromising.y;
// expanding the most promising node by adding each of its connected neighbours to the open list, unless they are already closed
// as nodes are added to the open list, their f, g, h, & parent values are recorded
// the g value of a node is equal to the g value of its parent + the cost of moving from the parent to the node itself. t
// as nodes are expanded, they are moved to the closed list
mostpromising.open = false;
mostpromising.closed = true;
closedlist.add(mostpromising);
// openlist.remove(mostpromising);
// northwest neighbour
if (mx-1 >= 0 && my-1 >= 0 && !allnodes[mx-1][my-1].closed) {
// checking if this node is the target node, and breaking if so
if (mx-1 == targx && my-1 == targy) {
allnodes[mx-1][my-1].open = true;
allnodes[mx-1][my-1].closed = false;
closedlist.remove(allnodes[mx-1][my-1]);
openlist.add(allnodes[mx-1][my-1]);
break;
}
if (!allnodes[mx-1][my-1].open) {
allnodes[mx-1][my-1].g = mostpromising.g + 1;
allnodes[mx-1][my-1].h = targx - mx + targy - my;
allnodes[mx-1][my-1].f = allnodes[mx-1][my-1].g + allnodes[mx-1][my-1].h;
allnodes[mx-1][my-1].open = true;
allnodes[mx-1][my-1].closed = false;
openlist.add(allnodes[mx-1][my-1]);
}
}
// north neighbour
if (my-1 >= 0 && !allnodes[mx][my-1].closed) {
// checking if this node is the target node, and breaking if so
if (mx == targx && my-1 == targy) {
allnodes[mx][my-1].open = true;
allnodes[mx][my-1].closed = false;
closedlist.remove(allnodes[mx][my-1]);
openlist.add(allnodes[mx][my-1]);
break;
}
if (!allnodes[mx][my-1].open) {
allnodes[mx][my-1].g = mostpromising.g + 1;
allnodes[mx][my-1].h = targx - mx + targy - my;
allnodes[mx][my-1].f = allnodes[mx][my-1].g + allnodes[mx][my-1].h;
allnodes[mx][my-1].open = true;
allnodes[mx][my-1].closed = false;
openlist.add(allnodes[mx][my-1]);
}
}
// northeast neighbour
if (mx+1 < 40 && my-1 >= 0 && !allnodes[mx+1][my-1].closed) {
// checking if this node is the target node, and breaking if so
if (mx+1 == targx && my-1 == targy) {
allnodes[mx+1][my-1].open = true;
allnodes[mx+1][my-1].closed = false;
closedlist.remove(allnodes[mx+1][my-1]);
openlist.add(allnodes[mx+1][my-1]);
break;
}
if (!allnodes[mx+1][my-1].open && !allnodes[mx+1][my-1].closed) {
allnodes[mx+1][my-1].g = mostpromising.g + 1;
allnodes[mx+1][my-1].h = targx - mx+1 + targy - my;
allnodes[mx+1][my-1].f = allnodes[mx+1][my-1].g + allnodes[mx+1][my-1].h;
allnodes[mx+1][my-1].open = true;
allnodes[mx+1][my-1].closed = false;
openlist.add(allnodes[mx+1][my-1]);
}
}
// west neighbour
if (mx-1 >= 0 && !allnodes[mx-1][my].closed) {
// checking if this node is the target node, and breaking if so
if (mx-1 == targx && my == targy) {
allnodes[mx-1][my].open = true;
allnodes[mx-1][my].closed = false;
closedlist.remove(allnodes[mx-1][my]);
openlist.add(allnodes[mx-1][my]);
break;
}
if (!allnodes[mx-1][my].open && !allnodes[mx-1][my].closed) {
allnodes[mx-1][my].g = mostpromising.g + 1;
allnodes[mx-1][my].h = targx - mx + targy - my;
allnodes[mx-1][my].f = allnodes[mx-1][my].g + allnodes[mx-1][my].h;
allnodes[mx-1][my].open = true;
allnodes[mx-1][my].closed = false;
openlist.add(allnodes[mx-1][my]);
}
}
// east neighbour
if (mx+1 < 40 && !allnodes[mx+1][my].closed) {
// checking if this node is the target node, and breaking if so
if (mx+1 == targx && my == targy) {
allnodes[mx+1][my].open = true;
allnodes[mx+1][my].closed = false;
closedlist.remove(allnodes[mx+1][my]);
openlist.add(allnodes[mx+1][my]);
break;
}
if (!allnodes[mx+1][my].open) {
allnodes[mx+1][my].g = mostpromising.g + 1;
allnodes[mx+1][my].h = targx - mx+1 + targy - my;
allnodes[mx+1][my].f = allnodes[mx+1][my].g + allnodes[mx+1][my].h;
allnodes[mx+1][my].open = true;
allnodes[mx+1][my].closed = false;
openlist.add(allnodes[mx+1][my]);
}
}
// southwest neighbour
if (mx-1 >= 0 && my+1 < 40 && !allnodes[mx-1][my+1].closed) {
// checking if this node is the target node, and breaking if so
if (mx-1 == targx && my+1 == targy) {
allnodes[mx-1][my+1].open = true;
allnodes[mx-1][my+1].closed = false;
closedlist.remove(allnodes[mx-1][my+1]);
openlist.add(allnodes[mx-1][my+1]);
break;
}
if (!allnodes[mx-1][my+1].open) {
allnodes[mx-1][my+1].g = mostpromising.g + 1;
allnodes[mx-1][my+1].h = targx - mx + targy - my;
allnodes[mx-1][my+1].f = allnodes[mx-1][my+1].g + allnodes[mx-1][my+1].h;
allnodes[mx-1][my+1].open = true;
allnodes[mx-1][my+1].closed = false;
openlist.add(allnodes[mx-1][my+1]);
}
}
// south neighbour
if (my+1 < 40 && !allnodes[mx][my+1].closed) {
// checking if this node is the target node, and breaking if so
if (mx == targx && my+1 == targy) {
allnodes[mx][my+1].open = true;
allnodes[mx][my+1].closed = false;
closedlist.remove(allnodes[mx][my+1]);
openlist.add(allnodes[mx][my+1]);
break;
}
if (!allnodes[mx][my+1].open) {
allnodes[mx][my+1].g = mostpromising.g + 1;
allnodes[mx][my+1].h = targx - mx + targy - my;
allnodes[mx][my+1].f = allnodes[mx][my+1].g + allnodes[mx][my+1].h;
allnodes[mx][my+1].open = true;
allnodes[mx][my+1].closed = false;
openlist.add(allnodes[mx][my+1]);
}
}
// southeast neighbour
if (mx+1 < 40 && my+1 < 40 && !allnodes[mx+1][my+1].closed) {
// checking if this node is the target node, and breaking if so
if (mx+1 == targx && my+1 == targy) {
allnodes[mx+1][my+1].open = true;
allnodes[mx+1][my+1].closed = false;
closedlist.remove(allnodes[mx+1][my+1]);
openlist.add(allnodes[mx+1][my+1]);
break;
}
if (!allnodes[mx+1][my+1].open) {
allnodes[mx+1][my+1].g = mostpromising.g + 1;
allnodes[mx+1][my+1].h = targx - mx+1 + targy - my;
allnodes[mx+1][my+1].f = allnodes[mx+1][my+1].g + allnodes[mx+1][my+1].h;
allnodes[mx+1][my+1].open = true;
allnodes[mx+1][my+1].closed = false;
openlist.add(allnodes[mx+1][my+1]);
}
}
}
// generate final path by pushing target onto stack, followed by its parent in closedlist, ..., followed by start node
for (int i = openlist.size()-1; i >= 0; i--) {
System.out.println("pushing x=" + openlist.get(i).x + " y =" + openlist.get(i).y);
finalpath.push(openlist.get(i));
}
hasPath = true;
return;
}
public void move(boolean map[][],int targx, int targy) {
if (hasPath) {
Node nextnode = finalpath.pop();
System.out.println("next node x=" + nextnode.x + " y=" + nextnode.y);
x = nextnode.x;
y = nextnode.y;
}
else {
// no path known, so just do a dumb 'run towards' behaviour
int newx=x, newy=y;
if (targx<x)
newx--;
else if (targx>x)
newx++;
if (targy<y)
newy--;
else if (targy>y)
newy++;
if ((newx < 40 && newx >= 0 && newy < 40 && newy >=0) && !map[newx][newy]) {
x=newx;
y=newy;
}
}
}
public void paint(Graphics g) {
g.drawImage(myImage, x*20, y*20, null);
}
}

View File

@ -0,0 +1,15 @@
public class Node {
public int x;
public int y;
public int g; // cost of getting from the starting node to this node
public int h; // estimated heuristic cost of getting from this node to the target node
public int f; // sum of g & h, the algorithm's best current estimate as to the total cost of travelling from the starting location to the target location via this node
public boolean closed = false;
public boolean open = false;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}

View File

@ -0,0 +1,42 @@
import java.awt.Graphics;
import java.awt.Image;
public class Player {
Image myImage;
int x=0,y=0;
int xSpeed=0, ySpeed=0;
public Player( Image i ) {
myImage=i;
x=10;
y=35;
}
public void setXSpeed( int x ) {
xSpeed=x;
}
public void setYSpeed( int y ) {
ySpeed=y;
}
public void move(boolean map[][]) {
int newx=x+xSpeed;
int newy=y+ySpeed;
// making sure that the newx & newy are not off the map or blocked
// if (e.getX() < 800 && e.getX() >= 0 && e.getY() < 800 && e.getY() >= 0) {
if ((newx < 40 && newx >= 0 && newy < 40 && newy >=0) && !map[newx][newy]) {
x=newx;
y=newy;
}
}
public void paint(Graphics g) {
g.drawImage(myImage, x*20, y*20, null);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1 @@
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000001100000000000000000000000000000000000000110000000000000000000000000000000000000001000000000000000000000000000000000000000100000000000000000000000000000000000000010000000000000000000000000000000000000011000000000000000000000011111111111111111100000000000000000000000000000000000000110000000000000000000000000000000000000001000000000000000000000000000000000000000100000000000000000000000000000000000000010000000000000000000000000000000000000011000000000000000000000000000000000000001100000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB