Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion AlbertMineSweeper.script.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ function Grid(numRow, numCol, numMines) {
this.cells = [];
for(var i = 0; i < this.numRow; i++) {
this.cells[i] = [];
$(".base").append("<div class=\"row\" id=\"row" + i.toString() + "\"><div>");
for(var j = 0; j < this.numCol; j++) {
this.cells[i][j] = new Cell();
$("#row"+i.toString()).append("<div class=\"cell\" data-row=\"" + i.toString() + "\" data-col=\"" + j.toString() + "\"></div>");
}
}
}
// Adjust Dimensions of Base
var edgeThickness = 2 * (parseInt($(".cell").css("border-left-width")) + parseInt($(".cell").css("margin")) + parseInt($(".cell").css("padding")));
$(".base").height(numRow * ($(".cell").height() + edgeThickness) + 10);
$(".base").width(numCol * ($(".cell").width() + edgeThickness) + 10);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You won't need to do these hacks if you apply the proper CSS rules to the board and cells. Delete the above 3 lines after adding these styles.

.base {
    display: inline-block; /* inline block will remove the grey space to the right - that's the difference between block and inline-block */
    padding: 5px; /* this will compensate for the removing of top/left styles */
    background-color: gray;
}
.cell {
    background-color: blue;
    height: 30px;
    width: 30px;
    position: relative;
    display: inline-block; /* <------------ Remove this because you override with display: flex below */
    margin: 2px;
    padding: 2px;
    text-align: center;
    color: blue;
    left: 5px; /* <------------ Remove this */

    display: flex; 
    align-items: center; 
    justify-content: center; 
}
.row {
    display: block;
    border: 0px;
    position: relative;
    top: 5px; /* <------------ Remove this */
    display: flex;
}


// Seed mines randomly in grid, adds them to minedCells[] and calculate each cell's risk
for(var c = 0; c < numMines; c++) {
Expand Down
3 changes: 0 additions & 3 deletions ams_board.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ div {

.base {
background-color: gray;
height: 410px;
width: 410px;
/*for a 15*15 grid.*/
}

.cell {
Expand Down
27 changes: 27 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "AlbertMineSweeper",
"version": "0.0.0",
"homepage": "https://github.com/albertliangcode/AlbertMineSweeper",
"authors": [
"albertliangcode <albertliangcode@gmail.com>"
],
"moduleType": [
"amd",
"es6",
"globals",
"node",
"yui"
],
"license": "MIT",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"jquery": "~2.1.3"
}
}
51 changes: 11 additions & 40 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
<h1>Albert Mine Sweeper</h1>
<h4>Project Status:</h4>
<ul>
<li>Current: End Conditions</li>
<li>Next: Adjustable Game</li>
<li>Current: Adjustable GameBoard</li>
<li>Next: User Interface for:
<ol>
<li>Resetting Game</li>
<li>Adjusting Game Settings</li>
</ol>
</li>
<li>Next: No First-Click Deaths</li>
<li>Next: End Conditions (UI Freeze)</li>
<li>Next: Clock</li>
<li>Next: Scoreboard?</li>
<li>Next: Match Graphics to MS Minesweeper?</li>
Expand All @@ -32,45 +33,15 @@ <h4>Project Status:</h4>
</ul>
</li>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the next steps:

<li>Refactoring: decouple model from view (separate grid logic from jquery dom manipulation)</li>
<li>tidy up: make the game replay-able</li>

</ul>


<form id="gridSetup">
Rows: <input type="text" name="rows"><br>
Columns: <input type="text" name="cols"><br>
Mines: <input type="text" name="mines"><br><br>
<input type="submit" value="Refresh Game"><br><br><br>
</form>
<div class="base" oncontextmenu="return false;">
<div class="row">
<!--The "." was needed, or else the margins in some of the cells became
unbalanced when some cells were visted and others not yet visited.-->
<div class="cell" data-row="0" data-col="0"></div>
<div class="cell" data-row="0" data-col="1"></div>
<div class="cell" data-row="0" data-col="2"></div>
<div class="cell" data-row="0" data-col="3"></div>
<div class="cell" data-row="0" data-col="4"></div>
</div>
<div class="row">
<div class="cell" data-row="1" data-col="0"></div>
<div class="cell" data-row="1" data-col="1"></div>
<div class="cell" data-row="1" data-col="2"></div>
<div class="cell" data-row="1" data-col="3"></div>
<div class="cell" data-row="1" data-col="4"></div>
</div>
<div class="row">
<div class="cell" data-row="2" data-col="0"></div>
<div class="cell" data-row="2" data-col="1"></div>
<div class="cell" data-row="2" data-col="2"></div>
<div class="cell" data-row="2" data-col="3"></div>
<div class="cell" data-row="2" data-col="4"></div>
</div>
<div class="row">
<div class="cell" data-row="3" data-col="0"></div>
<div class="cell" data-row="3" data-col="1"></div>
<div class="cell" data-row="3" data-col="2"></div>
<div class="cell" data-row="3" data-col="3"></div>
<div class="cell" data-row="3" data-col="4"></div>
</div>
<div class="row">
<div class="cell" data-row="4" data-col="0"></div>
<div class="cell" data-row="4" data-col="1"></div>
<div class="cell" data-row="4" data-col="2"></div>
<div class="cell" data-row="4" data-col="3"></div>
<div class="cell" data-row="4" data-col="4"></div>
</div>
</div>
<script type="text/javascript" src="AlbertMineSweeper.script.js"></script>
</body>
</html>
14 changes: 9 additions & 5 deletions ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ var stopGame = function () {


$(document).ready(function() {
//Must bind contextmenu event handler to each cell
//document.oncontextmenu = function() {return false;};
var grid = new Grid(5,5,2);
var report = grid.print();

var grid;
var report;
$('#gridSetup').submit(function(){
var $values = $('#gridSetup').serializeArray();
grid = new Grid(parseInt($values[0].value),parseInt($values[1].value),parseInt($values[2].value));
// var grid = new Grid(7,7,5);
report = grid.print();
}); // <<<The program kicks me out here.>>>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to know what is happening if you're not familiar with <form>. You made a small beginner's mistake by not preventing the default behavior of the form. Whenever you submit a form, it will make a synchronous POST request and refresh the page on the response. So you'll need to do this to stop the page refresh (which you were perceiving as the game kicking you out):

$('#gridSetup').submit(function(e){ // the first and only parameter to this event handler is a JQuery event object
  e.preventDefault();
  var $values = $('#gridSetup').serializeArray();
  ...


$('.cell').mousedown(function(event) {
var row = $(this).data('row');
var col = $(this).data('col');
Expand Down