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 8 of 8

Thread: TicTacToe error

  1. #1
    Junior Member monikathelover1999's Avatar
    Join Date
    Sep 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default TicTacToe error

    Hello, I'm trying to get this code to display a tic tac toe game, but it displays as 9 slim buttons in a row instead of a 3 x 3 grid as it should. Can someone tell me why this is happening?

    package games.board;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Board extends JComponent {
       private final Cell[][] cells;
    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];
       }
     
        @Override
       public String toString() {
           StringBuilder str = new StringBuilder();
     
           for (Cell[] cell : cells) {
               str.append("|");
               for (Cell cell1 : cell) {
                   switch (cell1.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("N");
                           break;
                       default:        //Empty
                           str.append(" ");
                   }
                   str.append("|");
               }
               str.append("\n");
           }
           return str.toString();
       } 
    }
    package games.board;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class Cell extends JButton {
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int offset = 5;
            Graphics2D g2 = (Graphics2D) g;
            g2.setStroke(new BasicStroke(5));
            switch(content) {
                case NOUGHT:  g2.setColor(Color.RED);
                g2.drawOval(offset, offset, this.getWidth() -
                        offset * 2, this.getHeight() - offset * 2);
                break;
                case CROSS:
                    g2.setColor(Color.BLACK);
                    g2.drawLine(offset, offset, this.getWidth() - 
                            offset  , this.getHeight() - offset );
                    g2.drawLine(this.getWidth() -
                            offset, offset , offset, this.getHeight()- offset);
                    break;
            }
        }
     
        private Mark content;
        private final int row, column;
     
        public Cell(int row, int column) {
            this.row = row;
            this.column = column;
            content = Mark.EMPTY;
        }
        public Mark getContent() { return content; }
        public void setContent(Mark content) { this.content = content;
        }
        public int getRow() { return row; }
        public int getColumn() { return column; }
    }
    package tictactoeguigame;
     
    import java.awt.event.*;
    import games.board.*;
    import javax.swing.*;
     
    public class TicTacToeGUIGame extends JFrame {
     
        private final Board gb;
        private int turn;
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                TicTacToeGUIGame ticTacToeGUIGame;
                ticTacToeGUIGame = new TicTacToeGUIGame();
            });
        }
        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, (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); 
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: TicTacToe error

    The code does not compile because of missing definitions. For example: Mark
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member monikathelover1999's Avatar
    Join Date
    Sep 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: TicTacToe error

    It compiles fine for me, it's just displaying the board improperly.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: TicTacToe error

    Yes, if you have all the class definitions. Make sure all needed definitions are posted.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: TicTacToe error

    in class Board change:

    //this.setLayout(new GridLayout());
    this.setLayout(new GridLayout(rows, columns));

  6. The Following User Says Thank You to zemiak For This Useful Post:

    monikathelover1999 (September 24th, 2019)

  7. #6
    Junior Member monikathelover1999's Avatar
    Join Date
    Sep 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: TicTacToe error

    Quote Originally Posted by zemiak View Post
    in class Board change:

    //this.setLayout(new GridLayout());
    this.setLayout(new GridLayout(rows, columns));
    Ah, that worked. Thank you!

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: TicTacToe error

    Now there is an interesting logic problem. Click on an empty square and an O is placed there. Click on that O will cause an exception, but the program continues. Now Click on another empty square and an O is placed there (NOT an X). Continuing you can get 3 Os (or 9) and no Xs
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member monikathelover1999's Avatar
    Join Date
    Sep 2019
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: TicTacToe error

    Quote Originally Posted by Norm View Post
    Now there is an interesting logic problem. Click on an empty square and an O is placed there. Click on that O will cause an exception, but the program continues. Now Click on another empty square and an O is placed there (NOT an X). Continuing you can get 3 Os (or 9) and no Xs
    Oh wow, you're right.

Similar Threads

  1. Help with my GUI for TicTacToe Game
    By nicsxox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 17th, 2018, 06:52 AM
  2. Tictactoe with out GUI
    By Andile in forum Java Theory & Questions
    Replies: 2
    Last Post: September 15th, 2012, 03:49 PM
  3. TicTacToe problem
    By Kratos in forum Java Theory & Questions
    Replies: 3
    Last Post: March 15th, 2012, 12:13 PM
  4. TicTacToe 2D Array help
    By steel55677 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 9th, 2011, 05:01 PM
  5. TicTacToe
    By Zerro900 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2011, 08:29 AM