Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code in java. Must be full program. Please I need this soon. Create a program th

ID: 3671856 • Letter: C

Question

Code in java. Must be full program. Please I need this soon.

Create a program that draws a circle with four seperate parts. Each part should be filled in with a different color: one red, one green, one yellow, and one blue. The result should look like this:

This program should randomly turn three of the four segments white, one at a time, in a pattern that can be replicated by the user. The user will then click each segment in the order that it was turned white. If the user replicates the pattern correctly, the program should display a message saying 'You won!' and close. If not, the program will display a message saying 'You lost!' and close.

Explanation / Answer

GUI.java


import static edu.princeton.cs.introcs.StdDraw.*;
import java.awt.Color;

public class GUI {

   public static Color VIOLET = new Color(102, 0, 153);

   public static void main(String[] args) {
       new GUI().run();
   }

   /** Game model associated with this GUI. */
   private Game game;

   /** Question or instructions for the user. */
   private String label;

   public GUI() {
       game = new Game();
       label = "";
   }

   private void drawGuess(Color color, double x, double y) {

       setPenColor(color);
       filledCircle(x, y, 0.03);

   }

   private void drawPins(int black, int white, double y) {

       setPenColor(BLACK);
       filledCircle(.8, y, 0.03);
       setPenColor(WHITE);
       text(.8, y, "" + black);

       setPenColor(BLACK);
       circle(.9, y, 0.03);
       text(.9, y, "" + white);

   }

   /** Waits for the user to press a key, then returns that character. */
   protected char waitForKey() {
       while (!hasNextKeyTyped()) {
           // Wait for keypress
       }
       return nextKeyTyped();
   }

   public void run() {

       double x = 0.1;
       double y = 0.9;
       double dx = 0.075;
       double dy = 0.075;

       label = "Press letter keyes (r, o, y, g, b, v) to guess";

       text(.5, .1, label);

       while (!game.isWon() && !game.isLost()) {

           String guess = "";

           for (int i = 0; i < 4; i++) {

               char key = waitForKey();

               if (key == 'r') {
                   guess += key;
                   drawGuess(RED, x + i * dx, y);
               } else if (key == 'o') {
                   guess += key;
                   drawGuess(ORANGE, x + i * dx, y);
               } else if (key == 'y') {
                   guess += key;
                   drawGuess(YELLOW, x + i * dx, y);
               } else if (key == 'g') {
                   guess += key;
                   drawGuess(GREEN, x + i * dx, y);
               } else if (key == 'b') {
                   guess += key;
                   drawGuess(BLUE, x + i * dx, y);
               } else if (key == 'v') {
                   guess += key;
                   drawGuess(VIOLET, x + i * dx, y);
               } else {
                   i--;
               }
           }

           game.guess(guess);
           drawPins(game.getNumberOfBlackPegs(guess),
                   game.getNumberOfWhitePegs(guess), y);

           y -= dy;

       }

       setPenColor(WHITE);
       filledRectangle(.5, .1, .5, .075);
       setPenColor(BLACK);

       if (game.isWon()) {
           text(.5, .1, "You won!");
       } else {
           text(.5, .1, "Sorry, you lost. Here is the answer:");
           char[] c = game.getCorrect().toCharArray();
           for (int i = 0; i < c.length; i++) {

               if (c[i] == 'r') {
                   drawGuess(RED, x + i * dx, 0);
               } else if (c[i] == 'o') {
                   drawGuess(RED, x + i * dx, 0);
               } else if (c[i] == 'y') {
                   drawGuess(YELLOW, x + i * dx, 0);
               } else if (c[i] == 'g') {
                   drawGuess(GREEN, x + i * dx, 0);
               } else if (c[i] == 'b') {
                   drawGuess(BLUE, x + i * dx, 0);
               } else if (c[i] == 'v') {
                   drawGuess(VIOLET, x + i * dx, 0);
               } else {
                   i--;
               }
           }
       }
   }
}


Game.java

import static edu.princeton.cs.introcs.StdRandom.uniform;

public class Game {

   private final char[] choices = { 'r', 'o', 'y', 'b', 'g', 'v' };

   private String code;

   private String cdeo;

   private String[] guess = new String[10];

   private int guesses = 0;

   public Game() {
       code = "";
       for (int i = 0; i < 4; i++) {
           code += choices[uniform(choices.length)];
       }
       cdeo = sort(code);
   }

   public Game(String code) {
       this.code = code;
       cdeo = sort(code);
   }

   public String sort(String string) {
       char[] a = string.toCharArray();

       for (int i = 1; i < a.length; i++) {
           for (int j = i; j > 0; j--) {
               if (a[j - 1] > (a[j])) {
                   char c = a[j];
                   a[j] = a[j - 1];
                   a[j - 1] = c;
               }
           }
       }

       String s = "";
       for (int i = 0; i < a.length; i++) {
           s += a[i];
       }

       return s;
   }

   public int getNumberOfBlackPegs(String guess) {
       int black = 0;
       for (int i = 0; i < guess.length(); i++) {
           if (code.charAt(i) == guess.charAt(i)) {
               black++;
           }
       }
       return black;
   }

   public int getNumberOfWhitePegs(String guess) {
       int white = 0;
       String egssu = sort(guess);
       int i = 0;
       int j = 0;
       while (i < cdeo.length() && j < egssu.length()) {
           if (cdeo.charAt(i) == egssu.charAt(j)) {
               white++;
               i++;
               j++;
           } else if (cdeo.charAt(i) > egssu.charAt(j)) {
               j++;
           } else {
               i++;
           }
       }
      
       white -= getNumberOfBlackPegs(guess);
      
       return white;
   }

   public void guess(String guess) {
       this.guess[guesses] = guess;
       guesses++;
   }

   public int getNumberOfGuessesMade() {
       return guesses;
   }

   public String getGuess(int i) {
       return guess[i];
   }

   public boolean isWon() {
       if (guesses > 0 && guess[guesses - 1].equals(code))
           return true;
       return false;
   }

   public boolean isLost() {
       if (!isWon() && guesses > 9)
           return true;
       return false;
   }

   public String getCorrect() {
       return code;
   }

}

GameTest.java


import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class GameTest {

   private Game game;

   @Before
   public void setUp() throws Exception {
       game = new Game("royr");
   }

   /**
   * This one is pretty simple. Just count the number of matches between the
   * guess and the correct answer (which was passed to the Game constructor).
   */
   @Test
   public void testGetNumberOfBlackPegs() {
       assertEquals(4, game.getNumberOfBlackPegs("royr"));
       assertEquals(2, game.getNumberOfBlackPegs("bogr"));
       assertEquals(1, game.getNumberOfBlackPegs("rroy"));
   }

   /**
   * Use any sorting algorithm, e.g., insertion sort. Since Strings are
   * immutable, you'll have to convert the argument to a char[] using the
   * String method toCharArray(), then convert it back to a String using the
   * String constructor that takes a char[] as an argument.
   */
   @Test
   public void testSort() {
       assertEquals("gory", game.sort("royg"));
   }

   /**
   * This is the difficult one. It is much easier if you first sort the guess
   * and the correct answer. Be sure to subtract the number of black pegs
   * before returning the result.
   */
   @Test
   public void testGetNumberOfWhitePegs() {
       assertEquals(0, game.getNumberOfWhitePegs("royr"));
       assertEquals(2, game.getNumberOfWhitePegs("brgy"));
       assertEquals(3, game.getNumberOfWhitePegs("rroy"));
       game = new Game("rooo");
       assertEquals(1, game.getNumberOfWhitePegs("grrr"));
       game = new Game("oorr");
       assertEquals(2, game.getNumberOfWhitePegs("bboo"));
   }
  
   /**
   * This tests some getters and the guess() method, which stores a guess.
   */
   @Test
   public void testGuess() {
       assertEquals(0, game.getNumberOfGuessesMade());
       game.guess("ygbv");
       assertEquals(1, game.getNumberOfGuessesMade());
       game.guess("ovgg");
       assertEquals(2, game.getNumberOfGuessesMade());
       assertEquals("ygbv", game.getGuess(0));
       assertEquals("ovgg", game.getGuess(1));
   }

   /**
   * The game is won if the last guess was correct.
   */
   @Test
   public void testIsWon() {
       assertFalse(game.isWon());
       game.guess("royr");
       assertTrue(game.isWon());
   }

   /**
   * The game is lost if there have been 10 guesses.
   */
   @Test
   public void testIsLost() {
       assertFalse(game.isLost());
       for (int i = 0; i < 9; i++) {
           game.guess("bvrg");
       }
       assertFalse(game.isLost());
       game.guess("bvrg");
       assertTrue(game.isLost());
   }

   /**
   * If the 10th guess is correct, the game is still won.
   */
   @Test
   public void testWinSupersedesLoss() {
       assertFalse(game.isLost());
       for (int i = 0; i < 9; i++) {
           game.guess("bvrg");
       }
       assertFalse(game.isLost());
       game.guess("royr");
       assertFalse(game.isLost());
       assertTrue(game.isWon());
   }

   /**
   * This tests another getter.
   */
   @Test
   public void testGetCorrect() {
       assertEquals("royr", game.getCorrect());
   }

   /**
   * The zero-argument constructor should construct a random 4-character
   * correct answer, using the letters r, o, y, g, b, and v.
   */
   @Test
   public void testRandomCorrect() {
       assertFalse(new Game().getCorrect().equals(new Game().getCorrect()));
   }

}