#include <iostream.h>
#include <string.h>

///////////////////////////////////////////////////////////////////////////
//
// This program simulates "Scrabble" on a small board of size 5x5.
// The letter values and number of tiles of each letter may need
//		adjustment!
// Simulating the remaining features of Scrabble -- real-size board,
//		bonus scores for certain board positions, 2/4 player mode,
//      7 letters per turn, etc. should not be hard to add.
//
///////////////////////////////////////////////////////////////////////////


enum BOOL {FALSE = 0, TRUE = 1};	// Useful!

enum DIRECTION {DOWN=0, ACROSS=1};  // This is useful in Scrabble.

// The following 26 constants define the values of the 26
//   Scrabble letters.

const int A=1,B=2,C=2,D=2,E=1,F=4,G=4,H=4,I=1,J=5,K=5,L=1,M=1,N=1,
		  O=1,P=2,Q=10,R=1,S=1,T=1,U=1,V=2,W=4,X=8,Y=2,Z=10;

// The following 26 constants define the number of tiles of each
//   of the 26 Scrabble letters.

const int AA=9,BB=2,CC=2,DD=4,EE=12,FF=2,GG=2,HH=2,II=8,JJ=1,KK=1,
		  LL=2,MM=4,NN=6,OO=8,PP=2,QQ=1,RR=6,SS=6,TT=6,UU=4,VV=2,
		  WW=2,XX=1,YY=2,ZZ=1;


// The following function gets the candidate word in w. It stores only
//  the letters of the alphabet and ignores everything else. The letters
//  are converted to lower case (a to z) before they are stored.

int  getInput(char w[]);
const int Values[] = {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,
					U,V,W,X,Y,Z};
const int Numbers[] = {AA,BB,CC,DD,EE,FF,GG,HH,II,JJ,KK,LL,MM,
					 NN,OO,PP,QQ,RR,SS,TT,UU,VV,WW,XX,YY,ZZ};
class Scrabble {
private:
	char Board[5][5];	// The Mini-Board for Scrabble.
	char Word[6];		// The word to be placed on the board.
	int x,y;			// Location where the word is to be placed.
	DIRECTION Direction;// The direction in which the word is placed.
	int Score;			// The total score of the only player.

public:
	void Initialize() {
		x = y = Score = 0;
		Direction = ACROSS;
		for(int i=0; i<6; i++)
			for(int j=0; j<6; j++)
				Board[i][j] = '\0';		// Fill with null chars.
	}

	void Display() {

		cout << endl;
		cout << "--------------" << endl;
		cout << "|            |" << endl;
		for(int i=0; i<5; i++) {
			cout << "| ";
			for(int j=0; j<5; j++)
				cout << " " << Board[i][j];
			cout << " |" << endl;
		}
		cout << "|            |" << endl;
		cout << "--------------" << endl;
		cout << "Total Score: " << Score << endl;
	}

	BOOL PlaceWord() {
		int i = 0;
		do {
			if(Board[x][y] == '\0') {   // Place letter only on an empty space.
				Board[x][y] = Word[i];  // Place word one char at a time.
				i++;					// Next char in word.
			}
			if(Direction == ACROSS)
				y++;				// If word is across, increment y.
			else
				x++;				// If word is down, increment x.
		}
		while (x<5 && y<5);	// Check if if board's edge is reached.

	// This return value may be useful later to tell whether word was
	//   successfully placed or not. We don't use it at this point.
		return TRUE;
	}

	int GetWord() {
		char tempWord[10];	// The keys typed by user are stored here.
		int i = 0, j= 0;	// Indices to traverse thru arrays	.

		for(i=0;i<6;i++)
			Word[i]='\0';
		i=0;
		cout << "Please enter a phrase:" << endl;
		cin >> tempWord;

		// While end of word not reached and size not exceeded
		while(tempWord[i] != '\0' && j<5){

		// Clear the fifth bit, i.e. convert to upper case letter.
			tempWord[i] &= 223;

			if(tempWord[i] >= 'A' && tempWord[i] <= 'Z'){
				Word[j] = tempWord[i];
				j++;
			}
			i++;
		}
		Word[j+1] = '\0';	// Store the null character at the end.
		return j;			// Return the number of letters (less
							//  the null character) in w.
	}

	void GetPlacement() {
		int ax,ay,dir;
		do {
			cout << "Please give the x and y locations of the above word:"<<endl;
			cin  >> ax >> ay;
		}
		while (ax<1 || ay<1 || ax>5 || ay>5);
		x = ax-1;
		y = ay-1;		// x and y have array indices in them,
						//   ready to be used by other functions.
		do {
			cout << "Please enter the direction: 0=Down, 1=Across"<<endl;
			cin  >> dir;
		}
		while(dir<0 || dir>1);
		Direction = dir==0 ? DOWN : ACROSS;
	}

	int CalculateScore() {
		int sc=0;
		int i=0;
		int ch;
		while(Word[i] !='\0' && i<6) {
			ch = (int)(Word[i] - 'A');
			sc = sc + Values[ch];
			i++;
		}
		return sc;
	}

	void AddScore(int sc) {
		Score += sc;
	}

	void Run() {
		int s;
		Initialize();
		while(TRUE) {
			Display();
			GetWord();
			GetPlacement();
			PlaceWord();
			s = CalculateScore();
			AddScore(s);
		}
	}

};

// The minimal main function.

void main() {
	Scrabble scrabble;
	scrabble.Run();
}


