Posted: September 13th, 2017

Graphics and Artificial Intelligence

Assignment Requirements

 

 

1 page report Write a Report as to how you would modify your code if the player was moving, this should be detailed enough that potentially it could be coded, though we are not asking you to write code, just explain how.

The following is the code;

This Code is an A* algorithm that allows the agent (hedgehog) to find the player (rabbit)

 

 

/*jslint browser:true */

/*global loadImages*/

/*global clearGrid*/

/*global displayImages*/

/*global scaleX*/

/*global scaleY*/

/*global ctx*/

 

var scenery = [[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 2, 2, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 2, 0, 0, 0],

[0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0],

[0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0],

[0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 2, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];

var gridState = true;

var Img1, Img2, Img3, Img4;

var Player = [-360, -140];

var Agent = [320, 260];

var moving;

function initialiseExample() {

“use strict”;

loadImages();

return;

}

// to start the game and move the Hedgehog toward to the player.

function startGame() {

“use strict”;

moving = setInterval(function () {moveAgent()}, 500);

}

//stop moving Hedgehog and chasing to the player.

function endGame() {

“use strict”;

clearInterval(moving);

}

function toggleGrid() {

“use strict”;

if (gridState === true) {

clearGrid(false, false, 40);

gridState = false;

displayImages();

} else {

clearGrid(true, false, 40);

gridState = true;

displayImages();

}

}

 

//The Agent will look for four directions and find the shortest way forward the Player to step. This function will loop until the Agent reaches the Player.

function moveAgent() {

“use strict”;

var i, m, n;

var checkingPoints = [];

var checkingX,checkingY, X1, Y1;

var distance = function(x1, y1, x2, y2) {

return Math.sqrt((x1 – x2) * (x1 – x2) + (y1 – y2) * (y1 – y2));

}

for (var i = 1; i <= 4; i++) {

switch(i){

//For suitable heuristics, the Agent always considers 4 directions: up, down, left, right distance to the Player for each case will be calculated and the optimized direction will be chosen.

case 1:

checkingX = Agent[0] + 40;

checkingY = Agent[1] + 0;

break;

case 2:

checkingX = Agent[0] + 0;

checkingY = Agent[1] + 40;

break;

case 3:

checkingX = Agent[0] + -40;

checkingY = Agent[1] + 0;

break;

case 4:

checkingX = Agent[0] + 0;

checkingY = Agent[1] + -40;

break;

default:

break;

}

if (checkingX >= 400 || checkingX <= -400 || checkingY >=340 || checkingY <= -300){

continue;

}

&nbsp;

var checkObstacle = false; // check if the position is available.

for (m = 0; m < 15; m = m + 1) {

for (n = 0; n < 20; n = n + 1) {

if (scenery[m][n]) {

Y1 = Number(300 – (40 * m));

X1 = Number((40 * n) + -400);

if (checkingX == X1 && checkingY == Y1){

checkObstacle = true;

}

}

}

}

if (checkObstacle){

continue;

}

checkingPoints.push([checkingX,checkingY]);

};

var checkedPoint = 0;

var minDistance = distance(Player[0],Player[1],checkingPoints[0][0],checkingPoints[0][1]);// find distance between two location.

for (var i = 0; i < checkingPoints.length; i++) {

console.log(distance(Player[0],Player[1],checkingPoints[i][0],checkingPoints[i][1]));// the location for the shortest distance.

if (distance(Player[0],Player[1],checkingPoints[i][0],checkingPoints[i][1]) < minDistance){

checkedPoint = i;

minDistance = distance(Player[0],Player[1],checkingPoints[i][0],checkingPoints[0][1]);

}

};

Agent = checkingPoints[checkedPoint]; //move the agent

clearGrid(true, true, 40);

displayImages();

if(distance(Agent[0],Agent[1],Player[0],Player[1]) == 0){ // check the distance between the agent and the player is 0.

clearInterval(moving);

}

}

&nbsp;

function loadImages() {

“use strict”;

var imgToLoad = 4, imgLoaded = 0;

var onImgLoad = function () {

imgLoaded = imgLoaded + 1;

if (imgLoaded === imgToLoad) {

displayImages();

}

};

Img1 = new Image();

Img1.src = ‘images/playerf.gif’;

Img1.onload = onImgLoad;

Img2 = new Image();

Img2.src = ‘images/hogf.gif’;

Img2.onload = onImgLoad;

Img3 = new Image();

Img3.src = ‘images/river.gif’;

Img3.onload = onImgLoad;

Img4 = new Image();

Img4.src = ‘images/tree.gif’;

Img4.onload = onImgLoad;

}

&nbsp;

function displayImages() {

“use strict”;

var X1, Y1, i, j;

//display User

X1 = scaleX(Number(Player[0]));

Y1 = scaleY(Number(Player[1]));

ctx.drawImage(Img1, X1, Y1);

//display Enemy

X1 = scaleX(Number(Agent[0]));

Y1 = scaleY(Number(Agent[1]));

ctx.drawImage(Img2, X1, Y1);

//display Scenery

for (i = 0; i < 15; i = i + 1) {

for (j = 0; j < 20; j = j + 1) {

if (scenery[i][j] === 1) {

Y1 = scaleY(Number(300 – (40 * i)));

X1 = scaleX(Number((40 * j) + -400));

ctx.drawImage(Img3, X1, Y1);

}

if (scenery[i][j] === 2) {

Y1 = scaleY(Number(300 – (40 * i)));

X1 = scaleX(Number((40 * j) + -400));

ctx.drawImage(Img4, X1, Y1);

}

}

}

}

&nbsp;

function doKeyDown(e) {

“use strict”;

var tempx, tempy;

if (e.keyCode === 119) { //W Key

tempy = Player[1];

tempy = tempy + 40;

Player[1] = tempy;

}

if (e.keyCode === 115) { //S Key

tempy = Player[1];

tempy = tempy – 40;

Player[1] = tempy;

}

if (e.keyCode === 97) { //A Key

tempx = Player[0];

tempx = tempx – 40;

Player[0] = tempx;

}

if (e.keyCode === 100) { //D Key

tempx = Player[0];

tempx = tempx + 40;

Player[0] = tempx;

}

clearGrid(gridState, false, 40);

displayImages();

}

Write a Report (1 page) as to how you would modify your code if the player was moving, this should be detailed enough that potentially it could be coded, though we are not asking you to write code, just explain how.

&nbsp;

RABBIT=PLAYER

&nbsp;

&nbsp;

&nbsp;

&nbsp;

Order Now

http://premiumbusinesswritings.com/order/

YOU CAN ALSO PLACE OTHER SIMILAR ORDERS ON OUR WEBSITE AND GET AMAZING DISCOUNTS!!!

Expert paper writers are just a few clicks away

Place an order in 3 easy steps. Takes less than 5 mins.

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
$0.00
Live Chat+1-631-333-0101EmailWhatsApp