Connect4

 avatar
unknown
java
a month ago
6.7 kB
3
Indexable
public class PlayerManager {
    ArrayDeque<Player> playerList;

    public PlayerManager(){
        this.playerList = new ArrayDeque<>();
    }

    public void registerPlayer(Player p){
        playerList.add(p);
    }

    public Player getCurrentPlayer(){
        return playerList.getFirst();
    }

    public void changeTurn(){
        Player p = this.getCurrentPlayer();
        this.playerList.removeFirst();
        this.playerList.addLast(p);
    }
}

public class Player {
    private String name;
    private Token token;

    public Player(String name, Token token){
        this.name = name;
        this.token = token;
    }
    public String getName(){
        return this.name;
    }
    public Token getToken(){
        return this.token;
    }
}


public class Game {
    private Board board;
    private PlayerManager manager;
    private BaseWinStrategy strategy;
    private Player winner;

    public Game(int size, PlayerManager manager){
        this.board = new Board(size);
        this.manager = manager;
        this.strategy = new DiagonalWS(new VerticalWS(new HorizontalWinS(null)));
    }

    public Player startGame(){
        int size = board.getSize();
        while(winner == null && !board.isBoardFull()){
            // get player to play
            Player playerToPlay = manager.getCurrentPlayer();
            // choose a row
            int colToPlay = new Random().nextInt(0, size);
            // play in that row
            int row = board.addToken(colToPlay, playerToPlay.getToken());
            if(row == -1){
                continue;
            }
            // print board
            board.printBoardState();
            // check if has won
            boolean hasWon = strategy.hasWon(row, colToPlay, board.getPlayingBoard(), playerToPlay.getToken());
            // if won then break
            if(hasWon){
                winner = playerToPlay;
                break;
            }
            else{
                manager.changeTurn();
            }
            // change player and continue
        }
        if(winner == null){
            System.out.println("\nIts a tie!!\n");
            return null;
        }
        System.out.println("\nPlayer " + winner.getName() +" has won!!\n");
        return winner;
    }
}

public class Board {
    private int size;
    private Token [][] playingBoard;

    public Board(int size){
        this.size = size;
        this.playingBoard = new Token[size][size];
    }

    public void printBoardState(){
        System.out.println("\nPlaying Board State: \n");
        for(int i = 0;i<size;i++){
            for(int j = 0;j<size;j++){
                if(playingBoard[i][j] == null){
                    System.out.print("_ ");
                }else {
                    System.out.print(playingBoard[i][j] + " ");
                }
            }
            System.out.println();
        }
    }

    public int addToken(int col, Token t){
        for(int i = size-1;i>=0;i--){
            if(playingBoard[i][col] == null){
                playingBoard[i][col] = t;
                return i;
            }
        }
        System.out.println("\nRow is filled please choose another row\n");
        return -1;
    }

    public int getSize(){
        return this.size;
    }

    public Token[][] getPlayingBoard(){
        return this.playingBoard;
    }

    public boolean isBoardFull(){
        for(Token [] t: playingBoard){
            for( Token i: t){
                if(i == null){
                    return  false;
                }
            }
        }
        return true;
    }
}

public abstract class BaseWinStrategy {
    private BaseWinStrategy nextStrategy;

    public BaseWinStrategy( BaseWinStrategy nextStrategy){
        this.nextStrategy = nextStrategy;
    }

    public boolean hasWon(int row, int col, Token[][] playingBoard, Token token){
        if(nextStrategy == null){
            return false;
        }
        return nextStrategy.hasWon(row, col, playingBoard, token);
    }
}


public class VerticalWS extends BaseWinStrategy{
    public VerticalWS(BaseWinStrategy strategy){
        super(strategy);
    }

    @Override
    public boolean hasWon(int row, int col, Token[][] playingBoard, Token token){
        int continous4 = 0;
        for (Token[] tokens : playingBoard) {
            if (tokens[col] != token) {
                continous4 = 0;
            } else {
                continous4++;
            }
            if (continous4 == 4) {
                return true;
            }
        }
        return super.hasWon(row, col, playingBoard, token);
    }
}

public class HorizontalWinS extends BaseWinStrategy {

    public HorizontalWinS(BaseWinStrategy nextStrategy){
        super(nextStrategy);
    }

    @Override
    public boolean hasWon(int row, int col, Token[][] playingBoard, Token token){
        int continous4 = 0;
        for(int i = 0;i<playingBoard.length;i++){
            if(playingBoard[row][i] != token){
                continous4 = 0;
            }
            else{
                continous4++;
            }
            if(continous4 == 4){
                return true;
            }
        }
        return super.hasWon(row, col, playingBoard, token);
    }
}

public class DiagonalWS extends BaseWinStrategy {
    public DiagonalWS(BaseWinStrategy nextStrategy) {
        super(nextStrategy);
    }

    @Override
    public boolean hasWon(int row, int col, Token[][] playingBoard, Token token) {
        boolean won = checkLeft(row, col, playingBoard, token) || checkRight(row, col, playingBoard, token);
        if(won){
            return won;
        }
        return super.hasWon(row, col, playingBoard, token);
    }

    private boolean checkLeft(int row, int col, Token[][] playingBoard, Token token){
        int count = 1;
        int r = row-1, c = col-1;
        while(r>=0 && c>=0 && playingBoard[r][c] == token){
            r--;
            c--;
            count++;
        }

        r = row+1; c = col+1;
        int size = playingBoard.length;
        while(r<size-1 && c<size-1 && playingBoard[r][c] == token){
            r++;
            c++;
            count++;
        }

        return count >= 4;
    }

    private boolean checkRight(int row, int col, Token[][] playingBoard, Token token){
        int count = 1;
        int size = playingBoard.length;
        int r = row-1, c = col+1;
        while(r>=0 && c<size-1 && playingBoard[r][c] == token){
            r--;
            c++;
            count++;
        }

        r = row+1; c = col-1;
        while(r<size-1 && c>=0 && playingBoard[r][c] == token){
            r++;
            c--;
            count++;
        }

        return count >= 4;
    }
}
Editor is loading...
Leave a Comment