Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Robot class
- public class Robot {
- private int name;
- public int place = 10;
- public boolean inGame = false;
- Robot(int name)
- {
- this.name = name;
- this.inGame = true;
- }
- boolean getInGame()
- {
- return this.inGame;
- }
- int getPlace()
- {
- return place;
- }
- int setNewPlace()
- {
- int random = (int)(Math.random()*4+1);
- int n = (int)(Math.random()*6+1);
- switch(random)
- {
- case 1 : this.place++;
- case 2 : this.place--;
- case 3 : this.place+=n;
- case 4 : this.place-=n;
- }
- return this.place;
- }
- }
- //Main class
- import java.util.*;
- public class MainRobot {
- public static boolean checkInGameToAll(Robot[] robots)
- {
- int count=0;
- for(int i = 0; i<robots.length;i++)
- {
- if(robots[i].inGame == true)
- count++;
- }
- if(count>1)
- return true;
- else
- return false;
- }
- public static boolean checkIfWin(Robot[] robots)
- {
- boolean flag = true;
- for(int i = 0; i<robots.length;i++)
- {
- if(robots[i].getPlace() == 100)
- flag = false;
- }
- return flag;
- }
- public static int checkWhoWon(Robot[] robots)
- {
- int serialWon = 0;
- if(checkIfWin(robots)==false)
- {
- for(int i = 0; i<robots.length;i++)
- {
- if(robots[i].inGame == true)
- serialWon = i;
- }
- return serialWon;
- }
- else{
- boolean flag = true;
- for(int i = 0; i<robots.length;i++)
- {
- while(flag == true)
- {
- if(robots[i].getPlace() == 100)
- {
- flag = false;
- serialWon = i;
- }
- }
- }
- return serialWon;
- }
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int robotsNum = in.nextInt();
- Robot[] robots = new Robot[robotsNum];
- for(int i=0;i<robots.length;i++)
- robots[i] = new Robot(i);
- while(checkInGameToAll(robots) && checkIfWin(robots))
- {
- for(int i=0;i<robots.length;i++)
- {
- if(robots[i].inGame == true)
- {
- robots[i].setNewPlace();
- if(robots[i].getPlace()>100 || robots[i].getPlace()<0)
- robots[i].inGame = false;
- else
- {
- for(int j=0;j<robots.length;j++)
- {
- if(robots[i].getPlace() == robots[j].getPlace() && i!=j)
- robots[j].inGame = false;
- }
- }
- }
- }
- }
- System.out.println("Robot"+checkWhoWon(robots)+" won!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment