/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

interface Playable
{
    public void assignChip(String playerName, Character chipValue);    
    public void selectChipPosition(String playerName, Character chipValue);
    public boolean checkAvailability(int inputAsIntegerZeroIndex, Character chipValue, PlayerOne plOne, PlayerTwo plTwo, String name);
    public void insertChip(int pos); 
    public boolean checkConnectFour(int pos, int columnChipPlacedZeroIndex, Character chipValue, String checkConnectFour);
    public void viewBoard();
}

public class Main
{
    public static void main (String[] args)
    {
        ConnectFour cf = new ConnectFour();
    }
}

class PlayerOne
{
    String[][] board;
    String playerOneName;
    String playerTwoName;
    PlayerTwo plTwo;
    
    enum Chips
    {
        RED('O'),
        YELLOW('X');
            
        final Character value;
        
        Chips(Character value)
        {
            this.value=value;
        }
    }
    
    public PlayerOne(ConnectFour cf, String playerOne, String playerTwo, PlayerTwo plTwo)
    {
        this.board=board;
        this.plTwo = plTwo;
        Chips yellow = Chips.YELLOW;
        Character yellowValue = yellow.value;
        this.playerOneName=playerOne;
        this.playerTwoName=playerTwo;
        cf.assignChip(playerOneName, yellowValue);    
        cf.selectChipPosition(playerOneName,yellowValue);
        cf.checkAvailability(cf.inputAsIntegerZeroIndex,yellowValue, this, plTwo, (playerOneName+"(Player 1)"));
        cf.checkConnectFour(cf.inputAsIntegerZeroIndex, cf.rowChipPlacedZeroIndex, yellowValue, playerOneName);
        
        plTwo = new PlayerTwo (cf, playerOneName, playerTwoName, this);
    }
}

class PlayerTwo
{
    String[][] board;
    PlayerOne plOne;
    String playerTwoName;
    String playerOneName;
    
     enum Chips
    {
        RED('O'),
        YELLOW('X');
            
        final Character value;
        
        Chips(Character value)
        {
            this.value=value;
        }
    }
    
    public PlayerTwo(ConnectFour cf, String playerOne, String playerTwo, PlayerOne plOne)
    {
        this.board=board;
        Chips red = Chips.RED;
        Character redValue = red.value;
        this.playerTwoName=playerTwo;
        this.playerOneName=playerOne;
        cf.assignChip(playerTwoName, redValue); 
        cf.selectChipPosition(playerTwoName,redValue);
        cf.checkAvailability(cf.inputAsIntegerZeroIndex,redValue, plOne, this, (playerTwoName+"(Player 2)"));
        cf.checkConnectFour(cf.inputAsIntegerZeroIndex, cf.rowChipPlacedZeroIndex, redValue, playerTwoName);
        plOne = new PlayerOne (cf, playerOneName,playerTwoName, this);
    }
}

class ConnectFour implements Playable
{
    String[][] board;
    Matcher matcher;
    Character input;
    int inputAsIntegerZeroIndex;
    int columnChipPlacedZeroIndex;
    int rowChipPlacedZeroIndex;
    int rowChipPlaced;
    int columnChipPlaced;
    int boardBaseLevel=0;
    int boardHeightZeroIndex;
    int boardWidth=7;
    int boardWidthZeroIndex = boardWidth-1;
    int sameColourTotal=0;
    int columnsFilled;
    boolean columnFull[] = new boolean[boardWidth];
    boolean isAvailable;
    boolean rightColour=false;
    int offset;
    int runningTotalSameColour;
    String playerOneName;
    String playerTwoName;
    String playerNameChipValue;
    int boardHeight;
    PlayerOne plOne;
    PlayerTwo plTwo;
    String lastChipName;
    Character lastChipValue;
    boolean isGameOver = false;
    
    public boolean checkAvailability(int input, Character chipValue, PlayerOne plOne, PlayerTwo plTwo, String name)
    {
        this.plOne=plOne;
        this.plTwo=plTwo;
        System.out.println("***CHECK AVAILABILITY****" + "Board height: " +boardHeight + " board Width:" + boardWidth);
        
        for (int k=(boardHeightZeroIndex); k>=0; k--)
        {
            if (board[k][input].equals("-"))
            {
                board[k][input]= String.valueOf(chipValue);
                rowChipPlacedZeroIndex=k;
                rowChipPlaced=rowChipPlacedZeroIndex+1;
                
                System.out.println("Chip: " + chipValue + " will be placed into column: " + (columnChipPlaced) + " row: " + rowChipPlaced);
                lastChipName = name;
                lastChipValue = chipValue;
                
                if (rowChipPlaced==1)
                {
                    isAvailable=false;
                }
                else
                {
                    isAvailable=true;
                    break;
                }
            }
         }
        
        if (!isAvailable)
        {
             if (columnsFilled==boardWidth)
            {
                System.out.println("GAME OVER - NO WINNER");
                System.exit(0);
            }
            
            if(!columnFull[columnChipPlacedZeroIndex])
            {
                columnFull[columnChipPlacedZeroIndex]=true;
                System.out.println("Column: " + columnChipPlaced + " is NOW full.");
                columnsFilled++;
                System.out.println("AVAILABILITY: Total of  " + columnsFilled + " columns filled");
            }
               
            System.out.println("Last chip inserted " + lastChipValue + " by: " + lastChipName);
               
            if (lastChipValue.equals('O'))
            {
                  plOne = new PlayerOne (this,playerOneName,playerTwoName, plTwo);
            }
            else
            {
                System.out.println("again one");
                plTwo = new PlayerTwo (this, playerOneName, playerTwoName, plOne);
            }
        }
        viewBoard();
        
        return true;
    }
    
    public void insertChip(int pos)
    {
        System.out.println("***INSERT CHIP****");
    }
    
    public boolean checkConnectFour(int pos, int row, Character chipValue, String playerName)
    {
        System.out.println("***CHECK CONNECT FOUR****");
        
        playerNameChipValue = playerName + "("+chipValue+")";
        
        System.out.println("VERTICAL CHECK => DOWNWARDS*****");
       
        if (rowChipPlacedZeroIndex!=5)
        {
            for (int count = rowChipPlacedZeroIndex; count<=boardHeightZeroIndex; count++)
            {
                if (board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    
                    if (sameColourTotal==4)
                    {
                        System.out.println("***1Congratulations " + playerNameChipValue + " Connect 4 in column:" + columnChipPlaced);
                        isGameOver=true;
                    }
                }
            }
            sameColourTotal=0;
        }
        System.out.println("HORIZONTAL CHECK => RIGHT*****");
        
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex)
        {
            for (int count = columnChipPlacedZeroIndex; count<=boardWidthZeroIndex; count++)
            {
                if (board[rowChipPlacedZeroIndex][count].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    rightColour=true;
                    
                    if (sameColourTotal==4)
                    {
                        System.out.println("***2Congratulations " +  playerNameChipValue + "  Connect 4 in row:" + rowChipPlaced);
                        isGameOver=true;
                        return true;
                    }
                }
            }
            
            runningTotalSameColour=sameColourTotal;
            sameColourTotal=0;
        }
        
        System.out.println("HORIZONTAL CHECK => LEFT*****");
        
        if (columnChipPlacedZeroIndex!=0)
        {
            for (int count = columnChipPlacedZeroIndex; count>=0; count--)
            {
                if (board[rowChipPlacedZeroIndex][count].equals(String.valueOf(chipValue)))
                {
                    sameColourTotal++;
                    
                    if (sameColourTotal+runningTotalSameColour==4 & !rightColour)
                    {
                        System.out.println("***3Congratulations " + playerNameChipValue + "  Connect 4 in row:" + rowChipPlaced);
                        isGameOver=true;
                        return true;
                    }
                    
                    if (sameColourTotal+runningTotalSameColour==5 & rightColour)
                    {
                        System.out.println("***4Congratulations " + playerNameChipValue + "  Connect 4 in row:" + rowChipPlaced);
                        isGameOver=true;
                        return true;
                    }
                }
            }
            offset=0;
            sameColourTotal=0;
            runningTotalSameColour=0;
            rightColour=false;
        }
        System.out.println("DIAGONAL CHECK 1");  
    
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedZeroIndex!=boardBaseLevel)
        {
            System.out.println("INSIDE D1 - Diagonal north east check");
            
            for (int count = rowChipPlacedZeroIndex; count>=0; count--)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)
                {
                    if (board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        rightColour=true;
                        sameColourTotal++;
                        offset++;
                        
                        if (sameColourTotal==4)
                        {
                            System.out.println("***5Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                    
                }
            }
            runningTotalSameColour=sameColourTotal;
            sameColourTotal=0;
            offset=0;
        }
        
        System.out.println("DIAGONAL CHECK 2"); 
                                                                         
        if (columnChipPlacedZeroIndex!=0 && rowChipPlacedZeroIndex!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D2 - Diagonal south west check");
            
            for (int count = rowChipPlacedZeroIndex; count<=boardHeightZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex-offset>=0)
                {
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal++;
                        offset++;
                        
                        if (sameColourTotal+runningTotalSameColour==4 & !rightColour)
                        {
                            System.out.println("***6Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                        
                        if (sameColourTotal+runningTotalSameColour==5 & rightColour)
                        {
                            System.out.println("***7Congratulations" + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                    
                }
            }
            runningTotalSameColour=0;
            rightColour=false;
            sameColourTotal=0;
            offset=0;
        }
        System.out.println("DIAGONAL CHECK 3"); 
                                      
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedZeroIndex!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D3 - Diagonal south east check");
                                                        
            for (int count = rowChipPlacedZeroIndex; count<=boardHeightZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)               
                {
                    if (board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        rightColour=true;
                        sameColourTotal++;
                        offset++;
                        System.out.println("value of offset: " + offset);
                    
                        if (sameColourTotal==4)
                        {
                            System.out.println("***8Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                    }
                    
                }
            }
            runningTotalSameColour=sameColourTotal;
            sameColourTotal=0;
            offset=0;
        }
        
        System.out.println("DIAGONAL CHECK 4"); 
        
        if (columnChipPlacedZeroIndex!=0 && rowChipPlacedZeroIndex!=0)
        {
            System.out.println("INSIDE D4 - Diagonal north west check");
                
            for (int count = rowChipPlacedZeroIndex; count>=0; count--)
            {
                if (columnChipPlacedZeroIndex-offset>=0)
                {
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        sameColourTotal++;
                        offset++;
                            
                        if (sameColourTotal+runningTotalSameColour==4 && !rightColour)
                        {
                            System.out.println("***9Congratulations " + playerNameChipValue + "  Connect 4 in diagonal");
                            isGameOver=true;
                            return true;
                        }
                        
                        if (sameColourTotal+runningTotalSameColour==5 && rightColour)
                        {
                            System.out.println("***10Congratulations" + playerNameChipValue + "  Connect 4 in diagonal");
                            return true;
                        }
                    }
                }
            }
            rightColour=false;
            runningTotalSameColour=0;
            sameColourTotal=0;
            offset=0;
        }
        return false;
    }
    
    public void configurePlayers()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter name for Player 1:");
        playerOneName = input.next();
        System.out.println("Enter name for Player 2:");
        playerTwoName = input.next();
    }
    
    public void assignChip(String playerName, Character chip)
    {
        System.out.println("***ASSIGN CHIP****");
        System.out.println(playerName + " has been assigned: " + chip + " chip");
    }
    
    public void selectChipPosition(String playerName, Character chip)
    {
        System.out.println("***SELECT CHIP POSITION****");
        Scanner in = new Scanner(System.in);
        
        viewBoard();
        
        do
        {
            System.out.println(playerName + "("+chip+")" +  ",  Which column would you like to insert the chip?");
            input = in.next().charAt(0);
        
            String regexColumns="[1234567]";
            Pattern pattern = Pattern.compile(regexColumns, Pattern.CASE_INSENSITIVE);
            matcher = pattern.matcher(Character.toString(input));
        
        }while (!matcher.find());
        
        inputAsIntegerZeroIndex = Character.getNumericValue(input-1);
        columnChipPlacedZeroIndex = Character.getNumericValue(input-1);
        
        columnChipPlaced = Character.getNumericValue(input);
    }
    
    public void viewBoard()
    {
        int j=0;
        int rowOnBoard=0;
        
        if (isGameOver)
        {
            System.out.println("\n****CURRENT BOARD****: " + "***Congratulations " + playerNameChipValue);
        }
        else
        {
            System.out.println("\n****CURRENT BOARD****");
        }
        
        StringBuilder [] sb  = new StringBuilder[13];
        
        for (int i=0; i<sb.length;i++)
        {
            if (i%2==0)
            {
                sb[i+j] =   new StringBuilder("|---|---|---|---|---|---|---|");
            }
            else
            {
                sb[i]=new StringBuilder("");
                
                if (rowOnBoard!=board.length)
                {
                    for (int m=0; m<board[0].length;m++)
                    {
                        if (m==0)
                        {
                            sb[i].append("|"+" " +board[rowOnBoard][m]+ " |");
                        }
                        else
                        {
                            sb[i].append(""+" "+board[rowOnBoard][m]+ " |");
                        }
                    }
                    rowOnBoard++;
                }
            }
        }
        for (StringBuilder gg: sb)
        {
            System.out.println(gg.toString());
        }
    }
    
    public ConnectFour() 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("*********Welcome to CONNECT 4**************");
        System.out.println("NOTE: Code will not terminate on a win, see onscreen messages");
        
        board = new String [][]   { {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                    {"-", "-", "-", "-", "-", "-", "-"},
                                };
        
        boardHeightZeroIndex = board.length-1;
        boardHeight=board.length;
        
        viewBoard();
        configurePlayers();
        PlayerOne plOne = new PlayerOne (this, playerOneName,playerTwoName, plTwo);
    }
}