#include "board.h"
CBoard::CBoard()
{
xmax = ymax = 15;
firstword = FALSE;
char tile_letters[26] = {'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'};
int numtiles[26] = {9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1};
int i;
int j;
for(i=0;i<26;i++) {
for(j=0;j<numtiles[i];j++) {
tilebag.AddHead(new CTile(tile_letters[i]));
}
}
tilebag.AddHead(new CTile());
tilebag.AddHead(new CTile());
for (i=0;i<15;i++) {
for (j=0;j<15;j++) {
squares[i][j] = NULL;
}
}
}
CObject* CBoard::GetRandomTile() {
CObject *tmp;
POSITION pos;
srand(time(NULL));
int limit = tilebag.GetCount();
int random;
if (limit == 0)
return NULL;
if (limit == 1) {
pos = tilebag.FindIndex(0);
tmp = tilebag.GetAt(pos);
tilebag.RemoveAt(pos);
return tmp;
}
random = rand()%limit;
if( ( pos = tilebag.FindIndex( random )) != NULL ) {
tmp = tilebag.GetAt(pos);
tilebag.RemoveAt(pos);
}
else {
tmp = NULL;
}
return tmp;
}
CObject* CBoard::GetBoardTile(int x, int y) {
return squares[x][y];
}
void CBoard::AddTile(CObject *tile) {
tilebag.AddTail(tile);
}
void CBoard::AddTile(CObject *tile, int x, int y) {
squares[x][y] = tile;
}
bool CBoard::IsEmpty(int x, int y) {
if (x < 0 || x >= xmax || y < 0 || y >= ymax)
return TRUE;
if (squares[x][y] == NULL) {
return TRUE;
}
else {
return FALSE;
}
}
int CBoard::GetScore(int x, int y) {
CObject *object;
CTile *tile;
if (x < 0 || x >= xmax || y < 0 || y >= ymax)
return 0;
if (squares[x][y] == NULL) {
return 0;
}
else {
object = squares[x][y];
tile = (CTile *)object;
return tile->GetValue();
}
}
CBoard::~CBoard()
{
CObject *object;
CTile *tile;
POSITION pos;
int i,j;
for( pos = tilebag.GetHeadPosition(); pos != NULL; ) {
object = tilebag.GetNext(pos);
tile = (CTile*) object;
delete tile;
}
for (i=0;i<15;i++) {
for (j=0;j<15;j++) {
object = squares[i][j] = NULL;
tile = (CTile*) object;
delete tile;
}
}
}
#ifndef BOARD_HEADER
#define BOARD_HEADER
#include "tile.h"
#include <afxcoll.h>
class CBoard {
public:
bool firstword;
CBoard();
CObject* GetRandomTile();
CObject* GetBoardTile(int x, int y);
void AddTile(CObject *tile);
void AddTile(CObject *tile, int x, int y);
bool IsEmpty(int x, int y);
int GetScore(int x, int y);
~CBoard ();
private:
int xmax;
int ymax;
CObject *squares[15][15];
CObList tilebag;
};
#endif