Java program

IndyDDR's online socialization center: general topics not related to specific coverage areas

Moderator: Moderators

Post Reply
User avatar
danc1005
Heavy
Heavy
Posts: 3237
Joined: Tue Feb 28, 2006 7:21 pm
Location: Da Fort
Contact:

Java program

Post by danc1005 » Thu Mar 15, 2007 3:42 pm

Hey, I'm sort of a n00b programming-wise, but I just finished this hangman prog in Java that I think is pretty cool...no GUI, not too many frills, but if anybody has the capability to run Java progs, would anyone be interested in playing it and giving me some pointers?
Image

User avatar
hascoolnickname
Heavy
Heavy
Posts: 1921
Joined: Sat Feb 26, 2005 10:47 pm
Location: :noitacoL

Post by hascoolnickname » Thu Mar 15, 2007 7:56 pm

hit me up sometime

User avatar
chocobojoe
Heavy
Heavy
Posts: 2455
Joined: Wed Feb 02, 2005 1:43 pm
Location: Fort Wayne

Post by chocobojoe » Thu Mar 15, 2007 8:23 pm

i would but i decided java is the most hateful programming language after taking 2 semesters of it in college

User avatar
MonMotha
Site Code Monkey
Site Code Monkey
Posts: 2505
Joined: Sun Jan 23, 2005 9:18 pm

Post by MonMotha » Thu Mar 15, 2007 9:02 pm

If you like, just PM me a link or whatever. A runnable jar file or just a bunch of classes is fine. I can build it from source if you like.

If you'd like to see the ultimate in CS120 level stupidity, check out SetPong. I take no responsibility for the code quality - it was written by me and another CS120 student who both had a fair bit of experience, but with none of it in Java (my experience was not even OOP). Information about the Set Game, which SetPong is based on, is available, appropriately, at http://www.setgame.com
A normality test:
+++ATH
If you are no longer connected to the internet, you need to apply more wax to your modem: it'll make it go faster.
If you find this funny, you're a nerd.
If neither of the above apply, you are normal. Congratulations.

User avatar
danc1005
Heavy
Heavy
Posts: 3237
Joined: Tue Feb 28, 2006 7:21 pm
Location: Da Fort
Contact:

Post by danc1005 » Fri Mar 16, 2007 3:36 pm

Haha I don't even know what a jar file is...I got .java and .class files, that's it.

On another note, Set is an awesome game.
Image

User avatar
Green Tea
Heavy
Heavy
Posts: 1863
Joined: Fri May 27, 2005 1:00 am
Location: Cleveland Ohio
Contact:

Post by Green Tea » Sun Mar 18, 2007 6:17 pm

the jar file is the compiled .class/.java files. if you want to post it in here using the code tags I'll take a look at it as well (I've been doing java for about 4 years now.)
dance1005 wrote:Fucking retarded bots, bumping threads with dildos.

User avatar
danc1005
Heavy
Heavy
Posts: 3237
Joined: Tue Feb 28, 2006 7:21 pm
Location: Da Fort
Contact:

Post by danc1005 » Sun Mar 18, 2007 7:25 pm

Green Tea wrote:the jar file is the compiled .class/.java files. if you want to post it in here using the code tags I'll take a look at it as well (I've been doing java for about 4 years now.)
How do I get it to make one of those files?

And sure:

Code: Select all

/**
* Dan C******
* Period 1
* finalHangman
*/

import java.io.*;
import java.util.Random;

//This program will let the user play a game of hangman, either with another user or with the computer

public class finalHangman
{

	public static String gallows(int guesses, int difficulty)
	{
		//This method will be used to draw the gallows for the hangman

		String gallows="";

		if (difficulty==1)
		{
			if (guesses==0)
			{
				gallows="_______\n|     |\n|\n|\n|\n|\n|\n|__________";
			}
			if (guesses==1)
			{
				gallows="_______\n|     |\n|     O\n|\n|\n|\n|\n|__________";
			}
			if (guesses==2)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|\n|\n|\n|__________";
			}
			if (guesses==3)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|     |\n|\n|\n|__________";
			}
			if (guesses==4)
			{
				gallows="_______\n|     |\n|     X\n|    /|\\\n|     |\n|    / \\\n|\n|__________";
			}
		}
		if (difficulty==2)
		{
			if (guesses==0)
			{
				gallows="_______\n|     |\n|\n|\n|\n|\n|\n|__________";
			}
			if (guesses==1)
			{
				gallows="_______\n|     |\n|     O\n|\n|\n|\n|\n|__________";
			}
			if (guesses==2)
			{
				gallows="_______\n|     |\n|     O\n|    /\n|\n|\n|\n|__________";
			}
			if (guesses==3)
			{
				gallows="_______\n|     |\n|     O\n|    /|\n|\n|\n|\n|__________";
			}
			if (guesses==4)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|\n|\n|\n|__________";
			}
			if (guesses==5)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|     |\n|\n|\n|__________";
			}
			if (guesses==6)
			{
				gallows="_______\n|     |\n|     X\n|    /|\\\n|     |\n|    / \\\n|\n|__________";
			}
		}
		if (difficulty==3)
		{
			if (guesses==0)
			{
				gallows="_______\n|     |\n|\n|\n|\n|\n|\n|__________";
			}
			if (guesses==1)
			{
				gallows="_______\n|     |\n|     O\n|\n|\n|\n|\n|__________";
			}
			if (guesses==2)
			{
				gallows="_______\n|     |\n|     O\n|    /\n|\n|\n|\n|__________";
			}
			if (guesses==3)
			{
				gallows="_______\n|     |\n|     O\n|    /|\n|\n|\n|\n|__________";
			}
			if (guesses==4)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|\n|\n|\n|__________";
			}
			if (guesses==5)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|   *\n|\n|\n|__________";
			}
			if (guesses==6)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|   * |\n|\n|\n|__________";
			}
			if (guesses==7)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|   * | *\n|\n|\n|__________";
			}
			if (guesses==8)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|   * | *\n|    /\n|\n|__________";
			}
			if (guesses==9)
			{
				gallows="_______\n|     |\n|     O\n|    /|\\\n|   * | *\n|    / \\\n|\n|__________";
			}
			if (guesses==10)
			{
				gallows="_______\n|     |\n|     X\n|    /|\\\n|   * | *\n|    / \\\n|   Ù   À\n|__________";
			}
		}

		return(gallows);
	}


	public static void main (String[]args) throws IOException
	{

		BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
		Random generator = new Random();

		//This section will define all the variables that will be used for the program
		String word="";
		int wordChoice=0;
		int players;
		int difficulty;
		String wrongGuesses="";
		int guesses=0;
		String guess;
		char guesss;
		String gallowss="";
		int counter=0;
		int correct;
		String wordCheck="";
		int win=0;

		//This section will let the user choose between 1-player or 2-player
		System.out.println("Choose a game:\n1. 1P vs. 2P\n2. 1P vs. Computer");
		players=Integer.parseInt(keyboard.readLine());

		//This section will determine the word that will be used, either by random choice or by user input
		if (players==1)
		{
			System.out.print("Enter a word that you would like to play with: ");
			word=keyboard.readLine();
			System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
		}
		if (players==2)
		{
			wordChoice=(generator.nextInt(50)+1);
			switch(wordChoice)
			{
				case 1: word="condescending";break;
				case 2: word="homogeneous";break;
				case 3: word="intellectual";break;
				case 4: word="language";break;
				case 5: word="jockey";break;
				case 6: word="celebrity";break;
				case 7: word="weapon";break;
				case 8: word="element";break;
				case 9: word="casino";break;
				case 10: word="concatenate";break;
				case 11: word="redundant";break;
				case 12: word="extinguish";break;
				case 13: word="undergarment";break;
				case 14: word="processor";break;
				case 15: word="racquet";break;
				case 16: word="taciturn";break;
				case 17: word="filibuster";break;
				case 18: word="touchdown";break;
				case 19: word="hangman";break;
				case 20: word="serendipity";break;
				case 21: word="playground";break;
				case 22: word="asinine";break;
				case 23: word="sweater";break;
				case 24: word="hillbilly";break;
				case 25: word="portfolio";break;
				case 26: word="preference";break;
				case 27: word="extraneous";break;
				case 28: word="erroneous";break;
				case 29: word="murder";break;
				case 30: word="emerald";break;
				case 31: word="heterogeneous";break;
				case 32: word="graduation";break;
				case 33: word="composition";break;
				case 34: word="punctuation";break;
				case 35: word="program";break;
				case 36: word="pantaloon";break;
				case 37: word="kaleidoscope";break;
				case 38: word="warfare";break;
				case 39: word="epoch";break;
				case 40: word="reiterate";break;
				case 41: word="utopia";break;
				case 42: word="antidisestablishmentarianism";break;
				case 43: word="inevitable";break;
				case 44: word="saxophone";break;
				case 45: word="dilemma";break;
				case 46: word="veterinarian";break;
				case 47: word="hydrangea";break;
				case 48: word="pragmatic";break;
				case 49: word="platinum";break;
				case 50: word="extravagant";break;
			}
		}

		//This section will let the user choose the difficulty
		System.out.println("\nChoose your difficulty:\n1. Hard (you only get 4 wrong guesses)\n2. Medium (you get 6 wrong guesses)\n3. Easy (you get 10 wrong guesses)");
		difficulty=Integer.parseInt(keyboard.readLine());

		//This section will change the number of guesses depending on the difficulty chosen
		switch(difficulty)
		{
			case 1: guesses=4;break;
			case 2: guesses=6;break;
			case 3: guesses=10;break;
		}

		//This section will define a new character array and fill it with dashes
		char wordGuesses[]=new char[word.length()];
		for (int i=0;i<=word.length()-1;i++)
		{
			wordGuesses[i]='-';
		}

		//This section makes up the bulk of the game, letting the user guess letters and displaying their progress
		while (counter<guesses)
		{
			gallowss=gallows(counter, difficulty);
			correct=0;
			System.out.println("\n" + gallowss);
			for (int i=0;i<=word.length()-1;i++)
			{
				System.out.print(wordGuesses[i]);
			}
			System.out.println("\nWrong Guesses: " + wrongGuesses);
			System.out.print("Guess a letter (or the word when you're ready!): ");
			guess=keyboard.readLine();
			guesss=guess.charAt(0);

			if (guess.equals(word))
			{
				win=1;
				counter=guesses;
			}
			else
			{
				for (int i=0;i<=word.length()-1;i++)
				{
					if (guesss==word.charAt(i))
					{
						wordGuesses[i]=guesss;
						correct=1;
					}
				}
				if (correct!=1)
				{
					counter=counter+1;
					wrongGuesses=wrongGuesses + guesss + " ";
				}
			}

			wordCheck="";
			for (int i=0;i<=word.length()-1;i++)
			{
				wordCheck=wordCheck + wordGuesses[i];
			}
			if (wordCheck.equals(word))
			{
				win=1;
				counter=guesses;
			}
		}

		//This section checks to see if the user has won or lost and displays the results
		if (win==1)
		{
			if (wordChoice==42)
			{
				System.out.println("Wow, you guessed antidisestablishmentarianism, the hardest word in the game!!");
			}
			else
			{
				System.out.println("\nGood Job, you guessed the word " + word + "!!\n");
			}
		}
		else
		{
			gallowss=gallows(counter, difficulty);
			System.out.println(gallowss);
			System.out.println("\nBetter luck next time...the word was " + word + ".\n");
		}
	}
}
Image

User avatar
hascoolnickname
Heavy
Heavy
Posts: 1921
Joined: Sat Feb 26, 2005 10:47 pm
Location: :noitacoL

Post by hascoolnickname » Sun Mar 18, 2007 10:47 pm

meant to hit you up on aim, but you can define a boolean to false and make a do while loop around your whole gallows method, the while being the boolean != true (or is false, same thing) and after your last if/else statements add another group of if/else statments- asking if they want to play again and reading in their answer

Code: Select all

if(keyboard.readLine().equalsIgnoreCase("yes")){
bool1 = true; //bool1 being your boolean that you defined outside of the do while loop
}
else
bool1 = false;
or something along those lines. I only played it once and briefly glanced over the code but it works pretty well.[/code]

User avatar
Green Tea
Heavy
Heavy
Posts: 1863
Joined: Fri May 27, 2005 1:00 am
Location: Cleveland Ohio
Contact:

Post by Green Tea » Tue Mar 20, 2007 10:48 am

hascoolnickname wrote:meant to hit you up on aim, but you can define a boolean to false and make a do while loop around your whole gallows method, the while being the boolean != true (or is false, same thing) and after your last if/else statements add another group of if/else statments- asking if they want to play again and reading in their answer

Code: Select all

if(keyboard.readLine().equalsIgnoreCase("yes")){
bool1 = true; //bool1 being your boolean that you defined outside of the do while loop
}
else
bool1 = false;
or something along those lines. I only played it once and briefly glanced over the code but it works pretty well.[/code]
Starting off, I forgot how annoying console input was in java :D (GUI is much easier)

The boolean idea would be good, you could also do some more switch cases in there.

A couple suggestions that will make life easier for your programming in the future:
first off I need to slap your wrist for the guesss char and gallowss string. That is a bad habit that I worked hard to get out of. Be sure to make every variable fairly clear when you use it, so it doesn't look like other words. a suggestion would be you could use:

Code: Select all

char guessChar;
String gallowStr="";
or something of the sort.

A good habit to get into as well is if you do initialize variables (which I typically don't do, but it's not a bad habit), you could give a brief description of every variable and what it is used for. I wish I could find a good example from my programming stuff but purdue CS department kindly deleted everything on my CS account after I changed my major from computational chemistry to ACS chemistry. Other than those suggestions it looks pretty good.

If I were to change one thing though it would be just to make it a GUI program. I hate console input.
dance1005 wrote:Fucking retarded bots, bumping threads with dildos.

User avatar
danc1005
Heavy
Heavy
Posts: 3237
Joined: Tue Feb 28, 2006 7:21 pm
Location: Da Fort
Contact:

Post by danc1005 » Tue Mar 20, 2007 2:42 pm

Yeah, I might go back and redo it after I learn how to make GUIs. Also, I was under the impression that you had to declare variables? That's the way my comp. programming teacher made it sound.
Image

User avatar
hascoolnickname
Heavy
Heavy
Posts: 1921
Joined: Sat Feb 26, 2005 10:47 pm
Location: :noitacoL

Post by hascoolnickname » Tue Mar 20, 2007 7:03 pm

he means you dont have to declare the variables before you actually use them, but you'll want to do so if you want it to be used in any other methods or inherited classes and if you define a variable within a loop you can only use it within that loop.

Post Reply