Skip to content

Instantly share code, notes, and snippets.

@ababup1192
Created November 13, 2014 07:47
Show Gist options
  • Save ababup1192/cef417856d47e554b7aa to your computer and use it in GitHub Desktop.
Save ababup1192/cef417856d47e554b7aa to your computer and use it in GitHub Desktop.
test
public class FootParts{
private int speed;
public FootParts(int speed){
this.speed = speed;
}
public int getSpeed(){
return speed;
}
public void setSpeed(int speed){
this.speed = speed;
}
public String getInfo(){
return "FootParts(speed=" + speed + ")";
}
}
public class RacingRobot{
private int id;
private static int maxId;
private String name;
private int distance;
private int energy;
private FootParts footParts;
private int speed;
public RacingRobot(int id, String name, int distance, int energy, int speed, FootParts footParts){
this.id = id;
maxId = id;
this.name = name;
this.distance = distance;
this.energy = energy;
this.speed = speed;
this.footParts = footParts;
}
public RacingRobot(String name, int speed){
this(++maxId, name, 0, 100, speed, new FootParts(15));
}
public RacingRobot(String name){
this(name, 10);
}
public void run(){
if(isRunning()){
energy = energy - 20;
distance += speed + footParts.getSpeed();
}
}
public boolean isRunning(){
return energy >= 0;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getDistance(){
return distance;
}
public void setDistance(int distance){
this.distance = distance;
}
public int getEnergy(){
return energy;
}
public void setEnergy(int energy){
this.energy = energy;
}
public int getSpeed(){
return speed;
}
public void setSpeed(int speed){
this.speed = speed;
}
public FootParts getFootParts(){
return footParts;
}
public void setFootParts(FootParts footParts){
this.footParts = footParts;
}
public String getInfo(){
return String.format("Robot(id=%2d, name=%6s, energy=%d, distance=%3d, %s)", id, name, energy, distance, footParts.getInfo());
}
}
public class Robot{
private int id;
private static int maxId;
private String name;
private int distance;
private int energy;
public Robot(int id, String name, int distance, int energy){
this.id = id;
maxId = id;
this.name = name;
this.distance = distance;
this.energy = energy;
}
public Robot(String name){
this(++maxId, name, 0, 100);
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getDistance(){
return distance;
}
public void setDistance(int distance){
this.distance = distance;
}
public int getEnergy(){
return energy;
}
public void setEnergy(int energy){
this.energy = energy;
}
public String getInfo(){
return String.format("Robot(id=%d, name=%6s, energy=%d, distance=%d)", id, name, energy, distance);
}
}
import java.util.Random;
public class RobotSimulator{
private RacingRobot[] robotArray;
private String[] robotNameArray = {"Ebi", "Kani", "Uni", "Tako", "Ika", "Maguro"};
public RobotSimulator(){
// ロボット登録
register();
// 初期状態
printRobotInfo();
System.out.println("レース開始!");
for(int i=0; i<5; i++){
for(int j=0; j<6; j++){
robotArray[j].run();
}
}
System.out.println("レース終了!");
// レース後
printRobotInfo();
}
public void register(){
robotArray = new RacingRobot[6];
Random random = new Random();
for(int i=0; i<6; i++){
robotArray[i] = new RacingRobot(robotNameArray[i] , random.nextInt(10));
}
}
public void printRobotInfo(){
System.out.println("-----ロボット情報-----");
for(int i=0; i<6; i++){
System.out.println(robotArray[i].getInfo());
}
System.out.println("--------------------");
}
public static void main(String[] args){
new RobotSimulator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment