Hangman (adam asmaca )

INFeRNaL

Özel Üye
22 Nis 2009
6,516
1
Mersin
/*
* 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 **** 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("\t\t\tGuess Next Digit Randomly");
guessButton.addActionListener(this);
container.add(guessButton);

limbCount = new JLabel("\nLimb Counter");
container.add(limbCount);
limbCountField = new JTextField(5);
limbCountField.setEditable(false);
limbCountField.setText(Integer.toString(5));
container.add(limbCountField);

randomLabel = new JLabel("\nYour 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 **** 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 (randomNumber == number1 || randomNumber == number2 || randomNumber == number3 || randomNumber == number4){

if(randomNumber == number1 && no1Gate == false){
gameStatus = CONTINUE;
no1Field.setText(Integer.toString(number1));
no1Gate = true;
correctCounter ++;
}

if(randomNumber == number2 && no2Gate == false){
gameStatus = CONTINUE;
no2Field.setText(Integer.toString(number2));
no2Gate = true;
correctCounter ++;
}

if(randomNumber == number3 && no3Gate == false){
gameStatus = CONTINUE;
no3Field.setText(Integer.toString(number3));
no3Gate = true;
correctCounter ++;
}

if(randomNumber == number4 && no4Gate == false){
gameStatus = CONTINUE;
no4Field.setText(Integer.toString(number4));
no4Gate = true;
correctCounter ++;
} }

// 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 **** 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 **** restart(){

randomField.setText("");

no1Field.setText("*");
no2Field.setText("*");
no3Field.setText("*");
no4Field.setText("*");

correctCounter = 0;

limbCounter = 5;
limbCountField.setText(Integer.toString(5));

number1 = 1 + (int)(Math.random()*9);
number2 = (int)(Math.random()*10);
number3 = (int)(Math.random()*10);
number4 = (int)(Math.random()*10);

no1Gate = false;
no2Gate = false;
no3Gate = false;
no4Gate = false;

}

} // end of class Hangman
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.