
/*
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.*;

interface playConnectFour
{
    public void insertChip();
    public boolean checkAvailability(int input, int colour, String name);
    public boolean checkConnectFour(int colour);
    public void viewBoard();
}

class connectFour implements playConnectFour 
{
    int [][] board;
    int rows = 6;
    int columns = 7;
    int j;
    int input;
    
    
    Player1 p1 = new Player1(this, board);  // not sure why this object ref needs to be passed down
    //Player2 p2 = new Player2(this,board);
    
    
    public connectFour(int[][] board)
    {
        this.board=board;
        
    }
    
    public void insertChip()
    {
        
    }
    
    
    public void viewBoard()
    {
        for (int [] temp: board)
        {
            System.out.println(Arrays.toString(temp));
        }
        
    }
    
    
    public boolean checkConnectFour(int colour)
    {
        //this will have to check if 4 in a row.
        
        // this will check first in the vertical direction
        
        //int rowsBelow = j;
        //int rowsAbove = (rows-1) -j;   // 1 has been subtracted due to z index in variable j
        int vertical=0;
        int horizontal=0;
        int diagonal=0;
        int counter=0;
        
        // CHECKING VERTICALLY
        
        // need logic here that if there is not a vertical yellow, it will set count back to 0
        // not so easy since it needs to count upwards and downwards
        
        for (int k = j; k>=0; k--)  // this is checking all rows below (same column) for yellows
        {
            if (board[k][input]==colour)  //there is also a yellow in that position
            {
                vertical++;
                
            }
            
            if (board[k][input]==0)  // this is problem part of code... since it might find non-matching colour below...
            // but vertically above there might be matching......
            // so need to store counter in another variable potentially.
            {
                counter=vertical;
                vertical=0;
                
                
            }
        }
        
         for (int k = j; k<=7; k++)  // this is checking all rows above (same column) for yellows
        {
            if (board[k][input]==colour)  //there is also a yellow in that position
            {
                vertical++;
                
            }
            
            if (board[k][input]==0)
            {
                counter=counter+vertical;
                vertical=0;
                
            }
            
        }
        
        if (counter==3)
        {
            System.out.println("connect 4");
            //perhaps need a function here to display the board array.
            return true;
        }
        
        //return false;
        
        
        // CHECKING HORIZONTALLY
        // WITH HORIZONTAL CHECK, NEED TO CHECK EACH LAYER
        // SO NEED ANOTHER FOR LOOP
        // to the right
        
        
        
            counter=0;
        
        for (int j=0; j<rows;j++)
        {
            counter=0;
        for (int m = input; m<=columns; m++)  // this is checking the same row to the right for yellows..
        // it is adding 1 to colsRight to ensure 5 cols are checked
        {
            if (board[j][m]==colour)  //there is also a yellow in that position
            {
                horizontal++;
                
            }
            
            if (board[j][m]==0)
            {
                counter=horizontal;
                horizontal=0;
                
            }
            
        }
        }
        
        
        // to the left
         
         for (int j=0; j<rows;j++)
        {
         
         for (int m = input; m>=0; m--)  // this is checking all rows below (same column) for yellows
        {
            if (board[j][m]==colour)  //there is also a yellow in that position
            {
                horizontal++;
                
            }
            
            if (board[j][m]==0)
            {
                counter=counter+horizontal;
                horizontal=0;
                
            }
            
        }
        }
        
        
        if (counter==3)
        {
            System.out.println("connect 4");
            //perhaps need a function here to display the board array.
            return true;
        }



   counter = 0;
// CHECKING DIAGONALLY
        
        for (int m = input; m<=columns-1; m++)   // this has to reduce columns by 1 to ensure no overrun diagonal check
        {
            if (board[j+1][m+1]==colour)  //there is also a yellow in that position
            {
                diagonal++;
                
            }
            
            if (board[j+1][m+1]==0)
            {
                counter=counter+diagonal;
                diagonal=0;
                
            }
            
            if (board[j-1][m+1]==colour)  //there is also a yellow in that position
            {
                diagonal++;
                
            }
            
            if (board[j-1][m+1]==0)
            {
                counter=counter+diagonal;
                diagonal=0;
                
            }
            
        }
        
        if (counter==3)
        {
            System.out.println("connect 4");
            //perhaps need a function here to display the board array.
            return true;
        }
        
        
         // checking diagonal in opposite direction
         
         counter=0;
         
         
         for (int m = input; m>0; m--)  // m is set greater than 0 to allow diagnonal to function
        {
            if (board[j-1][m-1]==colour)  //there is also a yellow in that position
            {
                diagonal++;
                
            }
            
            if (board[j-1][m-1]==0)
            {
                counter=diagonal;
                diagonal=0;
                
            }
            
            if (board[j+1][m-1]==colour)  //there is also a yellow in that position
            {
                diagonal++;
                
            }
            
            if (board[j+1][m-1]==0)
            {
                counter=counter+diagonal;
                diagonal=0;
                
            }
            
            
            
        }
        
        if (counter==3)
        {
            System.out.println("connect 4");
            //perhaps need a function here to display the board array.
            return true;
        }
        
        return false;
    
}
    public boolean checkAvailability(int input, int colour, String name)
    {
        
        this.input=input;
        
        String chipColour="";
        boolean availability=false;
        //yellow is being assigned value 1
        
        switch(colour)
        {
            case 1:
                chipColour="Yellow";
                break;
            
            case 2:
                chipColour="Red";
                break;
                
                default:
                
            
        }
        
        System.out.println(name + " has dropped the " + chipColour + " chip");
        System.out.println("This is the column chip placed in:" + input);
        
        
        for (j=0; j<7;j++)
        {
            //System.out.println("value of j: " + j);
            
          
            // the coin will drop to lowest position on the grid
            
            if (board[j][input]==0)   // all default values in declared array is 0
            {
                board[j][input]=colour;  // this will input a 1 to denote yellow if available.  2 to denote red
                availability=true;
                
                viewBoard();
                checkConnectFour(colour);
                break;
                
            }
            
        }
        
        if (availability)
        {
            return true;
            
        }
        
        return false;
        
    }
    
}


public class Main
{
    public static void main(String[] args) {
    System.out.println("Welcome to Online IDE!! Happy Coding :)");
    
    //int [][] board = new int [5][6];   // this ensures  7 columns and 6 rows
    
    int [][] board = {    {0,0,0,0,0,0,0}, 
                          {1,1,1,1,0,0,0},
                          {0,0,0,0,0,0,0},
                          {0,0,0,0,0,0,0},
                          {0,0,0,0,0,0,0},
                          {0,0,0,0,0,0,0},
                        };
                        
    System.out.println(board[1][1]);
                       
    
    connectFour cf = new connectFour(board);
    
    
    
    }
    
}

class Player1
{
    int[][] currentBoard;
    connectFour cf;
    chips yellow;
    String name = "Player 1";
    
    Player2 p2;
    
    enum chips
    {
        YELLOW, RED;
    }
    
    
    public Player1(connectFour cf, int[][] currentBoard)
    {
        this.currentBoard=currentBoard;
        this.cf=cf;
        chips yellow = chips.YELLOW;
        this.yellow=yellow;
        
        
        
        insertYellowChip(yellow);
        p2=new Player2(cf, currentBoard);   // do not need this object.. but it is here just incase need to get instance of p1
        
        
    }
    
   
    public void insertYellowChip(chips yellow)
    {
        System.out.println("Which column would you like to insert the coin");
        Scanner scan = new Scanner(System.in);
        int selection = scan.nextInt();
        
        cf.checkAvailability(selection,1,name);
    }
    
   
    
    public void insertYellowChip()
    {
        insertYellowChip(yellow);
    }
    
    
}


class Player2
{
    int[][]currentBoard;
    connectFour cf;
    String name = "Player 2";
    
    enum chips
    {
        YELLOW, RED;
    }
    
    
    public Player2(connectFour cf, int[][] currentBoard)
    {
        this.cf=cf;
        this.currentBoard=currentBoard;
        chips red = chips.RED;
        insertRedChip(red);
    }
    
    public void insertRedChip (chips red)
    {
        System.out.println("Which column would you like to insert the coin");
        Scanner scan = new Scanner(System.in);
        int selection = scan.nextInt();
        
        cf.checkAvailability(selection,2,name);
        
    }
    
    
}