项目

这里是一些留待你解决的更具挑战性的项目。【其中的某一些可能以后会作为本书的例子,所以可能会从这里拿掉】

老鼠和迷宫

首先,创建一个黑板(cite reference?)对象用来记录信息。在这个特殊的黑板上画迷宫,并且用它来显示由老鼠探测出来的迷宫的结构信息。

现在创建一个真正的迷宫,这个对象只暴露它自身很少一部分的信息——针对给定的坐标,它会告诉你紧挨着给它四周是墙壁还是空地,但是不会有其它更多的信息。对于新手,可以从一个文本文件读入迷宫(数据),但是可以考虑从因特网上找一个构造迷宫的算法。无论如何,最后的结果都应该是这么一个对象,这个对象可以根据给定的坐标找出它周围的墙壁和空地。还有一点,它还必须提供这个迷宫的入口以供查询。

最后,创建迷宫探路的老鼠Rat类。每个老鼠都可以与黑板和迷宫打交道,它向黑板报告自己的当前信息,并且根据自己所处的位置向迷宫请求新的信息。不管怎样,每次老鼠碰到迷宫分支点需要做出决策的时候,它就创建一个新的线程探测每一个分支。每个老鼠由它自己的线程驱动,当它走近死胡同以后,它会先向黑板报告自己探测的最终结果,然后它会结束自己所在的线程。

最终目标是绘出迷宫的完整地图,但是你必须得决定最后的结束条件是顺其自然找到的还是由黑板来判定。

下面是Jeremy Meyer写的一个实现的例子:

//: projects:Maze.java

package projects;

import java.util.*;

import java.io.*;

import java.awt.*;

public class Maze extends Canvas {

private Vector lines; // a line is a char array

private int width = -1;

private int height = -1;

public static void main (String [] args)

throws IOException {

if (args.length < 1) {

System.out.println("Enter filename");

System.exit(0);

}

Maze m = new Maze();

m.load(args[0]);

Frame f = new Frame();

f.setSize(m.width*20, m.height*20);

f.add(m);

Rat r = new Rat(m, 0, 0);

f.setVisible(true);

}

public Maze() {

lines = new Vector();

setBackground(Color.lightGray);

}

synchronized public boolean

isEmptyXY(int x, int y) {

if (x < 0) x += width;

if (y < 0) y += height;

// Use mod arithmetic to bring rat in line:

byte[] by =

(byte[])(lines.elementAt(y%height));

return by[x%width]==' ';

}

synchronized public void

setXY(int x, int y, byte newByte) {

if (x < 0) x += width;

if (y < 0) y += height;

byte[] by =

(byte[])(lines.elementAt(y%height));

by[x%width] = newByte;

repaint();

}

public void

load(String filename) throws IOException {

String currentLine = null;

BufferedReader br = new BufferedReader(

new FileReader(filename));

for(currentLine = br.readLine();

currentLine != null;

currentLine = br.readLine())  {

lines.addElement(currentLine.getBytes());

if(width < 0 ||

currentLine.getBytes().length > width)

width = currentLine.getBytes().length;

}

height = lines.size();

br.close();

}

public void update(Graphics g) { paint(g); }

public void paint (Graphics g) {

int canvasHeight = this.getBounds().height;

int canvasWidth  = this.getBounds().width;

if (height < 1 || width < 1)

return; // nothing to do

int width =

((byte[])(lines.elementAt(0))).length;

for (int y = 0; y < lines.size(); y++) {

byte[] b;

b = (byte[])(lines.elementAt(y));

for (int x = 0; x < width; x++) {

switch(b[x]) {

case ' ': // empty part of maze

g.setColor(Color.lightGray);

g.fillRect(

x*(canvasWidth/width),

y*(canvasHeight/height),

canvasWidth/width,

canvasHeight/height);

break;

case '*':     // a wall

g.setColor(Color.darkGray);

g.fillRect(

x*(canvasWidth/width),

y*(canvasHeight/height),

(canvasWidth/width)-1,

(canvasHeight/height)-1);

break;

default:      // must be rat

g.setColor(Color.red);

g.fillOval(x*(canvasWidth/width),

y*(canvasHeight/height),

canvasWidth/width,

canvasHeight/height);

break;

}

}

}

}

} ///:~

//: projects:Rat.java

package projects;

public class Rat {

static int ratCount = 0;

private Maze prison;

private int vertDir = 0;

private int horizDir = 0;

private int x,y;

private int myRatNo = 0;

public Rat(Maze maze, int xStart, int yStart) {

myRatNo = ratCount++;

System.out.println("Rat no." + myRatNo +

" ready to scurry.");

prison = maze;

x = xStart;

y = yStart;

prison.setXY(x,y, (byte)'R');

new Thread() {

public void run(){ scurry(); }

}.start();

}

public void scurry() {

// Try and maintain direction if possible.

// Horizontal backward

boolean ratCanMove = true;

while(ratCanMove) {

ratCanMove = false;

// South

if (prison.isEmptyXY(x, y + 1)) {

vertDir = 1; horizDir = 0;

ratCanMove = true;

}

// North

if (prison.isEmptyXY(x, y - 1))

if (ratCanMove)

new Rat(prison, x, y-1);

// Rat can move already, so give

// this choice to the next rat.

else {

vertDir = -1; horizDir = 0;

ratCanMove = true;

}

// West

if (prison.isEmptyXY(x-1, y))

if (ratCanMove)

new Rat(prison, x-1, y);

// Rat can move already, so give

// this choice to the next rat.

else {

vertDir = 0; horizDir = -1;

ratCanMove = true;

}

// East

if (prison.isEmptyXY(x+1, y))

if (ratCanMove)

new Rat(prison, x+1, y);

// Rat can move already, so give

// this choice to the next rat.

else {

vertDir = 0; horizDir = 1;

ratCanMove = true;

}

if (ratCanMove) { // Move original rat.

x += horizDir;

y += vertDir;

prison.setXY(x,y,(byte)'R');

}  // If not then the rat will die.

try {

Thread.sleep(2000);

} catch(InterruptedException e) {

throw new RuntimeException(e);

}

}

System.out.println("Rat no." + myRatNo +

" can't move..dying..aarrgggh.");

}

} ///:~

The maze initialization file:

//:! projects:Amaze.txt

* ** * * ** *

*** * ******* * ****

*** ***

***** ********** *****

* * * * ** ** * * * ** *

* * * * ** * * * * **

* ** * **

* ** * ** * ** * **

*** * *** ***** * *** **

* * * * * *

* ** * * * ** * *

///:~

其它关于迷宫的资源
    关于创建迷宫算法的讨论以及实现它们的java源代码:

http://www.mazeworks.com/mazegen/mazegen.htm

关于碰撞检测算法的讨论和其它针对自主物理对象(autonomous physical objects)单个或群体运动行为的讨论:

http://www.red3d.com/cwr/steer/

XML修饰器
    针对I/O读写器写一对修饰器(decorators)用来对XML编码(写修饰器)和解码(读修饰器)。

目录

翻译TIPatterns--项目(Projects)相关推荐

  1. 在线考试系统全套(任务书+开题报告+外文翻译+毕业论文+项目源代码)

    资源包括在线考试系统全套(任务书+开题报告+外文翻译+毕业论文+项目源代码+答辩ppt),项目是自己花了两个月开发的,最后获得了优秀论文,觉得好东西要拿给大家分享一下! 文件:n459.com/fil ...

  2. 翻译go项目代码英文注释

    #encoding=UTF-8 #!/usr/bin/python # encoding: utf-8 #filename: baidu-translate-gofile.py #author: ga ...

  3. 项目管理心得(二)——翻译项目

    书接上文.收集,确认需求是项目中最重要的环节之一,这次以翻译项目为例: 任务要求: 开发出一套,以计算机软件辅助"翻译的项目经理"进行项目成本控制和提高质量的软件. 要做好一件事, ...

  4. Angular项目目录结构

    前言:不支持MakeDown的博客园调格式的话,真的写到快o(╥﹏╥)o了,所以老夫还是转战到CSDN吧,这边就不更新啦啦啦~ CSDN地址:https://blog.csdn.net/Night20 ...

  5. github中repositories与projects区别

    一.概述 Github上边的repositories翻译为代码仓库,可以保存多个代码工程和项目的代码,资源,文本.图片......等; 而projects可以翻译为项目板,是project-board ...

  6. 如何同步更新 Github 上 Fork 的项目?

    Github Fork 过程概述 在 Github 上有很多优秀的开源项目,相信每一位热衷于技术的朋友都会在 Github 上 Fork 一些感兴趣的项目,然后在本地修改并提交.本文以 Galaxy ...

  7. 吴恩达课程翻译_中文学习资源:斯坦福大学CS231n计算机视觉课程

    hi,我是为你们的xio习操碎了心的和鲸社区男运营 我们的网站:和鲸社区 Kesci.co 我们的公众号:和鲸社区(ID:heywhale-kesci) 有干货,来! 大家好,此次本鲸给大家翻译的项目 ...

  8. 无后端完成在线翻译功能

    在线翻译 纯前端 + 百度翻译API + localStorage本地存储 完成提取文本中的生词,并返回单词的翻译结果 项目内容 之前背单词的时候发现,直接背单词的话太枯燥,直接阅读英文书籍.报纸又有 ...

  9. 【论文翻译】统一知识图谱学习和建议:更好地理解用户偏好

    一.摘要 将知识图谱(KG)纳入推荐系统有望提高推荐的准确性和可解释性.然而,现有方法主要假设KG是完整的并且简单地在实体原始数据或嵌入的浅层中转移KG中的"知识".这可能导致性能 ...

最新文章

  1. 最佳学习方法(3)听课--听一反三
  2. html表单验证js代码,JavaScript表单验证实现代码
  3. Oracle用户相关命令
  4. PoE交换机的选择和使用注意事项介绍
  5. 【2016年第2期】专题导读:大数据与社会治理
  6. 【后空翻机器人代码】斯坦福后空翻机器人设计、代码全开源,成本降至3000美元,人人皆可DIY|湾区人工智能...
  7. 智能一代云平台(三十五):后端架构再思考
  8. 图解Team Foundation Server 2013系列
  9. HDU 6185 2017广西邀请赛:Covering(矩阵快速幂)
  10. Struts2 Jakarta远程执行代码测试
  11. docker搭建python开发环境_PyCharm使用之利用Docker镜像搭建Python开发环境
  12. python常用代码大全-python代码大全
  13. python bartender_bartender使用教程 - 卡饭网
  14. js视频播放器/video详解
  15. 小程序审核失败:你的小程序涉及提供播放、观看等服务,请补充选择:文娱-其他视频类目。怎么解决呢
  16. “errmsg“ : “not master and slaveOk=false“_Mongo集群没有primary但有secondary时连接不上且不能读数据
  17. html5 mp3播放器源码,HTML5自定义mp3播放器源码
  18. trunc和round区别
  19. python ip动态代理_Python实现爬取可用代理IP
  20. js收起手机软件键盘

热门文章

  1. 如何利用固定IP在路由器中设置局域网IP映射到公网上
  2. UVA-12260-Free Goodies
  3. SCI论文投稿经验分享——如何在投稿前准备好需要的材料
  4. android特效集锦系列之八 仿快播搜索框悬浮文字搜索
  5. Symbian操作系统历史简介
  6. python抓取动态数据 A股上市公司基本信息
  7. 安川机器人原点丢失_ABB机器人零点丢失解决办法
  8. PMP证书在面试项目经理时有加持吗?解惑
  9. 数据泵expdp/impdp导入导出详细说明
  10. Android Studio新版本无法执行java的main函数