4 Replies Latest reply: Jan 30, 2011 8:17 AM by YoungWinston RSS

    Basic Tic Tac Toe programming help please

      I'm trying to make a seperate java class(there is another main that calls this using SWING interface) that makes mark on the board.

      It's supposed to do the following

      Method makeMark returns:

      a null if the cell indicated by row and column has already been marked
      an X if it is X's turn (X gets to go first)
      a Y if it is Y's turn

      Thus makeMark will have to keep of marks made previously and return the appropriate string
      for the requested cell.

      and here is what I got so far, but only getting empty clicks as a respond. Any help will be appreciated. thanks.

      import javax.swing.JButton;

      public class TicTacToeGame {

           public String makeMark(int row, int column){
      String player = "X";
      String [][] Enum = new String [3][3];

                for (row=0; row<=2; row++){
                     for (column=0; column<=2; column++)
                          if (Enum[row][column] == "")
      if (player == "X"){
      Enum[row][column] = "X";
      player = "Y";
      return "X";

      if (player == "Y"){
      Enum[row][column] = "O";
      player = "X";
      return "Y";
      return null;

                return player;

        • 1. Re: Basic Tic Tac Toe programming help please
          Only make the tic-tac-toe board (the two dimensional array) once (in the constructor of your classs) and store it in a class variable. Your makeMark method should only change it.

          For a complex assigment you probably want to split it up in smaller parts:
          -create a class that represents a tic-tac-toe board
          -create a method which returns X/O/empty for a row/column
          -create a method which sets X/O for a row column
          -create a method which returns the current player
          -now implement the assigment method using the previously create method

          Also "Enum" is a bad name for a variable since "enum" is a keyword - the obvious name is "board".
          • 2. Re: Basic Tic Tac Toe programming help please
            Cross posted

            • 3. Re: Basic Tic Tac Toe programming help please
              ok is there a simpler codes that will just flip-flop between X and O? I had it working before but somehow it's not working anymore. Before I had

              public String makeMark(int row, int column){

              player = "X";

              if (player = "X")
              player = "O";

              else if (player = "O")
              player = "X";


              or soemthing like this but it used to work, but I had to revise the code to do more things and it stopped even flip-flopping between X and O.

              Here's main code:

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

              * This class is the GUI for TicTacToe. It only handles user clicks on
              * buttons, which represent X's and O's
              * @author Hal Mendoza
              * CSE 21 - Jan 18, 2011, 8:18:22 PM
              * TicTacToe.java
              public class TicTacToe extends JPanel implements ActionListener {
              public final static int NUM_ROWS_COLUMNS = 3;
              private JButton arrayofButtons[][] = new JButton[NUM_ROWS_COLUMNS][NUM_ROWS_COLUMNS];
              TicTacToeGame board = new TicTacToeGame();

              public TicTacToe() {
                   BoxLayout ourLayout = new BoxLayout(this, BoxLayout.Y_AXIS);

              * Builds the panel with buttons
              * @return the panel with buttons
              private JPanel buildMainPanel() {
                   JPanel panel = new JPanel();
                   GridLayout gridLayout = new GridLayout(0, NUM_ROWS_COLUMNS);
                   for (int row = 0; row < arrayofButtons.length; ++row)
                        for (int column = 0; column < arrayofButtons[0].length; ++column) {
                             arrayofButtons[row][column] = new JButton();
                             arrayofButtons[row][column].setPreferredSize(new Dimension(50, 50));
                             // Use actionCommand to store x,y location of button
                             arrayofButtons[row][column].setActionCommand(Integer.toString(row) + " " +


                   return panel;

              * Called when user clicks buttons with ActionListeners.
              public void actionPerformed(ActionEvent e) {
                   JButton button = (JButton) e.getSource();
                   String xORo;
                   String rowColumn[] = button.getActionCommand().split(" ");

                   int row = Integer.parseInt(rowColumn[0]);
                   int column = Integer.parseInt(rowColumn[1]);
                   xORo = board.makeMark(row, column);

                   if (xORo != null)

              * Create the GUI and show it. For thread safety,
              * this method should be invoked from the
              * event dispatch thread.
              private static void createAndShowGUI() {
                   // Create and set up the window.
                   JFrame frame = new JFrame("Tic Tac Toe");

                   // Add contents to the window.
                   frame.add(new TicTacToe());

                   // Display the window.

              public static void main(String[] args) {
                   // Schedule a job for the event-dispatching thread:
                   // creating and showing this application's GUI.
                   javax.swing.SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
              • 4. Re: Basic Tic Tac Toe programming help please
                832236 wrote:
                ok is there a simpler codes that will just flip-flop between X and O? I had it working before but somehow it's not working anymore.
                Not being a GUI expert, I'll leave it up to others to diagnose your specific problem; but I will give you this piece of advice:

                Separate the "game playing" logic from your GUI. Completely.
                Right now, they're are all piled together, which makes it difficult for you (and us) to work out where the problem lies. I think if I were designing one, I'd probably have my board as a
                (or, possibly even better, some class that wraps a char and can detect illegal moves).
                Then, your GUI portion is concerned only with displaying your game board and reacting to an action (ie, converting it to some sort of "move").