diff --git a/.gitignore b/.gitignore index 292bf4e..834caaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ sem/ +*class *swp diff --git a/338/g-hangman/hangman.java b/338/g-hangman/hangman.java new file mode 100644 index 0000000..7e65ab7 --- /dev/null +++ b/338/g-hangman/hangman.java @@ -0,0 +1,98 @@ +// Name: Alejandro Santillana +import java.util.Random; + +import java.util.ArrayList; +import java.io.FileReader; +import java.io.FileInputStream; +import java.io.BufferedReader; +import java.io.IOException; + +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.scene.control.Label; +import javafx.scene.layout.StackPane; +import javafx.stage.Stage; +import javafx.scene.control.TextField; +import javafx.scene.layout.HBox; + +public class hangman extends Application{ + public static String target_word; + public static char[] masked_word; + + public static int attempt_count = 1; + + public static TextField input_field; + + public static void reveal_char(char c) { + // we should go through again and reveal any new characters + for(int i = 0;i 1 || str.length() == 0) { + return false; + } + // fallthrough checking proper length inputs + char c = str.charAt(0); + if(c < 'A') { + return false; + } + if(c>'Z' && c<'a') { + return false; + } + if(c > 'z') { + return false; + } + // fallthrough for letters of length of 1 + return true; + } + + + @Override + public void start(Stage stage) { + // draw the first image + image = new Image("./img/h1.gif", false); + + Label label = new Label("Word to guess: " + new String(masked_word)); + + // seutp the input field + input_field = new TextField(); + // scene for things to live inside of + Scene scene = new Scene(new StackPane(label), 640, 480); + stage.show(); + stage.setScene(scene); + } + + public static void main(String[] args) { + // First we'll read in our file + ArrayList words = new ArrayList(); + try { + BufferedReader reader = new BufferedReader(new FileReader("words.txt")); + String line; + while((line = reader.readLine())!= null) { + words.add(line); + } + } + catch(IOException e) { + e.printStackTrace(); + } + // pick a random item from our list as the word to guess for + Random r_idx = new Random(); + int index = r_idx.nextInt(words.size()); + target_word = new String(words.get(index)); + masked_word = new char[target_word.length()]; + + // seutp the masked word with underscores + for(int i = 0;i'Z' && c<'a') { + return false; + } + if(c > 'z') { + return false; + } + return true; + } + + // Returns a new character to put into the word + public static char useHint(String prev, String full) { + for(int i =0;i instructorList; + private ArrayList courseList; + private ArrayList studentList; + private String name; + + public School(/*String file,*/ String schoolName) { + this.instructorList = new ArrayList(); + this.courseList = new ArrayList(); + this.studentList = new ArrayList(); + + this.name = schoolName; + //readData(file); + } + public void readData(String filename) { + try { + BufferedReader reader = new BufferedReader(new FileReader(filename)); + // first line is how many instructors we want to add + int inst_count = Integer.parseInt(reader.readLine()); + for(int i =0;i grades; + + public String status; + + public Student(int id, String name, int courseId, float floatGrade, String letterGrade) { + this.name = name; + this.id = id; + grades = new HashMap(); + grades.put(courseId, new Grade(floatGrade, letterGrade)); + // default student status + this.status = "Enrolled"; + } + public String toString() { + return "Student number: " + this.id + "\n" + + "Name: " + this.name + "\n" + + "Status: " + this.status; + } + public float averageGrade() { + float sum = 0; + int count = 0; + for(Grade g : this.grades.values()) { + sum += g.num_grade; + count++; + } + return sum/(float)count; + } + public void graduate() { + for(Integer i : this.grades.keySet()) { + // remove from all the courses + this.grades.remove(i); + } + this.status = "Graduated"; + } + public void enroll(int courseId, float num_grade, String l_grade) { + grades.put(courseId, new Grade(num_grade, l_grade)); + } +} diff --git a/338/homework5/data1.txt b/338/homework5/data1.txt new file mode 100644 index 0000000..c562322 --- /dev/null +++ b/338/homework5/data1.txt @@ -0,0 +1,13 @@ +4 +100,Y. Byun,ybyun@csumb.edu,111-111-1111 +200,S. Narayanan,sathya@csumb.edu,222-222-2222 +300,M. Lara,lara@csumb.edu,333-333-3333 +250,S. Bude,bude@csumb.edu,444-123-4567 +3 +338,CST338 - Software Design,100,BIT 104 +205,CST205 - Multimedia Design and Programming,200,BIT 118 +306,CST306 - Game Engine Programming,100,BIT 104 +3 +7777,Alice Otter,338,89.50,B +8888,Bob Otter,205,75.00,C +7777,Alice Otter,306,98.00,A diff --git a/338/homework5/data2.txt b/338/homework5/data2.txt new file mode 100644 index 0000000..943c1d5 --- /dev/null +++ b/338/homework5/data2.txt @@ -0,0 +1,10 @@ +2 +500,G. Bruns,bruns@csumb.edu,555-222-2222 +300,O. Doe,doe@csumb.edu,444-333-3333 +1 +336,CST336 - Internet Programming,300,Library 1010 +4 +7777,Alice Otter,205,71.00,C +7777,Unknown Otter,205,71.00,C +7777,Alice Otter,310,81.55,B +9999,John Doe,338,89.50,B diff --git a/338/homework5/makefile b/338/homework5/makefile new file mode 100644 index 0000000..18fa62a --- /dev/null +++ b/338/homework5/makefile @@ -0,0 +1,8 @@ +all: + javac App.java Course.java Instructor.java School.java Student.java + +run: all + @java App + +clean: + rm -f *class diff --git a/338/lab2/can.java b/338/lab2/can.java new file mode 100644 index 0000000..47c88ce --- /dev/null +++ b/338/lab2/can.java @@ -0,0 +1,55 @@ +// Alejandro Santillana +/* +* given an array of int values, +* is there a index in the array where the sum of values[0 .. index] +* is equal to (or balances) the sum of values[index+1 .. length-1] +* look at the examples below +* Your task: design and code the canBalance methods that returns true or false +* the program should be efficient. +* can you solve the problem without using nested loops? 1 loop? 2 loops? +* Then run the program to see if your method works correctly. +*/ + +public class CanBalance { + + public static void main(String[] args) { + boolean r; + r = canBalance(new int[] {1, 1, 1, 2, 1}); + System.out.println("canBalance( [1, 1, 1, 2, 1] ) should be true. You returned "+r); + + r = canBalance(new int[] {2, 1, 1, 2, 1 }); + System.out.println("canBalance( [2, 1, 1, 2, 1]) should be false. You returned "+r); + + r = canBalance(new int[] {10, 10}); + System.out.println("canBalance( [10,10]) should be true. You returned "+r); + + r = canBalance(new int[] {10, 0, 1, -1, 10} ); + System.out.println("canBalance([10, 0, 1, -1, 10]) should be true. You returned "+r); + + r = canBalance(new int[] { 1}); + System.out.println(" should be false. You returned "+r); + + r = canBalance(new int[] {2, 1, 1, 1, 1}); + System.out.println("canBalance([2, 1, 1, 1, 1]) should be true. You returned "+r); + + } + + public static boolean canBalance(int[] a) { + int sum_a = 0; + int sub_b = 0; + + for(int i =0;i + + + + POST data + + + + + + + +
+ + + + +
+ + + diff --git a/338/lab5/build/h1.gif b/338/lab5/build/h1.gif new file mode 100644 index 0000000..e2145c2 Binary files /dev/null and b/338/lab5/build/h1.gif differ diff --git a/338/lab5/build/h2.gif b/338/lab5/build/h2.gif new file mode 100644 index 0000000..83880ff Binary files /dev/null and b/338/lab5/build/h2.gif differ diff --git a/338/lab5/build/h3.gif b/338/lab5/build/h3.gif new file mode 100644 index 0000000..4863465 Binary files /dev/null and b/338/lab5/build/h3.gif differ diff --git a/338/lab5/build/h4.gif b/338/lab5/build/h4.gif new file mode 100644 index 0000000..0d47929 Binary files /dev/null and b/338/lab5/build/h4.gif differ diff --git a/338/lab5/build/h5.gif b/338/lab5/build/h5.gif new file mode 100644 index 0000000..5547cb8 Binary files /dev/null and b/338/lab5/build/h5.gif differ diff --git a/338/lab5/build/h6.gif b/338/lab5/build/h6.gif new file mode 100644 index 0000000..9fb7596 Binary files /dev/null and b/338/lab5/build/h6.gif differ diff --git a/338/lab5/build/h7.gif b/338/lab5/build/h7.gif new file mode 100644 index 0000000..5804c46 Binary files /dev/null and b/338/lab5/build/h7.gif differ diff --git a/338/lab5/build/hangman/Game.java b/338/lab5/build/hangman/Game.java new file mode 100644 index 0000000..50f5dcc --- /dev/null +++ b/338/lab5/build/hangman/Game.java @@ -0,0 +1,112 @@ +package hangman; +/** + * Game contains the logic for game of hangman. + * + */ +public class Game { + + protected String word; + protected int remaining_guesses; + protected StringBuffer display; + protected String correctGuess; + protected String badGuess; + + /** + * Game constructor + * @param word - the word to be guessed + * @param guesses - number of incorrect guesses allowed + */ + public Game(String word, int guesses) { + this.word = word; + remaining_guesses = guesses; + correctGuess = ""; + badGuess = ""; + display = new StringBuffer(); + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (Character.isLetter(c)) { + display.append("_"); + } else { + display.append("#"); + } + display.append(" "); + } + } + + public String getWord() { + return word; + } + + public int getRemainingGuesses() { + return remaining_guesses; + } + + public String getDisplay() { + return display.toString(); + } + + /* return code from processGuess */ + + public static final int GOOD = 1; + public static final int BAD = 2; + public static final int WON = 3; + public static final int LOST = 4; + public static final int REPEAT_GOOD_GUESS = 5; + public static final int REPEAT_BAD_GUESS = 6; + + /** + * + * @param c - the letter guessed + * @return code + */ + public int processGuess(char c) { + if (correctGuess.indexOf(c) >= 0) { + return REPEAT_GOOD_GUESS; + } + + if (badGuess.indexOf(c) >= 0) { + remaining_guesses -= 1; + if (remaining_guesses <= 0 && display.indexOf("_") >= 0) { + return LOST; + } else { + return REPEAT_BAD_GUESS; + } + + } else { + boolean found = false; + for (int i = 0; i < word.length(); i++) { + if (c == word.charAt(i)) { + found = true; + correctGuess += c; + display.replace(2 * i, 2 * i + 1, word.substring(i, i + 1)); + } + } + if (!found) { + remaining_guesses -= 1; + badGuess += c; + if (remaining_guesses <= 0 && display.indexOf("_") >= 0) { + return LOST; + } else { + return BAD; + } + } + } + + if (display.indexOf("_") < 0) { + return WON; + } else { + return GOOD; + } + } + + /** + * user asks for a hint. + * @return code WON, LOST or GOOD. + */ + public int doHint() { + int k = display.indexOf("_"); + char c = word.charAt(k / 2); + int rc = processGuess(c); + return rc; + } +} diff --git a/338/lab5/build/hangman/Hangman.java b/338/lab5/build/hangman/Hangman.java new file mode 100644 index 0000000..b8d434d --- /dev/null +++ b/338/lab5/build/hangman/Hangman.java @@ -0,0 +1,156 @@ +package hangman; + +import java.io.*; +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; +import javafx.application.Application; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.Scene; +import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.stage.Stage; + + +public class Hangman extends Application { + + Image[] images = new Image[7]; + ArrayList words; + Game g; + ImageView imageView; + Text text1; + Text text2; + TextField textField; + + @Override + public void start(Stage stage) throws Exception { + + words = new ArrayList(); + readFile(); + + g = new Game(pickRandomWord(), 6); + + try { + //load image files + images[0] = new Image(new FileInputStream("./h1.gif")); + images[1] = new Image(new FileInputStream("./h2.gif")); + images[2] = new Image(new FileInputStream("./h3.gif")); + images[3] = new Image(new FileInputStream("./h4.gif")); + images[4] = new Image(new FileInputStream("./h5.gif")); + images[5] = new Image(new FileInputStream("./h6.gif")); + images[6] = new Image(new FileInputStream("./h7.gif")); + } catch (Exception e) { + System.out.println("Error. " + e.getMessage()); + System.exit(0); + } + + imageView = new ImageView(images[0]); + text1 = new Text("Guess a letter or ask for hint."); + text2 = new Text(g.getDisplay()); + textField = new TextField(); + textField.setOnAction(new GameController()); + VBox vbox = new VBox(10); + vbox.getChildren().addAll(imageView, text1, text2, textField); + + //Creating a scene object + Scene scene = new Scene(vbox, 250, 350); + stage.setTitle("Play Hangman"); + stage.setScene(scene); + stage.show(); + } + + public class GameController implements EventHandler { + + @Override + public void handle(ActionEvent ae) { + String user_input = textField.getText(); + //DEBUG System.out.println(user_input); + if (user_input.length() == 0) { + text1.setText("Enter a single letter or enter hint."); + text2.setText(g.getDisplay()); + textField.setText(""); + } else if (user_input.equalsIgnoreCase("hint")) { + int rc = g.doHint(); + imageView.setImage(images[6 - g.getRemainingGuesses()]); + if (rc == Game.WON) { + text1.setText("You won!"); + text2.setText(g.getDisplay()); + textField.setText(""); + } else if (rc == Game.LOST) { + text1.setText(""); + text2.setText("Game over. The word was: " + g.getWord()); + textField.setText(""); + } else { + text1.setText("Enter a guess or hint."); + text2.setText(g.getDisplay()); + textField.setText(""); + } + + } else { + char c = user_input.charAt(0); + int rc = g.processGuess(c); + switch (rc) { + case Game.BAD: + text1.setText("No " + c + " in the word. " + g.getRemainingGuesses() + " attempts left."); + textField.setText(""); + imageView.setImage(images[6 - g.getRemainingGuesses()]); + break; + case Game.GOOD: + text1.setText("Yes. There is a " + c + " in the word."); + text2.setText(g.getDisplay()); + textField.setText(""); + break; + case Game.LOST: + text1.setText("That was your last guess. Game Over"); + text2.setText("Word was: " + g.getWord()); + imageView.setImage(images[6]); + textField.setText(""); + break; + case Game.WON: + text1.setText("You won!"); + text2.setText(g.getDisplay()); + textField.setText(""); + break; + case Game.REPEAT_GOOD_GUESS: + text1.setText("You already guessed that letter."); + text2.setText(g.getDisplay()); + textField.setText(""); + case Game.REPEAT_BAD_GUESS: + text1.setText("You already guessed that letter."); + text2.setText(g.getDisplay()); + textField.setText(""); + imageView.setImage(images[6 - g.getRemainingGuesses()]); + } + } + } + } + + public static void main(String[] args) { + launch(args); + } + + public void readFile(){ + try { + File f = new File("words.txt"); + Scanner infile = new Scanner(f); + while (infile.hasNext()){ + words.add(infile.nextLine().trim()); + } + infile.close(); + + }catch (Exception e){ + System.out.println("Error exception. "+e.getMessage()); + System.exit(0); + } + } + + public String pickRandomWord() { + int k = new Random().nextInt(words.size()); + return words.get(k); + } + +} diff --git a/338/lab5/build/hangman/makefile b/338/lab5/build/hangman/makefile new file mode 100644 index 0000000..2094d43 --- /dev/null +++ b/338/lab5/build/hangman/makefile @@ -0,0 +1,18 @@ +cc=~/Downloads/jdk-11.0.2/bin/javac +fxlib=--module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib +ctrl=--add-modules javafx.controls + +env=~/Downloads/jdk-11.0.2/bin/java +jfile=Game.java Hangman.java +cfile="Hangman" + +default: + # takes a java file as entry to build + $(cc) $(fxlib) $(jfile) $(ctr) + +# ouchie +run: + $(env) $(fxlib) $(ctrl) $(cfile) + +clean: + rm -f *class diff --git a/338/lab5/build/hangman/run.sh b/338/lab5/build/hangman/run.sh new file mode 100644 index 0000000..9c3e220 --- /dev/null +++ b/338/lab5/build/hangman/run.sh @@ -0,0 +1,2 @@ +[ -z $1 ] && echo "no target" && exit 1 +java --module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib --add-modules javafx.controls $1 diff --git a/338/lab5/build/make.sh b/338/lab5/build/make.sh new file mode 100644 index 0000000..0b697ae --- /dev/null +++ b/338/lab5/build/make.sh @@ -0,0 +1 @@ +javac --module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib $1 --add-modules javafx.controls diff --git a/338/lab5/build/run b/338/lab5/build/run new file mode 100755 index 0000000..9c3e220 --- /dev/null +++ b/338/lab5/build/run @@ -0,0 +1,2 @@ +[ -z $1 ] && echo "no target" && exit 1 +java --module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib --add-modules javafx.controls $1 diff --git a/338/lab5/build/run.sh b/338/lab5/build/run.sh new file mode 100644 index 0000000..9c3e220 --- /dev/null +++ b/338/lab5/build/run.sh @@ -0,0 +1,2 @@ +[ -z $1 ] && echo "no target" && exit 1 +java --module-path /home/shockrah/Downloads/javafx-sdk-11.0.2/lib --add-modules javafx.controls $1 diff --git a/338/lab5/source/build.xml b/338/lab5/source/build.xml new file mode 100644 index 0000000..79a8edf --- /dev/null +++ b/338/lab5/source/build.xml @@ -0,0 +1,53 @@ + + Builds, tests, and runs the project javafxsample. + + + diff --git a/338/lab5/source/build/built-jar.properties b/338/lab5/source/build/built-jar.properties new file mode 100644 index 0000000..2c3fa6b --- /dev/null +++ b/338/lab5/source/build/built-jar.properties @@ -0,0 +1,4 @@ +#Wed, 20 Feb 2019 12:05:41 -0800 + + +C\:\\Users\\wisne\\Documents\\NetBeansProjects\\javafxsample= diff --git a/338/lab5/source/build/test/results/TEST-hangman.GameTest.xml b/338/lab5/source/build/test/results/TEST-hangman.GameTest.xml new file mode 100644 index 0000000..bbce1bf --- /dev/null +++ b/338/lab5/source/build/test/results/TEST-hangman.GameTest.xml @@ -0,0 +1,375 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + java.lang.NullPointerException + at hangman.GameTest.testProcessGuess(GameTest.java:91) + + + + java.lang.NullPointerException + at hangman.GameTest.testGetWord(GameTest.java:48) + + + + java.lang.NullPointerException + at hangman.GameTest.testGetRemainingGuesses(GameTest.java:62) + + + + java.lang.NullPointerException + at hangman.GameTest.testDoHint(GameTest.java:105) + + + + java.lang.NullPointerException + at hangman.GameTest.testGetDisplay(GameTest.java:76) + + + + + diff --git a/338/lab5/source/h1.gif b/338/lab5/source/h1.gif new file mode 100644 index 0000000..e2145c2 Binary files /dev/null and b/338/lab5/source/h1.gif differ diff --git a/338/lab5/source/h2.gif b/338/lab5/source/h2.gif new file mode 100644 index 0000000..83880ff Binary files /dev/null and b/338/lab5/source/h2.gif differ diff --git a/338/lab5/source/h3.gif b/338/lab5/source/h3.gif new file mode 100644 index 0000000..4863465 Binary files /dev/null and b/338/lab5/source/h3.gif differ diff --git a/338/lab5/source/h4.gif b/338/lab5/source/h4.gif new file mode 100644 index 0000000..0d47929 Binary files /dev/null and b/338/lab5/source/h4.gif differ diff --git a/338/lab5/source/h5.gif b/338/lab5/source/h5.gif new file mode 100644 index 0000000..5547cb8 Binary files /dev/null and b/338/lab5/source/h5.gif differ diff --git a/338/lab5/source/h6.gif b/338/lab5/source/h6.gif new file mode 100644 index 0000000..9fb7596 Binary files /dev/null and b/338/lab5/source/h6.gif differ diff --git a/338/lab5/source/h7.gif b/338/lab5/source/h7.gif new file mode 100644 index 0000000..5804c46 Binary files /dev/null and b/338/lab5/source/h7.gif differ diff --git a/338/lab5/source/manifest.mf b/338/lab5/source/manifest.mf new file mode 100644 index 0000000..1574df4 --- /dev/null +++ b/338/lab5/source/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/338/lab5/source/nbproject/build-impl.xml b/338/lab5/source/nbproject/build-impl.xml new file mode 100644 index 0000000..4372641 --- /dev/null +++ b/338/lab5/source/nbproject/build-impl.xml @@ -0,0 +1,1420 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/338/lab5/source/nbproject/configs/Run_as_WebStart.properties b/338/lab5/source/nbproject/configs/Run_as_WebStart.properties new file mode 100644 index 0000000..8c7e9c0 --- /dev/null +++ b/338/lab5/source/nbproject/configs/Run_as_WebStart.properties @@ -0,0 +1,2 @@ +# Do not modify this property in this configuration. It can be re-generated. +$label=Run as WebStart diff --git a/338/lab5/source/nbproject/configs/Run_in_Browser.properties b/338/lab5/source/nbproject/configs/Run_in_Browser.properties new file mode 100644 index 0000000..a2b3686 --- /dev/null +++ b/338/lab5/source/nbproject/configs/Run_in_Browser.properties @@ -0,0 +1,2 @@ +# Do not modify this property in this configuration. It can be re-generated. +$label=Run in Browser diff --git a/338/lab5/source/nbproject/genfiles.properties b/338/lab5/source/nbproject/genfiles.properties new file mode 100644 index 0000000..5ea7208 --- /dev/null +++ b/338/lab5/source/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=6ba13265 +build.xml.script.CRC32=9ac9c85d +build.xml.stylesheet.CRC32=8064a381@1.79.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=21992267 +nbproject/build-impl.xml.script.CRC32=67abe0ae +nbproject/build-impl.xml.stylesheet.CRC32=05530350@1.79.1.48 diff --git a/338/lab5/source/nbproject/jfx-impl.xml b/338/lab5/source/nbproject/jfx-impl.xml new file mode 100644 index 0000000..69e9f66 --- /dev/null +++ b/338/lab5/source/nbproject/jfx-impl.xml @@ -0,0 +1,4008 @@ + + + + + JavaFX-specific Ant calls + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ${cssfileslist} + + + + + + + + + + + + + + + + + + + + + + + + self.addMappedName( + (source.indexOf("jfxrt.jar") >= 0) || + (source.indexOf("deploy.jar") >= 0) || + (source.indexOf("javaws.jar") >= 0) || + (source.indexOf("plugin.jar") >= 0) + ? "" : source + ); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/338/lab5/source/nbproject/private/configs/Run_as_WebStart.properties b/338/lab5/source/nbproject/private/configs/Run_as_WebStart.properties new file mode 100644 index 0000000..7a1d99d --- /dev/null +++ b/338/lab5/source/nbproject/private/configs/Run_as_WebStart.properties @@ -0,0 +1,2 @@ +# Do not modify this property in this configuration. It can be re-generated. +javafx.run.as=webstart diff --git a/338/lab5/source/nbproject/private/configs/Run_in_Browser.properties b/338/lab5/source/nbproject/private/configs/Run_in_Browser.properties new file mode 100644 index 0000000..7a7b6e4 --- /dev/null +++ b/338/lab5/source/nbproject/private/configs/Run_in_Browser.properties @@ -0,0 +1,2 @@ +# Do not modify this property in this configuration. It can be re-generated. +javafx.run.as=embedded diff --git a/338/lab5/source/nbproject/private/private.properties b/338/lab5/source/nbproject/private/private.properties new file mode 100644 index 0000000..192a3be --- /dev/null +++ b/338/lab5/source/nbproject/private/private.properties @@ -0,0 +1,6 @@ +auxiliary.org-netbeans-modules-projectapi.issue214819_5f_fx_5f_enabled=true +# No need to modify this property unless customizing JavaFX Ant task infrastructure +endorsed.javafx.ant.classpath=. +javafx.run.inbrowser= +javafx.run.inbrowser.path=C:\\Program Files\\Internet Explorer\\iexplore.exe +user.properties.file=C:\\Users\\wisne\\AppData\\Roaming\\NetBeans\\8.1\\build.properties diff --git a/338/lab5/source/nbproject/private/private.xml b/338/lab5/source/nbproject/private/private.xml new file mode 100644 index 0000000..284eeec --- /dev/null +++ b/338/lab5/source/nbproject/private/private.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/338/lab5/source/nbproject/project.properties b/338/lab5/source/nbproject/project.properties new file mode 100644 index 0000000..42583ce --- /dev/null +++ b/338/lab5/source/nbproject/project.properties @@ -0,0 +1,115 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=hangmanfx +application.vendor=wisne +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +compile.on.save=true +compile.on.save.unsupported.javafx=true +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/hangmanfx.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +includes=** +# Non-JavaFX jar file creation is deactivated in JavaFX 2.0+ projects +jar.archive.disabled=true +jar.compress=false +javac.classpath=\ + ${javafx.classpath.extension} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit_4.classpath}:\ + ${libs.hamcrest.classpath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +javafx.application.implementation.version=1.0 +javafx.binarycss=false +javafx.classpath.extension=\ + ${java.home}/lib/javaws.jar:\ + ${java.home}/lib/deploy.jar:\ + ${java.home}/lib/plugin.jar +javafx.deploy.allowoffline=true +# If true, application update mode is set to 'background', if false, update mode is set to 'eager' +javafx.deploy.backgroundupdate=false +javafx.deploy.embedJNLP=true +javafx.deploy.includeDT=true +# Set true to prevent creation of temporary copy of deployment artifacts before each run (disables concurrent runs) +javafx.disable.concurrent.runs=false +# Set true to enable multiple concurrent runs of the same WebStart or Run-in-Browser project +javafx.enable.concurrent.external.runs=false +# This is a JavaFX project +javafx.enabled=true +javafx.fallback.class=com.javafx.main.NoJavaFXFallback +# Main class for JavaFX +javafx.main.class=javafxsample.Javafxsample +javafx.preloader.class= +# This project does not use Preloader +javafx.preloader.enabled=false +javafx.preloader.jar.filename= +javafx.preloader.jar.path= +javafx.preloader.project.path= +javafx.preloader.type=none +# Set true for GlassFish only. Rebases manifest classpaths of JARs in lib dir. Not usable with signed JARs. +javafx.rebase.libs=false +javafx.run.height=600 +javafx.run.width=800 +# Pre-JavaFX 2.0 WebStart is deactivated in JavaFX 2.0+ projects +jnlp.enabled=false +# Main class for Java launcher +main.class=com.javafx.main.Main +# For improved security specify narrower Codebase manifest attribute to prevent RIAs from being repurposed +manifest.custom.codebase=* +# Specify Permissions manifest attribute to override default (choices: sandbox, all-permissions) +manifest.custom.permissions= +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +run.classpath=\ + ${dist.jar}:\ + ${javac.classpath}:\ + ${build.classes.dir} +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/338/lab5/source/nbproject/project.xml b/338/lab5/source/nbproject/project.xml new file mode 100644 index 0000000..877a6cd --- /dev/null +++ b/338/lab5/source/nbproject/project.xml @@ -0,0 +1,25 @@ + + + org.netbeans.modules.java.j2seproject + + + + + + + + + + + + + hangmanfx + + + + + + + + + diff --git a/338/lab5/source/src/hangman/Game.java b/338/lab5/source/src/hangman/Game.java new file mode 100644 index 0000000..50f5dcc --- /dev/null +++ b/338/lab5/source/src/hangman/Game.java @@ -0,0 +1,112 @@ +package hangman; +/** + * Game contains the logic for game of hangman. + * + */ +public class Game { + + protected String word; + protected int remaining_guesses; + protected StringBuffer display; + protected String correctGuess; + protected String badGuess; + + /** + * Game constructor + * @param word - the word to be guessed + * @param guesses - number of incorrect guesses allowed + */ + public Game(String word, int guesses) { + this.word = word; + remaining_guesses = guesses; + correctGuess = ""; + badGuess = ""; + display = new StringBuffer(); + for (int i = 0; i < word.length(); i++) { + char c = word.charAt(i); + if (Character.isLetter(c)) { + display.append("_"); + } else { + display.append("#"); + } + display.append(" "); + } + } + + public String getWord() { + return word; + } + + public int getRemainingGuesses() { + return remaining_guesses; + } + + public String getDisplay() { + return display.toString(); + } + + /* return code from processGuess */ + + public static final int GOOD = 1; + public static final int BAD = 2; + public static final int WON = 3; + public static final int LOST = 4; + public static final int REPEAT_GOOD_GUESS = 5; + public static final int REPEAT_BAD_GUESS = 6; + + /** + * + * @param c - the letter guessed + * @return code + */ + public int processGuess(char c) { + if (correctGuess.indexOf(c) >= 0) { + return REPEAT_GOOD_GUESS; + } + + if (badGuess.indexOf(c) >= 0) { + remaining_guesses -= 1; + if (remaining_guesses <= 0 && display.indexOf("_") >= 0) { + return LOST; + } else { + return REPEAT_BAD_GUESS; + } + + } else { + boolean found = false; + for (int i = 0; i < word.length(); i++) { + if (c == word.charAt(i)) { + found = true; + correctGuess += c; + display.replace(2 * i, 2 * i + 1, word.substring(i, i + 1)); + } + } + if (!found) { + remaining_guesses -= 1; + badGuess += c; + if (remaining_guesses <= 0 && display.indexOf("_") >= 0) { + return LOST; + } else { + return BAD; + } + } + } + + if (display.indexOf("_") < 0) { + return WON; + } else { + return GOOD; + } + } + + /** + * user asks for a hint. + * @return code WON, LOST or GOOD. + */ + public int doHint() { + int k = display.indexOf("_"); + char c = word.charAt(k / 2); + int rc = processGuess(c); + return rc; + } +} diff --git a/338/lab5/source/src/hangman/Hangman.java b/338/lab5/source/src/hangman/Hangman.java new file mode 100644 index 0000000..b8d434d --- /dev/null +++ b/338/lab5/source/src/hangman/Hangman.java @@ -0,0 +1,156 @@ +package hangman; + +import java.io.*; +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; +import javafx.application.Application; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.Scene; +import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.VBox; +import javafx.scene.text.Text; +import javafx.stage.Stage; + + +public class Hangman extends Application { + + Image[] images = new Image[7]; + ArrayList words; + Game g; + ImageView imageView; + Text text1; + Text text2; + TextField textField; + + @Override + public void start(Stage stage) throws Exception { + + words = new ArrayList(); + readFile(); + + g = new Game(pickRandomWord(), 6); + + try { + //load image files + images[0] = new Image(new FileInputStream("./h1.gif")); + images[1] = new Image(new FileInputStream("./h2.gif")); + images[2] = new Image(new FileInputStream("./h3.gif")); + images[3] = new Image(new FileInputStream("./h4.gif")); + images[4] = new Image(new FileInputStream("./h5.gif")); + images[5] = new Image(new FileInputStream("./h6.gif")); + images[6] = new Image(new FileInputStream("./h7.gif")); + } catch (Exception e) { + System.out.println("Error. " + e.getMessage()); + System.exit(0); + } + + imageView = new ImageView(images[0]); + text1 = new Text("Guess a letter or ask for hint."); + text2 = new Text(g.getDisplay()); + textField = new TextField(); + textField.setOnAction(new GameController()); + VBox vbox = new VBox(10); + vbox.getChildren().addAll(imageView, text1, text2, textField); + + //Creating a scene object + Scene scene = new Scene(vbox, 250, 350); + stage.setTitle("Play Hangman"); + stage.setScene(scene); + stage.show(); + } + + public class GameController implements EventHandler { + + @Override + public void handle(ActionEvent ae) { + String user_input = textField.getText(); + //DEBUG System.out.println(user_input); + if (user_input.length() == 0) { + text1.setText("Enter a single letter or enter hint."); + text2.setText(g.getDisplay()); + textField.setText(""); + } else if (user_input.equalsIgnoreCase("hint")) { + int rc = g.doHint(); + imageView.setImage(images[6 - g.getRemainingGuesses()]); + if (rc == Game.WON) { + text1.setText("You won!"); + text2.setText(g.getDisplay()); + textField.setText(""); + } else if (rc == Game.LOST) { + text1.setText(""); + text2.setText("Game over. The word was: " + g.getWord()); + textField.setText(""); + } else { + text1.setText("Enter a guess or hint."); + text2.setText(g.getDisplay()); + textField.setText(""); + } + + } else { + char c = user_input.charAt(0); + int rc = g.processGuess(c); + switch (rc) { + case Game.BAD: + text1.setText("No " + c + " in the word. " + g.getRemainingGuesses() + " attempts left."); + textField.setText(""); + imageView.setImage(images[6 - g.getRemainingGuesses()]); + break; + case Game.GOOD: + text1.setText("Yes. There is a " + c + " in the word."); + text2.setText(g.getDisplay()); + textField.setText(""); + break; + case Game.LOST: + text1.setText("That was your last guess. Game Over"); + text2.setText("Word was: " + g.getWord()); + imageView.setImage(images[6]); + textField.setText(""); + break; + case Game.WON: + text1.setText("You won!"); + text2.setText(g.getDisplay()); + textField.setText(""); + break; + case Game.REPEAT_GOOD_GUESS: + text1.setText("You already guessed that letter."); + text2.setText(g.getDisplay()); + textField.setText(""); + case Game.REPEAT_BAD_GUESS: + text1.setText("You already guessed that letter."); + text2.setText(g.getDisplay()); + textField.setText(""); + imageView.setImage(images[6 - g.getRemainingGuesses()]); + } + } + } + } + + public static void main(String[] args) { + launch(args); + } + + public void readFile(){ + try { + File f = new File("words.txt"); + Scanner infile = new Scanner(f); + while (infile.hasNext()){ + words.add(infile.nextLine().trim()); + } + infile.close(); + + }catch (Exception e){ + System.out.println("Error exception. "+e.getMessage()); + System.exit(0); + } + } + + public String pickRandomWord() { + int k = new Random().nextInt(words.size()); + return words.get(k); + } + +} diff --git a/338/lab5/source/test/hangman/GameTest.java b/338/lab5/source/test/hangman/GameTest.java new file mode 100644 index 0000000..36df78b --- /dev/null +++ b/338/lab5/source/test/hangman/GameTest.java @@ -0,0 +1,102 @@ +package hangman; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +public class GameTest { + + public GameTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getWord method, of class Game. + */ + @Test + public void testGetWord() { + System.out.println("getWord"); + Game instance = null; + String expResult = ""; + String result = instance.getWord(); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of getRemainingGuesses method, of class Game. + */ + @Test + public void testGetRemainingGuesses() { + System.out.println("getRemainingGuesses"); + Game instance = null; + int expResult = 0; + int result = instance.getRemainingGuesses(); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of getDisplay method, of class Game. + */ + @Test + public void testGetDisplay() { + System.out.println("getDisplay"); + Game instance = null; + String expResult = ""; + String result = instance.getDisplay(); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of processGuess method, of class Game. + */ + @Test + public void testProcessGuess() { + System.out.println("processGuess"); + char c = ' '; + Game instance = null; + int expResult = 0; + int result = instance.processGuess(c); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of doHint method, of class Game. + */ + @Test + public void testDoHint() { + System.out.println("doHint"); + Game instance = null; + int expResult = 0; + int result = instance.doHint(); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + +} diff --git a/338/lab5/source/words.txt b/338/lab5/source/words.txt new file mode 100644 index 0000000..735a568 --- /dev/null +++ b/338/lab5/source/words.txt @@ -0,0 +1,7 @@ +computer +science +mathematics +religion +history +philosophy +language diff --git a/338/roman/Roman.java b/338/roman/Roman.java new file mode 100644 index 0000000..d5fe852 --- /dev/null +++ b/338/roman/Roman.java @@ -0,0 +1,127 @@ +import java.util.Scanner; +/** + * Roman numerals + * I 1 + * V 5 + * X 10 + * L 50 + * C 100 + * D 500 + * M 1000 + * + * Numbers are written in decreasing value + * Example: XII is 12, not IIX or IXI + * + * But there are special rules: + * I can come before V or X IV = 4, IX = 9 + * X can come before L or C XL = 40, XC = 90 + * C can come before D or M CD = 400 CM = 900 + */ +public class Roman { + + public static String intToRoman(int n) { + String ret = ""; + int val = n; + // dealing with the thousands case + if(n > 1000) { + int m = val / 1000; + for(int i =0;i=900) { + ret += "CM"; + val -= 900; + } + // 600-800 range + if(val>=600) { + int c = (val - 500)/100; + for(int i =0;i=500) { + ret += "D"; + val -= 500; + } + // 400 special case + if(val>=400) { + ret +="CD"; + val -= 400; + } + // 100 - 300 range + if(val>=100) { + int x = (val - 100)/100; + for(int i =0;i=90) { + val -=90; + ret += "XC"; + } + if(val>=50) { + ret += "L"; + int L=(val-50)/10; + + for(int i =0;i=40) { + ret += "XL"; + val -= 40; + } + if(val>=10) { + int x = (val - 10)/10; + for(int i =0;i5){ + int c = (val - 5); + for(int i =0;i