All Your Gaming Needs @cGeeks.com

Up to 87% savings on Geeks.com!

SECRET CAMCORDER Prices too low to show! Expires 8/31
This sale includes 22 camcorders from Sony, JVC, Samsung, and Canon!

Take 10% Off All MCM Electronics Purcases!

HP Store search box: Computers, Electronics, Hardware

Search:
   

Computer Game in Java

By Sergey Skudaev

Here you will find a Java code example for a computer game. The Java source code for this tutorial can be downloaded at the bottom of this page. You have to be familiar with Java Applets to understand the source code.

First player of the game enters a word in the text field. The text field displays asterisk characters for each letter, so that the second player of the game cannot read the word.. The first player of the game clicks Input button and only the first and the last letters of the word display in the upper text field array. The second player of the game guess a letter, type it in the guessed position and clicks the Check button. If the letter exist within the word, it displays in the right position, even if the player typed it in wrong position. If the letter does not exist it disappears from the array of text field.

For computer game applet I used different layouts to place panels with title, text field array, input text and buttons. Applet has BorderLayout layout. The BorderLayout has North area, Center area, South area, West and East areas.

The title panel is located in the North area. The text field array panel and text field panel are located on maim panel. The main panel has grid layout. It has two rows and one column. Text field array for letter of the word is located in the first row. the input text field is located in the second row, The main panel is located in the Center area.

The button panel is located in South area and hold two button.

Here you will learn how to use BorderLayout, FlowLayout and GridLayout.

Java Source Code for Computer Game Applet

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Guessword extends Applet implements ActionListener
{
    char charArr[];
    String input;
    int len;
    boolean equalFlag = false;
    private TextField tinput;
    private TextField[] txtCh = new TextField[15];
     Button btnInput, btnCheck;
    Panel pbtn;
    Panel ptxt;
    Panel pinput;
    Panel pmain;
    Panel pTitle;
    Label lblTitle;>br>     int count;

public void init()
{
     //set layout for applet
     setLayout(new BorderLayout());

     //create character array to hold single letters of the input word
     charArr = new char[15];
     for(int i=0;i<15;i++)
     charArr[i] = ' '; //initialize array with blank

     //create label for title with central alignment and font
     lblTitle = new Label("Guess word game", Label.CENTER);
     lblTitle.setFont(new Font("TimesRoman", Font.BOLD, 30));

     //create title panel and add title label to panel
     pTitle = new Panel();
     pTitle.setLayout(new FlowLayout(FlowLayout.CENTER));

     pTitle.add(lblTitle);

     //create panel for array of text field and set layout
     ptxt = new Panel();
     ptxt.setLayout(new FlowLayout());

     //Create array of text fields
     for(int i = 0; i <15; i++)
     {
     txtCh[i] = new TextField();
     }

     //add text array of fields to panel
     ptxt.add(txtCh[0]);
     ptxt.add(txtCh[1]);
     ptxt.add(txtCh[2]);
     ptxt.add(txtCh[3]);
     ptxt.add(txtCh[4]);
     ptxt.add(txtCh[5]);
     ptxt.add(txtCh[6]);
     ptxt.add(txtCh[7]);
     ptxt.add(txtCh[8]);
     ptxt.add(txtCh[9]);
     ptxt.add(txtCh[10]);
     ptxt.add(txtCh[11]);
     ptxt.add(txtCh[12]);
     ptxt.add(txtCh[13]);
     ptxt.add(txtCh[14]);

     //create panel for input text and set layout
     pinput = new Panel();
     pinput.setLayout(new FlowLayout());

     //create input text field and set echo character
     tinput = new TextField(15);

     tinput.setEchoChar('*');

     //add input text field to panel
     pinput.add(tinput);

     //create main panel with grid layout
     //with two rows and one column
     pmain=new Panel();
     pmain.setLayout(new GridLayout(2,1));

     //add text array panel and input text panel to central panel
     pmain.add(ptxt);
     pmain.add(pinput);

     //create button panel and button
     pbtn = new Panel();
     pbtn.setLayout(new FlowLayout());
     btnInput = new Button("Input");

     // add action listener to input button
     //to get button click event
     btnInput.addActionListener(this);

     //create check button and add both button to panel
     btnCheck = new Button("Check");
     btnCheck.addActionListener(this);
     pbtn.add(btnInput);
     pbtn.add(btnCheck);

      //add all panels to applet
      add(pTitle, BorderLayout.NORTH);
      add(pmain, BorderLayout.CENTER);
      add(pbtn, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e)
{

     //if user clicks input button
     //fill array with blank char to reset it
     if(e.getSource() == btnInput)
     {
          for(int i=0;i<15;i++)
          {
          charArr[i] = ' ';
          txtCh[i].setText("");
          }
          count = 0;

     input = tinput.getText();
     //get length of the word
     len = input.length();

     //fill array with a word
     for(int i = 1; i< len-1; i++)
     charArr[i] = input.charAt(i);

      //show first letter
     txtCh[0].setText(String.valueOf(input.charAt(0)));
     //show last letter
     txtCh[len-1].setText(String.valueOf(input.charAt(len-1)));

          //clear the input text
          tinput.setText("");

          } //if user clicks Check button
           else if (e.getSource() == btnCheck)
          {
           count += 1;

     //look through the text field array
     //if it contains a letter that exist in char array
     //display the letter in the right text field
      for(int i = 1; i<len-1; i++) //first for

     {

          if(!((txtCh[i].getText().equals(String.valueOf(charArr[i])))))
          {

             for(int n = 1; n<len-1; n++)
             {
              for(int j = 1; j < len-1; j++)
              {
                if( (txtCh[n].getText().equals(String.valueOf(charArr[j]))))
               {
                txtCh[j].setText(String.valueOf(charArr[j]));
               }
              }
             }
          }

           //if guessed letter is not in char array remove it
           if (!((txtCh[i].getText().equals(String.valueOf(charArr[i])))))
             txtCh[i].setText("");
           }//end first for

      showStatus(String.valueOf(count));
     } //else if source=btnCheck
   } //end function
} //end class

Download source file:

Guessword.java

Please rate the tutorial

1 2 3 4 5


Home