人気ブログランキング | 話題のタグを見る
プログラミング演習
 今日は、ハマちゃんと対戦。結果は・・・・



 最初、全く勝てなかったけど、プログラムを改良して初勝利!!!!

 相手は、索敵すると弾を連射してくるので、当たったら500移動するようにしました。
 あと、ランダムな移動を二つにわけ、移動用、方向転換用というようにしました。

 
 package mf;
import robocode.*;
import java.awt.Color;

/**
* Miffy - a robot by (your name here)
*/
public class Miffy extends Robot
{
//ここからプログラムスタート


boolean peek; // Don't turn if there's a robot there
// ロボットがいれば回転しない
double moveAmount; // How much to move
// どれだけ動くか

public void run() {
setColors(Color.yellow,Color.pink,new Color(150,0,150));
turnLeft(getHeading());//左に曲がる

turnGunRight(360);//砲台を右へ360度回転
while(true) { //もし真ならば
//ahead(50);//50前進
//turnRight(90);//右へ90度
turnGunRight(360);//砲台を右へ360度
setAdjustGunForRobotTurn(true);//

double r=Math.random();//方向のみ
if(r > 0.75){
ahead(50);
}else if(r >0.5){
turnLeft(90);//ahead(50);
}else if(r>0.25){
turnRight(90);//ahead(50);
}else back(50);

double s=Math.random(); //移動のみ
if(s > 0.75){
ahead(50);
}else if(s >0.5){
ahead(50);
}else if(s>0.25){
ahead(50);
}else back(50);
}
}

public void onScannedRobot(ScannedRobotEvent e) {//ロボット索敵
double energy;
System.out.println("found;"+e.getName());
fire(1);//見つけたらファイア!
System.out.println("energy;"+getEnergy());
scan();//索敵
}
public void onHitRobot(HitRobotEvent e) {
// If he's in front of us, set back up a bit.
// もし敵が私たちの前にいれば、少し後退します。
if (e.getBearing() > -90 && e.getBearing() < 90)
back(100);
// else he's in back of us, so set ahead a bit.
// そうでなければ敵は後ろにいるので、すこし前進します。
else
ahead(100);
}
public void onHitByBullet(ScannedRobotEvent e) {

if (getEnergy()>50){//エネルギーが50以上なら
fire(3);//火力3で発射
}
else{
fire(1);//そうでないならば火力1で発射
}

scan();
}

public void onHitByBullet(HitByBulletEvent e) {//敵に攻撃されたら
//turnLeft(90 - e.getBearing());//砲台の方向から左へ90度向く
back(500);//後ろへ90
}
public void onHitWall(HitWallEvent e) {//壁に当たったら
turnGunRight(180); //砲台を右へ180度
turnRight(90);
ahead(500);
}
}



 しかし、ハマちゃんも対策をしてきたのでまた勝てなくなり、再び改良。
 距離に応じてミサイルの威力を変更できるようにしました。
 (もし、距離が10未満であれば威力10の弾を発射、それ以外は1で)

package mf;
import robocode.*;
import java.awt.Color;

/**
* Miffy - a robot by (your name here)
*/
public class Miffy extends Robot
{
//ここからプログラムスタート


boolean peek; // Don't turn if there's a robot there
// ロボットがいれば回転しない
double moveAmount; // How much to move
// どれだけ動くか

public void run() {
setColors(Color.yellow,Color.pink,new Color(150,0,150));
turnLeft(getHeading());//左に曲がる

turnGunRight(360);//砲台を右へ360度回転
while(true) { //もし真ならば
//ahead(50);//50前進
//turnRight(90);//右へ90度
turnGunRight(360);//砲台を右へ360度
setAdjustGunForRobotTurn(true);//

double r=Math.random();//方向のみ
if(r > 0.75){
ahead(50);
}else if(r >0.5){
turnLeft(90);//ahead(50);
}else if(r>0.25){
turnRight(90);//ahead(50);
}else back(50);

double s=Math.random(); //移動のみ
if(s > 0.75){
ahead(50);
}else if(s >0.5){
ahead(50);
}else if(s>0.25){
ahead(50);
}else back(50);
}
}

public void onScannedRobot(ScannedRobotEvent e) {//ロボット索敵
double energy;
double distance;
distance = e.getDistance();
System.out.println("found;"+e.getName());
if(distance<10){
fire(10);//見つけたらファイア!
}else fire(1);
System.out.println("energy;"+getEnergy());
System.out.println("distance;"+distance);
scan();//索敵
}
public void onHitRobot(HitRobotEvent e) {
// If he's in front of us, set back up a bit.
// もし敵が私たちの前にいれば、少し後退します。
if (e.getBearing() > -90 && e.getBearing() < 90)
back(100);
// else he's in back of us, so set ahead a bit.
// そうでなければ敵は後ろにいるので、すこし前進します。
else
ahead(100);
}
public void onHitByBullet(ScannedRobotEvent e) {

if (getEnergy()>50){//エネルギーが50以上なら
fire(3);//火力3で発射
}
else{
fire(1);//そうでないならば火力1で発射
}

scan();
}

public void onHitByBullet(HitByBulletEvent e) {//敵に攻撃されたら
scan();
turnLeft(90 - e.getBearing());//砲台の方向から左へ90度向く
back(200);//後ろへ90
}
public void onHitWall(HitWallEvent e) {//壁に当たったら
turnGunRight(180); //砲台を右へ180度
turnRight(90);
ahead(200);
}
}
by viewtleaf | 2004-11-16 15:31 | JAVAプログラミング
<< イブラインロウ初等学校とのWe... ホバークラフト、完成 >>



毎日起こった出来事を記入し、「振り返り」に活用したいと思います。写真は我が子の作品。
by viewtleaf
S M T W T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31