//////////////////////////////////////////////////////////////////////////////
//
// File: hplayer.cc
//
// Purpose: Routines for managering the HumanPlayer class.
//
// Authors:
//   txe  Travis Emmitt
//
// Modifications:
//   15-APR-1998  txe  Initial creation
//   18-APR-1998  txe  Added PeekInto ()
//   23-APR-1998  txe  Using static debug, changed constructor
//   24-APR-1998  txe  Added NewGame(), NewMatch()
//
//////////////////////////////////////////////////////////////////////////////

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "board.h"
#include "player.h"
#include "hplayer.h"

///////////////////////////////////////////////////////////////////////////

HumanPlayer::HumanPlayer (char *name, int color)
           : Player      (name, color) {
}

///////////////////////////////////////////////////////////////////////////

void HumanPlayer::Feedback (int code) {
  Player::Feedback (code);
  switch (code) {
    case INVALID : cout << "\nInvalid move, try again\n\n"; break;
    case WIN     : cout << "\nYOU WON!!!\n";                break;
    case BLOCK   : cout << "\nNice block\n";                break;
    case LOSE    : cout << "\nYou Lost...\n";               break;
    case DRAW    : cout << "\nLet's call it a draw.\n";     break;
    default      : cout << "\nUnknown Feedback code:" << code << "\n";
  }
}

///////////////////////////////////////////////////////////////////////////

int HumanPlayer::GetMove () {
  ASSERT (board != NULL);

  char line[MAX_LEN+1];

  do {
    cout << "\nTURN #" << (turn + 1) << "\n";
    board->Print ();
    cout << "Enter x[0-" << size_x-1 << "] and y[0-" << size_y-1 << "]"
	 << " coords or 'h'elp, 'p'eek, 'q'uit: ";
    gets (line);
    
    switch (line[0]) {
      case 'h' : PrintHelp(); continue;
      case 'p' : PeekInto();  continue;
      case 'q' : return QUIT;
    }
  } while (sscanf (line, "%d %d", &move_x, &move_y) < 2);
  return 1;
}

///////////////////////////////////////////////////////////////////////////

int HumanPlayer::NewGame () {
  Player::NewGame ();
  cout << "\nGame #" << game << "\n";
  return 1;
}

///////////////////////////////////////////////////////////////////////////

int HumanPlayer::NewMatch () {
  Player::NewMatch ();
  cout << "\nMatch #" << match << "\n";
  return 1;
}

///////////////////////////////////////////////////////////////////////////

void HumanPlayer::PeekInto () {
  char line[MAX_LEN+1];
  int pick = 0;

  if (num_players == 1) {
    cout << "No other players!\n";
    return;
  }

  if (num_players == 2) {
    pick = 3 - color;
  }

  while (pick < 1 || pick == color || pick > num_players) {
    cout << "\nPeek into which opponent's brain?\n";
    for (int i = 1; i <= num_players; i++) {
      ASSERT (players[i] != NULL);
      if (i != color) {
	cout << "  (" << i << ") " << players[i]->name << "\n";
      }
    }
    pick = 0;
    gets (line);
    sscanf (line, "%d", &pick);
  }
  ASSERT (players[pick] != NULL);
  players[pick]->Peek ();
}
 
///////////////////////////////////////////////////////////////////////////

void HumanPlayer::PrintHelp () {
  ASSERT (board      != NULL);
  ASSERT (win_boards != NULL);

  cout << "\n-------------------------------------------------------------------\n";
  cout << "This is the current board:\n";
  board->Print ();

  cout << "You are color " << color << ".  Your goal is to make one of the"
       << " following shapes\nsomewhere ont the board with your pieces:\n";

  for (int i = 0; i < num_win_boards; i++) {
    ASSERT (win_boards[i] != NULL);
    win_boards[i]->Print();
  }
  cout << "-------------------------------------------------------------------\n\n";
}

///////////////////////////////////////////////////////////////////////////

