package lab4;

/**
 * A 3 x 3 tic-tac-toe board.
 * @author Julie
 * @correcteur Martin
 */

public class TicTacToe
{
	private String[][] board;
	private static final int ROWS = 3;
	private static final int COLUMNS = 3;

	/**
	   Constructs an empty board.
	*/
	public TicTacToe()
	{
	   board = new String[ROWS][COLUMNS];
	   // Fill with spaces
	   for (int i = 0; i < ROWS; i++)
	      for (int j = 0; j < COLUMNS; j++)
	         board[i][j] = " ";
	}

	/**
	   Sets a field in the board. The field must be unoccupied.
	   @param i the row index
	   @param j the column index
	   @param player the player ("x" or "o")
	*/
	public void set(int i, int j, String player)
	{
	   if (board[i][j].equals(" "))
	      board[i][j] = player;
	}

	/**
	   Creates a string representation of the board, such as
	   |x  o|
	   |  x |
	   |   o|
	   @return the string representation
	*/
	public String toString()
	{
	   String r = "";
	   for (int i = 0; i < ROWS; i++)
	   {
	      r = r + "|";
	      for (int j = 0; j < COLUMNS; j++)
	         r = r + board[i][j];
	      r = r + "|\n";
	   }
	   return r;
	}

	/**
	 *
	 */
	private static final int [][][] pair = {
			{{0,0},{0,1},{0,2}},
			{{1,0},{1,1},{1,2}},
			{{2,0},{2,1},{2,2}},
			{{0,0},{1,0},{2,0}},
			{{0,1},{1,1},{2,1}},
			{{0,2},{1,2},{2,2}},
			{{0,0},{1,1},{2,2}},
			{{0,2},{1,1},{2,0}}
	};
	String getWinner(){
		for (int i=0;i<8;i++)
			if (board[pair[i][0][0]][pair[i][0][1]] ==
			    board[pair[i][1][0]][pair[i][1][1]] &&
			    board[pair[i][2][0]][pair[i][2][1]] ==
			    board[pair[i][1][0]][pair[i][1][1]])
				return board[pair[i][0][0]][pair[i][0][1]];
		return " ";
	}

}
