Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. import edu.cmu.ri.createlab.terk.robot.finch.Finch;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. import java.awt.Color;
  7. import javax.swing.JOptionPane;
  8.  
  9. public class SimonFinch {
  10.     public static void main(String args[]) throws Exception{
  11.         Finch red_bot = new Finch();
  12.         Finch blue_bot = new Finch();
  13.         Finch green_bot = new Finch();
  14.         Finch yellow_bot = new Finch();
  15.         //Finch switch_bot = new Finch();
  16.        
  17.         Scanner input = new Scanner(System.in);
  18.         String continue_g = "yes";
  19.        
  20.         String colors[] = {"red", "blue", "green"};//Colors to be randomized
  21.        
  22.         ArrayList<String> game_s = new ArrayList<String>();//Declaring the game sequence as a list
  23.         ArrayList<String> player_s = new ArrayList<String>();//Declaring player sequence as a list
  24.         int Score = 0;//Declaring and storing the current score
  25.        
  26.         game_s.add((colors[new Random().nextInt(colors.length)]));//starts with one random colors on the game sequence
  27.         LightBots(2000, red_bot, blue_bot, green_bot, yellow_bot);
  28.         JOptionPane.showMessageDialog(null, "\nCommencing Game" +
  29.                                             "\nCover the Finch's Beak To Select Your Color For The Sequence" +
  30.                                             "\nClick OK To Start...");
  31.        
  32.         while(/*!switch_bot.isBeakUp()*/ continue_g.equals("yes"))
  33.         {
  34.            
  35.             for(int i = 0; i < game_s.size(); i++)
  36.             {
  37.                 int duration;
  38.                 //Changing the speed of ligthing up the bots after specific amount of loops
  39.                 if(i <= 5){
  40.                     duration = 3000;//Bots light up for 3 seconds for  loops
  41.                 }
  42.                 else if (i >= 6 && i <= 10){
  43.                     duration = 2000;//after 5 loops, it lights up for 2 seconds
  44.                 }
  45.                
  46.                 else {
  47.                     duration = 1000;//after 10 loops it only light up for 1 second
  48.                 }
  49.                 //Displays current sequence of the game_s list
  50.                 System.out.println("Sequence " + (i + 1) + ":");
  51.                 for (int j = 0; j < game_s.size(); j++)
  52.                 {
  53.                     DisplaySequence(duration, game_s.get(j), red_bot, blue_bot, green_bot, yellow_bot);  
  54.                 }
  55.                
  56.                 //Loop to enter values on the player_sequence list
  57.                 for(int k = 0; k < game_s.size(); k++)
  58.                 {
  59.                    
  60.                     System.out.println("Cover The Corrensponding Finch");
  61.                     //JOptionPane.showMessageDialog(null,   "Lift bot" +
  62.                             //"\nClick OK To Start...");
  63.                     WaitObstacle(red_bot, blue_bot, green_bot, yellow_bot);//Call to WaitObstacle                                      
  64.                     player_s.add(PlaySequence(red_bot, blue_bot, green_bot, yellow_bot));//Assigning returned value of the method to player_s
  65.                     //checks if value entered in the player_s matches with the value of the game_s
  66.                     if(game_s.get(k).equals(player_s.get(k)))
  67.                     {                      
  68.                         continue;
  69.                     }
  70.                     //if it doesn't match...
  71.                     else
  72.                     {
  73.                         player_s.clear();//...clear all values from player sequence and game sequence and then stop the loop
  74.                         game_s.clear();
  75.                         break;
  76.                     }
  77.                 }
  78.                
  79.                 //Once every value is added to the player list without breaks, it will be equal to the game sequence...
  80.                 if(game_s.equals(player_s))
  81.                 {
  82.                     game_s.add((colors[new Random().nextInt(colors.length)]));//...Therefore add another random color to the game sequence
  83.                     player_s.clear();//clear player sequence as the player needs to type the sequence again
  84.                     Score += 1;//add one to the score
  85.                     JOptionPane.showMessageDialog(null, "Your Score is " + Score + "\nClick Ok To Continue");//Display the current score
  86.                 }
  87.             }
  88.             Score = 0;
  89.             System.out.println("Game Over");//if player sequence is not equal to game sequence, break out of the loop and display game over
  90.             System.out.println("Would You Like To Continue?");
  91.             continue_g = input.nextLine();
  92.         }
  93.         input.close();//closes inputs
  94.     }      
  95.    
  96.     //User Defined Methods
  97.     //Lights up all the bots
  98.     public static void LightBots(int duration, Finch red, Finch blue, Finch green, Finch yellow){
  99.         red.setLED(Color.red, duration);
  100.         blue.setLED(Color.blue, duration);
  101.         green.setLED(Color.green, duration);
  102.         yellow.setLED(Color.yellow, duration);
  103.     }
  104.    
  105.     //Lights up the bots based on the game sequence array.
  106.     public static void DisplaySequence(int duration, String color, Finch red, Finch blue, Finch green, Finch yellow){
  107.         //value of color will be equal to the value in the game sequence array
  108.         //Lights up and buzzes finch based on the color store in the game sequence array
  109.         if(color.equals("red")){
  110.             red.buzz(50, 500);
  111.             red.setLED(Color.red, duration);
  112.             System.out.println(color);
  113.         }
  114.        
  115.         else if(color.equals("blue")){
  116.             blue.buzz(100, 500);
  117.             blue.setLED(Color.blue, duration);
  118.             System.out.println(color);
  119.         }
  120.         else if(color.equals("green")){
  121.             green.buzz(150, 500);
  122.             green.setLED(Color.green, duration);
  123.             System.out.println(color);
  124.         }
  125.         else if(color.equals("yellow")){
  126.             yellow.buzz(200, 500);
  127.             yellow.setLED(Color.yellow, duration);
  128.             System.out.println(color);
  129.         }
  130.         else{
  131.             System.out.println("your program sucks");
  132.         }
  133.     }
  134.    
  135.     //Waits for a finch to be tapped
  136.     public static void WaitObstacle(Finch red, Finch blue, Finch green, Finch yellow) throws Exception{
  137.         //loop while none of the bots have been tapped
  138.         while(  !red.isObstacle() && !blue.isObstacle()
  139.                 && !green.isObstacle() && yellow.isObstacle())
  140.         {
  141.             LightBots(1, red, blue, green, yellow);
  142.             Thread.sleep(1);//keeps pausing for one ms
  143.         }
  144.     }
  145.    
  146.     public static String PlaySequence(Finch red, Finch blue, Finch green, Finch yellow){
  147.         String color;
  148.         //Returns color in String format based on which finch has an obstacle
  149.         //Returned value will be added to the player_s array
  150.         if (red.isObstacle()){
  151.             red.buzz(50, 500);
  152.             red.setLED(Color.red, 500);
  153.             color = "red";
  154.            
  155.         }
  156.         else if (blue.isObstacle()){
  157.             blue.buzz(100, 500);
  158.             blue.setLED(Color.blue, 500);
  159.             color = "blue";        
  160.         }
  161.         else if (green.isObstacle()){
  162.             green.buzz(150, 500);
  163.             green.setLED(Color.green, 500);
  164.             color = "green";
  165.         }
  166.         else if (yellow.isObstacle()){
  167.             yellow.buzz(200, 500);
  168.             yellow.setLED(Color.yellow, 500);
  169.             color = "yellow";
  170.         }
  171.         else{
  172.             System.out.println("Give up on life");
  173.             color = "fail";
  174.         }
  175.         return color;
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement