Selaa lähdekoodia

Feature: initial commit

Febbweiss 12 vuotta sitten
commit
83e5559066
6 muutettua tiedostoa jossa 455 lisäystä ja 0 poistoa
  1. 184 0
      css/cards.css
  2. BIN
      images/cards.png
  3. 31 0
      index.html
  4. 59 0
      js/cards.js
  5. 180 0
      js/pyramid.js
  6. 1 0
      lib/jquery-1.7.1.min.js

+ 184 - 0
css/cards.css

@@ -0,0 +1,184 @@
+.card {
+	position: absolute;
+	width: 80px;
+	height: 124px;
+	background-image: url('../images/cards.png');
+	float: left;
+}
+
+.card.selected {
+	border: 2px solid yellow;
+}
+
+.clubs_1 {
+	background-position: 0px 0px;
+}
+.clubs_2 {
+	background-position: -80px 0px;
+}
+.clubs_3 {
+	background-position: -160px 0px;
+}
+.clubs_4 {
+	background-position: -240px 0px;
+}
+.clubs_5 {
+	background-position: -320px 0px;
+}
+.clubs_6 {
+	background-position: -400px 0px;
+}
+.clubs_7 {
+	background-position: -480px 0px;
+}
+.clubs_8 {
+	background-position: -560px 0px;
+}
+.clubs_9 {
+	background-position: -640px 0px;
+}
+.clubs_10 {
+	background-position: -720px 0px;
+}
+.clubs_11 {
+	background-position: -800px 0px;
+}
+.clubs_12 {
+	background-position: -880px 0px;
+}
+.clubs_13 {
+	background-position: -960px 0px;
+}
+
+.diamonds_1 {
+	background-position: 0px -125px;
+}
+.diamonds_2 {
+	background-position: -80px -125px;
+}
+.diamonds_3 {
+	background-position: -160px -125px;
+}
+.diamonds_4 {
+	background-position: -240px -125px;
+}
+.diamonds_5 {
+	background-position: -320px -125px;
+}
+.diamonds_6 {
+	background-position: -400px -125px;
+}
+.diamonds_7 {
+	background-position: -480px -125px;
+}
+.diamonds_8 {
+	background-position: -560px -125px;
+}
+.diamonds_9 {
+	background-position: -640px -125px;
+}
+.diamonds_10 {
+	background-position: -720px -125px;
+}
+.diamonds_11 {
+	background-position: -800px -125px;
+}
+.diamonds_12 {
+	background-position: -880px -125px;
+}
+.diamonds_13 {
+	background-position: -960px -125px;
+}
+
+.hearts_1 {
+	background-position: 0px -249px;
+}
+.hearts_2 {
+	background-position: -80px -249px;
+}
+.hearts_3 {
+	background-position: -160px -249px;
+}
+.hearts_4 {
+	background-position: -240px -249px;
+}
+.hearts_5 {
+	background-position: -320px -249px;
+}
+.hearts_6 {
+	background-position: -400px -249px;
+}
+.hearts_7 {
+	background-position: -480px -249px;
+}
+.hearts_8 {
+	background-position: -560px -249px;
+}
+.hearts_9 {
+	background-position: -640px -249px;
+}
+.hearts_10 {
+	background-position: -720px -249px;
+}
+.hearts_11 {
+	background-position: -800px -249px;
+}
+.hearts_12 {
+	background-position: -880px -249px;
+}
+.hearts_13 {
+	background-position: -960px -249px;
+}
+
+.spades_1 {
+	background-position: 0px -374px;
+}
+.spades_2 {
+	background-position: -80px -374px;
+}
+.spades_3 {
+	background-position: -160px -374px;
+}
+.spades_4 {
+	background-position: -240px -374px;
+}
+.spades_5 {
+	background-position: -320px -374px;
+}
+.spades_6 {
+	background-position: -400px -374px;
+}
+.spades_7 {
+	background-position: -480px -374px;
+}
+.spades_8 {
+	background-position: -560px -374px;
+}
+.spades_9 {
+	background-position: -640px -374px;
+}
+.spades_10 {
+	background-position: -720px -374px;
+}
+.spades_11 {
+	background-position: -800px -374px;
+}
+.spades_12 {
+	background-position: -880px -374px;
+}
+.spades_13 {
+	background-position: -960px -374px;
+}
+
+.joker_black {
+	background-position: 0px -498px;
+}
+.joker_red {
+	background-position: -80px -498px;
+}
+.background {
+	background-position: -160px -498px;
+}
+.none {
+	background-position: -240px -498px;
+}

BIN
images/cards.png


+ 31 - 0
index.html

@@ -0,0 +1,31 @@
+<!--
+Copyright (c) 2013 Fabrice ECAILLE aka Febbweiss
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+-->
+<html>
+	<head>
+		<title>Pyramid.js</title>
+		<script src="lib/jquery-1.7.1.min.js" type="text/javascript"></script>
+		<script src="js/pyramid.js" type="text/javascript"></script>
+		<script src="js/cards.js" type="text/javascript"></script>
+		<link href="css/cards.css" type="text/css"rel="stylesheet">
+	</head>
+	<body>
+		
+		<div id="playground" style="position: relative;float: left;">
+			<div id="stock" class="card background" style="position: absolute; left: 0;"></div>
+			<img id="drawn" class="card none"style="position: absolute; right: 0;" />
+		</div>
+			
+		<script language="javascript">
+			$(document).ready(function () {
+				Pyramid.init();
+			});
+		</script>
+	</body>
+</html>

+ 59 - 0
js/cards.js

@@ -0,0 +1,59 @@
+/*
+Copyright (c) 2013 Fabrice ECAILLE aka Febbweiss
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+var CARD_WIDTH = 80;
+var CARD_HEIGHT = 124;
+
+var Deck = {
+	cards: [
+	        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
+	        14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
+	        27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
+	        40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52
+	        ],
+	        
+	values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
+	
+	value: function(card) {
+		return card % Deck.values.length + 1;
+	},
+
+	familly: function( card ) {
+		if( card < 14 )
+			return "clubs";
+		else if( card < 27 )
+			return "diamonds";
+		else if( card < 40 )
+			return "hearts";
+		else
+			return "spades";
+	},
+	
+	point: function(card) {
+		return Deck.values[Deck.value(card) - 1];
+	},
+	
+	shuffle: function() {
+		for (var i = 0; i < Deck.cards.length; ++i) {
+			var x = parseInt(Math.random() * Deck.cards.length);
+			var y = parseInt(Math.random() * Deck.cards.length);
+			if (x == y)
+				continue;
+			var tmp = Deck.cards[x];
+			Deck.cards[x] = Deck.cards[y];
+			Deck.cards[y] = tmp;
+		}
+		return Deck.cards;
+	},
+	
+	pop: function() {
+		return Deck.cards.pop();
+	}
+}

+ 180 - 0
js/pyramid.js

@@ -0,0 +1,180 @@
+/*
+Copyright (c) 2013 Fabrice ECAILLE aka Febbweiss
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+var Pyramid = {
+	initial_set: [1, 2, 3, 4, 5, 6, 7],
+	
+	pyramid: new Array(7),
+	
+	selected: null,
+	drawn: new Array(),
+	drawn_count: 0,
+	
+	init: function() {
+		var maxCard = Pyramid.initial_set[ Pyramid.initial_set.length - 1];
+		var initial_x = (maxCard * CARD_WIDTH) / 2;
+		
+		var initial_y = 0;
+		var playground = $("#playground");
+		playground.width( maxCard * CARD_WIDTH);
+		playground.height( Pyramid.initial_set.lentgh * CARD_HEIGHT);
+		
+		
+		Deck.shuffle();
+		
+		for( var i = 0; i < Pyramid.initial_set.length; i++) {
+			var cards = Pyramid.initial_set[i];
+			var offsetx = initial_x - cards * CARD_WIDTH / 2;
+			Pyramid.pyramid[i] = new Array(cards);
+			for( var j = 0; j < cards; j++ ) {
+				var value = Deck.pop();
+				Pyramid.pyramid[i][j] = value;
+				$("<img/>",{
+					id:"card" + i +j,
+					class: "card " + Deck.familly( value ) + "_" + Deck.value(value),
+					style: "left: " + offsetx + "px; top: " + ( i * CARD_HEIGHT / 3 ) + "px;"
+				}).appendTo(playground);
+				
+				offsetx += CARD_WIDTH;
+			}
+		}
+
+		$("#stock").width( CARD_WIDTH ).height( CARD_HEIGHT );
+		$("#drawn").width( CARD_WIDTH ).height( CARD_HEIGHT );
+		$("#control").width( CARD_WIDTH ).height( 2 * CARD_HEIGHT );
+		
+		$(".card").click( Pyramid.click );
+	},
+
+	click: function(e) {
+		var elt = $(this);
+		var id = elt.attr('id');
+		
+		if( id === "stock" ) {
+			Pyramid.draw();
+			return false;
+		}
+		
+		var card = Pyramid.card(elt);
+		
+		if( !card )
+			return false;
+		
+		var point = Deck.point(card);
+		
+		if( point == 13 ) {
+			Pyramid.remove(elt);
+			Pyramid.win();
+			return true;
+		}
+		
+		if( elt.hasClass("selected") ) {
+			elt.removeClass("selected");
+			Pyramid.selected = null;
+			return false;
+		} 
+		
+		var selected = Pyramid.selected; 
+		if( selected ) {
+			var card = Pyramid.card(Pyramid.selected);
+			
+			if( (point + Deck.point(card)) == 13 ) {
+				Pyramid.remove(elt);
+				Pyramid.remove( selected );
+				Pyramid.win();
+			} else {
+				selected.removeClass("selected");
+			}
+			
+			Pyramid.selected = null;
+		} else {
+			elt.addClass("selected");
+			Pyramid.selected = elt;
+		}
+	},
+	
+	next: function() {
+		var card = Deck.pop();
+		Pyramid.drawn.push( card );
+		$("#drawn").removeClass().addClass("card").addClass("card " + Deck.familly( card ) + "_" + Deck.value(card));
+		if( Deck.cards.length == 0 ) {
+			$("#stock").removeClass();
+			return false;
+		}
+	},
+	
+	card: function(elt) {
+		var id = elt.attr('id');
+	
+		if( id === "drawn" ) {
+			if( Pyramid.drawn.length > 0 ) {
+				return Pyramid.drawn[Pyramid.drawn.length - 1];
+			} else
+				return false;
+		} else {
+			var i = parseInt(id.charAt(4));
+			var j = parseInt(id.charAt(5));
+			
+			if( typeof Pyramid.pyramid[i + 1] != "undefined" 
+				&& ( typeof Pyramid.pyramid[i + 1][ j ] != "undefined" || typeof Pyramid.pyramid[i + 1][ j + 1] != "undefined") )
+				return false;
+			
+			return Pyramid.pyramid[i][j];
+		}
+	},
+	
+	remove: function(elt) {
+		var id = elt.attr('id');
+		
+		if( id === "drawn" ) {
+			Pyramid.drawn.pop();
+			$("#drawn").removeClass();
+			if( Pyramid.drawn.length > 0 ) {
+				card = Pyramid.drawn[Pyramid.drawn.length - 1];
+				$("#drawn").addClass("card").addClass("card " + Deck.familly( card ) + "_" + Deck.value(card));
+			}
+		} else {
+			var i = parseInt(id.charAt(4));
+			var j = parseInt(id.charAt(5));
+			Pyramid.pyramid[i][j] = undefined;
+			elt.remove();
+		}
+	},
+	
+	draw: function() {
+		if( Deck.cards.length > 0 ) {
+			Pyramid.drawn_count++;
+			Pyramid.next();
+			return false;
+		}
+		
+		Deck.cards = Pyramid.drawn.reverse();
+		Pyramid.drawn = new Array();
+		$("#drawn").removeClass();
+		$("#stock").addClass("card").addClass("background");
+		$("#")
+	},
+	
+	win: function() {
+		var win = true;
+		$.each($(".card"), function(index, elt) {
+			var id = $(elt).attr('id');
+			if(  id !== "stock" && id !== "drawn")
+				win = false;
+		});
+		if( win ) 
+			Pyramid.show_win();
+		return win;
+	},
+	
+	show_win: function() {
+		alert( "Win in " + Pyramid.drawn_count + " draws !!!");
+	}
+}

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 1 - 0
lib/jquery-1.7.1.min.js


Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä