Permalink
Browse files

adding games section

  • Loading branch information...
1 parent ae9ac4f commit 3ff70548e380082a7dde27892539f8af54024594 @anttears committed Apr 8, 2011
View
@@ -0,0 +1,23 @@
+# Packages #
+############
+*.7z
+*.dmg
+*.gz
+*.tar
+*.zip
+*.rar
+
+# Logs #
+########
+*.log
+*.sql
+*.sqlite
+
+# OS stuff #
+#############
+.DS_Store?
+.DS_Store
+ethumbs.db
+Icon?
+Icon
+Thumbs.db
Binary file not shown.
@@ -0,0 +1,33 @@
+html, body {
+ height: 100%;
+}
+
+body {
+ text-align: center;
+ background: #efefef;
+ font-family: "Helvetica, Arial";
+ font-size: 75%;
+}
+
+#page {
+ margin: 0 auto;
+ text-align: left;
+ background: #fff;
+ height: 100%;
+ width: 760px;
+ padding: 0 12px;
+}
+
+table.ms {
+ border: 1px solid #aaa;
+ margin: 12px 5px;
+}
+
+table.ms td {
+ background-color: #ccc;
+ border: 1px solid #aeaeae;
+ text-align: center;
+ vertical-align: middle;
+ height: 2em;
+ width: 2em;
+}
@@ -0,0 +1,114 @@
+html, body, ul, ol, li, p,
+h1, h2, h3, h4, h5, h6,
+form, fieldset, a, dd {
+margin: 0;
+padding: 0;
+border: 0;
+}
+
+html{
+font-size: 100.01%;
+}
+
+form, input,
+textarea, select{
+ font-size:100%;
+}
+
+abbr,
+acronym,
+span.abbr {
+ cursor: help;
+ border-bottom: 1px dashed #000;
+}
+
+ul{
+ list-style:none;
+}
+
+table{
+ border-collapse: collapse;
+ font-size:100%;
+}
+
+a{
+ text-decoration:none;
+}
+
+a:hover{
+ text-decoration:underline;
+}
+
+img{
+ border:0;
+}
+
+/* UTILITY - generic classes information only */
+
+.inline{
+ display:inline;
+}
+
+.remove{
+ display:none;
+}
+
+.hideLegend legend{
+ display:none;
+}
+
+.hide,
+#skipNav {
+ position:absolute;
+ top:0;
+ left:-200em;
+ width:20em;
+}
+
+.fr{
+ float:right;
+}
+
+.fl{
+ float:left;
+}
+
+.tar{
+ text-align:right;
+}
+
+.tac{
+ text-align:center;
+}
+
+.tal{
+ text-align:left;
+}
+
+.half{
+ width:50%; /*Clearly if using 2 of these - there can be no margin, padding or borders attached */
+ padding:0;
+ margin:0;
+ border:0;
+}
+
+.halfish{
+ width:48%;
+}
+
+/* FULL CLEAR - forces a container to surround all children - regardless of floating */
+
+.fc:after, .FormRow:after{
+ content: ".";
+ display: block;
+ height: 0;
+ clear: both;
+ visibility: hidden;
+}
+
+.fc, .FormRow{ display: inline-block;}
+
+/* Hides from IE-mac \*/
+* html .fc, * html .FormRow { height: 1%; }
+.fc, .FormRow { display: block; }
+
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html lang="en" >
+ <head>
+ <meta name="language" content="en" />
+ <meta name="robots" content="all, follow" />
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta name="keywords" content='test' />
+ <meta name="description" content='test' />
+
+ <link href="css/utilities.css" type="text/css" rel="Stylesheet" />
+ <link href="css/default.css" type="text/css" rel="Stylesheet" />
+ <title>Test page</title>
+ <script>
+ // HTML 5 shiv - http://bit.ly/cm2Lrb
+ /*@cc_on'abbr article aside audio canvas details figcaption figure footer header hgroup mark menu meter nav output progress section summary time video'.replace(/\w+/g,function(n){document.createElement(n)})@*/
+ </script>
+ </head>
+ <body>
+ <div id="page">
+ <h2 id="mt">Minesweeper</h2>
+ <form action="#" method="post" id="gridForm">
+ <fieldset>
+ <legend>Select Grid Size</legend>
+ <label for="cols">Cols:</label>
+ <input name="cols" id="cols" value="4" />
+ <label for="rows">Rows:</label>
+ <input name="rows" id="rows" value="4" />
+ <label for="bombs">Bombs:</label>
+ <input name="bombs" id="bombs" value="3" />
+ <input type="button" value="Generate" />
+ </fieldset>
+ </form>
+ </div>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
+ <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
+ <script src="js/default.js"></script>
+ </body>
+</html>
Binary file not shown.
@@ -0,0 +1,161 @@
+function MineSweeper (c, r, b) {
+ var cols = c,
+ rows = r,
+ bombs = b,
+ tableFrag,
+ gridSquares = r * c,
+ attempt = 0,
+ revealedCnt = 0,
+ numberToReveal,
+ grid = [],
+ matrix = [[-1, -1],[0, -1],[1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]],
+ gridObjs = [];
+
+ function generateGrid () {
+ tableFrag = $('<table></table>').attr({
+ "class": "ms"
+ });
+ for (var i = 0; i < rows; i += 1) {
+ var tr = $('<tr></tr>');
+ var r = "r" + i;
+ for (var x = 0; x < cols; x += 1) {
+ var c = "c" + x;
+ grid.push(r + c);
+ gridObjs[r + c] = {},
+ gridObjs[r + c].type = "U",
+ gridObjs[r + c].revealed = false,
+ gridObjs[r + c].attempt = 0;
+
+ var td = $('<td></td>')
+ .attr("id", r + c)
+ .appendTo(tr)
+ .bind("click", clickSquare);
+ }
+ tr.appendTo(tableFrag);
+ }
+ }
+
+ function clickSquare () {
+ var td = $(this),
+ rc = td.attr("id");
+ attempt += 1;
+ revealSquare(rc);
+ }
+
+ function revealSquare (rc) {
+ var len = matrix.length,
+ sq = gridObjs[rc],
+ type = sq.type,
+ n = getNumbers(rc);
+
+ if (type === "B") {
+ alert("Game Over");
+ return;
+ }
+
+ if (revealedCnt === numberToReveal - 1) {
+ alert("winner");
+ return;
+ }
+
+ if (sq.revealed !== true) {
+ $("#" + rc).append(type);
+ sq.revealed = true;
+ revealedCnt += 1;
+ }
+
+ for (var i = 0; i < len; i += 1) {
+ var m = matrix[i],
+ newR = n[0] + m[0],
+ newC = n[1] + m[1],
+ newrc = "r" + newR + "c" + newC,
+ sqn = gridObjs[newrc];
+
+ if ((newR > -1 && newR < rows) && (newC > -1 && newC < cols) && sqn.attempt < attempt && !sqn.revealed && sqn.type !== "B") {
+ var t = sqn.type;
+ sqn.attempt = attempt;
+
+ sqn.revealed = true;
+ revealedCnt += 1;
+ $("#" + newrc).append(t);
+
+ if (t === 0) {
+ console.log("t = 0: " + newrc);
+ revealSquare(newrc);
+ }
+ }
+ }
+
+ }
+
+ function placeBombs () {
+ var list = grid.slice();
+ for (var i = 0; i < bombs; i += 1) {
+ var num = Math.floor(Math.random() * list.length),
+ sq = list[num];
+ tableFrag.find("#" + sq).append("B");
+ gridObjs[sq].type = "B";
+ list.splice(num, 1);
+ }
+ }
+
+ function bombNumbers (r, c) {
+ var cnt = 0,
+ rc = "r" + r + "c" + c,
+ len = matrix.length;
+
+ for (var i = 0; i < len; i += 1) {
+ var m = matrix[i],
+ newR = r + m[0],
+ newC = c + m[1],
+ newrc = "r" + newR + "c" + newC;
+
+ if ((newR > -1 && newR < rows) && (newC > -1 && newC < cols)) {
+ if (gridObjs[newrc].type === "B") {cnt += 1;}
+ }
+ }
+
+ //tableFrag.find("#" + rc).append(cnt);
+ gridObjs[rc].type = cnt;
+ }
+
+ function getNumbers (sq) {
+ var arr = sq.match(/r([0-9]+)c([0-9]+)/),
+ r = parseInt(arr[1]),
+ c = parseInt(arr[2]);
+ return[r, c];
+ }
+
+ function makeObj () {
+ var len = grid.length;
+ for (var i = 0; i < len; i += 1) {
+ var sq = grid[i],
+ obj = gridObjs[sq];
+ if (obj.type !== "B") {
+ var n = getNumbers(sq);
+ bombNumbers(n[0], n[1]);
+ }
+ }
+ }
+
+ this.init = function () {
+ generateGrid();
+ placeBombs();
+ makeObj();
+ $("#mt").after(tableFrag);
+ numberToReveal = grid.length - bombs;
+ }
+}
+
+
+$("document").ready(function () {
+ $("#gridForm").bind("submit, click", function () {
+ var form = $(this),
+ cols = form.find("#cols").val(),
+ rows = form.find("#rows").val(),
+ bombs = form.find("#bombs").val(),
+ ms = new MineSweeper(cols, rows, bombs);
+ ms.init();
+ return false;
+ });
+});
Oops, something went wrong.

0 comments on commit 3ff7054

Please sign in to comment.