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 Game errors

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

    Default TicTacToe Game errors

    Hello. I'm to get this TicTacToe program to stop and display the winner (or if there's a tie) and stop when the game ends. I'm trying to do this by getting the main method to read the output from the getOtutcome method via the for loops, but I can't get it to work. Is there some other way of doing this?

    package tictactoeguigame;
     
    import java.awt.event.*;
    import games.board.*;
    import javax.swing.*;
     
    public class TicTacToeGUIGame extends JFrame {
     
        private Board gb;
        private int turn;
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater( new Runnable () {
                public void run(){ new TicTacToeGUIGame(); }
            });
     
            //This if then statement is meant to read the returned value from the getOutcome and use the value to determine which message to display.
    if(getOutcome == Outcome.PLAYER1_WIN)
            JOptionPane.showMessageDialog(this, noughtOutcome);
        else if(getOutcome == Outcome.PLAYER2_WIN)
            JOptionPane.showMessageDialog(this, crossOutcome);
        else if(getOutcome == Outcome.TIE)
            JOptionPane.showMessageDialog(this,tieOutcome);
        return null;
        }
     
        //This method is the code that reads the marks on the board and returns a value based on which player won or if there is a tie.
    public static int getOutcome() {
        for (int rows = -1; rows < Board.cells.length; rows++) { 
            for (int cols = -1; cols < Board.cells[rows].length; cols++) { 
                return Outcome.TIE; 
            } 
        }
        for (int rows = -1; rows < Board.cells.length; rows++) { 
            for (int cols = -1; cols < Board.cells[rows].length; cols++) { 
                return Outcome.PLAYER1_WIN; 
            } 
        }
    for (int rows = 1; rows < Board.cells.length; rows++) { 
        for (int cols = 1; cols < Board.cells[rows].length; cols++) { 
            return Outcome.PLAYER2_WIN; 
        } 
    }
    return Outcome.CONTINUE;
    }
     
    //This code is meant to keep the turns going until the game ends, at which point it stops and the main method displays a message
        private void takeTurn(Cell c) {
            If (Outcome == Outcome.CONTINUE) {
            Mark curMark = (turn++ % 2 == 0)?Mark.NOUGHT: Mark.CROSS;
            gb.setCell(curMark,c.getRow(),c.getColumn());}
     
        }
     
        //This code is the GUI for the game, there aren't any problems here.
    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); 
        }
    }

    package games.board;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Board extends JComponent {
       public static Cell[][] cells;
       public Board(int rows, int columns, ActionListener ah) {
        cells = new Cell[rows][columns];
        this.setLayout(new GridLayout(rows,columns));
        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( 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("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 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 games.board;
     
    public enum Mark {EMPTY, NOUGHT, CROSS, YELLOW, RED, BLUE, GREEN, MAGENTA, ORANGE}

    package games.board;
     
    public enum Outcome {PLAYER1_WIN, PLAYER2_WIN, CONTINUE, TIE}

    package games.board;
     
    public enum Player {FIRST, SECOND}
    Last edited by monikathelover1999; October 17th, 2019 at 05:13 PM. Reason: Changed \ to / in code TAG

  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 Game errors

    I can't get it to work.
    Where in the code is the problem you describe? I do not see any comments in the code like:
    // <<<<<<< HOW TO GET PROGRAM TO STOP HERE???

    Can you add some comments to the code describing what it is supposed to do?


    Note: The code has lots of compiler errors that need to be fixed. Please fix the errors or copy the full text of the compiler's error messages and paste it here if you need help.
    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 Game errors

    I revised the code to include comments and this is what the compiler says.

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol: variable getOutcome
    location: class tictactoeguigame.TicTacToeGUIGame

    package tictactoeguigame;
     
    import java.awt.event.*;
    import games.board.*;
    import javax.swing.*;
     
    public class TicTacToeGUIGame extends JFrame {
     
        private Board gb;
        private int turn;
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater( new Runnable () {
                public void run(){ new TicTacToeGUIGame(); }
            });
     
            //This if then statement is meant to read the returned value from the getOutcome and use the value to determine which message to display.
    if(getOutcome == Outcome.PLAYER1_WIN)
            JOptionPane.showMessageDialog(this, noughtOutcome);
        else if(getOutcome == Outcome.PLAYER2_WIN)
            JOptionPane.showMessageDialog(this, crossOutcome);
        else if(getOutcome == Outcome.TIE)
            JOptionPane.showMessageDialog(this,tieOutcome);
        return null;
        }
     
        //This method is the code that reads the marks on the board and returns a value based on which player won or if there is a tie.
    public static int getOutcome() {
        for (int rows = -1; rows < Board.cells.length; rows++) { 
            for (int cols = -1; cols < Board.cells[rows].length; cols++) { 
                return Outcome.TIE; 
            } 
        }
        for (int rows = -1; rows < Board.cells.length; rows++) { 
            for (int cols = -1; cols < Board.cells[rows].length; cols++) { 
                return Outcome.PLAYER1_WIN; 
            } 
        }
    for (int rows = 1; rows < Board.cells.length; rows++) { 
        for (int cols = 1; cols < Board.cells[rows].length; cols++) { 
            return Outcome.PLAYER2_WIN; 
        } 
    }
    return Outcome.CONTINUE;
    }
     
    //This code is meant to keep the turns going until the game ends, at which point it stops and the main method displays a message
        private void takeTurn(Cell c) {
            If (Outcome == Outcome.CONTINUE) {
            Mark curMark = (turn++ % 2 == 0)?Mark.NOUGHT: Mark.CROSS;
            gb.setCell(curMark,c.getRow(),c.getColumn());}
     
        }
     
        //This code is the GUI for the game, there aren't any problems here.
    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); 
        }
    }

    package games.board;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Board extends JComponent {
       public static Cell[][] cells;
       public Board(int rows, int columns, ActionListener ah) {
        cells = new Cell[rows][columns];
        this.setLayout(new GridLayout(rows,columns));
        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( 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("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 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 games.board;
     
    public enum Mark {EMPTY, NOUGHT, CROSS, YELLOW, RED, BLUE, GREEN, MAGENTA, ORANGE}

    package games.board;
     
    public enum Outcome {PLAYER1_WIN, PLAYER2_WIN, CONTINUE, TIE}

    package games.board;
     
    public enum Player {FIRST, SECOND}

  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 Game errors

    cannot find symbol
    symbol: variable getOutcome
    location: class tictactoeguigame.TicTacToeGUIGame
    What line in what source file was that error on? Your posting left off that information.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: TicTacToe Game errors

    at tictactoeguigame.TicTacToeGUIGame.main(TicTacToeGU IGame.java:18)

    if(getOutcome == Outcome.PLAYER1_WIN)

  6. #6
    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 Game errors

    The compiler can not find a definition for the variable: getOutcome. Where is it defined?

    Did you mean to reference the method with that name? To call a method add () to the end of the name.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: TicTacToe Game errors

    Okay, that fixed that error, but now I'm getting these.

    Exception in thread "main" java.lang.NullPointerException
    at tictactoeguigame.TicTacToeGUIGame.getOutcome(TicTa cToeGUIGame.java:29)
    at tictactoeguigame.TicTacToeGUIGame.main(TicTacToeGU IGame.java:18)

  8. #8
    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 Game errors

    Exception in thread "main" java.lang.NullPointerException
    at tictactoeguigame.TicTacToeGUIGame.getOutcome(TicTa cToeGUIGame.java:29)
    Look at the statement on line 29 and find the variable with the null value. If you can't see any likely null values, add a print statement just before line 29 that prints the values of all the variables used on line 29. The print out will show you what variable has the null value.
    Then backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

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. Robot Game; Few errors (help)
    By Snake-KoRn in forum Android Development
    Replies: 19
    Last Post: July 19th, 2014, 02:10 PM
  3. java game errors
    By New2Programming in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2014, 06:20 PM
  4. Couple compile errors for dice game
    By smithmar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2012, 01:16 PM
  5. tictactoe client-server game
    By nuubikz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2011, 05:25 AM