Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Classes and Code Snippets

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Classes and Code Snippets Post your source code and classes here

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-08-2008, 12:07 PM
Blmaster Blmaster is offline
Learning Programmer
 
Join Date: Jul 2008
Posts: 34
Credits: 31
Rep Power: 1
Blmaster will become famous soon enough
Default Source Code: Guess the Number Game

This is a simple guess the number game that I though I should do since I just began Java. Then I kept changing it and now the code has a lot of code that new beginners can see and learn from. Atleast I think so. So here it is:
(if anyone doesnt understand my code or has a better way [im not that great, just started] to do something that i did, please feel free to reply)

(It might help to see the code better if you copy code into your complier)
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public cl*** GuessNumber implements ActionListener	{
	private final double VERSION = 2.1;
	//Initializing Main Window and Difficulty Window
	JFrame window = new JFrame("Guess The Number " + VERSION);
	JFrame DiffFrame = new JFrame("Difficulty");
	
	//Making Buttons
	JButton btnNewGame = new JButton("New Game");
	JButton btnInstruction = new JButton("Instructions");
	JButton btnDifficulty = new JButton("Change Difficulty");
	JButton btnAbout = new JButton("About");
	JButton btnExit = new JButton("Exit");
	JButton btnOK = new JButton("Ok");
	JButton btnDiff[] = new JButton[6];

	//Making Panel for Main Menu Buttons
	JPanel pnlMainMenu = new JPanel();
	//Making Panel for Difficulty Buttons
	JPanel pnlDifficulty = new JPanel();
	
	//All Variables used initialized here
	int diff = 100;
	int tries;
	int Secret=50;
	int Guess;
	int option = 0;
	boolean Cancel = false;
	
	GuessNumber()	{ //constructor
		//Setting Main Window properties
		window.setSize(270, 205);
		window.setLocation(530, 230);	
		window.setLayout(new FlowLayout(FlowLayout.CENTER));
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Setting Dificulty Window properties
		DiffFrame.setLocation(530, 230);	
		DiffFrame.setLayout(new BorderLayout());
		DiffFrame.setSize(230, 210);
		//MainMenu Panel Layout and adding Main Menu Buttons	 
		//								  GridLayout(int rows, int columns, int Horizontal_Gap, intVertical_Gap)
		pnlMainMenu.setLayout(new GridLayout(5, 1, 2, 8));
		pnlMainMenu.add(btnNewGame);
		pnlMainMenu.add(btnInstruction);
		pnlMainMenu.add(btnDifficulty);
		pnlMainMenu.add(btnAbout);
		pnlMainMenu.add(btnExit);
		pnlMainMenu.setBackground(Color.red);
		//Setting Layout for Difficulty Panel
		pnlDifficulty.setLayout(new GridLayout(6, 1, 2, 2));
		
		//Making Difficulty Buttons
		btnDiff[0] = new JButton("Very Easy (0 - 3)");
		btnDiff[1] = new JButton("Easy (0 - 50)");
		btnDiff[2] = new JButton("Medium (0 - 100)");
		btnDiff[3] = new JButton("Hard (0 - 500)");
		btnDiff[4] = new JButton("Very Hard (0 - 1000)");
		btnDiff[5] = new JButton("Custom (0 - ?)");
		
		//Adding Action Listener for MainMenu Buttons
		btnNewGame.addActionListener(this);
		btnInstruction.addActionListener(this);
		btnDifficulty.addActionListener(this);
		btnAbout.addActionListener(this);
		btnExit.addActionListener(this);
		btnOK.addActionListener(this);
		
		//Adding Difficulty Buttons to Panel and adding Action Listener
		for(int i=0; i<6; i++)	{
			btnDiff[i].addActionListener(this);
			pnlDifficulty.add(btnDiff[i]);
		}
		//Showing Window
		window.add(pnlMainMenu);
		window.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent click)	{
		System.out.println("Action Performed");
		if(click.getSource() == btnNewGame)	{
			NewGame();
		}
		if(click.getSource() == btnExit)	{
			option = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit Game" ,JOptionPane.YES_NO_OPTION);
			if(option == JOptionPane.YES_OPTION)
				System.exit(0);
		}
		if(click.getSource() == btnInstruction)	{
			JOptionPane.showMessageDialog(null,
			"Game:" + "\nClick New Game to start a new game.\nGuess a number between 0 and " + diff + ".\nKeep Guessing until you get it correct."
			+ "\n\nDifficulty:" + "\nYou can change the difficulty of the game\n in the Main Menu to a Custom range or a \npreset range."
			, "Instructions", JOptionPane.INFORMATION_MESSAGE);
		}
		if(click.getSource() == btnAbout)	{
			JOptionPane.showMessageDialog(null, "Title: Guess The Number" + "\nCreated: August 5, 2008" + "\nChanges:" + 
				"\n2.2- changed instruction message" + "\n2.1- changed win message" +
				"\n2.0- added difficulty change function." + "\n1.8- Cancel button works during New Game"	+
				"\n1.7- added About button." + "\n1.6- added Play Again function" + "\n1.5- added Instruction button." + 
				"\n1.2- added tries to guess number." + "\n1.1- added Exit button." + "\n1.0- made it GUI now." + 
				"\n0.5- added basic guessing function.",
				"About", JOptionPane.INFORMATION_MESSAGE);
		}
		if(click.getSource() == btnDifficulty)	{
			Change_Difficulty();
		}
		for(int i=0; i<6; i++)	{
			if(click.getSource() == btnDiff[i])	{
				if(click.getSource() == btnDiff[0])
					diff = 3;
				if(click.getSource() == btnDiff[1])
					diff = 50;
				if(click.getSource() == btnDiff[2])
					diff = 100;
				if(click.getSource() == btnDiff[3])
					diff = 500;
				if(click.getSource() == btnDiff[4])
					diff = 1000;
				if(click.getSource() == btnDiff[5])
					diff = Custom();
				DiffFrame.setVisible(false);
			}
		}
	}

	public void NewGame()	{
		tries = 1;
		Guess = 101;
		Secret = (int)((Math.random()) * (diff + 1));
		
		while(Guess != Secret)	{
			try	{
				if(tries == 1)	{
					Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: 1" + "\nGuess a number between 0 and " + diff, "Guess?", JOptionPane.PLAIN_MESSAGE));
					
					tries++;
				}	else	{
					if(Guess > Secret)
						Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: " + tries + "\n" + Guess + "\nGuess Lower..."));
					else if(Guess < Secret)
						Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: " + tries + "\n" + Guess + "\nGuess Higher..."));
					tries++;
				}
			} catch(NumberFormatException e)	{
				if(e.getMessage() == "null")	{
					option = JOptionPane.showConfirmDialog(null, "Are you sure you want to go back to the Main Menu?", "Cancel?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if(option == JOptionPane.YES_OPTION)	{
						Cancel = true;
						break;
					}
				}
				JOptionPane.showMessageDialog(null, "Error: " + e.getMessage() + "\nEnter whole numbers only!");
			}
		}
		if(!Cancel)	{
			tries--;
			JOptionPane.showMessageDialog(null, Guess + " is Correct!!\nYou WON in " + tries + " tries.", "Winner", JOptionPane.INFORMATION_MESSAGE);
			option = JOptionPane.showConfirmDialog(null, "Do you want to try again?", "Try Again?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(option == JOptionPane.YES_OPTION)
				NewGame();
		}
	}
	
	public void Change_Difficulty()	{
		DiffFrame.add(pnlDifficulty, BorderLayout.CENTER);
		DiffFrame.setVisible(true);
	}
	
	public int Custom()	{
		try	{
			diff = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number that you want to be the range! (0 to ?)", diff));
		} catch(NumberFormatException e)	{
				
		}
		return diff;
	}
	
	public static void main(String[] args)	{
		//Initializes the cl*** GuessNumber and the constructor GuessNumber()
		new GuessNumber();
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 08-10-2008, 09:23 AM
Blmaster Blmaster is offline
Learning Programmer
 
Join Date: Jul 2008
Posts: 34
Credits: 31
Rep Power: 1
Blmaster will become famous soon enough
Default Re: Source Code: Guess the Number Game

EDITED: fixed bug where the win message wouldnt show after you start new game and click cancel....

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public cl*** GuessNumber implements ActionListener	{
	private final double VERSION = 2.3;
	//Initializing Main Window and Difficulty Window
	JFrame window = new JFrame("Guess The Number " + VERSION);
	JFrame DiffFrame = new JFrame("Difficulty");
	
	//Making Buttons
	JButton btnNewGame = new JButton("New Game");
	JButton btnInstruction = new JButton("Instructions");
	JButton btnDifficulty = new JButton("Change Difficulty");
	JButton btnAbout = new JButton("About");
	JButton btnExit = new JButton("Exit");
	JButton btnOK = new JButton("Ok");
	JButton btnDiff[] = new JButton[6];

	//Making Panel for Main Menu Buttons
	JPanel pnlMainMenu = new JPanel();
	//Making Panel for Difficulty Buttons
	JPanel pnlDifficulty = new JPanel();
	
	//All Variables used initialized here
	int diff = 100;
	int tries;
	int Secret;
	int Guess;
	int option = 0;
	boolean Cancel = false;
	
	GuessNumber()	{ //constructor
		//Setting Main Window properties
		window.setSize(270, 205);
		window.setLocation(500, 260);	
		window.setLayout(new FlowLayout(FlowLayout.CENTER));
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Setting Dificulty Window properties
		DiffFrame.setSize(230, 210);
		DiffFrame.setLocation(530, 230);	
		DiffFrame.setLayout(new BorderLayout());
		//MainMenu Panel Layout and adding Main Menu Buttons	 
		//								  GridLayout(int rows, int columns, int Horizontal_Gap, intVertical_Gap)
		pnlMainMenu.setLayout(new GridLayout(5, 1, 2, 8));
		pnlMainMenu.add(btnNewGame);
		pnlMainMenu.add(btnInstruction);
		pnlMainMenu.add(btnDifficulty);
		pnlMainMenu.add(btnAbout);
		pnlMainMenu.add(btnExit);
		pnlMainMenu.setBackground(Color.red);
		//Setting Layout for Difficulty Panel
		pnlDifficulty.setLayout(new GridLayout(6, 1, 2, 2));
		
		//Making Difficulty Buttons
		btnDiff[0] = new JButton("Very Easy (0 - 3)");
		btnDiff[1] = new JButton("Easy (0 - 50)");
		btnDiff[2] = new JButton("Medium (0 - 100)");
		btnDiff[3] = new JButton("Hard (0 - 500)");
		btnDiff[4] = new JButton("Very Hard (0 - 1000)");
		btnDiff[5] = new JButton("Custom (0 - ?)");
		
		//Adding Action Listener for MainMenu Buttons
		btnNewGame.addActionListener(this);
		btnInstruction.addActionListener(this);
		btnDifficulty.addActionListener(this);
		btnAbout.addActionListener(this);
		btnExit.addActionListener(this);
		btnOK.addActionListener(this);
		
		//Adding Difficulty Buttons to Panel and adding Action Listener
		for(int i=0; i<6; i++)	{
			btnDiff[i].addActionListener(this);
			pnlDifficulty.add(btnDiff[i]);
		}
		//Showing Window
		window.add(pnlMainMenu);
		window.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent click)	{
		System.out.println("Action Performed");
		if(click.getSource() == btnNewGame)	{
			NewGame();
		}
		if(click.getSource() == btnExit)	{
			option = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Exit Game" ,JOptionPane.YES_NO_OPTION);
			if(option == JOptionPane.YES_OPTION)
				System.exit(0);
		}
		if(click.getSource() == btnInstruction)	{
			JOptionPane.showMessageDialog(null,
			"Game:" + "\nClick New Game to start a new game.\nGuess a number between 0 and " + diff + ".\nKeep Guessing until you get it correct."
			+ "\n\nDifficulty:" + "\nYou can change the difficulty of the game\n in the Main Menu to a Custom range or a \npreset range."
			, "Instructions", JOptionPane.INFORMATION_MESSAGE);
		}
		if(click.getSource() == btnAbout)	{
			JOptionPane.showMessageDialog(null, "Title: Guess The Number" + "\nCreated: August 5, 2008" + "\nChanges:" + 
				"\n2.3- fixed bug (Win Message wouldnt show)" + "\n2.2- changed instruction message" + "\n2.1- changed win message" +
				"\n2.0- added difficulty change function." + "\n1.8- Cancel button works during New Game"	+
				"\n1.7- added About button." + "\n1.6- added Play Again function" + "\n1.5- added Instruction button." + 
				"\n1.2- added tries to guess number." + "\n1.1- added Exit button." + "\n1.0- made it GUI now." + 
				"\n0.5- added basic guessing function.",
				"About", JOptionPane.INFORMATION_MESSAGE);
		}
		if(click.getSource() == btnDifficulty)	{
			Change_Difficulty();
		}
		for(int i=0; i<6; i++)	{
			if(click.getSource() == btnDiff[i])	{
				if(click.getSource() == btnDiff[0])
					diff = 3;
				if(click.getSource() == btnDiff[1])
					diff = 50;
				if(click.getSource() == btnDiff[2])
					diff = 100;
				if(click.getSource() == btnDiff[3])
					diff = 500;
				if(click.getSource() == btnDiff[4])
					diff = 1000;
				if(click.getSource() == btnDiff[5])
					diff = Custom();
				DiffFrame.setVisible(false);
			}
		}
	}

	public void NewGame()	{
		tries = 1;
		Guess = 101;
		Secret = (int)((Math.random()) * (diff + 1));
		Cancel = false;
		
		while(Guess != Secret)	{
			try	{
				if(tries == 1)	{
					Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: 1" + "\nGuess a number between 0 and " + diff, "Guess?", JOptionPane.PLAIN_MESSAGE));
					
					tries++;
				}	else	{
					if(Guess > Secret)
						Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: " + tries + "\n" + Guess + "\nGuess Lower..."));
					else if(Guess < Secret)
						Guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try: " + tries + "\n" + Guess + "\nGuess Higher..."));
					tries++;
				}
			} catch(NumberFormatException e)	{
				if(e.getMessage() == "null")	{
					option = JOptionPane.showConfirmDialog(null, "Are you sure you want to go back to the Main Menu?", "Cancel?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if(option == JOptionPane.YES_OPTION)	{
						Cancel = true;
						break;
					}
				}
				JOptionPane.showMessageDialog(null, "Error: " + e.getMessage() + "\nEnter whole numbers only!");
			}
		}
		if(!Cancel)	{
			tries--;
			JOptionPane.showMessageDialog(null, Guess + " is Correct!!\nYou WON in " + tries + " tries.", "Winner", JOptionPane.INFORMATION_MESSAGE);
			option = JOptionPane.showConfirmDialog(null, "Do you want to try again?", "Try Again?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
			if(option == JOptionPane.YES_OPTION)
				NewGame();
		}
	}
	
	public void Change_Difficulty()	{
		DiffFrame.add(pnlDifficulty, BorderLayout.CENTER);
		DiffFrame.setVisible(true);
	}
	
	public int Custom()	{
		try	{
			diff = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number that you want to be the range! (0 to ?)", diff));
		} catch(NumberFormatException e)	{
				
		}
		return diff;
	}
	
	public static void main(String[] args)	{
		//Initializes the cl*** GuessNumber and the constructor GuessNumber()
		new GuessNumber();
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-12-2008, 04:41 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 7,333
Last Blog:
Tramp Variables
Credits: 1
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: Source Code: Guess the Number Game

Excellent code, thank you for posting!
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-12-2008, 05:30 PM
Blmaster Blmaster is offline
Learning Programmer
 
Join Date: Jul 2008
Posts: 34
Credits: 31
Rep Power: 1
Blmaster will become famous soon enough
Default Re: Source Code: Guess the Number Game

Thanks!! Its all thanks to you for helping me activate my account. I still didnt receive any email from codecall, i looked in spam too.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-24-2008, 01:30 AM
Ollox360 Ollox360 is offline
Newbie
 
Join Date: Jun 2008
Posts: 17
Credits: 2
Rep Power: 2
Ollox360 is on a distinguished road
Default Re: Source Code: Guess the Number Game

cool, i need to test. btw, you should compile it before you posted and upload it to i mean ..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 08-24-2008, 10:17 AM
Blmaster Blmaster is offline
Learning Programmer
 
Join Date: Jul 2008
Posts: 34
Credits: 31
Rep Power: 1
Blmaster will become famous soon enough
Default Re: Source Code: Guess the Number Game

well i upload here the .jar file!


here it is:
Attached Files To view attachments in this forum your post count must be 1 or greater. You currently have 0 posts.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-25-2008, 10:21 AM
Ollox360 Ollox360 is offline
Newbie
 
Join Date: Jun 2008
Posts: 17
Credits: 2
Rep Power: 2
Ollox360 is on a distinguished road
Default Re: Source Code: Guess the Number Game

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-25-2008, 10:28 AM
morefood2001's Avatar   
morefood2001 morefood2001 is offline
Guru
 
Join Date: Jan 2008
Location: Western New York
Posts: 1,241
Last Blog:
VPS Hosting with Revie...
Credits: 879
Rep Power: 14
morefood2001 is a jewel in the roughmorefood2001 is a jewel in the roughmorefood2001 is a jewel in the roughmorefood2001 is a jewel in the rough
Send a message via AIM to morefood2001 Send a message via MSN to morefood2001 Send a message via Yahoo to morefood2001 Send a message via Skype™ to morefood2001
Default Re: Source Code: Guess the Number Game

Great code!!!! I will likely use this myself some day +rep!
__________________
Phil Matuskiewicz
My Personal Website My Other Website
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-25-2008, 07:47 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,237
Last Blog:
Passwords
Credits: 877
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default Re: Source Code: Guess the Number Game

I'm not sure I'll ever have a use for it, but the code is very well written nonetheless.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 08-25-2008, 07:51 PM
chili5's Avatar   
chili5 chili5 is online now
Code Warrior
 
Join Date: Mar 2008
Age: 15
Posts: 3,447
Credits: 312
Rep Power: 30
chili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to allchili5 is a name known to all
Default Re: Source Code: Guess the Number Game

That's excellent. Seems very easy to understand just by looking at it.

Great job.
__________________
Success is the ability to go from failure to failure without losing your enthusiasm. ~ Winston Churchill
Braingle
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Guess a Number 3n! C and C++ 2 12-02-2007 03:44 PM
Microsoft to Release .NET Framework Source Code in v3.5 Kernel Programming News 0 10-05-2007 07:45 AM
Valve: Half-Life 2 Source Code falco85 General Programming 3 10-28-2006 05:03 PM


All times are GMT -5. The time now is 11:06 AM.

Contest Stats

Xav ........ 1323.18
MeTh0Dz|Reb0rn ........ 1053.7
morefood2001 ........ 879.43
John ........ 877.37
marwex89 ........ 869.98
WingedPanther ........ 840.94
Brandon W ........ 751.07
chili5 ........ 312.39
Steve.L ........ 241.84
dcs ........ 216.02

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads