#include <string>
using namespace std;

#include "randgen.h"
#include "boggleboard.h"

static const char DEFAULT_CHAR = '@';     // scope limited to this file

BoggleBoard::BoggleBoard(int size)
    : myBoard(size,size,DEFAULT_CHAR)
// post: board constructed, filled with characters    
{
    init();
}

BoggleBoard::~BoggleBoard()
// post: nothing
{
    // implemented because C++ wants a destructor
    // in any inheritable class (virtual function exists)
}

int BoggleBoard::size() const
// post: returns size of board
{
    return myBoard.numrows();
}

char BoggleBoard::charAt(int row, int col) const
// pre: row and col are in range [0..size)
//      where size is constructor parameter
// post: returns character at position [row][col]
// exception: returns non-alphabetic character if row/col out of range    
    
{
    if (0 <= row && row < myBoard.numrows() &&
	0 <= col && col < myBoard.numcols())
    {
	return myBoard[row][col];
    }
    return DEFAULT_CHAR;
}

void BoggleBoard::print(ostream& out) const
// post: board printed to out, one row per line    
{
    int j,k;
    for(j=0; j < myBoard.numrows(); j++)
    {
	for(k=0; k < myBoard.numcols(); k++)
	{
	    out << charAt(j,k) << " ";
	}
	out << endl;
    }
}

void BoggleBoard::init()
// post: board initialized with letters
{
    static string vowels=  "aeiouy";           // allocate once, not per-call
    static string commons= "tnshrdlm";         // most common consonants
    static string others=  "bcfgjkpqvwxz";     // least common consonants

    RandGen gen;
    int j,k;
    
    string choice;
    for(j=0; j < myBoard.numrows(); j++)
    {
	for(k=0; k < myBoard.numcols(); k++)
        {
	    choice = others;                // P = 1/6 for other characters
            int pick = gen.RandInt(1,6);    // P = 1/2 for vowel
            if (pick <= 3)
            {
		choice = vowels;
            }
            else if (pick <= 5)             // P = 1/3 for common letter
            {
		choice = commons;
            }
            myBoard[j][k] = choice[gen.RandInt(0,choice.length()-1)];
         }
     }
}
