//Name: David Yang, Sean McMillan, Dominic Golab
//File: Bank.cpp
//For: CSE 30331: Scrabble Gmae
//12/6/09
/*Description: This file contains the functionality of the bank class.
The bank class holds the tiles for an individual player. It has an array of tiles that it uses as its bank. It also stores the score which it can add up and save.*/


#include <iostream>
#include<string>
#include "Bank.h"

using namespace std;

//constructor
Bank :: Bank(){
  score =0;

}


//draws letters from Letters
// '/' is for no letter in that spot in the bank
void Bank :: draw(Bag &bag){

  for(int i=0;i<7;i++){//for each spot in the bank
    if(letters[i].getLetter()=='/' && bag.isEmpty()==false){//if that spot is empty and there are tiles in the bank
	Tile temp = bag.draw();//draw tile
    letters[i].setLetter(temp.getLetter(),temp.getScore());/set bank spot to have same values as the drawn tile
    }
    else if(letters[i].getLetter()=='/'){//if empty
      letters[i].setLetter('/',0);//keep it empty
    }
  }

}

//exchanges tiles with the bag
void Bank :: exchange(Bag &bag, string temp){
	//checks off which letters are to be exchanged and exchanges them with the bag
  for(int i=0; i<temp.size(); i++)
    {
      for(int j=0; j<7; j++)
	{
	  if(temp[i]==letters[j].getLetter())
	    {
	      bag.exchange(letters[j]);//put letter in the bag
	      letters[j].setLetter('/',0);//sets that spot in the bank to empty
	      break;
	    }
	}
    }  
  draw(bag);//fillst eh bag back up

}

//Returns if the word can be put down based on constraints on the bank
bool Bank :: place(int rStart, int rEnd, int cStart, int cEnd, string word){
  bool exists;
  int count=0;
  int used[7]={0,0,0,0,0,0,0};

  //make sure the player has all the correct letters for the word
  for(int i=0;i<(int)word.size();i++){
    for(int j=0;j<7;j++)
	{
      if(word[i]==letters[j].getLetter())
	  {
		if(used[j]==0)//if the letter has been used then count won't go up
		{
			used[j]=1;//checks off which spot in the bank will be used for the word
			count++;//counts how many letters used
			break;
		}
      }
	  else if(word[i]==' ')//if the letter is already on the board
	  {
		  count++;//add to how many letters fulfilled
		  break;
	  }
/*	  else if(letters[j].getLetter()=='+')
	  {
		cout<< "match"<<endl;
		used[j]=1;
		cout<< "count " <<endl;
		count++;
		break;
	  }*/
    }
  }

	if(count!=word.size())//if we do not have all the letters necessary look to see if we have any wild card tiles
	{
		int shortHowMuch=word.size()-count;//finds out how many we are short
		for(int j=0; j<shortHowMuch; j++)//for each letter we are short
		{
		for(int i=0; i<7; i++)//goes through the bank to see if there are any wild cards
		{
			if(letters[i].getLetter()=='+')
			{
				if(used[i]==0)//if tile has not been used
				{
				used[i]=1;//mark as used
				count++;//add to the count
				break;
				}
			}
		}
		}
	}
  //if count equals the size of the given word, that means the bank has all the proper letters
  if(count==word.size()){
    exists=true;
  }
  else{
    exists=false;
  }
 
  if(exists==true)//if have all the letters
  {
	  for(int i=0; i<7; i++)
	  {
		  if(used[i]==1)//sets teh letters used to now be empty
		  {
			  letters[i].setLetter('/',0);
		  }
	  }
    //replaces the letters used with '/'
  /*
    for(int i=0;i<(int)word.size();i++)
	{
      for(int j=0;j<7;j++)
	  {
		if(word[i]==letters[j].getLetter())
		{
			letters[j].setLetter('/',0);
			break;
		}
      }
    }*/
  }



  else{//if not enough letters
    mvprintw(29,16,"You cannot make that word");//prints out error and refreshes the screen
    wrefresh(stdscr);
  }

  return exists;//returns if a word can be placed or not
}


//displays the contents of the bank (letter and value)
void Bank :: displayBank(){
  bank = subwin(stdscr,5,12,12,75);
  box(bank,'|','-');
  mvwprintw(bank,1,1,"Your Bank");

  for(int i=0;i<7;i++){
    mvwaddch(bank,2,i+2,letters[i].getLetter());
  }
  for(int i=0;i<7;i++){
    mvwprintw(bank,3,i+2,"%d",letters[i].getScore());
  }
  wrefresh(bank);
}


//adds score to players total
void Bank :: addScore(int points){
  score  = score+points;
}

//return the score held for the bank
int Bank :: Score(){

  return score;
  
}

//returns if the bank is empty
bool Bank :: empty(){

  bool empty=true;
//if there is a proper tile in the bank, the bank is not empty
  for(int i=0;i<7;i++){
    if(letters[i].getLetter()!='/'){
      empty=false;
    }
  }

  return empty;
}
