So I decided that the best way for me to learn to program with JS and jQuery was to try and build something that I liked. So I decided to make my own Chess board using minimal HTML and Mostly JS and Jquery.
The code below renders a board and inserts images on the starting positions of the tiles.
$(document).ready(function(){
//draw the chess board
var amount = 0;
var columnColor = "Black";
while(amount < 64){
if(amount % 9 === 0){
amount++
}
else if (amount < 18){
$(".mainWrapper").append('<div class="block'+columnColor+'"><img class="pawn" src="images/whitePawn.png" alt="White Pawn" height="42" width="42"></div>')
amount++
}
else if (amount > 45){
$(".mainWrapper").append('<div class="block'+columnColor+'"><img class="pawn" src="images/blackPawn.png" alt="White Pawn" height="42" width="42"></div>')
amount++
}
else{
$(".mainWrapper").append('<div class="block'+columnColor+'"></div>')
amount++
}
switch (columnColor){
case "White":
columnColor = "Black";
break;
case "Black":
columnColor = "White";
break;
}
};
});
console.log("I ran! :D");
My next step would be to make the pieces be able to move. Now at first I figured I would do this by using jQuery his .draggable
method. Which works (sort of) but doesn't do what I want it to since it doesn't move them in a clean way (pieces can overlap squares) and it doesn't limit the amount of squares a user can move.
So a quick google search later and I found THIS post on Stackoverflow. There they described a way to use appendTo
to move items to another parent div. This would be perfect (at least I think) for my situation.
Although since my understanding of JS is still limited I cant wrap my head around how to do that.
I understand that you would need some kind of selector class for a selected image. Then there needs to be a click
event that moves the selected image to the div you clicked on.
Just for your entertainment I couldn't surpass THIS.
Any help or hints are appreciated!