Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import edu.cmu.ri.createlab.terk.robot.finch.Finch;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Scanner;
- import java.awt.Color;
- import javax.swing.JOptionPane;
- public class SimonFinch {
- public static void main(String args[]) throws Exception{
- Finch red_bot = new Finch();
- Finch blue_bot = new Finch();
- Finch green_bot = new Finch();
- Finch yellow_bot = new Finch();
- //Finch switch_bot = new Finch();
- Scanner input = new Scanner(System.in);
- String continue_g = "yes";
- String colors[] = {"red", "blue", "green"};//Colors to be randomized
- ArrayList<String> game_s = new ArrayList<String>();//Declaring the game sequence as a list
- ArrayList<String> player_s = new ArrayList<String>();//Declaring player sequence as a list
- int Score = 0;//Declaring and storing the current score
- game_s.add((colors[new Random().nextInt(colors.length)]));//starts with one random colors on the game sequence
- LightBots(2000, red_bot, blue_bot, green_bot, yellow_bot);
- JOptionPane.showMessageDialog(null, "\nCommencing Game" +
- "\nCover the Finch's Beak To Select Your Color For The Sequence" +
- "\nClick OK To Start...");
- while(/*!switch_bot.isBeakUp()*/ continue_g.equals("yes"))
- {
- for(int i = 0; i < game_s.size(); i++)
- {
- int duration;
- //Changing the speed of ligthing up the bots after specific amount of loops
- if(i <= 5){
- duration = 3000;//Bots light up for 3 seconds for loops
- }
- else if (i >= 6 && i <= 10){
- duration = 2000;//after 5 loops, it lights up for 2 seconds
- }
- else {
- duration = 1000;//after 10 loops it only light up for 1 second
- }
- //Displays current sequence of the game_s list
- System.out.println("Sequence " + (i + 1) + ":");
- for (int j = 0; j < game_s.size(); j++)
- {
- DisplaySequence(duration, game_s.get(j), red_bot, blue_bot, green_bot, yellow_bot);
- }
- //Loop to enter values on the player_sequence list
- for(int k = 0; k < game_s.size(); k++)
- {
- System.out.println("Cover The Corrensponding Finch");
- //JOptionPane.showMessageDialog(null, "Lift bot" +
- //"\nClick OK To Start...");
- WaitObstacle(red_bot, blue_bot, green_bot, yellow_bot);//Call to WaitObstacle
- player_s.add(PlaySequence(red_bot, blue_bot, green_bot, yellow_bot));//Assigning returned value of the method to player_s
- //checks if value entered in the player_s matches with the value of the game_s
- if(game_s.get(k).equals(player_s.get(k)))
- {
- continue;
- }
- //if it doesn't match...
- else
- {
- player_s.clear();//...clear all values from player sequence and game sequence and then stop the loop
- game_s.clear();
- break;
- }
- }
- //Once every value is added to the player list without breaks, it will be equal to the game sequence...
- if(game_s.equals(player_s))
- {
- game_s.add((colors[new Random().nextInt(colors.length)]));//...Therefore add another random color to the game sequence
- player_s.clear();//clear player sequence as the player needs to type the sequence again
- Score += 1;//add one to the score
- JOptionPane.showMessageDialog(null, "Your Score is " + Score + "\nClick Ok To Continue");//Display the current score
- }
- }
- Score = 0;
- System.out.println("Game Over");//if player sequence is not equal to game sequence, break out of the loop and display game over
- System.out.println("Would You Like To Continue?");
- continue_g = input.nextLine();
- }
- input.close();//closes inputs
- }
- //User Defined Methods
- //Lights up all the bots
- public static void LightBots(int duration, Finch red, Finch blue, Finch green, Finch yellow){
- red.setLED(Color.red, duration);
- blue.setLED(Color.blue, duration);
- green.setLED(Color.green, duration);
- yellow.setLED(Color.yellow, duration);
- }
- //Lights up the bots based on the game sequence array.
- public static void DisplaySequence(int duration, String color, Finch red, Finch blue, Finch green, Finch yellow){
- //value of color will be equal to the value in the game sequence array
- //Lights up and buzzes finch based on the color store in the game sequence array
- if(color.equals("red")){
- red.buzz(50, 500);
- red.setLED(Color.red, duration);
- System.out.println(color);
- }
- else if(color.equals("blue")){
- blue.buzz(100, 500);
- blue.setLED(Color.blue, duration);
- System.out.println(color);
- }
- else if(color.equals("green")){
- green.buzz(150, 500);
- green.setLED(Color.green, duration);
- System.out.println(color);
- }
- else if(color.equals("yellow")){
- yellow.buzz(200, 500);
- yellow.setLED(Color.yellow, duration);
- System.out.println(color);
- }
- else{
- System.out.println("your program sucks");
- }
- }
- //Waits for a finch to be tapped
- public static void WaitObstacle(Finch red, Finch blue, Finch green, Finch yellow) throws Exception{
- //loop while none of the bots have been tapped
- while( !red.isObstacle() && !blue.isObstacle()
- && !green.isObstacle() && yellow.isObstacle())
- {
- LightBots(1, red, blue, green, yellow);
- Thread.sleep(1);//keeps pausing for one ms
- }
- }
- public static String PlaySequence(Finch red, Finch blue, Finch green, Finch yellow){
- String color;
- //Returns color in String format based on which finch has an obstacle
- //Returned value will be added to the player_s array
- if (red.isObstacle()){
- red.buzz(50, 500);
- red.setLED(Color.red, 500);
- color = "red";
- }
- else if (blue.isObstacle()){
- blue.buzz(100, 500);
- blue.setLED(Color.blue, 500);
- color = "blue";
- }
- else if (green.isObstacle()){
- green.buzz(150, 500);
- green.setLED(Color.green, 500);
- color = "green";
- }
- else if (yellow.isObstacle()){
- yellow.buzz(200, 500);
- yellow.setLED(Color.yellow, 500);
- color = "yellow";
- }
- else{
- System.out.println("Give up on life");
- color = "fail";
- }
- return color;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement