/* * Created on 19.Eki.2004 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */
/** * @author ZURGUN * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */
import java.awt.*; import java.awt.event.*;
import javax.swing.*;
// the class hangman extends JApplet and implements ActionListener public class Hangman extends JApplet implements ActionListener {
final int WON = 0, LOST = 1, CONTINUE = 2; // the variables are used in defining the game status
boolean no1Gate = false; // these variables are used to make sure that the if statement that boolean no2Gate = false; // checks the random number enters the if statemenet only once. boolean no3Gate = false; // In other words these variables are used to make sure that the boolean no4Gate = false; // won counter is incremented only once even if it enters the "if" more than once
int gameStatus = CONTINUE; // game status declaration
int limbCounter = 5; // limb counter that'll be decremented if the player makes a wrong guess
int correctCounter = 0; // correct counter will be incremented if the player makes a correct guess
int randomNumber = 0; // initialization of the random number
int number1 ; // initialization of digits of the number to be guessed int number2 ; int number3 ; int number4 ;
// GUI components that will be used JLabel limbCount, randomLabel; JTextField no1Field, no2Field, no3Field, no4Field, limbCountField, randomField; JButton guessButton;
//set up GUI components and generate the random number that'll be guessed public void init() { // obtain content pane and change its layout to Flowlayout Container container = getContentPane(); container.setLayout(new FlowLayout());
// create label and text field for the GUI
no1Field = new JTextField(5); no1Field.setEditable(false); container.add(no1Field); no1Field.setText("*");
no2Field = new JTextField(5); no2Field.setEditable(false); container.add(no2Field); no2Field.setText("*");
no3Field = new JTextField(5); no3Field.setEditable(false); container.add(no3Field); no3Field.setText("*");
no4Field = new JTextField(5); no4Field.setEditable(false); container.add(no4Field); no4Field.setText("*");
guessButton = new JButton(" Guess Next Digit Randomly"); guessButton.addActionListener(this); container.add(guessButton);
limbCount = new JLabel("
Limb Counter"); container.add(limbCount); limbCountField = new JTextField(5); limbCountField.setEditable(false); limbCountField.setText(Integer.toString(5)); container.add(limbCountField);
randomLabel = new JLabel("
Your guess is:"); container.add(randomLabel); randomField = new JTextField(5); randomField.setEditable(false); container.add(randomField); randomField.setText("");
// generation of random numbers number1 = 1 + (int)(Math.random()*9); number2 = (int)(Math.random()*10); number3 = (int)(Math.random()*10); number4 = (int)(Math.random()*10);
// System.out.println("" + number1 + number2 + number3 + number4); ==> this can be used to trace the corecctness of the code from the execution console
} //end method init
// process one guess public void actionPerformed(ActionEvent hangmanEvent){
randomNumber = randomGuess(); // call to the method randomGuess that generates a random number // System.out.println("" + randomNumber); ==> this can be used to trace the correctness of the code
// check if the guess is correct and check if it is entered before. increment as needed
// if a wrong guess is made decrement the limb counter. else{ gameStatus = CONTINUE; limbCounter --; limbCountField.setText(Integer.toString(limbCounter)); }
// if limbcounter is zero game status is LOST if(limbCounter == 0){ gameStatus = LOST; }
// if correct counter is 4 game status is WON if (correctCounter >= 4){ gameStatus = WON; }
displayMessage(); // call method displayMessage
} //end of action performed
// display the according message looking at the game status public void displayMessage(){ if(gameStatus == CONTINUE) showStatus("Click 'Guess Next Digit Randomly' again to make a guess"); else{ if (gameStatus == WON){ showStatus("You won the game. Click 'Guess Next Digit Randomly' to play again"); restart(); // call to the method restart } else{ showStatus("You lost the game. Click 'Guess Next Digit Randomly' to play again"); restart(); // call to the method restart } }
} // end of display message
// the method rendomGuess generates a random number for the guess of the user public int randomGuess(){
int x = (int) (Math.random()*10); randomField.setText(Integer.toString(x)); // user sees what he guessed by the randomGuess return x; }// end of method randomGuess
// the method restart, restarts the game public void restart(){
Sitede yayınlanan dosya ve dökümanların kullanımları sonucu oluşabilecek zararlardan duzenle.com sorumlu değildir. duzenle.com sitesinde yayınlanan tüm program, script ve benzeri dökümanları kurmadan yada çalıştırmadan önce virüs taramasından geçiriniz.