Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Programming Issues simple Game

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Programming Issues simple Game

    Hi very New to Java,

    struggling with a few fundamental principles. I have to write a noughts and crosses program, but there is a few thing I'm not quite understanding;

    why some of my actionPerformed do the same thing?
    how to select 'x' or '0' and then progress the game to start here's my code, might i add its incomplete areas highlighted red are the parts i'm struggling with the most , I have just added the things I will eventually need. hope its not too wishy washy. Any help would be appreciated thanks



    public class NoughtsAndCrossesGame extends JFrame
    {
    private final int FRAME_WIDTH = 500;
    private final int FRAME_HEIGHT = 500;
    private int playingAreaWidth;
    private int playingAreaHeight;

    private JPanel controlPanel;
    private JButton startButton;
    private JTextArea instructionsTA;
    private JButton xButton;
    private JButton oButton;
    private PlayingPanel gamePP;
    private JMenuBar bar;
    private JMenu fileMenu;
    private JMenuItem startAgainMenu;
    private JMenuItem exitMenu;

    private int currentPlayer = 0; // 1 for player 1, 2 for player 2
    // if 0 then game hasn't yet started
    private String [] playerSymbols; // holds X or O as chosen by players.
    private boolean gameUnderWay; // true once play has been pressed - until
    // Start again is selected.

    protected NoughtsAndCrossesBoard myNoughtsAndCrossesBoard;
    protected NoughtsAndCrosses myNoughtsAndCrosses;

    /**
    * Creates a new instance of NoughtsAndCrossesGame
    */
    public NoughtsAndCrossesGame(String title)
    {
    super(title);
    setUpGui();
    setUpGame();
    }


    private void setUpGui()
    {
    setSize(FRAME_WIDTH,FRAME_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    controlPanel = new JPanel();
    startButton = new JButton("Start");
    xButton = new JButton("X");
    oButton = new JButton("O");
    instructionsTA = new JTextArea("",5,30);
    instructionsTA.setEditable(false);

    playingAreaWidth = getWidth()-11;
    playingAreaHeight = getHeight() - 150;

    = new PlayingPanel();
    controlPanel.add(instructionsTA);
    controlPanel.add(xButton);
    controlPanel.add(oButton);
    controlPanel.add(startButton);

    bar = new JMenuBar();
    setJMenuBar(bar);
    fileMenu = new JMenu("File");
    startAgainMenu = new JMenuItem("Start again");
    exitMenu = new JMenuItem("Exit");
    bar.add(fileMenu);
    fileMenu.add(startAgainMenu);
    fileMenu.add(exitMenu);

    xButton.addActionListener(new ButtonWatcher());
    oButton.addActionListener(new ButtonWatcher());
    startButton.addActionListener(new ButtonWatcher());
    gamePP.addMouseListener(new MouseEventer());
    startAgainMenu.addActionListener(new MenuSelection());
    exitMenu.addActionListener(new MenuSelection());

    add(controlPanel, BorderLayout.SOUTH);
    add(gamePP, BorderLayout.CENTER);
    }

    private void setUpGame()
    {
    currentPlayer = 1;
    gameUnderWay = false;
    playerSymbols = new String [2] ;
    myNoughtsAndCrosses = new NoughtsAndCrosses();
    myNoughtsAndCrossesBoard = new NoughtsAndCrossesBoard(playingAreaWidth, playingAreaHeight);
    setUpButtons();
    }

    private void setUpButtons()
    {
    xButton.setEnabled(true);
    oButton.setEnabled(true);
    xButton.setVisible(true);
    oButton.setVisible(true);
    startButton.setVisible(false);
    startButton.setEnabled(false);
    gameUnderWay = false;
    instructionsTA.setText("First player - please choose 'O' or 'X'");
    }


    private class PlayingPanel extends JPanel
    {
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    myNoughtsAndCrossesBoard.paintComponent(g);
    }
    }

    private void startingGameButtons()
    {
    xButton.setEnabled(false);
    oButton.setEnabled(false);
    xButton.setVisible(false);
    oButton.setVisible(false);
    startButton.setEnabled(true);
    startButton.setVisible(true);
    }

    private void playInstructions()
    {
    instructionsTA.setText("Press Start to begin playing");
    }


    private class ButtonWatcher implements ActionListener
    {
    public void actionPerformed (ActionEvent a)
    {
    Object buttonPressed = a.getSource();

    if (buttonPressed.eqauls());

    {
    instructionsTA.setText("you have chosen" );

    }

    }
    }


    private class MouseEventer extends MouseAdapter

    {
    public void mouseClicked (MouseEvent m)
    {

    // incomplete

    }



    }
    MenuSelection class

    private class MenuSelection implements ActionListener
    {

    public void actionPerformed (ActionEvent a)
    {
    Object buttonPressed = a.getSource();


    if (buttonPressed == startAgainMenu);

    {
    // should restart game

    }

    if (buttonPressed.equals(exitMenu));

    {
    System.exit(0); }



    }
    }



    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,119
    My Mood
    Hungover
    Thanks
    140
    Thanked 590 Times in 505 Posts

    Default Re: Programming Issues simple Game

    When posting code, please use the highlight tags to preserve formatting.

    This code does not compile. I suggest fixing your compiler errors before asking about logic problems.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  2. [SOLVED] Having issues with simple logIn --- insert new user to th db
    By justyStepi in forum JDBC & Databases
    Replies: 5
    Last Post: August 27th, 2012, 07:06 PM
  3. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 08:21 PM
  4. Programming AI for simple game?
    By YouGoLast in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2011, 08:53 AM
  5. [SOLVED] Simple server client echo issues
    By Kakashi in forum Java Networking
    Replies: 4
    Last Post: March 3rd, 2011, 09:54 AM

Tags for this Thread