HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Tic Tac Toe game in Java OOP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
toeticjavatacgameoop

Problem

I have written a simple GUI Tic Tac Toe Application and since this is my first shot, I think it can be improved a lot.

Please tell me what you think about it and what you see I made wrong so I can avoid those mistakes the next time.

Is there code redundancy I don't see? Is everything declared and implemented at its right place?

Earlier I wrote a console based game and then tried to convert it in to object oriented based. I am not sure if this can be called object oriented because I have a little knowledge about object oriented structure as I am a beginner.

Main Class

package tic_tac_adv;

public class Tic_Tac_Adv {

public static void main(String[] args) {

    // Main CLass

    Control C = new Control(); 

}

}


Control Class

package tic_tac_adv;

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

public class Control extends JFrame {

/*
this is the main controler that controls
and combines different components of the game.
*/ 

private Board GameBoard; //Board and Button
private Tools TButtons; // Exit and Reset

Control() {

    setLayout(new BorderLayout());

    GameBoard = new Board();
    TButtons = new Tools();

    TButtons.SetObject(GameBoard);

    add(GameBoard, BorderLayout.CENTER);
    add(TButtons, BorderLayout.SOUTH);

    setVisible(true);
    setSize(350, 350);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}


Board Class

```
package tic_tac_adv;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Board extends JPanel implements ActionListener {

private JButton B1, B2, B3, B4, B5, B6, B7, B8, B9; // Buttons
private GameArray GArray; // Class with Array
private boolean Player = false;
private int PlayerMark = 1;

/*
Player is the Current players turn. if false player 1 will play else player 2
PlayerMark is to set number to the array "1" for player 1 and "2" for player 2
*/
Board() {

// creates the panel
setLayout(new GridLayout(3, 3));

Solution

Mathew had some great responses, but I'd like to explain a little more about using for loops. If you take a look at your Board() code right now, you might notice how a lot of the code is the same - when you make the JButtons, for example, you call new JButton("") nine times in a row. In general, having a lot of code that all looks the same or is copy-pasted is a bad idea. You can use a for loop with an array to clean this up a lot. Here's how Board() would look after using an array.

Board() {
    // creates the panel
    setLayout(new GridLayout(3, 3));

    buttons = new JButton[9];
    for (int i=0; i < buttons.length; i++) {
        buttons[i] = new JButton("");
    }

    SetGame();

    for (int i=0; i < buttons.length; i++) {
        add(buttons[i]);    
        buttons[i].addActionListener(this);
    }    
}


See how much shorter and less repetitive that is?

As a side note, by not specifying 'public' or 'private', you implicitly made your constructors package-private, which is rarely the right solution. I recommend specifying everything as either public or private, until you get to the point of distributing your code or compiled programs, in which case package-private might occasionally make sense.

Code Snippets

Board() {
    // creates the panel
    setLayout(new GridLayout(3, 3));

    buttons = new JButton[9];
    for (int i=0; i < buttons.length; i++) {
        buttons[i] = new JButton("");
    }

    SetGame();

    for (int i=0; i < buttons.length; i++) {
        add(buttons[i]);    
        buttons[i].addActionListener(this);
    }    
}

Context

StackExchange Code Review Q#57141, answer score: 10

Revisions (0)

No revisions yet.