/*
  Copyright (C) 2005 Dan Alcantara (dfalcantara@ucdavis.edu, http://wwwcsif.cs.ucdavis.edu/~alcantar)
  Based on the board game "Blokus" by Educational Insights, Inc.
  
  This program is free software; you can redistribute 
  it and/or modify it under the terms of the GNU General 
  Public License as published by the Free Software Foundation; 
  either version 2 of the License, or (at your option) any later version.
  
  This program is distributed in the hope that it will be useful, but 
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
  more details.
  
  You should have received a copy of the GNU General Public License along 
  with this program; if not, write to the Free Software Foundation, Inc., 
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  
  Full license visible at http://www.opensource.org/licenses/gpl-license.php
*/

#ifndef GAME_H
#define GAME_H

#include <vector>
#include "piece.h"
#include "player.h"
#include "board.h"
#include "ai.h"

class BlokishGame
{
public:
  enum TurnOutcome
  {
    GameOver, GetPlayerInput, DOING_AI, BeginAIThread, WaitForAI, FinishAIThread
  };

  BlokishGame():mAI(mBoard)
  {
    Reset();
  }

  //// Accessors & mutators
  AI::AIOptions* GetOptions()
  {
    return mAIOptions;
  }

  int GetAge(int nRow, int nCol)
  {
    return mBoard.GetAge(nRow, nCol);
  }

  float GetRound()
  {
    return mBoard.GetRound();
  }

  int GetNumDone()
  {
    return mAI.GetNumDone();
  }

  BlokishID GetCurrentPlayer()
  {
    return BlokishID(mnCurrentPlayer);
  }
  
  bool IsComputerControlled(int index)
  {
    return mbvComputerControlled[index];
  }

  void SetComputerControlled(int index, bool useAI)
  {
    mbvComputerControlled[index] = useAI;
  }

  TurnOutcome GetOutcome() const
  {
    return mOutcome;
  }

  void SetOutcome(BlokishGame::TurnOutcome outcome)
  {
    mOutcome = outcome;
  }

  bool IsGameOver()
  {
    for(unsigned nPlayer = 0; nPlayer < 4; nPlayer++)
      if(mbvPlayerDead[nPlayer] == false)
        return false;
        
    return true;
  }

  // Returns the pointer to the piece that has been chosen
  BlokishPiece* GetSelectedPiece()
  {
    return mpSelectedPiece;
  }

  // Sets the chosen piece.  This is the one that gets placed and hovers at the mouse.
  void SetSelectedPiece(unsigned nPiece)
  {
    if(nPiece < mvPlayers[mnCurrentPlayer].GetUnplayedPieces().size())
      mpSelectedPiece = mvPlayers[mnCurrentPlayer].GetUnplayedPieces()[nPiece];
  }

  std::vector<BlokishPiece*>& GetUnplayedPlayerPieces(BlokishID player = none)
  {
    if(player < MAX_PLAYER_ID)
      return mvPlayers[player].GetUnplayedPieces();
    else
      return mvPlayers[mnCurrentPlayer].GetUnplayedPieces();
  }

  bool IsUsableSpace(int nRow, int nCol)
  {
    return mBoard.IsUsableSpace(nRow, nCol);
  }

  BlokishID GetSpaceID(int nRow, int nCol)
  {
    return mBoard.GetSpaceID(nRow, nCol);
  }
 
  int GetNumRows()
  {
    return mBoard.GetHeight();
  }

  int GetNumCols()
  {
    return mBoard.GetWidth();
  }

  int GetScore(BlokishID player)
  {
    if(player < MAX_PLAYER_ID)
      return mvPlayers[player].GetScore();
    else
      return 0;
  }

  void PrintBoard()
  {
    mBoard.Print();
  }
  ////
  
  //// Save game handlers
  void Write(std::ofstream &out);
  bool Read(std::ifstream &in);
  ////

  // Resets the game.
  void Reset();

  // Reset the AI. Set it up for the current player.
  TurnOutcome ResetAI();

	// Steps the AI.  Returns true if it completed.
  bool TakeAIStep();
 
	// Finishes a turn.  Deletes a piece from a player's reserve, and finds the next live player.
  TurnOutcome FinishTurn();

  // Checks whether or not a player is dead.
  bool IsDead(BlokishID player, bool quickly);

  bool PlacePiece(BlokishPiece *piece, int row, int col)
  {
    return mBoard.PlacePiece(piece, row, col);
  }

  bool PieceCanBePlaced(BlokishPiece *piece, int row, int col)
  {
    return mBoard.PieceCanBePlaced(piece, row, col);
  }

protected:
  int                mnCurrentPlayer;
  BlokishBoard       mBoard;
  BlokishPiece       *mpSelectedPiece;
  BlokishBoard::Move mChosenMove;
  TurnOutcome        mOutcome;
  
  Player            mvPlayers[4];
  bool              mbvPlayerDead[4];
  bool              mbvComputerControlled[4];

  AI                mAI;
  AI::AIOptions     mAIOptions[4];
};

#endif
