Hi, if you don't know me my name is Dark, i need help with my assignment on Java. Could anyone help me. Here is what my project is about.

http://www.chevalier.nsw.edu.au/academic/computing/courses/sodad/axes.jpg

:-|

Recommended Answers

All 6 Replies

The link only contains a picture! More information would be useful!

Sorry, my bad. Don't worry about that pic, i have to do make a simple java game and i was thinking of dping Noughts and crosses, if you have a code for me to play that game could anyone give it to me because i really want to get up the class because i droped 4 places after the last 4 assessments

I took this code from a thread I dont remember ... so its not on my credit.... anywayz it'll be helpful to you ...

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);
	}
}

Hey, If you want to make cool Java Games, try to read the book titled Black art of Game Programming, I forgot the author though. : ) But If you want, you can e-mail me at grifter140@yahoo.com.

thanks mav2040 and nanosani

nanosani, with that code you gave me do you have another code for it but it uses the code in a different frame

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.