This site requires JavaScript, please enable it in your browser!
Greenfoot back
Peach
Peach wrote ...

2014/4/19

Hangman source code

Peach Peach

2014/4/19

#
Does anyone have the source code for the hangman game?
danpost danpost

2014/4/19

#
There is an attempt at a Hangman scenario with source code uploaded on the site.
Peach Peach

2014/4/19

#
thanks, but I've already look at this one. Are there any other ones?
danpost danpost

2014/4/19

#
That is the only one that came up on the search. Why don't you create your own !! (since there is really no decent one here yet)
Peach Peach

2014/4/19

#
how can you modify this code so it works in greenfoot? import java.util.Scanner; import java.util.Random; import java.io.*; public class Hangman { private Scanner in = new Scanner(System.in); private boardPiece board = {new boardPiece(0),new boardPiece(0),new boardPiece(3), new boardPiece(1),new boardPiece(2),new boardPiece(0)}; private String puzzle; private String puzzleWord; private int puzzleIndex; private Random generator = new Random(System.currentTimeMillis()); private boolean win; private boolean over; private String correct = ""; //private String guesses = ""; private char guesses = new char; private int guessed; private int misses; private String puzzles = new String; // = {"hello world","java is cool","athena is smelly","zach is awesome","athena is cool"}; public static void main(String args){ String letter; String again = "y"; Hangman game = new Hangman(); try{ BufferedReader in = new BufferedReader(new FileReader("word.txt")); int count = 0; while (in.ready()) { //while there is another line in the input file game.puzzles = in.readLine(); //get line of data count++; //Increment CWID counter } in.close(); } catch(IOException e){ System.out.println("Error during reading/writing"); } /*for(int x=0;x<game.puzzles.length;x++) System.out.println("PUZZLE " + x + ": " + game.puzzles);*/ System.out.println("Welcome to HangMan! Good Luck!"); while(again.charAt(0) == 'y'){ game.init(); while(!game.over){ game.printBoard(); //System.out.println("Guessed: " + game.guesses); game.printGuesses(); System.out.println("Puzzle: " + game.puzzle); System.out.println("Guess a letter: "); letter = game.in.next(); //game.guesses = game.guesses + " " + letter.charAt(0); game.guesses = letter.charAt(0); game.guessed++; game.sort(); if(game.puzzleWord.indexOf(letter.charAt(0)) != -1){ game.correct = game.correct + letter.charAt(0); game.puzzle = game.puzzleWord.replaceAll("","-"); if(game.puzzleWord.replaceAll("","").length() == 0){ game.win = true; game.over = true; } } else game.miss(); } game.printBoard(); System.out.println("Solution: " + game.puzzleWord); if(game.win) System.out.println("Congratulations! You've solved the puzzle!"); else System.out.println("You failed, failer!"); System.out.println(); System.out.println("Would you like to play again? (y/n)"); again = game.in.next(); } System.out.println("Goodbye!"); } void init(){ win = false; over = false; board.piece = " ______ "; board.piece = " | | "; board.piece = " | "; board.piece = " | "; board.piece = " | "; board.piece = " _______| "; puzzleIndex = generator.nextInt(puzzles.length); puzzleWord = puzzles; puzzle = puzzleWord.replaceAll("","-"); correct = ""; //guesses = ""; for(int x=0;x<26;x++) guesses = '~'; guessed = 0; misses = 0; } void printBoard(){ for(int x =0;x<6;x++) System.out.println(board.piece); } void miss(){ misses++; if(misses == 1) board.piece = " 0 | "; else if(misses == 2) board.piece = " \\0 | "; else if(misses == 3) board.piece = " \\0/ | "; else if(misses == 4) board.piece = " | | "; else if(misses == 5) board.piece = " / | "; else if(misses == 6){ board.piece = " / \\ | "; over = true; } } void printGuesses(){ System.out.print("Guesses: "); for(int x=0;x<26;x++){ if(guesses != '~') System.out.print(guesses + " "); } System.out.println(); } void sort(){ boolean doMore = true; while (doMore) { doMore = false; // assume this is last pass over array for (int i=0; i<guesses.length-1; i++) { if (guesses > guesses) { char temp = guesses; guesses = guesses; guesses = temp; doMore = true; // after an exchange, must look again } } } } class boardPiece{ public String piece; public int total; public int used; boardPiece(int x){ used = 0; total = x; } } }
davmac davmac

2014/4/19

#
Peach, please use code tags when posting code, to avoid formatting problems like in your previous post.
danpost danpost

2014/4/19

#
I just put up a half decent Hangman scenario that I threw together. Will clean up, document and make the main code available in the next day or two.
Peach Peach

2014/4/20

#
Thank you!
danpost danpost

2014/4/21

#
The source is now available for viewing. Run the scenario and click on a button.
Peach Peach

2014/4/22

#
how can I open the scenario in greenfoot?
danpost danpost

2014/4/22

#
Why do you need a copy of it? I thought you wanted to see how a decent one could be done (to learn from).
Peach Peach

2014/4/22

#
Yes I really wanted to see how a good one should be like. I would like to open it up in greenfoot and see what the codes are like (I won't copy any of it, I just want to use it as a reference and learn what I am doing wrong) because I am encountering a lot of problems while creating my own hangman scenario. Thank you very much!
danpost danpost

2014/4/22

#
You do not need to open it up in greenfoot to see the code. Just go to the scenario, click 'Run' and click one of the buttons in the top-right corner. The code will appear.
Peach Peach

2014/4/23

#
ok thank you!
You need to login to post a reply.