package cisc181.mylab_4;

// Christopher Rasmussen
// copyright 2017
// University of Delaware
// CISC181
// some code adapted from Brian Hall

import java.util.ArrayList;
import java.util.Random;

public class TicTacToeBoard {

  // define piece -- an empty square counts as "none"

  static enum Piece {
    NONE, X, O
  }

  // useful constants

  static final int NUM_PIECES = 9;
  static final int NO_MOVE_EXISTS = -1;
  static final int INITIAL_WINVALUE = 0;
  static final int WIN_POINTS = 1;
  static final int LOSS_POINTS = -10;
  static final int TIE_POINTS = 0;

  Piece[] boardArray;
  Random R;

  // default constructor

  TicTacToeBoard() {
    boardArray = new Piece[NUM_PIECES];
    R = new Random();
    clear();
  }

  // copy board contents

  void copyTo(TicTacToeBoard dest) {
    for (int i = 0; i < NUM_PIECES; i++)
      dest.boardArray[i] = this.boardArray[i];
  }

  // empty all squares

  void clear() {
    for (int i = 0; i < NUM_PIECES; i++)
      boardArray[i] = Piece.NONE;
  }

  // test if there are no empty squares left

  boolean full() {
    for (int i = 0; i < NUM_PIECES; i++)
      if (boardArray[i] == Piece.NONE)
        return false;
    return true;
  }

  // show state of board in console

  void print() {
    System.out.println("-----");
    for (int i = 0; i < NUM_PIECES; i++) {
      char c;
      switch (boardArray[i]) {
        case X:
          c = 'X';
          break;
        case O:
          c = 'O';
          break;
        default:
          c = '.';
      }
      System.out.print(c);

      if ((i + 1) % 3 == 0)
        System.out.printf("    %d %d %d\n", i-2, i-1, i);
      else
        System.out.print(" ");
    }
    System.out.println("-----");
  }

  // check for a win on current board -- return winner if any

  TicTacToeBoard.Piece checkWin() {

    if (boardArray[0] == boardArray[1] && boardArray[1] == boardArray[2] && boardArray[2] != Piece.NONE) return boardArray[2];  // first row
    if (boardArray[3] == boardArray[4] && boardArray[4] == boardArray[5] && boardArray[5] != Piece.NONE) return boardArray[5];  // second row
    if (boardArray[6] == boardArray[7] && boardArray[7] == boardArray[8] && boardArray[8] != Piece.NONE) return boardArray[8];  // third row
    if (boardArray[0] == boardArray[3] && boardArray[3] == boardArray[6] && boardArray[6] != Piece.NONE) return boardArray[6];  // first column
    if (boardArray[1] == boardArray[4] && boardArray[4] == boardArray[7] && boardArray[7] != Piece.NONE) return boardArray[7];  // second column
    if (boardArray[2] == boardArray[5] && boardArray[5] == boardArray[8] && boardArray[8] != Piece.NONE) return boardArray[8];  // third column
    if (boardArray[0] == boardArray[4] && boardArray[4] == boardArray[8] && boardArray[8] != Piece.NONE) return boardArray[8];  // first diagonal
    if (boardArray[2] == boardArray[4] && boardArray[4] == boardArray[6] && boardArray[6] != Piece.NONE) return boardArray[6];  // second diagonal

    return Piece.NONE;
  }

  // setter function to change board -- if illegal, return false

  boolean attemptMove(int moveIndex, Piece movePiece) {
    if (moveIndex < 0 || moveIndex >= NUM_PIECES || boardArray[moveIndex] != TicTacToeBoard.Piece.NONE)
      return false;
    else {
      boardArray[moveIndex] = movePiece;
      return true;
    }
  }

  // pick a random empty spot on the board -- return index (row major -- see print() function)
  // does not actually make the move -- doesn't know/care whose turn it is

  int chooseRandomMove() {

    ArrayList<Integer> moveIndexList = new ArrayList<>();

    for (int i = 0; i < NUM_PIECES; i++) {
      if (boardArray[i] == Piece.NONE)
        moveIndexList.add(i);
    }

    if (moveIndexList.size() == 0)
      return NO_MOVE_EXISTS;

    return moveIndexList.get(R.nextInt(moveIndexList.size()));
  }

  // simulate multiple games from different possible starting positions to assess
  // how good each move is

  int chooseMonteCarloMove(TicTacToeBoard.Piece curRealPlayer, int numRandomGames) {

    System.out.println("FILL THIS IN -- SUBSTITUTING RANDOM MOVE FOR NOW");
    return chooseRandomMove();
      
  }
}
