Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkhayes committed Oct 20, 2014
2 parents 33ca66c + 62a6f91 commit f99e9b7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 74 deletions.
36 changes: 15 additions & 21 deletions Connect4/js/bluePlayer.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
players.blue = {
move: function(callback) {
var bestMove;
var bestValue = -Infinity;

move: function(cb) {
for (var move = 0; move < 7; move++) {
var withMove = makeMove(-1, move);
if (withMove) { // An illegal move will return `null` for the new board.
var value = evaluateBoard(withMove, -1)
if (value > bestValue) {
bestValue = value;
bestMove = move;
}
}
}
setTimeout(function() {
var rand = Math.floor(Math.random()*16);
if (rand === 0) return cb (0)
if (rand === 1) return cb (1)
if (rand === 2) return cb (1)
if (rand === 3) return cb (2)
if (rand === 4) return cb (2)
if (rand === 5) return cb (2)
if (rand === 6) return cb (3)
if (rand === 7) return cb (3)
if (rand === 8) return cb (3)
if (rand === 9) return cb (3)
if (rand === 10) return cb (4)
if (rand === 11) return cb (4)
if (rand === 12) return cb (4)
if (rand === 13) return cb (5)
if (rand === 14) return cb (5)
if (rand === 15) return cb (6)

}, Math.random()*1000);
callback(bestMove);
}, 500);
}

}
88 changes: 44 additions & 44 deletions Connect4/js/redPlayer (solution).js
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,76 @@
If you want to try playing as the blue player, simply paste your algorithm into that file.
Functions available (in the global scope):
-- copyBoard(board):
Returns a copy of the board provided.
If no board is provided, copies the current game board.
-- makeMove(player, column, board):
Makes a move for the given player at the given column on the provided, and returns the board.
Copies the provided board, then makes a move at the specified column for the given player.
If no board is provided, makes the move on a copy of the current game board.
If the move is illegal, returns "NULL"
If the move is illegal, returns "NULL" (be sure to check for this!).
Remember, red is represented by 1, and blue is represented by -1.
-- evaluateBoard(board, player):
Returns a score expressing how good the provided board is for given player.
Remember, red is represented by 1, and blue is represented by -1.
-- printBoard(board):
Prints a copy of the board provided to the console.
If no board is provided, prints the current game board.
For debugging purposes only.
*/


players.red = {

move: function(callback) {
var depth = 5;
var depth = 4;

// Start with no best move, and a best value as low as possible.
var bestMove;
var bestScore = -Infinity;

var bestMove = 3;
var bestValue = -10000000;
// For each of the seven possible moves...
for (var move = 0; move < 7; move++) {
var copy = copyBoard();
var copyWithMove = makeMove(1, move, copy);
if (copyWithMove) {
var value = minimax(copyWithMove, depth, -1)
if (value > bestValue) {
bestValue = value;

var withMove = makeMove(1, move);
if (withMove) { // An illegal move will return `null` for the new board.
var score = deepEval(withMove, 1, depth);

// If it's the best we've seen so far, remember it.
if (score > bestScore) {
bestMove = move;
bestScore = score;
}
}
}

// Return the best move we've seen.
return callback(bestMove);
}

};

var minimax = function minimax(board, depth, currentPlayer) {
if (depth === 0) {
return evaluateBoard(board, currentPlayer);
}
if (currentPlayer === 1) {
var bestValue = -1000000;
for (var move = 0; move < 7; move++) {
var copy = copyBoard(board);
var copyWithMove = makeMove(currentPlayer, move, copy);
if (copyWithMove) {
var value = minimax(copyWithMove, depth - 1, currentPlayer * -1)
bestValue = Math.max(value, bestValue);
}
}
return bestValue;
}
else {
bestValue = 1000000;
for (var move = 0; move < 7; move++) {
var copy = copyBoard(board);
var copyWithMove = makeMove(currentPlayer, move, copy);
if (copyWithMove) {
var value = minimax(copyWithMove, depth - 1, currentPlayer * -1)
bestValue = Math.min(value, bestValue);
}
}
return bestValue;
var deepEval = function(board, player, depth) {
if (depth === 0) {
return evaluateBoard(board, player);
}

var bestScore = -Infinity;

for (var move = 0; move < 7; move++) {
// For each move your *opponent* could make...
var withMove = makeMove(-1*player, move, board);

// If it's legal...
if (withMove) {
// The score for YOU is -1 times the score for them
var score = -1 * deepEval(withMove, -1*player, depth - 1);

// Remember the best score.
if (score > bestScore) {
bestScore = score;
}
}
}
return bestScore;
}


Expand Down
35 changes: 26 additions & 9 deletions Connect4/js/redPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,49 @@
If you want to try playing as the blue player, simply paste your algorithm into that file.
Functions available (in the global scope):
-- copyBoard(board):
Returns a copy of the board provided.
If no board is provided, copies the current game board.
-- makeMove(player, column, board):
Makes a move for the given player at the given column on the provided, and returns the board.
Copies the provided board, then makes a move at the specified column for the given player.
If no board is provided, makes the move on a copy of the current game board.
If the move is illegal, returns "NULL"
If the move is illegal, returns "NULL" (be sure to check for this!).
Remember, red is represented by 1, and blue is represented by -1.
-- evaluateBoard(board, player):
Returns a score expressing how good the provided board is for given player.
Remember, red is represented by 1, and blue is represented by -1.
-- printBoard(board):
Prints a copy of the board provided to the console.
If no board is provided, prints the current game board.
For debugging purposes only.
*/


players.red = {

move: function(callback) {
var bestMove = 3;
var depth = 1;

// Start with no best move, and a best value as low as possible.
var bestMove;
var bestScore = -Infinity;

// For each of the seven possible moves...
for (var move = 0; move < 7; move++) {

// Whee! Randomly pick a score for the move
var score = Math.random();

// If it's the best we've seen so far, remember it.
if (score > bestScore) {
bestMove = move;
bestScore = score;
}
}

// Return the best move we've seen.
return callback(bestMove);
}

};


Expand Down

0 comments on commit f99e9b7

Please sign in to comment.