#include "board.h"
#include <iostream>
using namespace std;

Board::Board()
{
   for (auto& row : fields_)
   {
      for (auto& field : row)
      {
         field = State::empty;
      }
   }
}

void Board::print() const
{
   for (const auto& row : fields_)
   {
      for (const auto field : row)
      {
         switch (field)
         {
            using enum State;
         case empty:
            cout << "_ ";
            break;
         case white:
            cout << "O ";
            break;
         case black:
            cout << "X ";
            break;
         }
      }
      cout << '\n';
   }
   cout << '\n';
}

bool Board::isFree(int row, int col) const
{
   return fields_[row][col] == State::empty;
}

void Board::set(const Move& move)
{
   int row = move.getRow();
   int col = move.getCol();
   Color color = move.getColor();
   State state = color == Color::white ? State::white : State::black;
   fields_[col][row] = state;
}

bool Board::hasWin(Color color) const
{
   State s = color == Color::white ? State::white : State::black;
   const auto& f = fields_;
   if (f[0][0] == s && f[0][1] == s && f[0][2] == s) return true;
   if (f[1][0] == s && f[1][1] == s && f[1][2] == s) return true;
   if (f[2][0] == s && f[2][1] == s && f[2][2] == s) return true;
   if (f[0][0] == s && f[1][0] == s && f[2][0] == s) return true;
   if (f[0][1] == s && f[1][1] == s && f[2][1] == s) return true;
   if (f[0][2] == s && f[1][2] == s && f[2][2] == s) return true;
   if (f[0][0] == s && f[1][1] == s && f[2][2] == s) return true;
   if (f[0][2] == s && f[1][1] == s && f[2][0] == s) return true;
   return false;
}

bool Board::isFull() const
{
   for (const auto& row : fields_)
   {
      for (const auto field : row)
      {
         if (field == State::empty) return false;
      }
   }
   return true;
}
