Search code examples
javaswingtic-tac-toe

Java Tic Tac Toe GUI


The code works fine but problem is that i want 3 columns and three rows.. But output shows 6 columns instead of three rows and three columns

Here is a code in which problem appears.. Here is a main class:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import games.board.Board;
import games.board.Cell;
import games.board.Mark;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;



public class TicTacToeGUIGame extends JFrame  {

/**
 * @param args
 */
private Board gb;
private int turn;
private void takeTurn(Cell c) {
    Mark curMark = (turn++ % 2 == 0)? Mark.NOUGHT
    : Mark.CROSS;
    gb.setCell(curMark, c.getRow(), c.getColumn());
    }
private TicTacToeGUIGame() {
    gb = new Board(3, 3, new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    Cell c = (Cell) ae.getSource();
    takeTurn(c);
    }
    });
    this.add(gb);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setTitle("TIC-TAC-TOE");
    this.setSize(300, 300);
    this.setVisible(true);
    }


public static void main(String[] args) {
    // TODO Auto-generated method stub
    SwingUtilities.invokeLater( new Runnable () {
        public void run() { new TicTacToeGUIGame(); }
        });

}

}

Here is a board class:

package games.board;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class Board extends JPanel {
private Cell[][] cells;
public Board(int rows, int columns) {
cells = new Cell[rows][columns];
for( int r = 0; r < cells[0].length; r++ ) {
for (int c = 0; c < cells[1].length; c++) {
cells[r][c] = new Cell(r,c);
}
}
}
public Board(int rows, int columns, ActionListener ah) {
    cells = new Cell[rows][columns];
    this.setLayout(new GridLayout());
    for( int r = 0; r < cells.length; r++ ) {
    for (int c = 0; c < cells[r].length; c++) {
    cells[r][c] = new Cell(r,c);
    this.add(cells[r][c]);
    cells[r][c].addActionListener(ah);
    }
    }
    }
public void setCell(Mark mark, int row, int column) throws
IllegalArgumentException {
if (cells[row][column].getContent() == Mark.EMPTY)
cells[row][column].setContent(mark);
else throw new IllegalArgumentException("Player already there!");
}
public Cell getCell(int row, int column) {
return cells[row][column];
}
public String toString() {
StringBuilder str = new StringBuilder();
for( int r = 0; r < cells.length; r++ ) {
str.append("|"); 


for (int c = 0; c < cells[r].length; c++) {
switch(cells[r][c].getContent()) {
case NOUGHT:
    str.append("O");
    break;
    case CROSS:
    str.append("X");
    break;
    case YELLOW:
    str.append("Y");
    break;
    case RED:
    str.append("R");
    break;
    case BLUE:
    str.append("B");
    break;
    case GREEN:
    str.append("G");
    break;
    case MAGENTA:
    str.append("M");
    break;
    case ORANGE:
    str.append("M");
    break;
    default: //Empty
    str.append("");
    }
    str.append("|");
    }
    str.append("\n");
    }
    return str.toString();

}

}

Solution

  • i want 3 columns and three rows.. But output shows 6 columns

    You need to specify proper parameters to the GridLayout:

    this.setLayout(new GridLayout(0, 3));
    

    This will tell the grid to contain 3 columns. The number of rows will depend on the number of components you add.