/******************************************************************************

 * Tic Tac Toe (C) 25/Jun/2017 by Coty R. Miller

 *

 * Requires Java SE 8 (JDK 8)

 *

 * Compile with: javac ticTacToe.java

 * Run with: java ticTacToe

 *

 *****************************************************************************/

import java.util.*;


class ticTacToe {

    

    public static String[] lst = new String[]{" "," "," "," "," "," "," "," "," "," "};

    public static int currentPlayer = 0;

    

    //=============================================================================

    // Render screen routine.

    //=============================================================================

    public static String crtplr()

{

    if(currentPlayer == 0)

    return "x";

    else

    return "o";

    }

    //=============================================================================

    // Clear the game buffer.

    //=============================================================================

    public static void clearString()

{

    int x = 9;

    while(x!=0)

    {

    lst[x] = " ";

    x--;

    }

    }

    //=============================================================================

    // Render screen routine.

    //=============================================================================

    public static void renderScreen() {

    System.out.println(lst[1] + " | " + lst[2] + " | " + lst[3]);

    System.out.println("---------");

    System.out.println(lst[4] + " | " + lst[5] + " | " + lst[6]);

    System.out.println("---------");

    System.out.println(lst[7] + " | " + lst[8] + " | " + lst[9]);

    System.out.print("-: ");

    }

    //=============================================================================

    // Main game.

    //=============================================================================

    public static void main(String args[]) {

    Scanner keyboard = new Scanner(System.in);

    int totalthing = 0;

    int zx;

    while(totalthing != 9) {

    System.out.println("Player '" + crtplr() +

    "' Press a key 1-9 to select a cell. Use '^C' to quite.");

    

    renderScreen();            // Render the game board.

    

    zx = keyboard.nextInt();

    //

    // If the key pressed is 1-9 AND the place is not taken

    // then place the X or the O in the buffer.

    //

    if(zx >= 0 && zx <= 10 && lst[zx] == " ")

    {

    lst[zx] = crtplr();

    totalthing++;

    if(currentPlayer==0)

    currentPlayer=1;

    else

    currentPlayer=0;

    }

    //=================================================================

    // The "algorithm" for checking the winnings.

    //-----------------------------------------------------------------

    // Check all rows from the top down.

    //=================================================================

    if(lst[4] == lst[1] && lst[7] == lst[1] && lst[1] != " ") {

    System.out.println(" Player '"+lst[1]+"' Wins!  ");

    renderScreen();        // Render the winning screen before we

    // clear the buffer.

    clearString();         // Clear the buffer.

    }

    if(lst[5] == lst[2] && lst[8] == lst[2] && lst[2] != " ") {

    System.out.println(" Player '"+lst[2]+"' Wins!  ");

    renderScreen();

    clearString();

    }

    if(lst[6] == lst[3] && lst[9] == lst[3] && lst[3] != " ") {

    System.out.println(" Player '"+lst[3]+"' Wins!  ");

    renderScreen();

    clearString();

    }

    //-----------------------------------------------------------------

    // Check from left to right for a win.

    //-----------------------------------------------------------------

    if(lst[2] == lst[1] && lst[3] == lst[1] && lst[1] != " ") {

    System.out.println(" Player '"+lst[1]+"' Wins!  ");

    renderScreen();

    clearString();

    }

    if(lst[5] == lst[4] && lst[6] == lst[4] && lst[4] != " ") {

    System.out.println(" Player '"+lst[4]+"' Wins!  ");

    renderScreen();

    clearString();

    }

    if(lst[8] == lst[7] && lst[9] == lst[7] && lst[7] != " ") {

    System.out.println(" Player '"+lst[7]+"' Wins!  ");

    renderScreen();

    clearString();

    }

    //-----------------------------------------------------------------

    // Check cross ways

    //-----------------------------------------------------------------

    if(lst[5] == lst[1] && lst[9] == lst[1] && lst[1] != " ") {

    System.out.println(" Player '"+lst[1]+"' Wins!  ");

    renderScreen();

    clearString();

    }

    if(lst[5] == lst[3] && lst[7] == lst[3] && lst[3] != " ") {

    System.out.println(" Player '"+lst[3]+"' Wins!  ");

    renderScreen();

    clearString();

    }

    }

    

    }

}