Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DepthFirstSearch in JAVA .. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ PATHING MAIN CLASS import java.util.List; import java.util.ArrayList; import java.util.LinkedList; import processing.core.*; public class PathingMain extends PApplet { private List imgs;

DepthFirstSearch in JAVA ..

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PATHING MAIN CLASS

import java.util.List; import java.util.ArrayList; import java.util.LinkedList; import processing.core.*; public class PathingMain extends PApplet { private List imgs; private int current_image; private long next_time; private PImage background; private PImage obstacle; private PImage goal; private List path; private static final int TILE_SIZE = 32; private static final int ANIMATION_TIME = 100; private GridValues[][] grid; private static final int ROWS = 15; private static final int COLS = 20; private static enum GridValues { BACKGROUND, OBSTACLE, GOAL, SEARCHED }; private Point wPos; private boolean drawPath = false; public void settings() { size(640,480); } /* runs once to set up world */ public void setup() { path = new LinkedList(); wPos = new Point(2, 2); imgs = new ArrayList(); imgs.add(loadImage("images/wyvern1.bmp")); imgs.add(loadImage("images/wyvern2.bmp")); imgs.add(loadImage("images/wyvern3.bmp")); background = loadImage("images/grass.bmp"); obstacle = loadImage("images/vein.bmp"); goal = loadImage("images/water.bmp"); grid = new GridValues[ROWS][COLS]; initialize_grid(grid); current_image = 0; next_time = System.currentTimeMillis() + ANIMATION_TIME; } /* set up a 2D grid to represent the world */ private static void initialize_grid(GridValues[][] grid) { for (int row = 0; row BACKGROUND; } } //set up some obstacles for (int row = 2; row OBSTACLE; } for (int row = 8; row OBSTACLE; } for (int col = 1; col OBSTACLE; } grid[13][14] = GridValues.GOAL; } private void next_image() { current_image = (current_image + 1) % imgs.size(); } /* runs over and over */ public void draw() { // A simplified action scheduling handler long time = System.currentTimeMillis(); if (time >= next_time) { next_image(); next_time = time + ANIMATION_TIME; } draw_grid(); draw_path(); image(imgs.get(current_image), wPos.x * TILE_SIZE, wPos.y * TILE_SIZE); } private void draw_grid() { for (int row = 0; row TILE_SIZE + TILE_SIZE * 3 / 8, p.y * TILE_SIZE + TILE_SIZE * 3 / 8, TILE_SIZE / 4, TILE_SIZE / 4); } } } private void draw_tile(int row, int col) { switch (grid[row][col]) { case BACKGROUND: image(background, col * TILE_SIZE, row * TILE_SIZE); break; case OBSTACLE: image(obstacle, col * TILE_SIZE, row * TILE_SIZE); break; case SEARCHED: fill(0, 128); rect(col * TILE_SIZE + TILE_SIZE / 4, row * TILE_SIZE + TILE_SIZE / 4, TILE_SIZE / 2, TILE_SIZE / 2); break; case GOAL: image(goal, col * TILE_SIZE, row * TILE_SIZE); break; } } public static void main(String args[]) { PApplet.main("PathingMain"); } public void keyPressed() { if (key == ' ') { //clear out prior path and re-initialize grid path.clear(); initialize_grid(grid); //EXAMPLE - replace with dfs moveOnce(wPos, grid, path); } else if (key == 'p') { drawPath ^= true; } else if (key == 'c') { path.clear(); initialize_grid(grid); } }  /* Replace (and rename) the below with a depth first search. This code provided only as an example of moving in in one direction for one tile - it mostly is for illustrating how you might test the occupancy grid and add nodes to path! */ private boolean moveOnce(Point pos, GridValues[][] grid, List path) { Point rightN = new Point(pos.x +1, pos.y ); //test if this is a valid grid cell if (withinBounds(rightN, grid) && grid[rightN.y][rightN.x] != GridValues.OBSTACLE && grid[rightN.y][rightN.x] != GridValues.SEARCHED) { //check if my right neighbor is the goal if (grid[rightN.y][rightN.x] == GridValues.GOAL) { path.add(0, rightN); return true; } //set this value as searched grid[rightN.y][rightN.x] = GridValues.SEARCHED; } return false; } private static boolean withinBounds(Point p, GridValues[][] grid) { return p.y >= 0 && p.y = 0 && p.x  

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PATH CLASS

public class Point { public final int x; public final int y; public Point(int x, int y) { this.x = x; this.y = y; } } 
Lab depth first search This lab explores recursion through the implementation of a depth-first search on a two-dimensional grid (a simple graph). Objectives .To implement depth first search Practice thinking about trajectory planning on a 2D grid structure Overview Get the base code here: https:/iclassroom.github.comaiCuJxC-e Begin with the base code. Note that this code represents the world as a 2D occupancy grid. Note that Processing, the tool we will use for visual disply of the 'world' and 'path', uses a 2D coordinate system (sometimes used to represent images), where (o, 0 is the upper left comer. This means that as the y value of a coordinate increases, the point is lower in the screen/world. This image shows the general structure. 1 2 point (x,y) 4 5 6 A (4,5) Example: A (4,5); 114

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions