IRCForumları - IRC ve mIRC Kullanıcılarının Buluşma Noktası

IRCForumları - IRC ve mIRC Kullanıcılarının Buluşma Noktası (https://www.ircforumlari.net/)
-   JavaScript (https://www.ircforumlari.net/javascript/)
-   -   Java (AWT) ile Adam Asmaca Oyunu (https://www.ircforumlari.net/javascript/20300-java-awt-ile-adam-asmaca-oyunu.html)

Cheki 04 Temmuz 2006 11:05

Java (AWT) ile Adam Asmaca Oyunu
 
Kod:

/*


 *

 * 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("\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 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 (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 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(){

               

                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



Tüm Zamanlar GMT +3 Olarak Ayarlanmış. Şuanki Zaman: 04:30.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO
Copyright ©2004 - 2025 IRCForumlari.Net Sparhawk