Piece: X, O, Z, $ etc. (Any symbol and any number of players)
Board: M*M
Player
Board
class Board{ public: Board() { // Initialize the board } // returns the whole board int[][] getBoard(); // returns one of the following: // "First", "Second", "Draw", "Undecided" String getWinner(); // returns: "First" or "Second" String getCurrentPlayer(); // Idea for makeMove() functions void makeMove(Move move);}
Classes
PlayingPiece
- pieceType: PieceType+ PlayingPiece(PieceType T)Maybe create two more classes::PlayingPieceX extends PlayingPiecePlayingPieceX() { super(PieceType.CROSS);}PlayingPieceO extends PlayingPiecePlayingPieceO() { super(PieceType.NOUGHTS);}
class TicTacGame { // helpful to remove from front and insert at the end private Deque<Player> players; private Board gameBoard; TicTacToeGame() { players = new ArrayDeque<>(); Player player1 = new Player("John", new PlayingPieceX()); Player player2 = new Player("Jane", new PlayingPieceO()); players.add(player1); players.add(player2); gameBoard = new Board(3); // 3 * 3 board } public String startGame() { boolean noWinner = true; while (noWinner) { Player playerTurn = players.removeFirst(); gameBoard.printBoard(); if (gameBoard.getEmptyCellsCount() == 0) { // game has ended // no winner found noWinner = false; continue; } // read user input // add piece boolean pieceAddedSuccessfully = gameBoard.addPiece( inputRow, inputCol, playerTurn.getPlayingPiece() ); if (!pieceAddedSuccessfully) { // handle error // add back to the first of list players.addFirst(playerTurn); continue; } players.addLast(playerTurn); if (isThereWinner(...)) { return playerTurn.getName(); } } } private isThereWinner(....) { ... }}