hey everyone
:confused: i have a new java task comming up and it involves making a game that impliments buttons. for this task i have started to build a golf game but everything i do seems to not work i keep getting stuc.
bellow is a copy of my task.
ps..thankyou ricster for the help with my last task some of your code was helpful :mrgreen:


Outline: An Object Oriented Program (OOP) is an application that uses data objects that have properties and methods that determine what they do. Examples of such objects are screen buttons, images and frames.

What to do: Create a Java application that allows the user to play a simple game using buttons. One button is to allow the user to reset the game at any stage.

The application should present at least one screen frame (window) which contains the necessary objects for the user to play the game. It should give the title of the game, how to play it and the game's creator.

You need to create a single, properly formatted Appleworks file called Documentation which includes these sections:

the title, author and purpose of the project
a brief description of the game itself and how to start it executing
a list of all the files included in your project. For each of these files, give the name of the file, its function in the project and a full listing of the Java code, including comments.
What to submit: When you are ready to submit your work,

create a folder called OOPGame.
copy all the files you need for your game ie image/sound files, application file and documentation file etc, into your OOPGame folder.
give the files names that are appropriate for the game.
copy the OOPGame folder to your Hand in folder
Assessment: Your project will be mark according to these criteria:

File management
management of the project files;
Documentation
is set out with suitable section headers, page numbers, styles and fonts;
contains the correct sections for your project;
text is easy to read;
the listings contain sufficient comment statements;
the code uses appropriate identifier names;
the logic of the code is easy to follow;
Implementation
the game presents well on screen;
the application executes without error;
the game provides suitable feedback to the user;
the application uses basic screen elements such as buttons, text areas etc to play the game;
the reset button allows the user to start again;
the application includes advanced elements such as images, sounds etc;
the game 'keeps score';
the use of special features to enhance the running of the game;
-oOo-

I am not allowed to write the whole program for you, however I can help you out in certain areas(more to do with actual programming).
It is up to you to decide on what kind of game to make.
I would start off by creating a spider/brain storm diagram with the ideas you have. Then narrow them down further and pick one that you are satisifed with. You could then perhaps do some sketches of what you want your game to look like and then move onto how to implement these parts in java.

Hope that makes sense! rickster ;)

are you able to give me an example of a java game with program code
thanks

Have a look at the file attached, a simple version of NoughtsAndCrosses. You have to get yourself familiar with using 2-Dimensional Arrays though! :confused:

This should get you started.

Attachments
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NoughtsAndCrosses extends JPanel implements ActionListener
{
    private boolean gameOver; // to keep track of the game status
    private String player; // to keep track of the current player
    private Panel board = new Panel(); // to hold the nine cells
    private Button [][]cell= new Button [3][3]; // the cells
    // these next two labels provide a border around the cells
    private Label blankL = new Label("   ");
    private Label blankR = new Label("   ");
    // the next two labels are centre alligned
    private Label error = new Label ("",1);
    private Label info = new Label ("player X to start", 1);

    // the constructor
    public NoughtsAndCrosses()
    {
      
      setBackground(Color.yellow);
      gameOver = false;
      player = "X"; // player X to start the game
      board.setLayout(new GridLayout(3,3)); // discussed later
      // creates and adds nine buttons to the board
      for (int i = 0; i<3; i++)
      {
        for (int j=0; j<3;j++)
        {
          cell[i][j] = new Button();
          cell[i][j].addActionListener(this);
          board.add(cell[i][j]);
        }
       }
       // positions the items on the screen
       setLayout(new BorderLayout());
       add("Center",board);
       add ("West", blankL);
       add("East", blankR);
       add("North", info);
       add("South",error);
    }

    public void actionPerformed(ActionEvent e)
    {
       if (!gameOver)// process mouse click only if game is not over
       {
        for (int i = 0; i<3; i++)
        {
          for (int j=0; j<3;j++)
          {
            if (e.getSource()== cell[i][j])
            {
              processEvent(i,j); // call worker method to process event
            }
          }
        }
       }
    }

    // worker method to process a given button press
    private void processEvent(int i, int j)
    {
      // check no attempt made to move into an occupied cell
      if (cell[i][j].getLabel().equals("X") ||
                    cell[i][j].getLabel().equals("O"))
      {
          error.setText("This cell already taken!!");
      }
      else
      {
        // clear any error messages
        error.setText("");
        // change button label to current player
        cell[i][j].setLabel(player);
        // check whether this move results in game over
        if (hasWon(i, j))// process game over
        {
          info.setText("  player " + player + " has won!");
          gameOver = true;
        }
        else // process game not over
        {
          // change player
          if (player.equals("X"))
          {
              player = "O";
          }
          else
          {
              player = "X";
          }
          info.setText("player "+player+" to go next");
        }
     }
   }

    // worker method to check if game over
    private boolean hasWon(int i, int j) 
    {
        boolean won;
        // check current row
        won = true;
        for(int col = 0; col<3; col++)
        {
            if (!cell[i][col].getLabel().equals(player))
            {
              won = false;
            }
        }
        if (!won)
        {
        // check current column
            won = true;
            for(int row = 0; row<3; row++)
            {
              if (!cell[row][j].getLabel().equals(player))
              {
                won = false;
              }
            } 
        }
        if (!won)
        {
            // check left diagonal
            won = true;
            for(int num = 0; num<3; num++)
            {
              if (!cell[num][num].getLabel().equals(player))
              {
                won = false;
              }
            }
        }
        if (!won)
        {
            // check right diagonal
            won = true;
            for(int num = 0; num<3; num++)
            {
              if (!cell[2-num][num].getLabel().equals(player))
              {
                won = false;
              }
            }
        }
        return won;
    }
    
    public static void main (String [] args)
	{
		//run the program creating a new JFrame
    	JFrame frame = new JFrame("Noughts and Crosses v1.0");
		NoughtsAndCrosses game = new NoughtsAndCrosses();
		frame.setSize(230,230);
		frame.getContentPane().add(game);
		frame.setVisible(true);
	}
}
This article has been dead for over six months. Start a new discussion instead.