package tictactoe;

import exception.InputInvalidException;
import exception.PlacementFailedException;
import game.AbstractGame;
import game.Coordinate;
import game.PieceType;

/**
 * This class runs a Tic-Tac-Toe game.
 */
public class TicTacToeSinglePlayer extends AbstractGame {

	/**
	 * This variable is the piece the player is placing on the board. Index 0 is
	 * the player, index 1 is the ai.
	 */
	private final PieceType[] pieces;

	/** This integer keeps track of the current team's turn. */
	private int currentIndex;

	/** The human's index */
	private final static int HU = 0;
	/** The ai's index */
	private final static int AI = 1;

	/**
	 * This constructor passes the Board of this game to the super class
	 * constructor and defines the instance variables of this object.
	 */
	public TicTacToeSinglePlayer(PieceType playerPiece) {
		super(new TicTacToeBoard());

		pieces = new PieceType[2];
		pieces[0] = playerPiece;
		pieces[1] = playerPiece == PieceType.X ? PieceType.O : PieceType.X;

		currentIndex = playerPiece == PieceType.X ? 0 : 1;
	}

	/**
	 * This method represents a single placement of a piece by one of the
	 * players in the game. Then, the current team swaps. If the user input
	 * fails, then the current team is not changed.
	 */
	@Override
	public void next() {
		Coordinate input;

		if (currentIndex == HU) {
			try {
				input = getConsole().getSquareCoordinates(this);
			} catch (InputInvalidException e) {
				errorMessage(e.toString());
				return;
			}
			System.out.println();
		} else {
			input = getAIMove();
		}

		try {
			getBoard().attemptPlace(pieces[currentIndex], input);
		} catch (PlacementFailedException e) {
			errorMessage(e.toString());
			return;
		}

		printBoard();
		currentIndex = currentIndex == HU ? AI : HU;

	}

	/**
	 * This method has the AI determine its move and return the corresponding square the move is located on.
	 * 
	 * @return the ai will place a piece on this square
	 */
	private Coordinate getAIMove() {
		// find winning lines
		// count number of pieces in each line
		// end one turn victories
		// randomly place the piece
		// lines 1-3 are columns, lines 4-6 are rows, lines 7 and 8 are diagonals
		
		return new Coordinate(0, 0);
	}

	/**
	 * This method checks if the game should end. This method asks the board if
	 * the game should end and gives a message if the game has ended.
	 */
	@Override
	public void check() {
		char team = getBoard().gameEnd();

		if (team != ' ') {
			if (team == '_') {
				System.out.println("It's a tie!");
			} else {
				System.out.println(team + " won!");
			}

			endGame();
		}
	}
}
@@ -15,16 +15,47 @@
 toe;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0A%0D%0A
 import e
@@ -1966,16 +1966,21 @@
 sponding
+%0D%0A%09 *
  square 
@@ -2004,16 +2004,16 @@
 ed on.%0D%0A
-
 %09 * %0D%0A%09 
@@ -2297,16 +2297,22 @@
 nd 8 are
+%0D%0A%09%09//
  diagona
@@ -2321,38 +2321,334 @@
 %0D%0A%09%09
-%0D%0A%09%09return new Coordinate(0, 0
+char%5B%5D%5B%5D tempBoard = getBoard().getBoard();%0D%0A%09%09int size = tempBoard.length;%0D%0A%09%09ArrayList%3CCoordinate%3E list = new ArrayList%3CCoordinate%3E(9);%0D%0A%09%09for (int i = size * size - 1; i %3E= 0; i--) %7B%0D%0A%09%09%09list.add(new Coordinate(i / size, i %25 size));%0D%0A%09%09%7D%0D%0A%09%09System.out.println(list);%0D%0A%0D%0A%09%09return list.get((int) (Math.random() * list.size())
 );%0D%0A

add ai random piece placement