Skip to content

Instantly share code, notes, and snippets.

@kevincolten
Created September 5, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincolten/8174593c4f8959790dcd to your computer and use it in GitHub Desktop.
Save kevincolten/8174593c4f8959790dcd to your computer and use it in GitHub Desktop.
Checkers
var colors = require('colors/safe');
var prompt = require('prompt');
function Checker(color, position) {
this.team = color;
this.symbol = null;
if (color === 'red') {
this.symbol = colors.red(String.fromCharCode(0x12022));
} else {
this.symbol = colors.blue(String.fromCharCode(0x12022));
}
this.position = position;
// this.simpleMoves = function() {
// var availableMoves = [];
// if (this.team === 'red') {
// moves.push([ this.position[0] + 1, this.position[1] + 1 ]);
// moves.push([ this.position[0] + 1, this.position[1] - 1 ]);
// } else {
// moves.push([ this.position[0] - 1, this.position[1] + 1 ]);
// moves.push([ this.position[0] - 1, this.position[1] - 1 ]);
// }
// return availableMoves;
// }
this.movePiece = function(newPosition) {
this.position = newPosition;
}
}
function Board() {
this.checkers = [];
this.resetBoard = function() {
var bgw = colors.bgWhite(' ');
this.board = [
[' ', '1', '2', '3', '4', '5', '6', '7', '8'],
['1', bgw, ' ', bgw, ' ', bgw, ' ', bgw, ' '],
['2', ' ', bgw, ' ', bgw, ' ', bgw, ' ', bgw],
['3', bgw, ' ', bgw, ' ', bgw, ' ', bgw, ' '],
['4', ' ', bgw, ' ', bgw, ' ', bgw, ' ', bgw],
['5', bgw, ' ', bgw, ' ', bgw, ' ', bgw, ' '],
['6', ' ', bgw, ' ', bgw, ' ', bgw, ' ', bgw],
['7', bgw, ' ', bgw, ' ', bgw, ' ', bgw, ' '],
['8', ' ', bgw, ' ', bgw, ' ', bgw, ' ', bgw]
];
}
this.printBoard = function() {
for (var i = 0; i < this.board.length; i++) {
console.log(this.board[i].join(' '));
}
}
this.placePieces = function() {
for (var i = 0; i < this.checkers.length; i++) {
this.board[this.checkers[i].position[0]][this.checkers[i].position[1]] = this.checkers[i].symbol;
}
}
this.createPieces = function() {
var redSpots = [
[1, 2], [1, 4], [1, 6], [1, 8],
[2, 1], [2, 3], [2, 5], [2, 7]
];
var blueSpots = [
[7, 2], [7, 4], [7, 6], [7, 8],
[8, 1], [8, 3], [8, 5], [8, 7]
];
for (var i = 0; i < 8; i++) {
this.checkers.push(new Checker('red', redSpots[i]));
this.checkers.push(new Checker('blue', blueSpots[i]));
}
}
this.locatePiece = function(position) {
var selectedPiece;
for (var i = 0; i < this.checkers.length; i++) {
if (this.checkers[i].position[0] === position[0] && this.checkers[i].position[1] === position[1]) {
selectedPiece = this.checkers[i];
}
}
return selectedPiece;
}
}
function Game() {
this.board = new Board();
this.start = function() {
this.board.createPieces();
this.board.resetBoard();
this.board.placePieces();
this.getPrompt();
}
this.getPrompt = function() {
var that = this;
this.board.printBoard();
prompt.get(['start', 'finish', 'remove'], function (error, result) {
var piece = that.board.locatePiece([ parseInt(result.start[0]), parseInt(result.start[1]) ]);
piece.position = [ parseInt(result.finish[0]), parseInt(result.finish[1]) ]
if (result.remove !== '') {
var deadPiece = that.board.locatePiece([ parseInt(result.remove[0]), parseInt(result.remove[1]) ]);
var deadPieceIndex = that.board.checkers.indexOf(deadPiece);
that.board.checkers.splice(deadPieceIndex, 1);
}
that.board.resetBoard();
that.board.placePieces();
that.getPrompt();
});
}
}
var game = new Game();
game.start();
@draughts
Copy link

THE INTERNATIONAL CHECKERS GAME

The international checkers game is practiced on 100 boxes in most French-speaking countries (France, Belgium, Switzerland, Canada and the African continent), but also in the Netherlands and in the countries of the former Soviet Union.
You can discover the complete rules in a few minutes
Rules of the game

The purpose of the game

The game is won if:
we take all the pieces of the opponent
we block all the opponent's pieces
the opponent gives up
The game is void (or surrendered) if:
neither player can take all the opposing pieces (eg 3 against 1)
both players agree

Initial provision

The checkers game is played on a board of 10 squares out of 10 oriented with a dark box at the bottom left and at the top right.
Each player has 20 pieces placed on the first 4 rows.
Players play each in turn. Whites always start.

The movement of the pieces

The pieces move one space forward diagonally (we play only on dark squares).
A pawn who arrives on the last row and stops there is promoted lady. It is then covered with a pawn of the same color.

The lady's movement

The lady moves on the same diagonal as many boxes as she wants, forward and backward.
His movement is only limited by the other pieces on the checkerboard.

Taking by the pawn

To take with a pawn, you must:
to be placed next to an opposing pawn
jump over the opponent's piece and go to the empty space behind it
remove the popped pawn
The plug can also be done behind.
The catch is obligatory.
If, after taking a first pawn, you find yourself in a stuck position, you must continue, until this is no longer possible.
The pieces must be removed at the end of the take and not one by one as and when.
In the following example, whites must take six pieces.

Lady's

Since the lady has more room to maneuver, she also has greater possibilities for the catches. The following example should be enough to make you understand the mechanism. The lady must take any pawn on her diagonal (if there is a free space behind) and must fork whenever a new take is possible.
At the end of the take, she can stop on one of the three boxes marked with a cross.
We can only pass once on the same pawn. However, you can go twice on the same box.

The majority decision

This is the least known rule. It is essential.
When multiple shots are possible, always take the side of the largest number of pieces . This means that if you can take a lady or two pieces, you have to take both pieces. The quality of the stake or parts to be taken does not intervene. A lady is worth a pawn.
Opposite, the white pawn must take three black pawns. He is not allowed to take the two black ladies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment