Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

prototype syntax improvements

  • Loading branch information...
commit d7cfbf2092bc9119a62ed3487e1680fefae53efe 1 parent d180ea5
Anatoly Mironov authored
Showing with 123 additions and 123 deletions.
  1. BIN  img/dice/locked5.png
  2. +123 −123 js/yatzy.js
BIN  img/dice/locked5.png
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
246 js/yatzy.js
View
@@ -1,15 +1,9 @@
/*
- * Detta är logiken bakom spelet
- * Meningen är att separera själva spelet från eventhantering mm
- * Jag vill försöka göra det objektorienterat
- * Men det är svårt. Det här med prototyperna är inte lätt efter att
- * ha programmerat i strikta Java och ganska strikta C#
+ * Anatoly Mironov 2009-2011
*/
var maxRounds = 15; // lite som final
var maxThrowings = 3;
function Match(quantityOfPlayers) {
-// mediatorklass för Spelar-games...
-// ska skriva den i lab 1C
this.gamesArrayIndex = 0; // för att kunna växla mellan games
this.quantityOfPlayers = quantityOfPlayers;
this.games = this.getANewGamesArray(quantityOfPlayers);
@@ -38,27 +32,26 @@ function Game(nr, name) {
this.lowerSum = 0;
this.rounds = [];
- this.faceArray = new Array();
+ this.faceArray = [];
//upper section
- this.faceArray["aces"] = new Aces();
- this.faceArray["twos"] = new Twos();
- this.faceArray["threes"] = new Threes();
- this.faceArray["fours"] = new Fours();
- this.faceArray["fives"] = new Fives();
- this.faceArray["sixes"] = new Sixes();
+ this.faceArray.aces = new Aces();
+ this.faceArray.twos = new Twos();
+ this.faceArray.threes = new Threes();
+ this.faceArray.fours = new Fours();
+ this.faceArray.fives = new Fives();
+ this.faceArray.sixes = new Sixes();
//lower section
this.faceArray.onePair = new OnePair();
- this.faceArray["twoPair"] = new TwoPair();
- this.faceArray["threeOfAKind"] = new ThreeOfAKind();
- this.faceArray["fourOfAKind"] = new FourOfAKind();
- this.faceArray["smallStraight"] = new SmallStraight();
- this.faceArray["largeStraight"] = new LargeStraight();
- this.faceArray["fullHouse"] = new FullHouse();
- this.faceArray["chance"] = new Chance();
- this.faceArray["yahtzee"] = new Yahtzee();
+ this.faceArray.twoPair = new TwoPair();
+ this.faceArray.threeOfAKind = new ThreeOfAKind();
+ this.faceArray.fourOfAKind = new FourOfAKind();
+ this.faceArray.smallStraight = new SmallStraight();
+ this.faceArray.largeStraight = new LargeStraight();
+ this.faceArray.fullHouse = new FullHouse();
+ this.faceArray.chance = new Chance();
+ this.faceArray.yahtzee = new Yahtzee();
}
-//statisk array till Game
Game.faceDescr = {
"aces": "ETTOR: Det gäller att få ihop så stort antal ETTOR som möjligt. Varje ETTA ger en poäng.",
"twos": "TVÅOR-SEXOR: Här gäller samma som för ETTOR. Varje lika tärning ger poäng efter antalet prickar....",
@@ -78,61 +71,57 @@ Game.faceDescr = {
"chance": "glöm inte att skriva ner den regeln också",
"yahtzee": "YATZY: Här måste en spelare få alla fem tärningarna lika. YATZY ger alltid 50 poäng."
};
-// prototyp för att ha metoder för objekt
-// http://mckoss.com/jscript/object.htm
-Game.prototype.roll = function() {
- if (this.currentRoundNr < maxRounds) {
- this.rounds[this.currentRoundNr] = new Round();
- try {
- this.rounds[this.currentRoundNr].roll();
- this.currentRoundNr++;
+Game.prototype = {
+ roll : function() {
+ if (this.currentRoundNr < maxRounds) {
+ this.rounds[this.currentRoundNr] = new Round();
+ try {
+ this.rounds[this.currentRoundNr].roll();
+ this.currentRoundNr++;
+ }
+ catch(err) {
+ throw err;
+ }
+
}
- catch(err) {
- //skicka exeption vidare
- throw err;
+ else {
+ throw "du har slösat alla dina rounds!";
}
-
- }
- else {
- //vi skapar nya exceptions
- // kolla här:
- // http://www.w3schools.com/js/js_throw.asp
- throw "du har slösat alla dina rounds!";
- //showMessage("du har slösat alla dina rounds!");
+ },
+ updateSums : function() {
+ this.updateUpperSum();
+ this.updateBonus();
+ this.updateLowerSum();
+ },
+ updateUpperSum : function() {
+ this.upperSum =
+ this.faceArray.aces.face.score
+ + this.faceArray.twos.face.score
+ + this.faceArray.threes.face.score
+ + this.faceArray.fours.face.score
+ + this.faceArray.fives.face.score
+ + this.faceArray.sixes.face.score;
+ },
+ updateBonus : function() {
+ if (this.upperSum > 62) {
+ this.bonus = 50;
+ }
+ },
+ updateLowerSum : function() {
+ this.lowerSum = this.upperSum
+ + this.bonus
+ + this.faceArray.onePair.face.score
+ + this.faceArray.twoPair.face.score
+ + this.faceArray.threeOfAKind.face.score
+ + this.faceArray.fourOfAKind.face.score
+ + this.faceArray.smallStraight.face.score
+ + this.faceArray.largeStraight.face.score
+ + this.faceArray.fullHouse.face.score
+ + this.faceArray.chance.face.score
+ + this.faceArray.yahtzee.face.score;
}
};
-Game.prototype.updateSums = function() {
- this.updateUpperSum();
- this.updateBonus();
- this.updateLowerSum();
-};
-Game.prototype.updateUpperSum = function() {
- this.upperSum =
- this.faceArray["aces"].face.score
- + this.faceArray["twos"].face.score
- + this.faceArray["threes"].face.score
- + this.faceArray["fours"].face.score
- + this.faceArray["fives"].face.score
- + this.faceArray["sixes"].face.score;
-}
-Game.prototype.updateBonus = function() {
- if (this.upperSum > 62) {
- this.bonus = 50;
- }
-}
-Game.prototype.updateLowerSum = function() {
- this.lowerSum = this.upperSum
- + this.bonus
- + this.faceArray["onePair"].face.score
- + this.faceArray["twoPair"].face.score
- + this.faceArray["threeOfAKind"].face.score
- + this.faceArray["fourOfAKind"].face.score
- + this.faceArray["smallStraight"].face.score
- + this.faceArray["largeStraight"].face.score
- + this.faceArray["fullHouse"].face.score
- + this.faceArray["chance"].face.score
- + this.faceArray["yahtzee"].face.score;
-}
+
function Round() {
this.throwing = 0; // only three throwings available
this.dice = new Array(new Die(), new Die(), new Die(), new Die(), new Die()); // five dice
@@ -157,7 +146,7 @@ Round.prototype.roll = function() {
throw "du får bara kasta tre gånger";
//showMessage("du får bara kasta tre gånger");
}
-}
+};
function Die() {
this.amount = 0;
this.locked = false; // default - false;
@@ -169,13 +158,13 @@ function Face() { //en allmän prototyp -typ superklass
this.potential = 0; // för att eventuellt visa möjliga resultat vid mouseover
this.reported = false;
}
-Face.prototype.prototype = {
+Face.prototype = {
report: function() {
this.score = this.potential;
this.potential = 0;
this.reported = true;
}
-}
+};
//helpers
function getSumOf(diceArray, faceNumber) {
@@ -202,7 +191,7 @@ function Aces() {
}
Aces.prototype.updatePotential = function(dice) {
this.face.potential = getSumOf(dice, 1);
-}
+};
function Twos() {
this.face = new Face();
}
@@ -212,15 +201,17 @@ Twos.prototype.updatePotential = function(dice) {
function Threes() {
this.face = new Face();
}
-Threes.prototype.updatePotential = function(dice) {
- this.face.potential = getSumOf(dice, 3);
-}
+Threes.prototype = {
+ updatePotential : function(dice) {
+ this.face.potential = getSumOf(dice, 3);
+ }
+};
function Fours() {
this.face = new Face();
}
Fours.prototype.updatePotential = function(dice) {
this.face.potential = getSumOf(dice, 4);
-}
+};
function Fives() {
this.face = new Face();
}
@@ -236,19 +227,23 @@ Sixes.prototype.updatePotential = function(dice) {
function OnePair() {
this.face = new Face();
}
-OnePair.prototype.updatePotential = function(dice) {
- //vi måste hämta om möjligt två möjliga par och välja det med högsta poäng
- var counter = 0;
- for (var i = 1; i <= 6; i++) {
- if (getQuantityOf(dice, i) >= 2) {
- this.face.potential = i * 2; // vi räknar från 1 till 6, då blir det med högsta
- counter++;
- if (counter >= 2) { // det kan vara högst två par
- break;
+OnePair.prototype = {
+ updatePotential: function(dice) {
+ //get a pair of the same dice with the highest score
+ var counter = 0;
+ for (var i = 1; i <= 6; i++) {
+ if (getQuantityOf(dice, i) >= 2) {
+ //begin with 1 and go to 6 in order to get the highest possible
+ this.face.potential = i * 2;
+ counter++;
+ //there can be only two pairs with same dice from five dice
+ if (counter >= 2) {
+ break;
+ }
}
}
}
-}
+};
function TwoPair() {
this.face = new Face();
}
@@ -265,7 +260,7 @@ TwoPair.prototype.updatePotential = function(dice) {
}
}
}
-}
+};
function ThreeOfAKind() {
this.face = new Face();
}
@@ -312,47 +307,52 @@ LargeStraight.prototype.updatePotential = function(dice) {
this.face.potential = 20;
}
}
-function FullHouse() { // Kåk
+function FullHouse() {
this.face = new Face();
}
-FullHouse.prototype.updatePotential = function(dice) {
- // Det fungerar nästan som TwoPair, fast det ena är med tre stycken
- var counter = 0;
- var sum = 0;
- for (var i = 1; i <= 6; i++) {
- if (getQuantityOf(dice, i) == 2) {
- sum += i * 2;
- counter++;
+FullHouse.prototype = {
+ updatePotential: function(dice) {
+ var counter = 0;
+ var sum = 0;
+ for (var i = 1; i <= 6; i++) {
+ if (getQuantityOf(dice, i) == 2) {
+ sum += i * 2;
+ counter++;
+ }
+ if (getQuantityOf(dice, i) == 3) {
+ sum += i * 3;
+ counter++;
+ }
}
- if (getQuantityOf(dice, i) == 3) {
- sum += i * 3;
- counter++;
+ if (counter == 2) {
+ this.face.potential = sum;
}
}
- if (counter == 2) { // det måste finnas två "par"
- this.face.potential = sum;
- }
}
function Chance() {
this.face = new Face();
}
-Chance.prototype.updatePotential = function(dice) {
- //this.face.potential = upperSectionCalculate(dice, 6);
- this.face.potential = 0;
- for (var i = 0; i < 5; i++) {
- this.face.potential += dice[i].amount;
+Chance.prototype = {
+ updatePotential : function(dice) {
+ //this.face.potential = upperSectionCalculate(dice, 6);
+ this.face.potential = 0;
+ for (var i = 0; i < 5; i++) {
+ this.face.potential += dice[i].amount;
+ }
}
-}
+};
function Yahtzee() {
this.face = new Face();
}
-Yahtzee.prototype.updatePotential = function(dice) {
- if( (getQuantityOf(dice, 1) == 5)
- || (getQuantityOf(dice, 2) == 5)
- || (getQuantityOf(dice, 3) == 5)
- || (getQuantityOf(dice, 4) == 5)
- || (getQuantityOf(dice, 5) == 5)
- || (getQuantityOf(dice, 6) == 5)) {
- this.face.potential = 50;
+Yahtzee.prototype = {
+ updatePotential : function(dice) {
+ if( (getQuantityOf(dice, 1) == 5)
+ || (getQuantityOf(dice, 2) == 5)
+ || (getQuantityOf(dice, 3) == 5)
+ || (getQuantityOf(dice, 4) == 5)
+ || (getQuantityOf(dice, 5) == 5)
+ || (getQuantityOf(dice, 6) == 5)) {
+ this.face.potential = 50;
+ }
}
-}
+};
Please sign in to comment.
Something went wrong with that request. Please try again.