PDA

View Full Version : solitaire array code



SacrificialLamb
February 5th, 2007, 05:41 PM
i'm making a solitaire game and am havening a problem with the dealing (arranging the cards in rows)this is the code i have for that

function solitaire() {
//shuffleC()
w = 92
h = 165

var row1:Array = Array();
var row2:Array = Array();
var row3:Array = Array();
var row4:Array = Array();
var row5:Array = Array();
var row6:Array = Array();
var row7:Array = Array();
var rows:Array = [row1, row2, row3, row4, row5, row6, row7];

for (i=0;i<(rows.length);i++){
for ((j=0)(f=i);j<i;(j++)(f+=(7-j))){
va = rows[i]
//rows[i].push(deck[f])
rows[i][j] = deck[f]
}
}
//tester
trace(deck.toString())
for (i=0;i<rows.length;i++){
trace(i +": " +rows[i].toString())
}
}

the problem is this

dq,d8,s2,cA,c4,d10,sA,ck,s10,c7,c5,sk,h7,d5,cq,s9, c2,c9,h6,c3,s6,c10,s7,h8,d4,h10,s5,s4,hq,h2,sq,hk, h5,c8,hj,d7,s3,dj,dk,h4,s8,d9,cj,d3,h3,sj,c6,d6,h9 ,d2,dA,hA
0:
1: d8
2: s2,c7
3: cA,c5,c2
4: c4,sk,c9,s7
5: d10,h7,h6,h8,s4
6: sA,d5,c3,d4,hq,hk
it skips the first row and misses a card off each deck and misses cards in the array that will become important latter




and hear is the deck code so you can test it with out havening to wright this out

function newDeck() {
deck = new Array();
deck = ["hA", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10", "hj", "hq", "hk",
"cA", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "cj", "cq", "ck",
"dA", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10", "dj", "dq", "dk",
"sA", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "sj", "sq", "sk"]
}

Nich
February 5th, 2007, 09:16 PM
Change "j < i" to "j <= i" in your loop. Because you are starting on row 0, you want the loop to include the values when j and i = 0.

nathan99
February 5th, 2007, 10:15 PM
var root:MovieClip = this;
function createCard(w:Number, h:Number):MovieClip {
var nc:MovieClip = root.createEmptyMovieClip("c"+root.getNextHighestDepth(), root.getNextHighestDepth());
nc.beginFill(0xff0000)
nc.lineStyle(1)
nc.lineTo(w, 0);
nc.lineTo(w, h);
nc.lineTo(0, h);
nc.lineTo(0, 0);
return nc
}
for (i=7; i>0; i--) {
for (j=i; j>0; j--) {
tmp = createCard(10,15);
tmp._x = (12*j)+100
tmp._y = (13*-i)+100
}
}
hope that helps, sorry couldn't read too much into your wquestion only had 5 mins

nathan99
February 6th, 2007, 01:02 AM
var root:MovieClip = this;
var deck:Array = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var decks:Array = [deck.join("_").split("_"), deck.join("_").split("_"), deck.join("_").split("_"), deck.join("_").split("_")];
Array.prototype.removeCard = function(id:Number):Array {
for (var i in this) {
if (id == i) {
this.splice(i, 1);
}
}
return this;
};
function getKind(id:Number):String {
switch (id) {
case 0 :
return "Clubs";
case 1 :
return "Spades";
case 2 :
return "Hearts";
case 3 :
return "Diamonds";
}
}
function createCard(w:Number, h:Number):MovieClip {
var nc:MovieClip = root.createEmptyMovieClip("c"+root.getNextHighestDepth(), root.getNextHighestDepth());
nc.beginFill(0xff0000);
nc.lineStyle(1);
nc.lineTo(w, 0);
nc.lineTo(w, h);
nc.lineTo(0, h);
nc.lineTo(0, 0);
return nc;
}
for (i=7; i>0; i--) {
for (j=i; j>0; j--) {
var randDeck:Number = Math.floor(Math.random()*decks.length);
var randCard:Number = Math.floor(Math.random()*decks[randDeck].length);
tmp = createCard(10, 15);
tmp.cardFam = getKind(randDeck);
tmp.id = decks[randDeck][randCard];
decks[randDeck].removeCard(randCard);
tmp._x = (12*j)+100;
tmp._y = (13*-i)+100;
tmp.onPress = function():Void {
trace("Card Number: "+this.id+"\nCard Family: "+this.cardFam+"\n--------------");
};
}
}
trace("The pack still has: Clubs; "+decks[0]+"\nSpades; "+decks[1]+"\nHearts; "+decks[2]+"\nDiamonds; "+decks[3]);
Had another min