const $ = require('jquery');
const board = require('./board');
const Piece = require('./piece');

function Game(players, board) {
    this.players = players;
    this.turn = 0;
    this.winner = null;
    this.over = false;
    this.board = board;
}

Game.prototype.start = function(){
    welcomePlayers(this.players);
    placeFirstFourPieces(this.board);
};

Game.prototype.respondToClick = function(x, y) {
    console.log(x, y);
    var square = this.board.squares[0];
    square.placePiece('white');
}

function placeFirstFourPieces(board) {
    var square0 = board.squares[27];
    var square1 = board.squares[28];
    var square2 = board.squares[36];
    var square3 = board.squares[35];

    var squares = [square0, square1, square2, square3];

    squares.forEach(function(square,index){
        if (index % 2 == 0){
            square.placePiece('white');
            //square.piece = new Piece('white', square);
        } else {
            square.placePiece('black');
            //square.piece = new Piece('black', square);
        }
    });
}

function welcomePlayers(players) {
    var first = players[0];
    var second = players[1];
    $('#title').prepend(`<h1>Welcome ${first.name}(${first.color}) and ${second.name}(${second.color})!</h1>`)
}

module.exports = Game;

const $ = require('jquery');
const board = require('./board');
const Piece = require('./piece');

function Game(players, board) {
    this.players = players;
    this.turn = 0;
    this.winner = null;
    this.over = false;
    this.board = board;
}

Game.prototype.start = function(){
    welcomePlayers(this.players);
    placeFirstFourPieces(this.board);
};

Game.prototype.respondToClick = function(x, y) {
    var selectedSquare = this.board.squares.filter(function(square){
        console.log(square);
        console.log(x,y);
        var xMatch = (x >= square.x) && (x <= square.x + square.width);
        var yMatch = (y >= square.y) && (y <= square.y + square.width);
        return xMatch && yMatch;
    });
    console.log(selectedSquare);
    selectedSquare[0].placePiece('white');
}

function placeFirstFourPieces(board) {
    var square0 = board.squares[27];
    var square1 = board.squares[28];
    var square2 = board.squares[36];
    var square3 = board.squares[35];

    var squares = [square0, square1, square2, square3];

    squares.forEach(function(square,index){
        if (index % 2 == 0){
            square.placePiece('white');
            //square.piece = new Piece('white', square);
        } else {
            square.placePiece('black');
            //square.piece = new Piece('black', square);
        }
    });
}

function welcomePlayers(players) {
    var first = players[0];
    var second = players[1];
    $('#title').prepend(`<h1>Welcome ${first.name}(${first.color}) and ${second.name}(${second.color})!</h1>`)
}

module.exports = Game;

Add xy coordinates to the placePiece function