HBV201G Viðmótsforritun Forritunarverkefni 4 Ágiskunarleikur eða RPN reiknivél (Java Swing) 5. mars 2012 Nemandi: Dæmakennari: Sveinn Flóki Guðmundsson Elvar Helgason 1 of 5
GuessGame.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.random; How to use: GuessGame mygame = new GuessGame(); After: mygame is a new GuessGame, with int secret_number random from 0 to 1000, int num_of_tries = 0, with instructions and suitable feedback, and Boolean you_won set to false. public class GuessGame Function: sets the location of JFrame frame to the center of the screen static void centerwindow(jframe frame) Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((dimension.getwidth() - frame.getwidth()) / 2); int y = (int) ((dimension.getheight() - frame.getheight()) / 2); frame.setlocation(x, y); Function: returns true if String s contains an integer, else returns false static boolean str_has_int(string s) int a; try a = Integer.parseInt(s); catch(numberformatexception e) return false; return true; private JTextField entryguess; private JButton buttonguess, buttonre, buttonexit; private JLabel labelinfo, labelhint; int num_of_tries = 0; int secret_number; Boolean you_won = false; public GuessGame() JFrame frame = new JFrame("Simple Guess Game"); frame.setdefaultcloseoperation(jframe.exit_on_close); entryguess = new JTextField(10); buttonguess = new JButton("Guess"); buttonre = new JButton("Restart"); buttonexit = new JButton("Exit"); labelinfo = new JLabel("<html><div style=\"width: 308px; height: 40px; text-align: center;\"><p>this is a game where you guess a number between 0 and 1000. To guess a number write it in the entry below and press \"Guess\". If you're smart you shouldn't have to guess more than 11 times.</p></div></html>"); labelhint = new JLabel("<html><div style=\"width: 310px; height: 40px; text-align: center;\"><p>you haven't guessed a number yet.</div></html>"); entryguess.addactionlistener(new OnGuess()); buttonguess.addactionlistener(new OnGuess()); buttonre.addactionlistener(new OnRefresh()); buttonexit.addactionlistener(new QuitGuessGame()); Container framecontainer = frame.getcontentpane(); JPanel panel1 = new JPanel(); 2 of 5
framecontainer.add(labelinfo, BorderLayout.PAGE_START); panel1.add(entryguess); panel1.add(buttonguess); panel1.add(buttonre); panel1.add(buttonexit); framecontainer.add(panel1, BorderLayout.CENTER); framecontainer.add(labelhint, BorderLayout.PAGE_END); Random g = new Random(); secret_number = g.nextint(1000); System.out.println("Secret number: " + secret_number); frame.setsize(200,200); frame.pack(); centerwindow(frame); frame.setvisible(true); Before: int secret_number between 0 and 1000, int num_of_tries=>0, JTextField entryguess set, JLabel labelhint set, and Boolean you_won set to false or true. After: If you_won is true, empty entryguess and do nothing more. Try to turn string from entryguess to int, if it fails return suitable string to labelhint and do nothing more. Raise num_of_tries by 1. Compare secret_number and n, return suitable string to labelhint, if secret_number == n, set you_won to true. private class OnGuess extends AbstractAction String s = entryguess.gettext(); entryguess.settext(""); if(you_won) return; String string_guesses = ""; String string_hint = ""; String how_great = ""; if(str_has_int(s)) int n = Integer.parseInt(s); num_of_tries++; if (num_of_tries == 1) string_guesses = "You've guessed once. "; else string_guesses = "You've guessed " + num_of_tries + " times. "; cheat?"; Master Of Orion. Congratulations!"; you can't do better?"; if (secret_number == n) if (num_of_tries == 1) how_great = "WTF? You sure you didn't else if (num_of_tries < 6) how_great = "You clearly are the true else if (num_of_tries < 12) how_great = "Well done, but are you sure else if (num_of_tries > 11) how_great = "Meh! You won't impress 3 of 5
anyone with a score like that!"; " + how_great + ""; Try again!"; Try again!"; string_hint = "The number " + n + " is correct. you_won = true; else if (secret_number > n) string_hint = "The number " + n + " is too low. else if (secret_number < n) string_hint = "The number " + n + " is too high. else string_hint = "You must write a number in the entry!"; labelhint.settext("<html><div style=\"width: 310px; height: 40px; text-align: center;\"><p>"+string_guesses+string_hint+"</p></p></div></html>"); Before: int secret_number=>0, int num_of_tries=>0, JLabel labelhint set, and Boolean you_won set to false or true. After: Set secret_number to a new random number from 0 to 1000. Set you_won to false. Set num_of_tries to 0. Return suitable string to labelhint. private class OnRefresh extends AbstractAction Random r = new Random(); secret_number = r.nextint(1000); num_of_tries = 0; you_won = false; labelhint.settext("<html><div style=\"width: 310px; height: 40px; text-align: center;\"><p>new Game. You haven't guessed a number yet.</p></p></div></html>"); System.out.println("New game."); System.out.println("Secret number: " + secret_number); After: Quit the application. (rather crude operation but effective) private class QuitGuessGame extends AbstractAction System.exit(0); public static void main(string[] args) new GuessGame(); 4 of 5
Hægt er að nálgast skrána GuessGame.java hér: https:notendur.hi.is/sfg6/vidmotsforritun/forritunarverkefni4.zip 5 of 5