当前位置:网站首页>Robocode教程8——AdvancedRobot
Robocode教程8——AdvancedRobot
2022-04-23 05:45:00 【dawnsun001】
在这次教程中,我们将要学习AdvancedRobot和Robot的区别,学会AdvancedRobot是我们写出高智商机器人的前提。
能够高效率运行的机器人都是继承的AdvancedRobot,因为Robot是线程阻塞式的,AdvancedRobot是线程非阻塞式的。他们的区别比较明显,分析while(true){}里面的代码来理解线程阻塞与非阻塞就比较容易了。
public void run() {
while(true) {
ahead(100);
turnGunRight(90);
}
}
在Robocode中每一个机器人都是一个独立的线程,我们知道不管是extends Thread还是implements Runnable 都要实现public void run()方法,这是必不可少的。为了保证机器人不断的运行自己的程序,所以线程中用了while(true)循环。在每一次循环中,ahead(100)机
器人向前运动100象素,接着turnGunRight(90) 右转炮90度,你会发现机器人是按照严格的一句句代码执行,只有执行完ahead(100)后,才会执行turnGunRight(90),然后接着循环。我们的机器人就会在战场上走一个正方形线路。
下面我们来分析另一个代码,这段代码继承自AdvancedRobot,你会发现每个方法前面都加了一个set,这是为了便于和Robot进行区别。
public void run() {
while(true) {
setAhead(100);
setTurnGunRight(90);
execute();
}
}
和上面几乎一样的代码,只是多了一句execute();而这个execute()就是非阻塞代码的关键,这里涉及java的多线程编程,我们不多讲,我们可以这样理解execute(),它相当于一个标志,程序记录前面所有代码要执行的内容,但不执行,只有接到execute()命令后,以前记录的命令将会交替运行,对于上面的代码,既是前进一小步,转动一个小角度,前进一小步,转动一个小角度,如此循环。于是在战场上我们的机器人将走一个圆形录像。
好了,关于Robocode的基本内容就这些了,有了这些知识写出一些比较不错的机器人了。但是要写出更加聪明的实战机器人,我们还需要很多算法,后面我们将依依介绍这些算法,包括随机运动,伪随机运动,直线预测瞄准,圆周预测瞄准,统计瞄准,虚拟波瞄准等等。
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dawnsun2013/article/details/17718563
边栏推荐
- Addition, deletion, query and modification of data
- 队列解决约瑟夫问题
- Animation - Introduction to keyframes
- Optional best practices
- 9.Life, the Universe, and Everything
- Export the articles written in CSDN to PDF format
- 7.Domino piling
- Sakura substring thinking
- Cf6d lizards and fundamentals 2 problem solving
- The onnx model of yolov5 removes the transfer layer
猜你喜欢
随机推荐
The most practical chrome plug-in
Customized communication between threads (reentrantlock)
Supply chain service terms
C array
Busybox initrd and initialization process
程序設計訓練
Export the articles written in CSDN to PDF format
20 excellent plug-ins recommended by idea
小区房价可视化
词频统计
Example of reentrant lock thread waiting to wake up
Rust 中的 Rc智能指针
Understanding and installing MySQL
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
SVN简单操作命令
8. Integer Decomposition
Rust的闭包类型(Fn, FnMut, FnOne的区别)
POJ - 2955 brackets interval DP
程序设计训练
SQL -- data filtering and grouping