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}