#include "queens.hh"
#include <cstdlib>
#include <iostream>
#include <vector>
#include <random>

using namespace queens;

Configuration::Configuration(int m, int n, int number_of_pieces) {
    this->number_of_pieces = number_of_pieces;
    this->board = new Board;

    this->board->m = m; //x direction
    this->board->n = n; //y direction

    //get a random number generator:
    //the random device that will seed the generator
    std::random_device seeder;
    //then make a mersenne twister engine
    std::mt19937 engine(seeder());
    //then the easy part... the distribution
    std::uniform_int_distribution<int> m_rand(1, m);
    std::uniform_int_distribution<int> n_rand(1, n);

    for (int i=0; i<number_of_pieces; i++)
    {
        this->pieces.push_back(Queen(m_rand(engine), n_rand(engine)));
    }
}

std::vector<Conflict> Configuration::get_conflicts()
{
    std::vector<Conflict> conflicts;
    for (int i = 0; i<this->number_of_pieces; i++)
    {

        // The piece we will check against the others:
        Piece* piece = &this->pieces[i];
        // For every new i we need to check one fewer i
        for (int j = i+1; j<number_of_pieces; j++)
        {
            Piece* enemy_piece = &this->pieces[j];
            if (piece->x == enemy_piece->x or
                piece->y == enemy_piece->y or
                piece->x - enemy_piece->x == piece->y - enemy_piece->x)
            {
                Conflict conflict;
                conflict.piece_1 = i;
                conflict.piece_1_x = piece->x;
                conflict.piece_1_y = piece->y;
                conflict.piece_2 = j;
                conflict.piece_2_x = enemy_piece->x;
                conflict.piece_2_y = enemy_piece->y;
                conflicts.push_back(conflict);
            }
        }
    }
    return conflicts;
}

Piece::Piece(int x, int y)
{
    this->x = x;
    this->y = y;
}