Simple AI.
This commit is contained in:
1 parent
55b64cdcdd
commit
07ada7b421
4 files changed
+123
-14
No files matched your search
+3
-2
@@ -4,7 +4,8 @@
|
||||
<script src="utils.js"></script>
|
||||
<script src="tetris-canvas.js"></script>
|
||||
<script src="tetris.js"></script>
|
||||
<script src="tetris-control.js"></script>
|
||||
<!-- <script src="tetris-control.js"></script>-->
|
||||
<script src="tetris-ai.js"></script>
|
||||
|
||||
<style>
|
||||
#tetris-board {
|
||||
@@ -18,7 +19,7 @@
|
||||
<body>
|
||||
<div><canvas id="tetris-board" height="720" width="480" /></div>
|
||||
|
||||
<div>
|
||||
<div style="display:none;">
|
||||
<ul style>Controls:
|
||||
<li>a,d - left/right</li>
|
||||
<li>w - rotate</li>
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
var ACTION_LEFT = 1;
|
||||
var ACTION_RIGHT = 2;
|
||||
var ACTION_ROTATE = 3;
|
||||
var ACTION_DROP = 4;
|
||||
|
||||
var game;
|
||||
|
||||
function getAllPossibleDropLocations(brickId) {
|
||||
var dropLocations = [[game.width/2,game.height+1,0]];
|
||||
|
||||
// Go as far down as possible:
|
||||
var goDown = function(x, y, rot) {
|
||||
var counter = 0;
|
||||
while (game.isValidBrickLoc(brickId, [x, y - counter], rot)) {
|
||||
counter++;
|
||||
}
|
||||
dropLocations.push([x,y - counter+1,rot]);
|
||||
}
|
||||
|
||||
// For each of the four rotations:
|
||||
for (var i = 0; i < 4; i++) {
|
||||
// Try to go as far left as possible:
|
||||
var counter = 0;
|
||||
while(game.isValidBrickLoc(brickId, [game.currentBrickLoc[0]-counter,game.currentBrickLoc[1], i], i)) {
|
||||
goDown(game.currentBrickLoc[0]-counter, game.currentBrickLoc[1], i);
|
||||
counter++;
|
||||
}
|
||||
|
||||
// Try to go as far right as possible:
|
||||
var counter = 0;
|
||||
do {
|
||||
goDown(game.currentBrickLoc[0]+counter, game.currentBrickLoc[1], i);
|
||||
counter++;
|
||||
} while(game.isValidBrickLoc(brickId, [game.currentBrickLoc[0]+counter,game.currentBrickLoc[1], i], i));
|
||||
}
|
||||
|
||||
return dropLocations;
|
||||
}
|
||||
|
||||
function chooseDropLocation(brickId) {
|
||||
var dropLocations = getAllPossibleDropLocations(brickId);
|
||||
|
||||
/* Just find the lowest location possible: */
|
||||
var best = 0;
|
||||
for (var i = 0; i < dropLocations.length; i++) {
|
||||
if (dropLocations[i][1] < dropLocations[best][1]) {
|
||||
best = i;
|
||||
}
|
||||
}
|
||||
|
||||
return dropLocations[best];
|
||||
}
|
||||
|
||||
function chooseAction(dropLocation) {
|
||||
var dx = dropLocation[0] - game.currentBrickLoc[0];
|
||||
var dy = dropLocation[1] - game.currentBrickLoc[1];
|
||||
var dr = (dropLocation[2] - game.currentBrickRot + 4) % 4;
|
||||
|
||||
if (dr != 0) return [ACTION_ROTATE, dr];
|
||||
if (dx < 0) return [ACTION_LEFT, -dx];
|
||||
if (dx > 0) return [ACTION_RIGHT, dx];
|
||||
return [ACTION_DROP, -dy];
|
||||
}
|
||||
|
||||
function doAction(action) {
|
||||
if (action[1] == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (action[0] == ACTION_LEFT) {
|
||||
game.moveCurrentBrickHorizontally(-1);
|
||||
} else if (action[0] == ACTION_RIGHT) {
|
||||
game.moveCurrentBrickHorizontally(1);
|
||||
} else if (action[0] == ACTION_ROTATE) {
|
||||
game.rotateCurrentBrick();
|
||||
} else if (action[0] == ACTION_DROP) {
|
||||
game.hardDrop();
|
||||
/* do nothing. */
|
||||
}
|
||||
}
|
||||
|
||||
function startAI() {
|
||||
var dropLocation = chooseDropLocation(game.currentBrickId);
|
||||
var action = chooseAction(dropLocation);
|
||||
doAction(action);
|
||||
|
||||
window.setTimeout(startAI, 100);
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
game = new TetrisGame("tetris-board");
|
||||
game.startGame();
|
||||
startAI();
|
||||
});
|
||||
+3
-7
@@ -1,15 +1,10 @@
|
||||
var game = null;
|
||||
var wasRecentlyRotated = false;
|
||||
|
||||
function doTurn() {
|
||||
function initControls() {
|
||||
document.onkeypress = onKeyPressed;
|
||||
document.onkeydown = onKeyDown;
|
||||
document.onkeyup = onKeyUp;
|
||||
if (game.doTurn()) {
|
||||
window.setTimeout(doTurn, 333);
|
||||
} else {
|
||||
alert('You lost!');
|
||||
}
|
||||
}
|
||||
|
||||
function onKeyDown(e) {
|
||||
@@ -50,6 +45,7 @@ function onKeyUp(e) {
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
initControls();
|
||||
game = new TetrisGame("tetris-board");
|
||||
doTurn();
|
||||
game.startGame();
|
||||
});
|
||||
@@ -27,6 +27,8 @@ function TetrisGame(id) {
|
||||
/**************/
|
||||
/* Variables: */
|
||||
/**************/
|
||||
this.gameIsOver = false;
|
||||
|
||||
this.width = 10;
|
||||
this.height = 15;
|
||||
this.canvas = new TetrisCanvas(id, this.width, this.height);
|
||||
@@ -68,11 +70,13 @@ function TetrisGame(id) {
|
||||
}
|
||||
|
||||
this.resetCurrentBrick = function() {
|
||||
this.currentBrickId = this.getRandomBrick();
|
||||
this.currentBrickRot = 0;
|
||||
this.currentBrickLoc = this.getBrickStartingLocation(this.currentBrickId);
|
||||
|
||||
this.redraw();
|
||||
if (! this.gameIsOver) {
|
||||
this.currentBrickId = this.getRandomBrick();
|
||||
this.currentBrickRot = 0;
|
||||
this.currentBrickLoc = this.getBrickStartingLocation(this.currentBrickId);
|
||||
|
||||
this.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
this.brickShape = function(brickId, brickRot) {
|
||||
@@ -176,6 +180,7 @@ function TetrisGame(id) {
|
||||
var y = this.currentBrickLoc[1]+shape[i][1];
|
||||
|
||||
if (y >= this.height) {
|
||||
this.gameIsOver = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -254,6 +259,19 @@ function TetrisGame(id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
this.loop = function(game) {
|
||||
if (! game.doTurn()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
window.setTimeout(game.loop, 333, game);
|
||||
|
||||
}
|
||||
|
||||
this.startGame = function() {
|
||||
this.loop(this);
|
||||
}
|
||||
|
||||
/****************/
|
||||
/* Constructor: */
|
||||
/****************/
|
||||
|
||||
Reference in new issue
Block a user