SHARE
TWEET

Untitled

a guest Jun 18th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package rst;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.Scanner;
  6. import javax.swing.Action;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11.  
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPanel;
  14. import static rst.Rst.show;
  15.  
  16. public class Rst {
  17.  
  18.     static char[] puzzle;
  19.     static char[] display;
  20.  
  21.     public static void main(String[] args) {
  22.         welcomingmethod();
  23.         String[] players = createPlayers();
  24.         int[] scores = new int[players.length];
  25.         int currentPlayer = 0;
  26.  
  27.         while (!checkWin(puzzle, display)) {
  28.             if (currentPlayer >= players.length) {
  29.                 currentPlayer = 0;
  30.             }
  31.             if (guess(puzzle, display, players[currentPlayer])) {
  32.                 show(display);
  33.                 scores[currentPlayer] += 10;
  34.                 JOptionPane.showMessageDialog(null, "Correct!");
  35.  
  36.             } else {
  37.                 JOptionPane.showMessageDialog(null, "Incorrect!");
  38.                 currentPlayer++;
  39.  
  40.             }
  41.         }
  42.  
  43.         printScores(players, scores);
  44.  
  45.     }
  46.  
  47.     public static void printScores(String[] players, int[] scores) {
  48.         for (int i = 0; i < players.length; i++) {
  49.             System.out.println(players[i] + ": " + scores[i]);
  50.         }
  51.     }
  52.  
  53.     public static void show(String[] display) {
  54.         for (int i = 0; i < display.length; i++) {
  55.             System.out.print(display[i] + " ");
  56.         }
  57.         System.out.println("");
  58.     }
  59.  
  60.     public static boolean guess(String[] puzzle, String[] display, String player) {
  61.         String guess = JOptionPane.showInputDialog(player + ": Guess a letter in the puzzle");
  62.  
  63.         boolean correct = false;
  64.         for (int i = 0; i < puzzle.length; i++) {
  65.             if (puzzle[i].equals(guess)) {
  66.                 display[i] = guess;
  67.                 correct = true;
  68.             }
  69.         }
  70.         return correct;
  71.  
  72.     }
  73.  
  74.     public static boolean checkWin(String[] puzzle, String[] display) {
  75.         for (int i = 0; i < puzzle.length; i++) {
  76.             if (!puzzle[i].equals(display[i])) {
  77.                 return false;
  78.             }
  79.         }
  80.         return true;
  81.     }
  82.  
  83.     public static String[] createPlayers() {
  84.         int numOfPlayers = Integer.parseInt(JOptionPane.showInputDialog("Enter number of players "));
  85.        
  86.         String[] players = new String[];
  87.  
  88.         for (int i = 0; i < players.length; i++) {
  89.             System.out.print("Enter player " + (i + 1) + " name: ");
  90.             players[i] = input.nextLine();
  91.         }
  92.  
  93.         return players;
  94.     }
  95.  
  96.     private static void welcomingmethod() {
  97.         JFrame frame2 = new JFrame("Welcome");
  98.         frame2.setVisible(true);
  99.         frame2.setSize(1000, 1000);
  100.         JButton newPuzzle = new JButton("Press for new puzzle");
  101.         frame2.add(newPuzzle);
  102.         newPuzzle.addActionListener(new ActionListener() {
  103.             public void actionPerformed(ActionEvent e) {
  104.                 String word = JOptionPane.showInputDialog("Choose a word to use");
  105.                 puzzle = word.toCharArray();
  106.                 display = new char[puzzle.length];
  107.                 for (int i = 0; i < display.length; i++) {
  108.                     display[i] = '_';
  109.                 }
  110.             }
  111.         });
  112.  
  113.     }
  114.  
  115. //    static class Action implements ActionListener {
  116. //
  117. //        public void actionPerformed(ActionEvent e) {
  118. //            JFrame frame2 = new JFrame("You win");
  119. //            frame2.setVisible(true);
  120. //            frame2.setSize(1000, 1000);
  121. //            JLabel winimage = new JLabel();
  122. //            winimage.setIcon(new ImageIcon("images/popart_car_red.png"));
  123. //            winimage.setBounds((1300), (240), winimage.getPreferredSize().width, winimage.getPreferredSize().height);
  124. //
  125. //            frame2.add(winimage);
  126. //
  127. //        }
  128. //    }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top