I can compile the code, but when I try to run the program it says that it cannot find the main class. Any help would be greatly appreciated.
package tictactoe; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * */ public class Main { public void play() { TicTacToe game = new TicTacToe(); boolean continuePlaying = true; while (continuePlaying) { game.init(); System.out.println(); System.out.println(); System.out.println(game.drawBoard()); System.out.println(); String player = null; while (!game.winner() && game.getPlays() < 9) { player = game.getPlayer() == 1 ? game.getWinX() : game.getWinO(); boolean validPick = false; while (!validPick) { String square = game.getPrompt(); if (square.length() == 1 && Character.isDigit(square.toCharArray()[0])) { int pick = 0; try { pick = Integer.parseInt(square); } catch (NumberFormatException e) { } validPick = game.placeMarker(pick); } if (!validPick) { System.out.println("Square can not be selected. Retry"); } } game.switchPlayers(); System.out.println(); System.out.println(game.drawBoard()); System.out.println(); } if (game.winner()) { System.out.println("Game Over - " + player + " WINS!!!"); } else { System.out.println("Game Over - Draw"); } } }
private class TicTacToe{ private char[][] board = new char[3][3]; private String winX; private String winO; private int player; private char marker1; private char marker2; private int plays; private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); protected void init() { int counter = 0; for (int indexR = 0; indexR < 3; indexR++) { for (int indexC = 0; indexC < 3; indexC++) { board[indexR][indexC] = Character.forDigit(++counter, 10); } } player = 1; plays = 0; } protected void switchPlayers() { if (getPlayer() == 1) { setPlayer(2); } else { setPlayer(1); } setPlays(getPlays() + 1); } protected boolean placeMarker(int play) { for (int indexR = 0; indexR < 3; indexR++) { for (int indexC = 0; indexC < 3; indexC++) { if (board[indexR][indexC] == Character.forDigit(play, 10)) { board[indexR][indexC] = (getPlayer() == 1) ? getMarker1() : getMarker2(); return true; } } } return false; } protected boolean winner() { //Checking rows char current = ' '; for (int indexR = 0; indexR < 3; indexR++) { int indexC = 0; for (indexC = 0; indexC < 3; indexC++) { if (!Character.isLetter(board[indexR][indexC])) { break; } if (indexC == 0) { current = board[indexR][indexC]; } else if (current != board[indexR][indexC]) { break; } if (indexC == 2) { //Found winner return true; } } } //Checking columns for (int indexR = 0; indexR < 3; indexR++) { current = ' '; int indexC = 0; for (indexC = 0; indexC < 3; indexC++) { if (!Character.isLetter(board[indexC][indexR])) { break; } if (indexC == 0) { current = board[indexC][indexR]; } else if (current != board[indexC][indexR]) { break; } if (indexC == 2) { //Found winner return true; } } } //Checking diagonals current = board[0][0]; if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) { return true; } current = board[2][0]; if (Character.isLetter(current) && board[1][1] == current && board[0][2] == current) { return true; } return false; } protected String getPrompt() { String prompt = ""; try { prompt = reader.readLine(); } catch (IOException ex) { } return prompt; } protected String drawBoard() { StringBuilder builder = new StringBuilder("*"); for (int indexR = 0; indexR < 3; indexR++) { for (int indexC = 0; indexC < 3; indexC++) { builder.append("[").append(board[indexR][indexC]).append("]"); } builder.append("*"); } return builder.toString(); } public int getPlayer() { return player; } public void setPlayer(int player) { this.player = player; } public char getMarker1() { return marker1; } public void setMarker1(char marker1) { this.marker1 = marker1; } public char getMarker2() { return marker2; } public void setMarker2(char marker2) { this.marker2 = marker2; } public int getPlays() { return plays; } public void setPlays(int plays) { this.plays = plays; } public String getWinX() { return winX; } public void setWinX(String winX) { this.winX =winX; } public String getWinO() { return winO; } public void setWinO(String winO) { this.winO = winO; } } }