Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Application Five - HANGMAN
- // 5th Six Weeks
- //
- //-----------------------------------------------------------------------------
- //
- // Name:
- // Period:
- // Teacher:
- // Computer Science I-K
- //
- ///////////////////////////////////////////////////////////////////////////////
- package BlackBox;
- import static java.lang.System.*;
- import java.io.*;
- import java.util.*;
- import java.awt.*;
- import javax.swing.*;
- public class App05
- {
- public static void main(String[] args) throws IOException
- {
- Game hangman = new Game();
- hangman.playGame();
- // hangman.setUp();
- // hangman.step();
- }
- }
- class Game extends BlackBox
- {
- //int x;
- //int y;
- private DataBase dBase;
- private boolean quit;
- private boolean reset;
- private String theWord;
- private boolean win;
- private boolean lose;
- private char theLetter;
- private int correctGuesses;
- private int wrongGuesses;
- private ArrayList<Character> usedLetters = new ArrayList<Character>();
- public Game() throws IOException
- {
- dBase = new DataBase();
- categoryBox.setText(dBase.getCategory());
- resetGameVariables();
- }
- /*public void test()
- {
- out.println(dBase);
- out.println("The category is " + dBase.getCategory());
- out.println("The word is " + dBase.getRandomWord());
- theWordBox.setText(dBase.getRandomWord());
- messageBox.setText("hi \n");
- messageBox.append("hi \n");
- messageBox.append("hi \n");
- usedLettersBox.setText("hi \n");
- scoreBox.setText("hi \n");
- letterBox.setText("hi \n");
- pen.setColor(Color.RED);
- pen.fillOval(100,100,20,40);
- pen.drawString("Bad things will happen here", 150, 50);
- repaint();
- } */
- private void resetGameVariables()
- {
- quit = false;
- reset = false;
- win = false;
- lose = false;
- usedLetters.clear();
- correctGuesses = 0;
- wrongGuesses = 0;
- theLetter = ' ';
- theWordBox.setText("");
- messageBox.setText("");
- usedLettersBox.setText("");
- scoreBox.setText("");
- letterBox.setText("");
- clearDrawingArea();
- }
- public void playGame()
- {
- while(!quit)
- {
- gameEngine();
- if(!reset && !quit)
- playAgain();
- }
- exit(0);
- // messageBox.setText("Done! Step 06 works! YEAH!");
- }
- private void gameEngine()
- {
- resetGameVariables();
- getTheWord();
- // theWordBox.setText(theWord);
- // messageBox.setText(dBase.toString());
- setup();
- if(!quit)
- showWordBlanks();
- showScore();
- while(!win && !lose && !quit && !reset)
- {
- if(theLetter != ' ')
- {
- processTheLetter();
- showScore();
- checkForWinner();
- animateGraphics();
- }
- delay(50);
- showResults();
- }
- // pen.setColor(Color.red);
- // pen.drawString("Play game here.", 10, 25);
- // for(int x = 0; x< 100; x+= 20)
- // pen.drawString("Round" + x/20, 10, 50 + x);
- // repaint();
- }
- private void checkForWinner()
- {
- win = (correctGuesses >= theWord.length());
- lose = (wrongGuesses >= 6);
- }
- private void showResults()
- {
- if(win)
- {
- JOptionPane.showMessageDialog
- (
- null,
- "Congratulations! You Win!",
- "Game Over",
- JOptionPane.INFORMATION_MESSAGE
- );
- Winner();
- //Loser();
- }
- if(lose)
- {
- JOptionPane.showMessageDialog
- (
- null,
- "You Lose!\nThe Phrase was \"" + theWord + "\"",
- "Game Over",
- JOptionPane.INFORMATION_MESSAGE
- );
- Loser();
- }
- }
- private void animateGraphics()
- {
- switch(wrongGuesses)
- {
- case 1: part1(); break;
- case 2: part2(); break;
- case 3: part3(); break;
- case 4: part4(); break;
- case 5: part5(); break;
- case 6: part6(); break;
- }
- repaint();
- }
- void part1()
- {
- Color skin = new Color(255,176,138);
- pen.setColor(skin);
- pen.fillOval(229,70,40,40);
- pen.setColor(Color.BLACK);
- pen.fillOval(236,83,5,5);
- pen.fillOval(252,83,5,5);
- pen.drawLine(242,101,251,101);
- Color brown = new Color(128,64,0);
- pen.setColor(brown);
- pen.fillOval(233,104,35,7);
- Color clothes = new Color(147,0,0);
- pen.setColor(clothes);
- //pen.fillRect(222,70,55,35);
- //pen.fillOval(210,69,35,35);
- //pen.fillOval(255,69,35,35);
- pen.fillOval(225,70,50,10);
- //pen.drawLine(250,40,230,80);
- //pen.drawLine(250,40,270,80);
- Polygon tri = new Polygon();
- tri.addPoint(250,40);
- tri.addPoint(230,80);
- tri.addPoint(270,80);
- pen.fill(tri);
- //pen.drawString("WRONG ACTION ONE", 10, 50);
- }
- void part2()
- {
- Color cape = new Color(0,128,192);
- pen.setColor(cape);
- pen.fillRect(225,110,50,90);
- Color cape2 = new Color(255,255,0);
- pen.setColor(cape2);
- pen.fillRect(225,190,50,8);
- Color clothes = new Color(147,0,0);
- pen.setColor(clothes);
- pen.fillRect(238,110,25,45);
- Color belt = new Color(128,64,0);
- pen.setColor(belt);
- pen.fillRect(238,140,25,6);
- //pen.drawString("WRONG ACTION TWO", 10, 60);
- }
- void part3()
- {
- Color skin = new Color(255,176,138);
- pen.setColor(skin);
- pen.drawLine(238,110,227,160);
- pen.fillOval(223,157,10,10);
- Color clothes = new Color(147,0,0);
- pen.setColor(clothes);
- pen.fillOval(219,106,25,10);
- //pen.drawString("WRONG ACTION THREE", 10, 70);
- }
- void part4()
- {
- Color staff = new Color(128,64,0);
- pen.setColor(staff);
- pen.fillRect(300,60,8,150);
- Color skin = new Color(255,176,138);
- pen.setColor(skin);
- pen.drawLine(262,110,295,130);
- pen.fillOval(295,127,10,10);
- Color clothes = new Color(147,0,0);
- pen.setColor(clothes);
- pen.fillOval(257,106,25,10);
- //pen.drawString("WRONG ACTION FOUR", 10, 80);
- }
- void part5()
- {
- pen.fillRect(239,150,10,50);
- pen.setColor(Color.BLACK);
- pen.fillOval(230,198,20,10);
- //pen.drawString("WRONG ACTION FIVE", 10, 90);
- }
- void part6()
- {
- Color clothes = new Color(147,0,0);
- pen.setColor(clothes);
- pen.fillRect(253,150,10,50);
- pen.setColor(Color.BLACK);
- pen.fillOval(245,198,20,10);
- pen.drawArc(242,96,10,10,360,200);
- }
- void Loser()
- {
- if(lose)
- {
- clearDrawingArea();
- pen.clearRect(0,0,1000,1000);
- pen.setColor(Color.BLACK);
- pen.fillRect(0,0,1000,1000);
- Color green = new Color(128,255,0);
- Color orange = new Color(255,128,64);
- Color purple = new Color(128,0,255);
- pen.setColor(green);
- pen.drawLine(25,0,25,600);
- pen.setColor(purple);
- pen.drawLine(50,0,50,600);
- pen.setColor(orange);
- pen.drawLine(75,0,75,600);
- pen.setColor(green);
- pen.drawLine(100,0,100,600);
- pen.setColor(purple);
- pen.drawLine(125,0,125,600);
- pen.setColor(orange);
- pen.drawLine(150,0,150,600);
- pen.setColor(green);
- pen.drawLine(175,0,175,600);
- pen.setColor(purple);
- pen.drawLine(200,0,200,600);
- pen.setColor(orange);
- pen.drawLine(225,0,225,600);
- pen.setColor(green);
- pen.drawLine(250,0,250,600);
- pen.setColor(purple);
- pen.drawLine(275,0,275,600);
- pen.setColor(orange);
- pen.drawLine(300,0,300,600);
- pen.setColor(green);
- pen.drawLine(325,0,325,600);
- pen.setColor(purple);
- pen.drawLine(350,0,350,600);
- pen.setColor(orange);
- pen.drawLine(375,0,375,600);
- pen.setColor(green);
- pen.drawLine(400,0,400,600);
- pen.setColor(purple);
- pen.drawLine(425,0,425,600);
- pen.setColor(orange);
- pen.drawLine(450,0,450,600);
- pen.setColor(green);
- pen.drawLine(475,0,475,600);
- pen.setColor(purple);
- pen.drawLine(500,0,500,600);
- pen.setColor(orange);
- pen.drawLine(525,0,525,600);
- pen.setColor(green);
- pen.drawLine(550,0,550,600);
- pen.setColor(purple);
- pen.drawLine(575,0,575,600);
- pen.setColor(orange);
- Color skin = new Color(255,176,138);
- pen.setColor(skin);
- pen.fillOval(229,70,40,40);
- pen.setColor(Color.BLACK);
- pen.drawLine(238,82,245,87);
- pen.drawLine(250,82,257,87);
- pen.drawLine(245,82,238,87);
- pen.drawLine(257,82,250,87);
- //pen.fillOval(236,83,5,5);
- //pen.fillOval(252,83,5,5);
- pen.drawLine(242,101,251,101);
- Color brown = new Color(128,64,0);
- pen.setColor(brown);
- pen.fillOval(233,104,35,7);
- // pen.setColor(clothes);
- //pen.fillRect(222,70,55,35);
- //pen.fillOval(210,69,35,35);
- //pen.fillOval(255,69,35,35);
- pen.fillOval(225,70,50,10);
- //pen.drawLine(250,40,230,80);
- //pen.drawLine(250,40,270,80);
- Polygon tri = new Polygon();
- tri.addPoint(250,40);
- tri.addPoint(230,80);
- tri.addPoint(270,80);
- pen.fill(tri);
- Color cape = new Color(0,128,192);
- pen.setColor(cape);
- pen.fillRect(225,110,50,90);
- Color cape2 = new Color(255,255,0);
- pen.setColor(cape2);
- pen.fillRect(225,190,50,8);
- // pen.setColor(clothes);
- pen.fillRect(238,110,25,45);
- Color belt = new Color(128,64,0);
- pen.setColor(belt);
- pen.fillRect(238,140,25,6);
- pen.setColor(skin);
- pen.drawLine(238,110,227,160);
- pen.fillOval(223,157,10,10);
- // pen.setColor(clothes);
- pen.fillOval(219,106,25,10);
- pen.setColor(skin);
- pen.drawLine(262,110,269,160);
- pen.fillOval(265,157,10,10);
- // pen.setColor(clothes);
- pen.fillOval(257,106,25,10);
- pen.fillRect(239,150,10,50);
- pen.setColor(Color.BLACK);
- pen.fillOval(230,198,20,10);
- // pen.setColor(clothes);
- pen.fillRect(253,150,10,50);
- pen.setColor(Color.BLACK);
- pen.fillOval(245,198,20,10);
- pen.drawArc(242,96,10,10,360,200);
- repaint();
- //pen.setBackgroundImage("gamevillage.jpg");
- }
- //pen.drawString("WRONG ACTION SIX", 10, 100);
- }
- void Winner()
- {
- if(win)
- {
- clearDrawingArea();
- pen.clearRect(0,0,1000,1000);
- Color sky = new Color(7,182,248);
- pen.setColor(sky);
- pen.fillRect(0,0,1000,1000);
- Color grass = new Color(0,128,64);
- pen.setColor(grass);
- pen.fillRect(0,200,1000,1000);
- Color yellow = new Color(255,255,0);
- pen.setColor(yellow);
- pen.fillOval(10,10,50,50);
- Color cloud = new Color(255,255,255);
- pen.setColor(cloud);
- pen.fillOval(250,10,30,30);
- pen.fillOval(260,15,20,30);
- pen.fillOval(260,5,20,30);
- pen.fillOval(270,10,20,30);
- pen.fillOval(270,15,20,30);
- pen.fillOval(270,5,20,30);
- pen.fillOval(280,10,20,30);
- pen.fillOval(280,15,20,30);
- pen.fillOval(280,5,20,30);
- pen.fillOval(290,10,20,30);
- pen.fillOval(380,20,30,30);
- pen.fillOval(390,25,20,30);
- pen.fillOval(390,15,20,30);
- pen.fillOval(400,20,20,30);
- pen.fillOval(400,25,20,30);
- pen.fillOval(400,15,20,30);
- pen.fillOval(410,20,20,30);
- pen.fillOval(410,25,20,30);
- pen.fillOval(420,15,20,30);
- pen.fillOval(420,20,20,30);
- pen.setColor(Color.BLACK);
- pen.fillRect(70,100,100,100);
- pen.fillRect(250,100,200,100);
- pen.setColor(Color.BLUE);
- pen.fillRect(80,130,20,20);
- pen.fillRect(370,130,20,20);
- pen.fillRect(300,130,20,20);
- Color door = new Color(128,64,0);
- pen.setColor(door);
- pen.fillRect(130,159,25,40);
- pen.fillRect(400,159,25,40);
- pen.setColor(Color.RED);
- Polygon roof1 = new Polygon();
- roof1.addPoint(120,50);
- roof1.addPoint(70,100);
- roof1.addPoint(170,100);
- pen.fill(roof1);
- Polygon roof2 = new Polygon();
- roof2.addPoint(350,50);
- roof2.addPoint(250,100);
- roof2.addPoint(450,100);
- pen.fill(roof2);
- /*
- Color green = new Color(128,255,0);
- Color orange = new Color(255,128,64);
- Color purple = new Color(128,0,255);
- pen.setColor(green);
- pen.drawLine(25,0,25,600);
- pen.setColor(purple);
- pen.drawLine(50,0,50,600);
- pen.setColor(orange);
- pen.drawLine(75,0,75,600);
- pen.setColor(green);
- pen.drawLine(100,0,100,600);
- pen.setColor(purple);
- pen.drawLine(125,0,125,600);
- pen.setColor(orange);
- pen.drawLine(150,0,150,600);
- pen.setColor(green);
- pen.drawLine(175,0,175,600);
- pen.setColor(purple);
- pen.drawLine(200,0,200,600);
- pen.setColor(orange);
- pen.drawLine(225,0,225,600);
- pen.setColor(green);
- pen.drawLine(250,0,250,600);
- pen.setColor(purple);
- pen.drawLine(275,0,275,600);
- pen.setColor(orange);
- pen.drawLine(300,0,300,600);
- pen.setColor(green);
- pen.drawLine(325,0,325,600);
- pen.setColor(purple);
- pen.drawLine(350,0,350,600);
- pen.setColor(orange);
- pen.drawLine(375,0,375,600);
- pen.setColor(green);
- pen.drawLine(400,0,400,600);
- pen.setColor(purple);
- pen.drawLine(425,0,425,600);
- pen.setColor(orange);
- pen.drawLine(450,0,450,600);
- pen.setColor(green);
- pen.drawLine(475,0,475,600);
- pen.setColor(purple);
- pen.drawLine(500,0,500,600);
- pen.setColor(orange);
- pen.drawLine(525,0,525,600);
- pen.setColor(green);
- pen.drawLine(550,0,550,600);
- pen.setColor(purple);
- pen.drawLine(575,0,575,600);
- pen.setColor(orange);
- */
- Color skin = new Color(255,176,138);
- pen.setColor(skin);
- pen.fillOval(229,70,40,40);
- pen.setColor(Color.BLACK);
- pen.fillOval(236,83,5,5);
- pen.fillOval(252,83,5,5);
- pen.drawArc(242,92,10,10,180,200);
- //pen.drawLine(242,101,251,101);
- Color brown = new Color(128,64,0);
- pen.setColor(brown);
- pen.fillOval(233,104,35,7);
- Color clothes = new Color(147,0,0);
- pen.setColor(clothes);
- //pen.fillRect(222,70,55,35);
- //pen.fillOval(210,69,35,35);
- //pen.fillOval(255,69,35,35);
- pen.fillOval(225,70,50,10);
- //pen.drawLine(250,40,230,80);
- //pen.drawLine(250,40,270,80);
- Polygon tri = new Polygon();
- tri.addPoint(250,40);
- tri.addPoint(230,80);
- tri.addPoint(270,80);
- pen.fill(tri);
- Color cape = new Color(0,128,192);
- pen.setColor(cape);
- pen.fillRect(225,110,50,90);
- Color cape2 = new Color(255,255,0);
- pen.setColor(cape2);
- pen.fillRect(225,190,50,8);
- pen.setColor(clothes);
- pen.fillRect(238,110,25,45);
- Color belt = new Color(128,64,0);
- pen.setColor(belt);
- pen.fillRect(238,140,25,6);
- pen.setColor(skin);
- pen.drawLine(238,110,227,160);
- pen.fillOval(223,157,10,10);
- pen.setColor(clothes);
- pen.fillOval(219,106,25,10);
- Color staff = new Color(128,64,0);
- pen.setColor(staff);
- pen.fillRect(300,60,8,150);
- pen.setColor(skin);
- pen.drawLine(262,110,295,130);
- pen.fillOval(295,127,10,10);
- //pen.drawLine(262,110,269,160);
- //pen.fillOval(265,157,10,10);
- pen.setColor(clothes);
- pen.fillOval(257,106,25,10);
- pen.fillRect(239,150,10,50);
- pen.setColor(Color.BLACK);
- pen.fillOval(230,198,20,10);
- pen.setColor(clothes);
- pen.fillRect(253,150,10,50);
- pen.setColor(Color.BLACK);
- pen.fillOval(245,198,20,10);
- repaint();
- //pen.setBackgroundImage("gamevillage.jpg");*/
- }
- }
- private void getTheWord()
- {
- theWord = dBase.getRandomWord();
- if(theWord == null)
- {
- JOptionPane.showMessageDialog
- (
- null,
- "No words available!",
- "CRITICAL ERROR!",
- JOptionPane.ERROR_MESSAGE
- );
- quit = true;
- }
- }
- void setup()
- {
- pen.setColor(Color.BLACK);
- pen.fillRect(0,0,1000,1000);
- pen.setColor(Color.BLUE);
- pen.fillOval(100,180,300,60);
- pen.drawOval(74,171,350,100);
- pen.drawLine(30,0,72,220);
- pen.drawLine(100,0,142,258);
- pen.drawLine(65,0,105,200);
- pen.drawLine(145,0,180,200);
- pen.drawLine(195,0,230,200);
- pen.drawLine(470,0,422,219);
- pen.drawLine(435,0,397,189);
- pen.drawLine(400,0,352,256);
- pen.drawLine(367,0,322,176);
- pen.drawLine(327,0,282,176);
- //Color magic = new Color(0,128,255,10);
- //pen.setColor(magic);
- //pen.fillRect(100,100,50,50);
- /*
- Color blue = new Color(0,128,255);
- pen.setColor(blue);
- pen.fillRect(0,0,1000,1000);
- Color Green = new Color(0,128,0);
- pen.setColor(Green);
- pen.fillRect(0,290,700,200);
- Color yellow = new Color(255,255,0);
- pen.setColor(yellow);
- pen.fillOval(50,30,80,80);
- Color white = new Color(255,255,255);
- pen.setColor(white);
- pen.fillOval(280,35,50,50);
- pen.fillOval(295,25,50,50);
- pen.fillOval(295,45,50,50);
- pen.fillOval(315,35,50,50);
- pen.fillOval(325,25,50,50);
- pen.fillOval(325,45,50,50);
- pen.fillOval(345,35,50,50);
- pen.fillOval(345,45,50,50);
- pen.fillOval(345,25,50,50);
- pen.fillOval(365,35,50,50);
- Color Brown = new Color(128,64,0);
- pen.setColor(Brown);
- pen.drawLine(145,50,145,200);
- pen.drawLine(145,50,250,50);
- pen.drawLine(250,50,250,70);
- pen.setColor(Brown);
- pen.fillRect(125,200,200,50);
- pen.fillRect(45,240,350,10);
- pen.fillRect(60,240,10,70);
- pen.fillRect(370,240,10,70);
- */
- repaint();
- /*
- head()
- {
- pen.fillOval(100,100,100,100);
- }
- body()
- {
- pen.fillRect(100,100,100,100);
- }
- leftArm()
- {
- pen.drawLine(20,20,30,30);
- }
- rightArm()
- {
- pen.fillLine(40,40,50,50);
- }
- leftLeg()
- {
- pen.fillLine(70,70,80,80);
- }
- rightLeg()
- {
- pen.fillRect(90,90,100,100);
- }
- */
- }
- private void playAgain()
- {
- if(lose) // THIS WORKS
- {
- Loser();
- }
- // THIS DOES NOT WORK YET, CHECK THE LOGICAL ORDER OF OCCURRENCE.
- if(win)
- {
- Winner();
- }
- int choice = JOptionPane.showConfirmDialog
- (
- null,
- "Do you want to play again?",
- "PLAY AGAIN?",
- JOptionPane.YES_NO_OPTION
- );
- if(choice == JOptionPane.NO_OPTION)
- quit = true;
- }
- public void newGameAction()
- {
- //messageBox.setText("This is the new game action button");
- reset = true;
- }
- public void showWordAction()
- {
- JOptionPane.showMessageDialog
- (
- null,
- "The word is " + theWord,
- "For Teacher Use Only!",
- JOptionPane.INFORMATION_MESSAGE
- );
- //messageBox.setText("This is the show word action button");
- }
- public void hintAction()
- {
- messageBox.setText("There are no hints in this game! Quit pushing this button! Dx");
- }
- public void exitAction()
- {
- quit = true;
- //messageBox.setText("This is the exit action button");
- }
- public void letterBoxAction()
- {
- String inputValue = letterBox.getText();
- if(inputValue.length() != 1)
- {
- messageBox.setText("You did not enter a single letter \n");
- return;
- }
- char possibleLetter = inputValue.charAt(0);
- possibleLetter= Character.toUpperCase(possibleLetter);
- if(!Character.isLetter(possibleLetter))
- {
- messageBox.setText("Your choice is not the valid letter. \n");
- return;
- }
- if(usedLetters.contains(possibleLetter))
- {
- messageBox.setText("That letter has already been used. \n");
- return;
- }
- theLetter = possibleLetter;
- usedLetters.add(theLetter);
- Collections.sort(usedLetters);
- usedLettersBox.setText(usedLetters.toString());
- messageBox.setText("");
- //messageBox.setText("This is the letter box action button");
- }
- private void showWordBlanks()
- {
- String all = "";
- for(int x = 0; x < theWord.length(); x++)
- if(Character.isLetter(theWord.charAt(x)))
- all += "-";
- else
- {
- all += theWord.charAt(x);
- correctGuesses++;
- }
- theWordBox.setText(all);
- }
- private void showScore()
- {
- scoreBox.setText("Correct Score: " + correctGuesses + "\n");
- scoreBox.append("Wrong Score: " + wrongGuesses + "\n");
- }
- private void processTheLetter()
- {
- boolean letterFound = false;
- String userSolution = theWordBox.getText();
- String all = "";
- for(int x = 0; x < theWord.length(); x++)
- if(Character.toUpperCase(theWord.charAt(x)) == theLetter)
- {
- letterFound = true;
- correctGuesses++;
- all += theWord.charAt(x);
- }
- else
- all += userSolution.charAt(x);
- if(!letterFound)
- wrongGuesses++;
- theLetter = ' ';
- theWordBox.setText(all);
- }
- }
- class DataBase
- {
- private ArrayList<String> wordList = new ArrayList<String>();
- private String category;
- public DataBase() throws IOException
- {
- String name = "words.dat";
- Scanner fileScan = new Scanner(new File(name));
- category = fileScan.nextLine();
- while(fileScan.hasNext())
- wordList.add(fileScan.nextLine());
- }
- public String toString()
- {
- String all = "";
- for(String data: wordList)
- all+= data + "\n";
- return all;
- }
- public String getCategory()
- {
- return category;
- }
- public String getRandomWord()
- {
- if(wordList.isEmpty())
- return null;
- int index = (int)(Math.random() * wordList.size());
- String word = wordList.get(index);
- wordList.remove(index);
- return word;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement