Skip to content

Commit

Permalink
Started bejeweled
Browse files Browse the repository at this point in the history
  • Loading branch information
cintiamh committed Dec 12, 2012
1 parent c08927d commit b395669
Show file tree
Hide file tree
Showing 16 changed files with 3,218 additions and 50 deletions.
180 changes: 130 additions & 50 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Binary file added bejeweled/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions bejeweled/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<!-- Enabling Web Application Mode -->
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Bejeweled by PandaJapa</title>
<link rel="stylesheet" href="styles/main.css">
<link rel="stylesheet" href="styles/mobile.css"
media="screen and (max-device-width:768px) and (orientation: portrait),
screen and (max-device-width: 1024px) and (orientation: landscape)">
<script type="text/javascript" src="scripts/modernizr.js"></script>
<script type="text/javascript" src="scripts/loader.js"></script>
</head>
<body>
<div id="game">
<div class="screen" id="splash-screen">
<h1 class="logo">Bejeweled by PandaJapa</h1>
<span>Click to continue</span>
</div>
<div class="screen" id="install-screen">
<h1 class="logo">Bejeweled by PandaJapa</h1>
<span>
Click on install button to install the game to your home screen.
</span>
</div>
<div class="screen" id="main-menu">
<h1 class="logo">Bejeweled by PandaJapa</h1>
<ul class="menu">
<li><button name="game-screen">Play</button></li>
<li><button name="hiscore">Highscore</button></li>
<li><button name="about">About</button></li>
<li><button name="exit-screen">Exit</button></li>
</ul>
</div>
<div class="screen" id="game-screen"></div>
<div class="screen" id="high-scores"></div>
</div>
</body>
</html>
Binary file added bejeweled/scripts/.DS_Store
Binary file not shown.
182 changes: 182 additions & 0 deletions bejeweled/scripts/board.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
jewel.board = (function() {

var settings,
jewels,
cols,
rows,
baseScore,
numJewelTypes;

function randomJewel() {
return Math.floor(Math.random() * numJewelTypes);
}

function fillBoard() {
var x, y,
type;
jewels = [];
for (x = 0; x < cols; x++) {
jewels[x] = [];
for (y = 0; y < rows; y++) {
type = randomJewel();
while ((type === getJewel(x-1, y) &&
type === getJewel(x-2, y)) ||
(type === getJewel(x, y-1) &&
type === getJewel(x, y-2))) {
type = randomJewel();
}
jewels[x][y] = type;
}
}
}

function getJewel(x, y) {
if (x < 0 || x > cols - 1 || y < 0 || y > rows - 1) {
return -1;
}
else {
return jewels[x][y];
}
}

function initialize(callback) {
settings = jewels.settings;
numJewelTypes = settings.numJewelTypes;
baseScore = settings.baseScore;
cols = settings.cols;
rows = settings.rows;
fillBoard();
callback();
}

// returns the number jewels in the longest chain that includes (x, y)
function checkChain(x, y) {
var type = getJewel(x, y),
left = 0, right = 0,
down = 0, up = 0;

// look right
while (type === getJewel(x + right + 1, y)) {
right++;
}

// look left
while (type === getJewel(x - left - 1, y)) {
left++;
}

// look up
while (type === getJewel(x, y + up + 1)) {
up++;
}

// look down
while (type === getJewel(x, y - down - 1)) {
down++;
}

return Math.max(left + 1 + right, up + 1 + down);
}

// returns true if (x1, y1) can be swapped with (x2, y2) to form a new match
function canSwap(x1, y1, x2, y2) {
var type1 = getJewel(x1, y1),
type2 = getJewel(x2, y2),
chain;

if (!isAdjacent(x1, y1, x2, y2)) {
return false;
}

// temporarily swap jewels
jewels[x1][y1] = type2;
jewels[x2][y2] = type1;

chain = (checkChain(x2, y2) > 2 ||
checkChain(x1, y1) >2);

// swap back
jewels[x1][y1] = type1;
jewels[x2][y2] = type2;

return chain;
}

// returns true if (x1, y1) is adjacent to (x2, y2)
function isAdjacent(x1, y1, x2, y2) {
var dx = Math.abs(x1 - x2),
dy = Math.abs(y1 - y2);
return (dx + dy === 1);
}

// returns a two-dimensional map of chain-lengths
function getChains() {
var x, y,
chains = [];

for (x = 0; x < cols; x++) {
chains[x] = [];
for (y = 0; y < rows; y++) {
chains[x][y] = checkChain(x, y);
}
}
return chains;
}

function check() {
var chains = getChains(),
hadChains = false, score = 0,
removed = [], moved = [], gaps = [];

for (var x = 0; x = cols; x++) {
gaps[x] = 0;
for (var y = rows - 1; y >= 0; y--) {
if (chains[x][y] > 2) {
hadChains = true;
gaps[x]++;
removed.push({
x : x, y : y,
type : getJewel(x, y)
});

// add points to score
score += baseScore * Math.pow(2, (chains[x][y] - 3));
} else if (gaps[x] > 0) {
moved.push({
toX : x, toY : y + gaps[x],
fromX : x, fromY : y,
type : getJewel(x, y)
});
jewels[x][y + gaps[x]] = getJewel(x, y);
}
}
}

// fill from top
for (y = 0; y < gaps[x]; y++) {
jewels[x][y] = randomJewel();
moved.push({
toX : x, toY : y,
fromX : x, fromY : y - gaps[x],
type : jewels[x][y]
});
}
}

function print() {
var str = "";
for (var y = 0; y < rows; y++) {
for (var x = 0; x < cols; x++) {
str += getJewel(x, y) + " ";
}
str += "\r\n";
}
console.log(str);
}

return {
initialize : initialize,
canSwap : canSwap,
print : print
};
})();
34 changes: 34 additions & 0 deletions bejeweled/scripts/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
jewel.dom = (function() {
var $ = Sizzle;

function hasClass(el, clsName) {
var regex = new RegExp("(^|\\s)" + clsName + "(\\s|$)");
return regex.test(el.className);
}

function addClass(el, clsName) {
if (!hasClass(el, clsName)) {
el.className += " " + clsName;
}
}

function removeClass(el, clsName) {
var regex = new RegExp("(^|\\s)" + clsName + "(\\s|$)");
el.className = el.className.replace(regex, " ");
}

function bind(element, event, handler) {
if (typeof element == "string") {
element = $(element)[0];
}
element.addEventListener(event, handler, false);
}

return {
$ : $,
hasClass : hasClass,
addClass : addClass,
removeClass : removeClass,
bind : bind
};
})();
32 changes: 32 additions & 0 deletions bejeweled/scripts/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
jewel.game = (function() {
var dom = jewel.dom,
$ = dom.$;

function setup() {
// hide the address bar on Android devices
if (/Android/.test(navigator.userAgent)) {
$("html")[0].style.height = "200%";
setTimeout(function() {
window.scrollTo(0, 1);
}, 0);
}
}

// hide the active screen (if any) and show the screen with the specified id
function showScreen(screenId) {
var activeScreen = $("#game .screen.active")[0],
screen = $("#" + screenId)[0];
if (activeScreen) {
dom.removeClass(screen, "active");
}
// run the screen module
jewel.screens[screenId].run();
// display the screen html
dom.addClass(screen, "active");
}

// expose public methods
return {
showScreen : showScreen
};
})();
Binary file not shown.
60 changes: 60 additions & 0 deletions bejeweled/scripts/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var jewel = {
screens : {},
settings : {
rows : 8,
cols : 8,
baseScore : 100,
numJewelTypes : 7
}
};

// wait until main document is loaded
window.addEventListener("load", function(){
Modernizr.addTest("standalone", function() {
return(window.navigator.standalone != false);
});

// start dynamic loading
// loading stage 1
Modernizr.load([
{
// these files are always loaded
load : [
"scripts/sizzle.js",
"scripts/dom.js",
"scripts/game.js"//,
//"scripts/screen.splash.js",
//"scripts/screen.main-menu.js"
]//,
// called when all files have finished loading and executing
//complete: function() {
// show the first screen
// jewel.game.showScreen("splash-screen");
//}
},{
test : Modernizr.standalone,
yep: "scripts/screen.splash.js",
nope: "scripts/screen.install.js",
complete: function() {
if (Modernizr.standalone) {
jewel.game.showScreen("splash-screen");
} else {
jewel.game.showScreen("install-screen");
}
}
}
]);

// loading stage 2
if (Modernizr.standalone) {
Modernizr.load([
{
load: [
"scripts/screen.main-menu.js",
"scripts/board.js"
]
}
]);
}

}, false);
Loading

0 comments on commit b395669

Please sign in to comment.