//This is my Scrabble Engine Program, commented for your pleasure...

#include "scr_LocalServer.h"

//Global Variables:

//probably most of you are going to end up with a dynamic initialization, so I guess I'll dothat
scr_LocalServer* TheGame;
int howmany;


void AnnounceNumPlayers(int total){
 cout<<"There are "<<total<<" players.\n";
 howmany=total;
}

void PrintFinalScores(int* scores){
char a;
 for(a=0;a<howmany;a++){
  cout<<"Player "<<a<<" received "<<scores[a]<<" points.\n";
 }
}

void AcquireHumanDecision(){
 int x,y,dx,works;
 int BSIZE=TheGame->BoardSize();//cache the value of the Board's dimensions
 char **representation;
 char input[20],*tiles = new char[TheGame->GetNumberTilesInRack()];
 scr_Move* TheMove=0;
  //This whole do...while loop basically is the interface for a human player
  //with a full implementation including computer players, this will become a mere subsection of the loop
  //instead of the whole loop
  representation = new char*[BSIZE];
  for(x=0;x<BSIZE;x++) representation[x] = new char[BSIZE];
  //Board output code...hopefully someone will get something graphical going
  TheGame->GetCopyOfBoard(representation);
  cout<<"   ";
  for(x=0;x<BSIZE; x++) cout<<(int)x<<' ';
  cout<<'\n';
  for(y=0;y<BSIZE;y++){
   cout<<(int)y<<"| ";
   for(x=0;x<BSIZE;x++) cout<<representation[x][y]<<' '; //we can do this because the board is already loaded with the characters
   cout<<'\n';
  }
  
  //Just a friendly reminder
  cout<<"There are "<<TheGame->HowManyInBag()<<" in the bag.\n";
  cout<<"Player:"<<TheGame->GetCurPlayerNum()<<"  Score is "<<TheGame->GetCurPlayerScore()<<'\n';

  //output the player's tiles
  TheGame->CopyCurPlayerTiles(tiles);
  for(x=0;x<TheGame->GetNumberTilesInRack();x++)
   cout<<tiles[x]<<' ';
  cout<<'\n';

  //get input for move..now be nice, it doesn't check all those stupid error cases
  cout<<"Choices: (m)ove, (t)hrowback Tiles, (p)ass, or (q)uit: ";
  cin>>input;
  switch(input[0]){
   case 'q':{
    TheMove= new scr_Move(10); //can be any number-this makes a quitting move
    TheGame->SubmitMove(TheMove);
    cout<<"byebye\n";
    break;
   }
   case 'm':{
    //the format of the move: x coordinate, y coordinate, direction, string
    cout<<"Enter your move: ";
    cin>>x>>y>>dx>>input;
    TheMove= new scr_Move(x,y,dx,input);
    works=TheGame->SubmitMove(TheMove);
    if(works>-1) cout<<"\nMove was worth: "<<works<<'\n';
    else cout<<"\nINVALID MOVE\n";
    break;
   }
   //handle the player if he wants to throw back his tiles
   case 't':{
    cout<<"Please type in the tiles you want to throw out (no spaces between them):\n";
    cin>>input;
    TheMove= new scr_Move(input);
    works=TheGame->SubmitMove(TheMove);
    //now we test if the exchange went through, if f=1, all is well, but f=0, the player loses his turn
    if(works) cout<<"The deed is done.\n";
    else cout<<"There are too few tiles in the bag or you made a typo. You're out of luck buddy.\n";
    break;
   }
   default:{
    TheMove= new scr_Move();
    TheGame->SubmitMove(TheMove);
    break;
   }
  }
  delete [] tiles;
  for(x=0;x<BSIZE;x++) delete [] representation[x];
  delete [] representation;
  delete TheMove;
}

//don't worry about these four functions right now...they're not for this version
bool operator>(const scr_CoordList &op1, const scr_CoordList &op2){
 return(op1.x>op2.x);
}

bool operator<(const scr_CoordList &op1, const scr_CoordList &op2){
 return(op1.x<op2.x);
}

bool operator==(const scr_CoordList &op1, const scr_CoordList &op2){
 return(op1.x==op2.x);
}

bool operator!=(const scr_CoordList &op1, const scr_CoordList &op2){
 return(op1.x!=op2.x);
}

void main(){
int count, type;
 //All input and output is in hexadecimal
 cout.unsetf(ios::dec);
 cout.setf(ios::hex);
 cin.unsetf(ios::dec);
 cin.setf(ios::hex);

 TheGame = new scr_LocalServer();
 
 //here it is, a quick and dirty interface to the scrabble engine....
 cout<<"Be wary, for all numerical input/output is in hexadecimal!\n";
 //we don't serve computers here (yet)
 count=0;
 while(1){
  cout<<"What is the type of player is "<<count<<"?\n 0 : No more players\n 1 : Human\n";
  cin>>type;
  if(type==0) break;
  switch (type){
   case 1:{
    TheGame->CreatePlayer(HUMAN);
   	break;
   }
  }
  count++;
 }
 TheGame->RunGame();
 delete TheGame;
}
