/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var GridCell = Container.expand(function (letter, row, col) {
var self = Container.call(this);
self.row = row;
self.col = col;
self.letter = letter;
self.isFound = false;
self.isSelected = false;
var border = self.attachAsset('cellBorder', {
anchorX: 0.5,
anchorY: 0.5
});
var background = self.attachAsset('gridCell', {
anchorX: 0.5,
anchorY: 0.5
});
var selectedBg = self.attachAsset('selectedCell', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
var foundBg = self.attachAsset('foundCell', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
var letterText = new Text2(letter, {
size: 30,
fill: 0x000000
});
letterText.anchor.set(0.5, 0.5);
self.addChild(letterText);
self.setSelected = function (selected) {
self.isSelected = selected;
selectedBg.alpha = selected ? 1 : 0;
};
self.setFound = function () {
self.isFound = true;
foundBg.alpha = 1;
selectedBg.alpha = 0;
letterText.style.fill = "#ffffff";
};
return self;
});
var WordListItem = Container.expand(function (word, index) {
var self = Container.call(this);
self.word = word;
self.found = false;
var wordText = new Text2(word.toUpperCase(), {
size: 32,
fill: 0x333333
});
wordText.anchor.set(0, 0.5);
self.addChild(wordText);
var strikethrough = LK.getAsset('cellBorder', {
width: wordText.width + 20,
height: 4,
anchorX: 0,
anchorY: 0.5,
alpha: 0
});
strikethrough.x = -10;
self.addChild(strikethrough);
self.markFound = function () {
self.found = true;
wordText.style.fill = "#888888";
strikethrough.alpha = 1;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf5f5f5
});
/****
* Game Code
****/
var GRID_SIZE = 12;
var CELL_SIZE = 60;
var GRID_START_X = 2048 / 2 - GRID_SIZE * CELL_SIZE / 2;
var GRID_START_Y = 300;
var words = ['CAT', 'DOG', 'BIRD', 'FISH', 'TREE', 'BOOK', 'GAME', 'WORD', 'HOUSE', 'LIGHT', 'WATER', 'MUSIC'];
var grid = [];
var gridCells = [];
var wordList = [];
var selectedCells = [];
var isDragging = false;
var foundWords = [];
// Generate random letter
function getRandomLetter() {
return String.fromCharCode(65 + Math.floor(Math.random() * 26));
}
// Create empty grid
function createGrid() {
for (var i = 0; i < GRID_SIZE; i++) {
grid[i] = [];
gridCells[i] = [];
for (var j = 0; j < GRID_SIZE; j++) {
grid[i][j] = getRandomLetter();
}
}
}
// Place word in grid
function placeWord(word, row, col, direction) {
var directions = {
horizontal: [0, 1],
vertical: [1, 0],
diagonal1: [1, 1],
diagonal2: [1, -1],
horizontal_r: [0, -1],
vertical_r: [-1, 0],
diagonal1_r: [-1, -1],
diagonal2_r: [-1, 1]
};
var dir = directions[direction];
for (var i = 0; i < word.length; i++) {
var newRow = row + dir[0] * i;
var newCol = col + dir[1] * i;
if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE) {
grid[newRow][newCol] = word[i];
}
}
}
// Check if word can be placed
function canPlaceWord(word, row, col, direction) {
var directions = {
horizontal: [0, 1],
vertical: [1, 0],
diagonal1: [1, 1],
diagonal2: [1, -1],
horizontal_r: [0, -1],
vertical_r: [-1, 0],
diagonal1_r: [-1, -1],
diagonal2_r: [-1, 1]
};
var dir = directions[direction];
for (var i = 0; i < word.length; i++) {
var newRow = row + dir[0] * i;
var newCol = col + dir[1] * i;
if (newRow < 0 || newRow >= GRID_SIZE || newCol < 0 || newCol >= GRID_SIZE) {
return false;
}
}
return true;
}
// Place all words in grid
function placeWords() {
var directions = ['horizontal', 'vertical', 'diagonal1', 'diagonal2', 'horizontal_r', 'vertical_r', 'diagonal1_r', 'diagonal2_r'];
for (var w = 0; w < words.length; w++) {
var word = words[w];
var placed = false;
var attempts = 0;
while (!placed && attempts < 100) {
var row = Math.floor(Math.random() * GRID_SIZE);
var col = Math.floor(Math.random() * GRID_SIZE);
var direction = directions[Math.floor(Math.random() * directions.length)];
if (canPlaceWord(word, row, col, direction)) {
placeWord(word, row, col, direction);
placed = true;
}
attempts++;
}
}
}
// Create visual grid
function createVisualGrid() {
for (var i = 0; i < GRID_SIZE; i++) {
for (var j = 0; j < GRID_SIZE; j++) {
var cell = new GridCell(grid[i][j], i, j);
cell.x = GRID_START_X + j * CELL_SIZE;
cell.y = GRID_START_Y + i * CELL_SIZE;
gridCells[i][j] = cell;
game.addChild(cell);
}
}
}
// Create word list
function createWordList() {
var startY = GRID_START_Y;
var startX = GRID_START_X + GRID_SIZE * CELL_SIZE + 100;
for (var i = 0; i < words.length; i++) {
var wordItem = new WordListItem(words[i], i);
wordItem.x = startX;
wordItem.y = startY + i * 50;
wordList.push(wordItem);
game.addChild(wordItem);
}
}
// Get cell at position
function getCellAtPosition(x, y) {
for (var i = 0; i < GRID_SIZE; i++) {
for (var j = 0; j < GRID_SIZE; j++) {
var cell = gridCells[i][j];
var bounds = {
left: cell.x - CELL_SIZE / 2,
right: cell.x + CELL_SIZE / 2,
top: cell.y - CELL_SIZE / 2,
bottom: cell.y + CELL_SIZE / 2
};
if (x >= bounds.left && x <= bounds.right && y >= bounds.top && y <= bounds.bottom) {
return cell;
}
}
}
return null;
}
// Clear selection
function clearSelection() {
for (var i = 0; i < selectedCells.length; i++) {
if (!selectedCells[i].isFound) {
selectedCells[i].setSelected(false);
}
}
selectedCells = [];
}
// Get selected word
function getSelectedWord() {
var word = "";
for (var i = 0; i < selectedCells.length; i++) {
word += selectedCells[i].letter;
}
return word;
}
// Check if word is valid
function isValidWord(word) {
for (var i = 0; i < words.length; i++) {
if (words[i] === word && foundWords.indexOf(word) === -1) {
return true;
}
}
return false;
}
// Mark word as found
function markWordFound(word) {
foundWords.push(word);
// Mark cells as found
for (var i = 0; i < selectedCells.length; i++) {
selectedCells[i].setFound();
}
// Mark word in list as found
for (var i = 0; i < wordList.length; i++) {
if (wordList[i].word === word) {
wordList[i].markFound();
break;
}
}
LK.getSound('wordFound').play();
LK.setScore(LK.getScore() + word.length * 10);
// Check win condition
if (foundWords.length === words.length) {
LK.setTimeout(function () {
LK.showYouWin();
}, 1000);
}
}
// Check if cells form a line
function isValidSelection(cells) {
if (cells.length < 2) return true;
var deltaRow = cells[1].row - cells[0].row;
var deltaCol = cells[1].col - cells[0].col;
// Normalize direction
if (deltaRow !== 0) deltaRow = deltaRow > 0 ? 1 : -1;
if (deltaCol !== 0) deltaCol = deltaCol > 0 ? 1 : -1;
for (var i = 2; i < cells.length; i++) {
var currentDeltaRow = cells[i].row - cells[i - 1].row;
var currentDeltaCol = cells[i].col - cells[i - 1].col;
if (currentDeltaRow !== 0) currentDeltaRow = currentDeltaRow > 0 ? 1 : -1;
if (currentDeltaCol !== 0) currentDeltaCol = currentDeltaCol > 0 ? 1 : -1;
if (currentDeltaRow !== deltaRow || currentDeltaCol !== deltaCol) {
return false;
}
}
return true;
}
// Initialize game
createGrid();
placeWords();
createVisualGrid();
createWordList();
// Create title
var titleText = new Text2('WORD QUEST', {
size: 64,
fill: 0x2196F3
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 2048 / 2;
titleText.y = 150;
game.addChild(titleText);
// Create score display
var scoreText = new Text2('Score: 0', {
size: 36,
fill: 0x333333
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
scoreText.y = 60;
game.down = function (x, y, obj) {
var cell = getCellAtPosition(x, y);
if (cell && !cell.isFound) {
isDragging = true;
clearSelection();
selectedCells.push(cell);
cell.setSelected(true);
LK.getSound('selectLetter').play();
}
};
game.move = function (x, y, obj) {
if (isDragging) {
var cell = getCellAtPosition(x, y);
if (cell && !cell.isFound && selectedCells.indexOf(cell) === -1) {
var tempSelection = selectedCells.slice();
tempSelection.push(cell);
if (isValidSelection(tempSelection)) {
selectedCells.push(cell);
cell.setSelected(true);
LK.getSound('selectLetter').play();
}
}
}
};
game.up = function (x, y, obj) {
if (isDragging) {
isDragging = false;
if (selectedCells.length > 1) {
var selectedWord = getSelectedWord();
var reverseWord = selectedWord.split('').reverse().join('');
if (isValidWord(selectedWord)) {
markWordFound(selectedWord);
} else if (isValidWord(reverseWord)) {
markWordFound(reverseWord);
} else {
clearSelection();
}
} else {
clearSelection();
}
}
};
game.update = function () {
scoreText.setText('Score: ' + LK.getScore());
}; ===================================================================
--- original.js
+++ change.js
@@ -31,9 +31,9 @@
anchorY: 0.5,
alpha: 0
});
var letterText = new Text2(letter, {
- size: 36,
+ size: 30,
fill: 0x000000
});
letterText.anchor.set(0.5, 0.5);
self.addChild(letterText);
@@ -85,13 +85,13 @@
/****
* Game Code
****/
-var GRID_SIZE = 10;
-var CELL_SIZE = 72;
+var GRID_SIZE = 12;
+var CELL_SIZE = 60;
var GRID_START_X = 2048 / 2 - GRID_SIZE * CELL_SIZE / 2;
var GRID_START_Y = 300;
-var words = ['CAT', 'DOG', 'BIRD', 'FISH', 'TREE', 'BOOK', 'GAME', 'WORD'];
+var words = ['CAT', 'DOG', 'BIRD', 'FISH', 'TREE', 'BOOK', 'GAME', 'WORD', 'HOUSE', 'LIGHT', 'WATER', 'MUSIC'];
var grid = [];
var gridCells = [];
var wordList = [];
var selectedCells = [];