#include "game.h"
Game::Game() {
    // set up lockOut
    for(int i = 0; i<white1;i++){
        lockOut.push_back(false);
    }
    // set the state of game [how to move from state to state ("game logic?")]
    state = NOT_STARTED;
    // dice have been initialized with random seed
};
bool Game::addPlayer(std::string name){
    if(NOT_STARTED == state){
        PlayerCard newplayer(name);
        players.push_back(newplayer);
        fprintf (stdout, "added: %s to the game\n", name.c_str());
        return true;
    }
    else{
        fprintf(stdout, "player %s was not added to the game\n", name.c_str());
        return false;
    }
};
void Game::score()
{
    // **CALCULATE EACH PLAYERS' SCORE**
    std::vector<int> pointsGuide = {0,1,3,6,10,15,21,28,36,45,55,66,78};
    // check players' card for marked boxes
    for(int i = 0;i <players.size();i++)
    {
        // clear each players' previous score
        players[i].score = 0;
        // calculate penalty amount
        players[i].score -= players[i].penaltyCount*PENALTY_VALUE;
        // check each color(row) on playerCard
        for(int j = 0;j <white1;j++)
        {
            int count = 0;
            // check each box in row
            for(int k =0;k <CHECKBOXES;k++)
            {
                if(true == players[i].rowColors[j][k])
                {
                    count++;
                }
            }
            players[i].score += pointsGuide[count];
        }
    }
    // **CHECK IF GAME IS DONE**
    // check each player's card for penalties
    for(int i = 0;i<players.size();i++)
    {
        // check for maximum penalty to see if game is over
        if(players[i].penaltyCount == MAX_PENALTY){state = FINISHED;}      
    }
    // check for lockout to see if game is over
    int lockCount = 0;
    for(int i = 0;i<lockOut.size();i++)
    {
        if(true == lockOut[i]){lockCount++;}
    }
    if(MAX_LOCKOUT <= lockCount)
    {
        state = FINISHED;
    }
}
void Game::turn(PlayerCard activePlayer)
{
    // start all players' turns
    for(int i = 0; i <players.size();i++)
    {
        players[i].isTurnDone = false;
    }
    // roll the dice
    dice.roll();
    // check to see if all players are done with turn
    while(true)
    {
        bool temp = true;
        for(int i = 0; i <players.size();i++)
        {
            temp = temp && players[i].isTurnDone;
        }
        if(temp){break;}
        Sleep(1);
    }
    // add selection to players' cards
    for(int i = 0; i <players.size();i++)
    {
        int t;
        // everyone can select a color and a check box
        if(-1<(t = players[i].selection[everyone][row]))
        {
            int color = players[i].selection[everyone][row];
            int checkbox = players[i].selection[everyone][column];
            players[i].rowColors[color][checkbox] = true;
        }
        // active player can select a color and check box
        if(-1<(t = players[i].selection[active][row]))
        {
            int color = players[i].selection[active][row];
            int checkbox = players[i].selection[active][column];
            players[i].rowColors[color][checkbox] = true;
        }
        // player selects to add penalty, increase the penalty count
        if(0<(t = players[i].selection[penalty][0]))
        {
            players[i].penaltyCount++;
        }
    }
    // score the cards
    score();
}

// generate text: per player(48boxes, penalty count), locked off rows, dice roll, game end(when it occurs) send it
std::string Game::send()
{
    std::string output;
    if(dice.dice.size()==0){fprintf(stdout, "kyle is cool\n");}
    // add dice values to output
    for(int i = 0; i < dice.dice.size(); i++)
    {
        output.push_back(dice.dice[i] + '0');
    }
    // look at each player card 
    for(int i = 0; i < players.size(); i++)
    {
        // check each row on the player's card
        for(int j = 0; j < white1; j++)
        {
            // check each box on the player card and then store output
            for(int k = 0; k < CHECKBOXES;k++)
            {
                // look at checkbox value, add it to output
                output.push_back(players[i].rowColors[j][k] ? 'T' : 'F');
            }
        }
        // add penalty count to output
        output.push_back(players[i].penaltyCount + '0');
    }
    // add locked off row to output
    for(int i = 0; i < lockOut.size();i++)
    {
        output.push_back(lockOut[i] ? 'T' : 'F');
    }
    // add game end to output
    output.push_back(state == FINISHED ? 'T' : 'F');
    // print everything needed to send current game state to players
    fprintf(stdout, "the output is: %s\n", output.c_str());
    return output;
}

// kyle's intention is to check off red 12 and blue 5
// " 1143"
// emma's intention is to take a penalty
// "1"
void Game::receive(std::string userInput)
{
    players[0].newSelection();
    // penalties are marked to card if user string = 1
    players[0].selection[penalty][0] = 1;
    // did the user send a number?
    // if a user did not send a number no changes take place
    // take the userinput (string) and parse it into parts
    // what is the length of the userInput? length of 2, 4? (boxes 0-9 are represented as 00,01,02...)
    players[0].selection[everyone][row] = green;
    players[0].selection[everyone][column] = 5;
    players[0].selection[active][row] = yellow;
    players[0].selection[active][column] = 11;
    // convert userInput into number
    // user number to change the boolean on corresponding spot on playerCard
}