Guest User

Untitled

a guest
Feb 9th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. //Robot class
  2. public class Robot {
  3.    
  4.     private int name;
  5.     public int place = 10;
  6.     public boolean inGame = false;
  7.    
  8.     Robot(int name)
  9.     {
  10.         this.name = name;
  11.         this.inGame = true;
  12.     }
  13.     boolean getInGame()
  14.     {
  15.         return this.inGame;
  16.     }
  17.     int getPlace()
  18.     {
  19.         return place;
  20.     }
  21.     int setNewPlace()
  22.     {
  23.         int random = (int)(Math.random()*4+1);
  24.         int n = (int)(Math.random()*6+1);
  25.         switch(random)
  26.         {
  27.         case 1 : this.place++;
  28.         case 2 : this.place--;
  29.         case 3 : this.place+=n;
  30.         case 4 : this.place-=n;
  31.         }
  32.         return this.place;
  33.     }
  34. }
  35.  
  36.  
  37. //Main class
  38. import java.util.*;
  39. public class MainRobot {
  40.     public static boolean checkInGameToAll(Robot[] robots)
  41.     {
  42.         int count=0;
  43.         for(int i = 0; i<robots.length;i++)
  44.         {
  45.             if(robots[i].inGame == true)
  46.                 count++;
  47.         }
  48.         if(count>1)
  49.             return true;
  50.         else
  51.             return false;
  52.        
  53.     }
  54.     public static boolean checkIfWin(Robot[] robots)
  55.     {
  56.         boolean flag = true;
  57.         for(int i = 0; i<robots.length;i++)
  58.         {          
  59.                 if(robots[i].getPlace() == 100)
  60.                         flag = false;
  61.         }
  62.         return flag;
  63.     }
  64.     public static int checkWhoWon(Robot[] robots)
  65.     {
  66.         int serialWon = 0;
  67.         if(checkIfWin(robots)==false)
  68.         {
  69.             for(int i = 0; i<robots.length;i++)
  70.             {
  71.                 if(robots[i].inGame == true)
  72.                     serialWon = i; 
  73.             }
  74.             return serialWon;
  75.         }
  76.         else{
  77.         boolean flag = true;
  78.         for(int i = 0; i<robots.length;i++)
  79.         {
  80.             while(flag == true)
  81.             {
  82.                 if(robots[i].getPlace() == 100)
  83.                 {
  84.                     flag = false;
  85.                     serialWon = i;
  86.                 }
  87.             }
  88.         }
  89.         return serialWon;
  90.         }
  91.     }
  92.        
  93.     public static void main(String[] args) {
  94.         Scanner in = new Scanner(System.in);
  95.         int robotsNum = in.nextInt();
  96.         Robot[] robots = new Robot[robotsNum];
  97.         for(int i=0;i<robots.length;i++)
  98.             robots[i] = new Robot(i);
  99.  
  100.         while(checkInGameToAll(robots) && checkIfWin(robots))
  101.         {
  102.             for(int i=0;i<robots.length;i++)
  103.             {
  104.                 if(robots[i].inGame == true)
  105.                 {
  106.                     robots[i].setNewPlace();
  107.                     if(robots[i].getPlace()>100 || robots[i].getPlace()<0)
  108.                         robots[i].inGame = false;
  109.                     else
  110.                     {
  111.                         for(int j=0;j<robots.length;j++)
  112.                         {
  113.                             if(robots[i].getPlace() == robots[j].getPlace() && i!=j)
  114.                                 robots[j].inGame = false;
  115.                                
  116.                         }
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.  
  122.         System.out.println("Robot"+checkWhoWon(robots)+" won!");
  123.        
  124.  
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment