/****
* Classes
****/
// Disc class for the game pieces
var Disc = Container.expand(function (player) {
var self = Container.call(this);
self.fall = function (targetY) {
var distance = targetY - self.y;
var speed = Math.sqrt(2 * distance);
self.y += speed;
if (self.y > targetY) {
self.y = targetY;
self.speed = -self.speed * 0.5;
}
};
self.player = player;
var discGraphics = self.attachAsset(player.color, {
anchorX: 0.5,
anchorY: 0.5
});
});
// Grid class for the game board
var Grid = Container.expand(function () {
var self = Container.call(this);
// Method to print the color of discs in each row
self.printRows = function () {
for (var row = 0; row < 6; row++) {
var rowColors = [];
for (var col = 0; col < 7; col++) {
if (self.cells[row][col]) {
rowColors.push(self.cells[row][col].player.color);
} else {
rowColors.push('empty');
}
}
console.log('Row ' + row + ': ' + rowColors.join(', '));
}
};
self.cells = [];
for (var row = 0; row < 6; row++) {
self.cells[row] = [];
for (var col = 0; col < 7; col++) {
self.cells[row][col] = null; // Initialize all cells as empty
}
}
// Method to add a disc to the grid
self.addDisc = function (col, player) {
for (var row = 5; row >= 0; row--) {
if (!self.cells[row][col]) {
var disc = new Disc(player);
disc.x = col * 220 + 360; // Calculate x position based on column
disc.y = 0; // Start at the top of the screen
self.cells[row][col] = disc;
game.addChild(disc);
disc.fall(row * 220 + 310); // Fall to the correct position
LK.on('tick', function () {
disc.fall(row * 220 + 310);
});
return true; // Return true if a disc was successfully added
}
}
return false; // Return false if the column is full
};
// Method to check for a win condition
self.checkWin = function () {
for (var row = 0; row < 6; row++) {
for (var col = 0; col < 7; col++) {
if (self.cells[row][col]) {
var color = self.cells[row][col].player.color;
// Check horizontally
if (col < 4 && self.cells[row][col + 1] && self.cells[row][col + 1].player.color == color && self.cells[row][col + 2] && self.cells[row][col + 2].player.color == color && self.cells[row][col + 3] && self.cells[row][col + 3].player.color == color) {
return true;
}
// Check vertically
if (row < 3 && self.cells[row + 1][col] && self.cells[row + 1][col].player.color == color && self.cells[row + 2][col] && self.cells[row + 2][col].player.color == color && self.cells[row + 3][col] && self.cells[row + 3][col].player.color == color) {
return true;
}
// Check diagonally (up-right and down-right)
if (col < 4 && self.cells[row + 1] && self.cells[row + 1][col + 1] && self.cells[row + 1][col + 1].player.color == color && self.cells[row + 2] && self.cells[row + 2][col + 2] && self.cells[row + 2][col + 2].player.color == color && self.cells[row + 3] && self.cells[row + 3][col + 3] && self.cells[row + 3][col + 3].player.color == color) {
return true;
}
if (row > 2 && self.cells[row - 1] && self.cells[row - 1][col + 1] && self.cells[row - 1][col + 1].player.color == color && self.cells[row - 2] && self.cells[row - 2][col + 2] && self.cells[row - 2][col + 2].player.color == color && self.cells[row - 3] && self.cells[row - 3][col + 3] && self.cells[row - 3][col + 3].player.color == color) {
return true;
}
}
}
}
return false;
};
});
var Player = Container.expand(function (color) {
var self = Container.call(this);
self.color = color;
self.score = 0;
self.addScore = function () {
self.score++;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var turnIndicator = game.attachAsset('turnIndicator', {
anchorX: 0.5,
anchorY: 0.5,
x: 100,
y: 100,
tint: 0xff5757
});
// Initialize the game board
var winText = new Text2('', {
size: 150,
fill: '#ffffff'
});
winText.anchor.set(0.5, 0.5);
LK.gui.center.addChild(winText);
var grid = new Grid();
game.addChild(grid);
// Create the players
var players = [new Player('redDisc'), new Player('blueDisc')];
// Current player's turn: 0 for red, 1 for blue
var currentPlayer = 0;
// Handle touch events to add discs to the grid
game.on('down', function (obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
var col = Math.floor((pos.x - 305) / 220); // Calculate column based on touch position
if (col >= 0 && col < 7) {
if (grid.addDisc(col, players[currentPlayer])) {
if (grid.checkWin()) {
winText.setText(players[currentPlayer].color + ' wins!');
console.log(players[currentPlayer].color + ' wins!');
LK.setTimeout(function () {
LK.showGameOver();
}, 5000);
} else {
currentPlayer = (currentPlayer + 1) % 2; // Switch player only if a disc was successfully added
turnIndicator.tint = players[currentPlayer].color === 'redDisc' ? 0xff5757 : 0x3d4aff;
}
}
}
});
// Main game loop
var debugCounter = 0;
LK.on('tick', function () {
// Game logic updates, such as checking for win conditions, go here
grid.checkWin();
debugCounter++;
});