#include "olok.h"
Olok::Olok()
{
    clear();
}
void Olok::clear()
{
    // clear the grid to be rebuilt as empty
    grid.clear();
    // make 11 boxes for each colored row (not the white ones)
    for(int j = 0; j<white1; j++)
    {
        RowColor tempRow;
        tempRow.color = j;
        tempRow.direction = j < (white1/2) ? RIGHT_TO_LEFT : LEFT_TO_RIGHT;
        // make 11 numbers for each row based on direction
        for (int i = 0; i<CHECKBOXES; i++)
        {
            tempRow.number.push_back(RIGHT_TO_LEFT == tempRow.direction ? (DICE_SUM_MIN+i) : (DICE_SUM_MAX+i));
        }
        grid.push_back(tempRow);  
        for (int i = 0; i<CHECKBOXES; i++)
        {
            grid[j].row.push_back(false);
        }
    }
    // clear penalty count 
    penaltyCount = 0;
}

// get checked off box count for a specific color
int Olok::getXCount(int color)
{
    for(int i = 0; i < grid.size(); i++)
    {
        if(color != grid[i].color){continue;}
        return grid[i].xCount;
    }
    return 0;
}

// add checked off box to a specific index on a color
void Olok::addX(struct Move move)
{
    int color = move.color, index = move.index;
    for(int i = 0; i < grid.size(); i++)
    {
        if(color != grid[i].color){continue;}
        grid[i].row[index] = true;
        grid[i].lastIndex = index;
    }

}
// check for last value of index
int Olok::getLastIndex(int color)
{
    for(int i = 0; i < grid.size(); i++)
    {
        if(color != grid[i].color){continue;}
        return grid[i].lastIndex;
    }
    return CHECKBOXES;
}

// add any Xs from input Olok into this Olok
void Olok::addOlok(Olok o)
{
    // find changes in currentTurnOlok and transfer to cumulativeOlok
    for(int j = 0;j <o.grid.size();j++)
    {
        // loop over all Xs in a color
        for(int k = 0;k <o.grid[j].row.size();k++)
        {
            // check for true values in currentTurnOlok's grid
            if(true ==o.grid[j].row[k])
            {
                // transfer true values from currentTurnOlok's grid to cumulativeOlok's grid
                grid[j].row[k] = true;
            }
        }
    }
    // player selects to add penalty, increase the penalty count
    penaltyCount += o.penaltyCount;
}

std::string Olok::toString(void) 
{
    std::string output;
    // check each row on the grid on the scoresheet
    for(int j = 0; j < grid.size(); j++)
    {
        // check each box on the row and then store output
        for(int k = 0; k < grid[j].row.size();k++)
        {
            // look at checkbox value, add it to output
            output.push_back(grid[j].row[k] ? 'T' : 'F');
        }
    }
    // add penalty count to output
    output.push_back(penaltyCount + '0');
    return output;
}

// check to see if color matches one of the RowColor(s) on grid
boolean Olok::isColor(int color)
{
    for(int i = 0; i < grid.size(); i++)
    {
        if(color != grid[i].color){continue;}
        return true;
    }
    return false;
}