-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
finished homework 7 #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
// See cardset-example.js for examples | ||
|
||
var MemoryCards = (function() { | ||
function Ctor() { | ||
//... | ||
this.values = function() { | ||
|
||
} | ||
|
||
this.match = function(a,b) { | ||
|
||
} | ||
var dragons = [ | ||
['shadow', 1], ['shadow', 2], | ||
['horned', 1], ['horned', 2], | ||
['black', 1], ['black', 2], | ||
['blue', 1], ['blue', 2], | ||
['green', 1], ['green', 2], | ||
['red', 1], ['red', 2], | ||
['white', 1], ['white', 2], | ||
['brass', 1], ['brass', 2], | ||
['bronze', 1], ['bronze', 2], | ||
['copper', 1], ['copper', 2], | ||
['gold', 1], ['gold', 2], | ||
['silver', 1], ['silver', 2] | ||
]; | ||
|
||
function CardsCtor() { | ||
this.values = function() { | ||
return dragons.slice(); | ||
}; | ||
this.match = function(card1,card2) { | ||
return card1[0] === card2[0]; | ||
}; | ||
this.display = function(val) { | ||
|
||
} | ||
return val; | ||
}; | ||
} | ||
//... | ||
|
||
return Ctor; | ||
return CardsCtor; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
/* Whatever CSS you want for GUI...*/ | ||
|
||
#memorygame { | ||
/* ... */ | ||
td { | ||
width: 50px; | ||
height: 50px; | ||
background-color: #999; | ||
text-align: center; | ||
} | ||
|
||
.white { | ||
background-color: #fff; | ||
} | ||
|
||
.grey { | ||
background-color: #999; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,55 @@ | ||
var MemoryGUI = (function () { | ||
var MemoryGui = (function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your GUI is functioning pretty well. Code is laid out appropriately and all the thinking is being done by the game logic which is perfect. I'd like to see the cards have a distinct back and front, if only to really simulate the idea that the game is actually flipping anything. At the moment the game end up showing about three cards at once if a person clicks quickly enough through the cards. This is a small detail and seems nitpicky, but I think it's worth enhancing the functionality here so that only two cards are ever showing, either by tinkering with the setTimeout delay time, or maybe just disable clicking while two cards are showing. The last suggestion I have is to consider adding some comments in your code. Your functions from lines 8 through 34 are pretty obvious in what they do, but around line 36 the meaning behind your variables and lines become less clear. Of course, we understand its ultimate function because it's the homework, but getting into the practice of leaving comments will help out later. Good work overall! |
||
|
||
//... | ||
|
||
function GuiCtor(container,game) { | ||
game.gui(this); // link game to this gui | ||
game.gui(this); | ||
|
||
//... | ||
|
||
// public instance methods | ||
// (you may instead attach these to a prototype if you prefer) | ||
this.reset = function() { | ||
//... | ||
game.reset(); | ||
var tds = document.getElementsByTagName('td'); | ||
for (var i = 0; i < tds.length; i++) { | ||
tds[i].setAttribute('class', 'grey'); | ||
tds[i].innerHTML = ''; | ||
} | ||
} | ||
this.show = function(where,displayString) { | ||
//... | ||
document.getElementById(where).innerHTML = displayString[0]; | ||
} | ||
this.removeSoon = function(whereArr) { | ||
//... | ||
setTimeout(function() { | ||
whereArr.forEach(function(n) { | ||
var card = document.getElementById(n) | ||
card.innerHTML = ''; | ||
card.setAttribute('class','white'); | ||
}) | ||
},500) | ||
} | ||
this.hideSoon = function(whereArr) { | ||
//... | ||
setTimeout(function() { | ||
whereArr.forEach(function(n) { | ||
document.getElementById(n).innerHTML = ''; | ||
}) | ||
},500) | ||
} | ||
|
||
var container = document.getElementById(container); | ||
var grid = document.createElement('table'); | ||
var cellId = 0; | ||
for (var r = 0; r < game.size()/4; r++) { | ||
var row = grid.insertRow(r) | ||
for (var c = 0; c < 4; c++) { | ||
var cell = row.insertCell(c); | ||
cell.addEventListener('click', function(event) { | ||
var id = this.getAttribute('id'); | ||
game.lift(parseInt(id)); | ||
}) | ||
cell.setAttribute('id', cellId); | ||
cellId++; | ||
} | ||
} | ||
container.appendChild(grid); | ||
|
||
// Do some initial setup and rendering... | ||
} | ||
|
||
return GuiCtor; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
|
||
var cards,game,gui; //global vars can been inspected in console, making debugging easier | ||
function go() { | ||
// you'll need to study the modules to understand how to plug them in... | ||
cards = //?????? | ||
game = //?????? | ||
gui = new MemoryGUI('memorygame',game); //'memorygame' is the id of div where gui should be inserted' | ||
var cards = new MemoryCards(); | ||
var cardGame = new MemoryGame(cards); | ||
var cardGui = new MemoryGui('board', cardGame); | ||
|
||
var button = document.createElement('button'); | ||
button.addEventListener('click', cardGui.reset) | ||
button.innerHTML = 'reset'; | ||
document.getElementById('board').appendChild(button); | ||
} | ||
|
||
window.addEventListener("load",go); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I first tested it out I thought that it was a color matching game and then I was really confused when I got to "horned" and "shadow." Now I see that they are dragons! Are you going to add any pictures or anything to boost the dragon-ness of it?
Aside from that everything looks good. Eric has a few suggestions about functionality. Nice work!