//Anton Dubrau
//Assignment 2

import java.util.Scanner;

class ConnectThree{
  //declare/initialize board as empty
  static char b11=' ',b12=' ',b13=' ',b14=' ',
    b21=' ',b22=' ',b23=' ',b24=' ',
    b31=' ',b32=' ',b33=' ',b34=' ';
  
  
  //printing board
  public static void printBoard(){
    System.out.println("");
    System.out.println("    |"+b11+" "+b12+" "+b13+" "+b14+"|");
    System.out.println("    |"+b21+" "+b22+" "+b23+" "+b24+"|");
    System.out.println("    |"+b31+" "+b32+" "+b33+" "+b34+"|");
    System.out.println(" -----------------");
    System.out.println("     1 2 3 4");
  }
  
  
  //checks whether the given player won
  //if so prints a message and exits the program
  //input: a char denoting the player ('X' or 'O')
  public static void checkWinAndExit(char p){
    boolean won = false;
    
    //check horizontals
    if ((b12==p && b13==p) && (b11==p || b14==p)) won=true;
    if ((b22==p && b23==p) && (b21==p || b24==p)) won=true;
    if ((b32==p && b33==p) && (b31==p || b34==p)) won=true;
    
    //check verticals
    if (b11==p && b21==p && b31 == p) won=true;
    if (b12==p && b22==p && b32 == p) won=true;
    if (b13==p && b23==p && b33 == p) won=true;
    if (b14==p && b24==p && b34 == p) won=true;
    
    //check diagonals
    if (b11==p && b22==p && b33==p) won=true;
    if (b12==p && b23==p && b24==p) won=true;
    if (b31==p && b22==p && b13==p) won=true;
    if (b32==p && b23==p && b14==p) won=true;
    
    //checking whether player won and exit
    if (won){
      System.out.println("Player "+p+" won!");
      System.exit(0);
    }
  }


  //place a piece at location n for player p
  //input: the slot where to place the piece as an integer, and the player as a char ('X' or 'O')  
  public static void placePiece(int location, char player){
    //code for 1st column
    if (location == 1){
      if (b31 == ' '){
        b31 = player;
      } else if (b21 == ' '){
        b21 = player;
      } else if (b11 == ' '){
        b11 = player;
      }
    }
    
    //code for 2nd column
    if (location == 2){
      if (b32 == ' '){
        b32 = player;
      } else if (b22 == ' '){
        b22 = player;
      } else if (b12 == ' '){
        b12 = player;
      }
    }
    
    //code for 3rd column
    if (location == 3){
      if (b33 == ' '){
        b33 = player;
      } else if (b23 == ' '){
        b23 = player;
      } else if (b13 == ' '){
        b13 = player;
      }
    }
    
    //code for 4th column
    if (location == 4){
      if (b34 == ' '){
        b34 = player;
      } else if (b24 == ' '){
        b24 = player;
      } else if (b14 == ' '){
        b14 = player;
      }
    }
  }
  
  
  //make turn - returns true if the turn leads to a win
  //input: the player to make a turn as a char ('X' or 'O')
  public static void makeTurn(char player){
    //getting where to play
    System.out.print(player+"'s turn, where to place: ");
    Scanner scanner = new Scanner(System.in);
    int location = scanner.nextInt();
    
    //placing piece
    placePiece(location,player);
    
    //printing board
    printBoard();
    
    //checking winning and exit
    checkWinAndExit(player);
  }
  
  
  //main program
  public static void main(String[] args){
    //print the board first
    printBoard();
    
    //do all 12 turns
    makeTurn('X'); makeTurn('O');
    makeTurn('X'); makeTurn('O');
    makeTurn('X'); makeTurn('O');
    makeTurn('X'); makeTurn('O');
    makeTurn('X'); makeTurn('O');
    makeTurn('X'); makeTurn('O');
    
    //no one won...
    System.out.println("There was a draw!");
  }
}

