/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

//We know in Connect4 code I had to make this adjustment... So I am doing identical in this piece of code 
// *** THERE IS ODD BEHAVIOUR WITH THIS VARIABLE *****************
//if this variable is taken out of the for loop, it does not get assigned value / loses its value in associated left check
//runningTotalSameColour=sameColourTotal;

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 columnChipPlacedZeroIndex, int rowChipPlacedZeroIndex, Character chipValue, PlayerOne plOne, PlayerTwo plTwo, String name);
    public boolean checkTicTacToe(Character chipValue, String checkConnectFour);
    public void viewBoard();
}

public class Main
{
    public static void main (String[] args)
    {
        TicTacToe nc = new TicTacToe();
    }
}

class PlayerOne
{
    String[][] board;
    String playerOneName;
    String playerTwoName;
    PlayerTwo plTwo;
    
    enum Chips
    {
        NOUGHT('O'),
        CROSS('X');
            
        final Character value;
        
        Chips(Character value)
        {
            this.value=value;
        }
    }
    
    public PlayerOne(TicTacToe nc, String playerOne, String playerTwo, PlayerTwo plTwo)
    {
        this.board=board;
        this.plTwo = plTwo;
        Chips cross = Chips.CROSS;
        Character crossValue = cross.value;
        this.playerOneName=playerOne;
        this.playerTwoName=playerTwo;
        nc.assignChip(playerOneName, crossValue);    
        nc.selectChipPosition(playerOneName,crossValue);
        nc.checkAvailability(nc.columnChipPlacedZeroIndex, nc.rowChipPlacedZeroIndex,crossValue, this, plTwo, (playerOneName+"(Player 1)"));
        nc.checkTicTacToe(crossValue, playerOneName);
        
        plTwo = new PlayerTwo (nc, playerOneName, playerTwoName, this);
    }
}

class PlayerTwo
{
    String[][] board;
    PlayerOne plOne;
    String playerTwoName;
    String playerOneName;
    
    enum Chips
    {
        NOUGHT('O'),
        CROSS('X');
            
        final Character value;
        
        Chips(Character value)
        {
            this.value=value;
        }
    }
    
    public PlayerTwo(TicTacToe nc, String playerOne, String playerTwo, PlayerOne plOne)
    {
        this.board=board;
        Chips nought = Chips.NOUGHT;
        Character noughtValue = nought.value;
        this.playerTwoName=playerTwo;
        this.playerOneName=playerOne;
        nc.assignChip(playerTwoName, noughtValue); 
        nc.selectChipPosition(playerTwoName,noughtValue);
        nc.checkAvailability(nc.columnChipPlacedZeroIndex, nc.rowChipPlacedZeroIndex, noughtValue, plOne, this, (playerTwoName+"(Player 2)"));
        nc.checkTicTacToe(noughtValue, playerTwoName);
        plOne = new PlayerOne (nc, playerOneName,playerTwoName, this);
    }
}

class TicTacToe implements Playable
{
    StringBuilder regex;
    int rowChipPlacedAdjusted;
    boolean hasConfigureConsecutiveSymbols;
    boolean hasConfigurePlayers;
    boolean hasConfigureBoardSize;
    int numberConsecutiveForWin;
    int numChipsPlaced;
    String regexOptions;
    Pattern patternCols;
    Pattern patternRows;
    Scanner scanner;
    String[][] board;
    Matcher matcherRows;
    Matcher matcherCols;
    Integer inputInteger;
    Character inputCharColumn;
    Character inputCharRow;
    int rowChipPlacedZeroIndex;
    int columnChipPlacedZeroIndex;
    int rowChipPlaced;
    int columnChipPlaced;
    
    int boardHeightZeroIndex;
    int boardBaseLevelZeroIndex;
    int boardWidth;
    int boardWidthZeroIndex;
    int boardFirstColumnZeroIndex = 0;
    int numberSameSymbolTotal=0;
    
    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 inputCol, int inputRow, Character chipValue, PlayerOne plOne, PlayerTwo plTwo, String name)
    {
        this.plOne=plOne;
        this.plTwo=plTwo;
        System.out.println("***CHECKING AVAILABILITY****" + "\tBoard height: " +boardHeight + "\tboard Width: " + boardWidth + "\tnumber consecutive for win: " +  numberConsecutiveForWin);
        
        rowChipPlacedZeroIndex=inputRow;      
        rowChipPlacedAdjusted=Math.abs((rowChipPlaced-boardHeight));
        
        if (board[rowChipPlacedAdjusted][inputCol].equals("-"))
        {
            board[rowChipPlacedAdjusted][inputCol]= String.valueOf(chipValue);
                
            System.out.println("Chip: " + chipValue + " will be placed into column: " + (columnChipPlaced) + " row: " + rowChipPlaced);
            lastChipName = name;
            lastChipValue = chipValue;
            isAvailable=true;
            numChipsPlaced=numChipsPlaced+1;
        }
        else
        {
            isAvailable=false;
                
            System.out.println("Last chip inserted " + lastChipValue + " by: " + lastChipName);
            
            if(numChipsPlaced<(boardWidth*boardHeight))
            {
                if (lastChipValue.equals('O'))
                {
                    plOne = new PlayerOne (this,playerOneName,playerTwoName, plTwo);
                }
                else
                {
                    System.out.println(name + ", your turn again. please choose a vacant spot");
                    plTwo = new PlayerTwo (this, playerOneName, playerTwoName, plOne);
                }
            }
        }

        viewBoard();
        
        return true;
    }
    
    public boolean checkTicTacToe(Character chipValue, String playerName)
    {
        System.out.println("***CHECK TIC-TAC-TOE****"); 
        
        System.out.println("End user inputted chip: " + rowChipPlacedAdjusted + "," + columnChipPlacedZeroIndex + "(zero index notation)");
        
        playerNameChipValue = playerName + "("+chipValue+")";
        
        System.out.println("VERTICAL CHECK => DOWNWARDS*****");
        
        if (rowChipPlacedAdjusted!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE - Vertical check downwards");
            
            for (int count = rowChipPlacedAdjusted; count<=boardHeightZeroIndex; count++)
            {
                if (!board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal=0;
                    break;
                }
                
                if (board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal++;
		            runningTotalSameColour=numberSameSymbolTotal;
                     
                    if (numberSameSymbolTotal>1)
                    {
                        rightColour=true;
                    }
                     
                    if (numberSameSymbolTotal==numberConsecutiveForWin)
                    {
                        System.out.println("***5Congratulations " + playerNameChipValue + "  Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") "     +"in column: " + columnChipPlaced + "\tN <-> S via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");
                    
                        isGameOver=true;
                    }
                }
            }

            
            numberSameSymbolTotal=0;
        }
        
        System.out.println("VERTICAL CHECK => UPWARDS******");
        
        if (rowChipPlacedAdjusted!=boardBaseLevelZeroIndex)
        {
            System.out.println("INSIDE - Vertical check upwards");
            
            for (int count = rowChipPlacedAdjusted; count>=boardBaseLevelZeroIndex; count--)
            {
                if (!board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal=0;
                    break;
                }
                
                if (board[count][columnChipPlacedZeroIndex].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal++;
                    
                    if (rightColour)
                    {
                        runningTotalSameColour=runningTotalSameColour-1;
                    }
                    
                    if (numberSameSymbolTotal+runningTotalSameColour==numberConsecutiveForWin)
                    {
                        System.out.println("***5Congratulations " + playerNameChipValue + " Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") " + "in column: " + columnChipPlaced + "\tN <-> S via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");

                        isGameOver=true;
                        return true;
                    }
                }
            }
            offset=0;
            numberSameSymbolTotal=0;
            runningTotalSameColour=0;
            rightColour=false;
        }
        
        System.out.println("HORIZONTAL CHECK => RIGHT*****");
        
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex)
        {
            System.out.println("INSIDE - horizontal check right");
            for (int count = columnChipPlacedZeroIndex; count<=boardWidthZeroIndex; count++)
            {
                if (!board[rowChipPlacedAdjusted][count].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal=0;
                    break;
                }
                
                if (board[rowChipPlacedAdjusted][count].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal++;
                    runningTotalSameColour=numberSameSymbolTotal;
                    
                    if (numberSameSymbolTotal>1)
                    {
                        rightColour=true;
                    }
                    if (numberSameSymbolTotal==numberConsecutiveForWin)
                    {
                        System.out.println("***4Congratulations " + playerNameChipValue + " Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") " + "in row: " + rowChipPlaced + "\tW <-> E via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");;

                        isGameOver=true;
                        return true;
                    }
                }
            }            
            
            numberSameSymbolTotal=0;
        }
        
        System.out.println("HORIZONTAL CHECK => LEFT*****");
        
        if (columnChipPlacedZeroIndex!=boardFirstColumnZeroIndex)
        {
            System.out.println("INSIDE - horizontal check left");
            for (int count = columnChipPlacedZeroIndex; count>=boardFirstColumnZeroIndex; count--)
            {
                if (!board[rowChipPlacedAdjusted][count].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal=0;
                    break;
                }
                
                if (board[rowChipPlacedAdjusted][count].equals(String.valueOf(chipValue)))
                {
                    numberSameSymbolTotal++;
                    
                    if (rightColour)
                    {
                        runningTotalSameColour=runningTotalSameColour-1;
                    }
                    
                    if (numberSameSymbolTotal+runningTotalSameColour==numberConsecutiveForWin)
                    {
                        System.out.println("***5Congratulations " + playerNameChipValue + "  Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") " + "in row: " + rowChipPlaced +  "\tW <-> E via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");

                        isGameOver=true;
                        return true;
                    }
                }
            }
            offset=0;
            numberSameSymbolTotal=0;
            runningTotalSameColour=0;
            rightColour=false;
        }

        System.out.println("DIAGONAL CHECK 1");  
    
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedAdjusted!=boardBaseLevelZeroIndex)
        {
            System.out.println("INSIDE D1 - Diagonal north east check");
            
            for (int count = rowChipPlacedAdjusted; count<=boardBaseLevelZeroIndex; count--)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)
                {
                    if (!board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal=0;
                        break;
                    }
                
                    if (board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal++;
			            runningTotalSameColour=numberSameSymbolTotal;
                        
                        if (numberSameSymbolTotal>1)
                        {
                            rightColour=true;
                        }
                        offset++;
                        
                        if (numberSameSymbolTotal==numberConsecutiveForWin)
                        {
                            System.out.println("***6Congratulations " + playerNameChipValue + "  Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") " + " in diagonal: " + "\tNE <-> SW via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");;

                            isGameOver=true;
                            return true;
                        }
                    }
                }
            }
            
            numberSameSymbolTotal=0;
            offset=0;
        }
    
        System.out.println("DIAGONAL CHECK 2"); 
                                                                         
        if (columnChipPlacedZeroIndex!=boardFirstColumnZeroIndex && rowChipPlacedAdjusted!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D2 - Diagonal south west check");
            
            for (int count = rowChipPlacedAdjusted; count<=boardHeightZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex-offset>=boardFirstColumnZeroIndex)
                {
                    if (!board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal=0;
                        break;
                    }
                    
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal++;
                        offset++;
                        
                        if (rightColour)
                        {
                            runningTotalSameColour=runningTotalSameColour-1;
                        }
                        
                        if (numberSameSymbolTotal+runningTotalSameColour==numberConsecutiveForWin)
                        {
                            System.out.println("***7Congratulations " + playerNameChipValue + "  Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") " + "in diagonal: " + "\tSW <-> NE via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");

                            isGameOver=true;
                            return true;
                        }
                    }
                }
            }
            runningTotalSameColour=0;
            rightColour=false;
            numberSameSymbolTotal=0;
            offset=0;
        }
        
        System.out.println("DIAGONAL CHECK 3"); 
                                      
        if (columnChipPlacedZeroIndex!=boardWidthZeroIndex && rowChipPlacedAdjusted!=boardHeightZeroIndex)
        {
            System.out.println("INSIDE D3 - Diagonal south east check");
                                                        
            for (int count = rowChipPlacedAdjusted; count<=boardHeightZeroIndex; count++)
            {
                if (columnChipPlacedZeroIndex+offset<=boardWidthZeroIndex)               
                {
                    if (!board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal=0;
                        break;
                    }
                    
                    if (board[count][columnChipPlacedZeroIndex+offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal++;
			            runningTotalSameColour=numberSameSymbolTotal;
                        
                        if (numberSameSymbolTotal>1)
                        {
                            rightColour=true;
                        }
                        
                        offset++;
                        
                        if (numberSameSymbolTotal==numberConsecutiveForWin)
                        {
                            System.out.println("***8Congratulations " + playerNameChipValue + "  Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") " + " in diagonal: " + "\tNW <-> SE via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");

                            isGameOver=true;
                            return true;
                        }
                    }
                    
                }
            }
            
            numberSameSymbolTotal=0;
            offset=0;
        }
        
        System.out.println("DIAGONAL CHECK 4"); 
        
        if (columnChipPlacedZeroIndex!=boardFirstColumnZeroIndex && rowChipPlacedAdjusted!=boardBaseLevelZeroIndex)
        {
            System.out.println("INSIDE D4 - Diagonal north west check");
                
            for (int count = rowChipPlacedAdjusted; count>=boardBaseLevelZeroIndex; count--)
            {
                if (columnChipPlacedZeroIndex-offset>=boardFirstColumnZeroIndex)
                {
                    if (!board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal=0;
                        break;
                    }
                    
                    if (board[count][columnChipPlacedZeroIndex-offset].equals(String.valueOf(chipValue)))
                    {
                        numberSameSymbolTotal++;
                        offset++;
                        
                        if (rightColour)
                        {
                            runningTotalSameColour=runningTotalSameColour-1;
                        }
                    
                        if (numberSameSymbolTotal+runningTotalSameColour==numberConsecutiveForWin)
                        {
                            System.out.println("***9Congratulations " + playerNameChipValue + "  Tic-Tac-Toe " + "Streak("+numberConsecutiveForWin+") " + " in diagonal: " + "\tNW <-> SE via: "+ "["+columnChipPlaced+" , " + rowChipPlaced+"]");
                            isGameOver=true;
                            return true;
                        }
                    }
                }
            }
            rightColour=false;
            runningTotalSameColour=0;
            numberSameSymbolTotal=0;
            offset=0;
        }
        
        if (numChipsPlaced==(boardWidth*boardHeight))
        {
            System.out.println("GAME OVER - NO WINNER");
            System.exit(0);
        }
        
        return false;
    }
    
    public void menu()
    {
        regexOptions="[12345]";
	patternRows = Pattern.compile(regexOptions, Pattern.CASE_INSENSITIVE);
        
        try
        {
            do
            {
                scanner = new Scanner(System.in);                
        
                System.out.println("***MENU****: " + "\tPlayer One: "+ playerOneName + "\tPlayer Two: " + playerTwoName + "\t\tBoard Size (W X H):  " + boardWidth+" X " + boardHeight + "\tNumber symbols win: " + numberConsecutiveForWin);
                System.out.println("1. Configure Players");
                System.out.println("2. Configure board size");
                System.out.println("3. Configure consecutive symbols for win");
                System.out.println("4. Start game");
                System.out.println("5. Exit");
                System.out.println("Please make a choice");
        
                inputInteger = scanner.nextInt();
                matcherRows = patternRows.matcher(String.valueOf(inputInteger));
        
            }while (!matcherRows.find());
        }
        catch (InputMismatchException e)
        {
            menu();
        }
        
        System.out.println("SELECTED OPTION: " + inputInteger);
        
        switch(inputInteger)
        {
            case 1:
                configurePlayers();
                menu();
                break;
                
                case 2:
                    configureBoardSize();
                    menu();
                    break;
                    
                    case 3:
                        if (hasConfigureBoardSize)
                        {
                            configureConsecutiveSymbols();
                        }
                        else
                        {
                            System.out.println("Please complete\t\t2.Configure board size");
                            
                        }
                        menu();
                        break;
                        
                        case 4:
                            if (hasConfigureBoardSize && hasConfigurePlayers && hasConfigureConsecutiveSymbols)
                            {
                                System.out.println("WELCOME: " + playerOneName + " , " + playerTwoName+"\n");
                                PlayerOne plOne = new PlayerOne (this, playerOneName,playerTwoName, plTwo);
                            }
                            else
                            {
                                System.out.println("Please complete 1,2,3");
                                menu();
                            }
                            
                            break;
                            
                            case 5:
                            break;
                            default:
                            System.exit(0);
        }
    }
    
    public void configureBoardSize()
    {
        try
        {
            do 
            {
                scanner = new Scanner(System.in);
                System.out.println("Enter width (w):");
                boardWidth = scanner.nextInt();
                System.out.println("Enter height (h):");
                boardHeight = scanner.nextInt();
                
            }while (((boardHeight==3 && boardWidth==1) || (boardHeight==1 && boardWidth==3)));
        }
        catch (InputMismatchException e)
        {
            configureBoardSize();
        }
        
        boardWidthZeroIndex = boardWidth-1;
        boardHeightZeroIndex=boardHeight-1;
        board = new String[boardHeight][boardWidth];
        
        for (int i=0; i<boardHeight;i++)
        {
            for (int j=0; j<boardWidth;j++)
            {
                board[i][j]="-";
            }
        }
        
        boardBaseLevelZeroIndex = 0;
        boardHeight=board.length;
        
        boardWidth=board[0].length;
        hasConfigureBoardSize=true;
    }
    
    public void configureConsecutiveSymbols()
    {
        scanner = new Scanner(System.in);
        
        try
        {
            do
            {
                System.out.println("Number consecutive symbols in row/diagonal constituing a win (greater than 2):");
                System.out.println("Should be less than equal to Width (" +boardWidth+") and Height (" + boardHeight+")");
                
                numberConsecutiveForWin = scanner.nextInt();
                System.out.println("numberConsecutiveForWin: " + numberConsecutiveForWin);
            
            }while (numberConsecutiveForWin<=2 || (numberConsecutiveForWin>boardWidth && numberConsecutiveForWin>boardHeight));
        }
        catch (InputMismatchException e)
        {
            configureConsecutiveSymbols();
        }
        hasConfigureConsecutiveSymbols = true;
    }
    
    public void configurePlayers()
    {
        scanner = new Scanner(System.in);
        System.out.println("Enter name for Player 1:");
        
        playerOneName = scanner.next();
        System.out.println("Enter name for Player 2:");
        playerTwoName = scanner.next();
        
        hasConfigurePlayers=true;
    }
    
    public void assignChip(String playerName, Character chip)
    {
        System.out.println("***ASSIGNED CHIP****");
        System.out.println(playerName + " has been assigned: " + chip + " chip");
    }
    
    public void selectChipPosition(String playerName, Character chip)
    {
        System.out.println("***SELECT CHIP POSITION****");
        scanner = new Scanner(System.in);
        
        viewBoard();
        
        do
        {
            System.out.println(playerName + "("+chip+")" +  ",  Which column would you like to insert the chip?");
            inputCharColumn = scanner.next().charAt(0);
            
            System.out.println(playerName + "("+chip+")" +  ",  Which row would you like to insert the chip?");
            inputCharRow = scanner.next().charAt(0);
            
            regex=new StringBuilder();
            int numColumnRegex=1;
            
            regex.append("[");
            
            do
            {
                regex.append(Integer.toString(numColumnRegex));
                numColumnRegex++;
                
            }while (numColumnRegex<=boardWidth);
            
            regex.append("]");
            regexOptions=regex.toString();
            System.out.println("AVAILABLE ROWS: " + regexOptions);
            
            patternCols = Pattern.compile(regexOptions, Pattern.CASE_INSENSITIVE);
            matcherCols = patternCols.matcher(Character.toString(inputCharColumn));
            
            regex=new StringBuilder();
            int numRowRegex=1;
            
            regex.append("[");
            
            do
            {
                regex.append(Integer.toString(numRowRegex));
                numRowRegex++;
                
            }while (numRowRegex<=boardHeight);
            
            regex.append("]");
            regexOptions=regex.toString();
            System.out.println("AVAILABLE COLUMNS: " + regexOptions);
            
            patternRows = Pattern.compile(regexOptions, Pattern.CASE_INSENSITIVE);
            matcherRows = patternRows.matcher(Character.toString(inputCharColumn));
            
        }while (!matcherRows.find() && !matcherCols.find());
        
        columnChipPlacedZeroIndex = Character.getNumericValue(inputCharColumn-1);
        columnChipPlaced = Character.getNumericValue(inputCharColumn);
        
        rowChipPlacedZeroIndex = Character.getNumericValue(inputCharRow-1);
        rowChipPlaced = Character.getNumericValue(inputCharRow);
    }
    
    public void viewBoard()
    {
        int j=0;
        int rowOnBoard=0;
        
        if (isGameOver)
        {
            System.out.println("\n****CURRENT BOARD****: " + "***Congratulations " + playerNameChipValue);
            
            isGameOver=false;
        }
        else
        {
            System.out.println("\n****CURRENT BOARD****");
        }
        
        StringBuilder [] sb  = new StringBuilder[((board.length*2)+1)];
        
        for (int i=0; i<sb.length;i++)
        {
            if (i%2==0)
            {
                for (int f=0; f<board[0].length;f++)
                {
                    if (f==0)
                    {
                        sb[i+j]=new StringBuilder("|---|");
                    }
                    else
                    {
                        sb[i+j].append("---|");
                    }
                }
            }
            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 TicTacToe() 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("*********Welcome to TIC-TAC-TOE**************");
        System.out.println("NOTE: Code will not terminate on a win, see onscreen messages");
        
        menu();
        
        if (hasConfigureBoardSize)
        {
            viewBoard();
        }
    }
}