TicTacToe| Full Working Code | LLD
Anonymous User
1026

Components - Board, PieceType, Player, PlayingPiece, PlayingPieceO, PlayingPieceX

Board class

package Components;

import java.lang.constant.PackageDesc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Board {
    public int size;
    public PlayingPiece[][] board;

    public Board(int size) {
        this.size = size;
        this.board = new PlayingPiece[size][size];
    }

    public boolean addPiece(int row, int col, PlayingPiece playingPiece) {
        if(board[row][col] != null) return false;
        board[row][col] = playingPiece;
        return true;
    }

    public List<Pair<Integer,Integer>> getFreeCells() {
        List<Pair<Integer,Integer>> freeCells = new ArrayList<>();
        for(int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if(board[i][j] == null) {
                    Pair<Integer,Integer> pair = new Pair<>(i, j);
                    freeCells.add(pair);
                }
            }
        }
        return freeCells;
    }

    public void printBoard() {
        for(int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(board[i][j] == null ? "  |" : board[i][j].pieceType + " |");
            }
            System.out.println();
        }
    }
}

Pair class

package Components;

public class Pair<K,V> {
    K key;
    V val;

    public  Pair(K key, V val) {
        this.key = key;
        this.val = val;
    }
}

PieceType enum

package Components;

public enum PieceType {
    X,
    O
}

Player class

package Components;

public class Player {
    public String userId;
    public PlayingPiece playingPiece;

    public Player(String userId, PlayingPiece playingPiece) {
        this.userId = userId;
        this.playingPiece = playingPiece;
    }
}

PlayingPiece class

package Components;

public class PlayingPiece {
    public PieceType pieceType;

    PlayingPiece(PieceType pieceType) {
        this.pieceType = pieceType;
    }
}

PlayingPieceO class

package Components;

public class PlayingPieceO extends PlayingPiece{
    public PlayingPieceO() {
        super(PieceType.O);
    }
}

PlayingPieceX class

package Components;

public class PlayingPieceX extends PlayingPiece{
    public PlayingPieceX() {
        super(PieceType.X);
    }
}

TicTacToeGame class

import Components.*;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class TicTacToeGame {
    Deque<Player> players;
    Board gameBoard;

    TicTacToeGame() {
        initializeGame();
    }

    public void initializeGame() {
        players = new LinkedList<>();

        PlayingPieceX playingPieceX = new PlayingPieceX();
        PlayingPieceO playingPieceO = new PlayingPieceO();

        Player player1 = new Player("Player1", playingPieceX);
        Player player2 = new Player("Player2", playingPieceO);

        players.offer(player1);
        players.offer(player2);

        gameBoard = new Board(3);
    }

    public String startGame() {
        boolean noWinner = true;
        while(noWinner) {
            Player playerTurn = players.removeFirst();

            gameBoard.printBoard();
            List<Pair<Integer,Integer>> freeCells = gameBoard.getFreeCells();

            if(freeCells.isEmpty()) {
                noWinner = false;
                continue;
            }

            System.out.print("Player: " + playerTurn.userId + " Enter row and column : ");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.nextLine();
            String[] values = s.split(",");
            int row = Integer.parseInt(values[0]);
            int col = Integer.parseInt(values[1]);


            boolean pieceAddedSuccessfully = gameBoard.addPiece(row, col, playerTurn.playingPiece);

            if(!pieceAddedSuccessfully) {
                System.out.println("Incorrect position, try again");
                players.addFirst(playerTurn);
                continue;
            }
            players.addLast(playerTurn);


            boolean winner = isThereWinner(row, col, playerTurn.playingPiece.pieceType);
            if(winner) {
                return playerTurn.userId;
            }
        }
        return "tie";
    }

    public boolean isThereWinner(int row, int col, PieceType pieceType) {
        boolean rowMatch = true, colMatch = true, diagonalMatch = true, antidiagonalMatch = true;

        for(int i = 0; i < gameBoard.size; i++) {
            if(gameBoard.board[row][i] == null || gameBoard.board[row][i].pieceType != pieceType) rowMatch = false;
        }

        for(int i = 0; i < gameBoard.size; i++) {
            if(gameBoard.board[i][col] == null || gameBoard.board[i][col].pieceType != pieceType) colMatch = false;
        }

        for(int i = 0,j = 0; i < gameBoard.size; i++, j++) {
            if(gameBoard.board[i][j] == null || gameBoard.board[i][j].pieceType != pieceType) diagonalMatch = false;
        }

        for(int i = 0,j = gameBoard.size - 1; i < gameBoard.size; i++, j--) {
            if(gameBoard.board[i][j] == null || gameBoard.board[i][j].pieceType != pieceType) antidiagonalMatch = false;
        }

        return rowMatch || colMatch || diagonalMatch || antidiagonalMatch;
    }
}

Main class

public class Main {
    public static void main(String[] args) {
        TicTacToeGame ticTacToeGame = new TicTacToeGame();
        System.out.println("Game winner is : " + ticTacToeGame.startGame());
    }
}

image

Comments (2)