面向对象编程,走迷宫
建立maze.js, mazespace.js, robot.js
界面逻辑写在robotmazeinterface.js
"use strict";//maze.js
function Maze(width,height){
this.width=width;
this.height=height;
this.startX = null;
this.startY = null;
this.startOrientation = null;
this.endX = null;
this.endY = null;
this.directions=["north","east","south","west"];
this.spaces=[];
for(var x=1;x<=width;x++){
this.spaces[x]=[];
for(var y=1;y<=height;y++){
this.spaces[x][y]=new MazeSpace(this.directions);
}
}
}
Maze.prototype.setStart=function(x,y,orientation){
if(this.isInBounds(x,y)&&this.isValidDirection(orientation)){
this.startX=x;
this.startY=y;
this.startOrientation=orientation;
return true;
}
}
Maze.prototype.setEnd=function(x,y){
this.endX=x;
this.endY=y;
}
Maze.prototype.setWall=function(x,y,direction){
if(this.isInBounds(x,y)&&this.isValidDirection(direction)){
this.spaces[x][y].setWall(direction);
return true;
}
return false;
}
Maze.prototype.isValidDirection=function(direction){
return this.directions.indexOf(direction)!==-1;
}
Maze.prototype.isInBounds=function(x,y){
return x>0&&x<=this.width&&y>0&&y<=this.height;
}
Maze.prototype.canMove=function(x,y,direction){
if(!this.isValidDirection(direction)){
return false;
}
if(!this.isInBounds(x,y)){
return false;
}
var forwardX,forwardY;
switch(direction){
case "north":
forwardX=x;
forwardY=y+1;
break;
case "east":
forwardX=x+1;
forwardY=y;
break;
case "south":
forwardX=x;
forwardY=y-1;
break;
case "west":
forwardX=x-1;
forwardY=y;
break;
}
if(!this.isInBounds(forwardX,forwardY)){
return false;
}
if(this.spaces[x][y][direction]){
return false;
}
var opposites={
north:"south",
east:"west",
south:"north",
west:"east"
};
if(this.spaces[forwardX][forwardY][opposites[direction]]){
return false;
}
return true;
}
// "use strict";
// function Maze(width,height) {
// this.width = width;
// this.height = height;
// this.startX = null;
// this.startY = null;
// this.startOrientation = null;
// this.endX = null;
// this.endY = null;
// this.directions = ["north","east","south","west"];
// this.spaces = [];
// var x, y;
// for (x=1; x <= width; x += 1) {
// this.spaces[x] = [];
// for (y=1; y <= height; y += 1) {
// this.spaces[x][y] = new MazeSpace(this.directions);
// }
// }
// }
// Maze.prototype.setStart = function(x, y, orientation) {
// if (this.isInBounds(x, y) && this.isValidDirection(orientation)) {
// this.startX = x;
// this.startY = y;
// this.startOrientation = orientation;
// return true;
// }
// return false;
// }
// Maze.prototype.setEnd = function (x, y) {
// if (!this.isInBounds(x, y)) {
// return false;
// }
// this.endX = x;
// this.endY = y;
// return true;
// }
// Maze.prototype.setWall = function (x, y, direction) {
// if (this.isInBounds(x, y) && this.isValidDirection(direction)) {
// this.spaces[x][y].setWall(direction);
// return true;
// }
// return false;
// }
// Maze.prototype.isValidDirection = function(direction) {
// return this.directions.indexOf(direction) !== -1;
// }
// Maze.prototype.isInBounds = function (x, y) {
// return x > 0 && x <= this.width && y > 0 && y <= this.height;
// }
"use strict";
function MazeSpace(directions){
for(var i=0;i<directions.length;i++){
this[directions[i]]=false;
}
}
MazeSpace.prototype.setWall=function(direction){
this[direction]=true;
}
"use strict";
function Robot(){
this.x=null;
this.y=null;
this.orientation=null;
this.maze=null;
}
Robot.prototype.setMaze=function(maze){
this.maze=maze;
this.x=maze.startX;
this.y=maze.startY;
this.orientation=maze.startOrientation;
}
Robot.prototype.turnRight=function(){
if(!this.maze||!this.maze.isValidDirection(this.orientation)){
return false;
}
var rights={
north:"east",
east:"south",
south:"west",
west:"north"
}
this.orientation=rights[this.orientation];
return true;
}
Robot.prototype.turnLeft=function(){
if(!this.maze||!this.maze.isValidDirection(this.orientation)){
return false;
}
var lefts={
north:"west",
east:"north",
south:"east",
west:"south"
}
this.orientation=lefts[this.orientation];
return true;
}
Robot.prototype.moveForward=function(){
if(!this.canMoveForward()){
return false;
}
switch(this.orientation){
case "north":
this.y++;
break;
case "east":
this.x++;
break;
case "south":
this.y--;
break;
case "west":
this.x--;
break;
}
return true;
}
Robot.prototype.canMoveForward=function(){
if(!this.maze){
return false;
}
return this.maze.canMove(this.x,this.y,this.orientation);
}

浙公网安备 33010602011771号