xxxxxxxxxx
302
deck=[];
deck2=[]; // true order deck;
var ndeck=[]; // Deck after dealing
var count=0; // How many times we have gone
var complete=[]; // Keeps track of all completed
var pows=[]; // Keeps track of what power we are on
var indx;
var same=false; // turns true if deck is in order
//through the deck
var limit=509;
deckx=5;
decky=5;
var numShuff=0;
var numdeal=1; // Number of card to deal each time
cw=65;
ch=100;
var dl=1; // deck length
var mySound;
function preload() {
soundFormats('wav', 'ogg');
mySound = loadSound('ding.wav');
}
function setup() {
//frameRate(.1)
createCanvas(1100, 1400);
initializeDeck();
background(0);
}
function initializeDeck()
{
deck=newDeck(dl); //Create unshuffled deck of 52 cards
deck2=newDeck(dl);
//deck[0].name=7;
// console.log(deck.pop());
for(var i=0; i<numShuff; i++) // Shuffle a lot
{
deck=shuffleDeck(deck); // Shuffle deck just created
}
//console.table(deck);
}
function draw() {
indx=dl-1;
//background(0);
row=0;
col=0;
// for(var i=0; i<deck.length; i++)
// {
// showCard(col*cw,((10+ch)*row)+10, deck[indx]);
// if(col==12)
// {
// col=0;
// row++;
// }
// else
// {
// col+=50;
// }
// indx--;
// }
fill(0);
rect(width-200, 0, 200,200);
fill("white");
text("Cycles: " + count, width-200, 50);
text("Deck Size: " + dl, width-200, 70);
same=true;
for(var f=0; f<deck.length && same; f++)
{
if(deck[f].value!=deck2[f].value)
{
same=false;
}
}
//if(count<limit)
if(!same || count==0)
{
count++;
execute();
if(count>dl)
{
dl++;
count=0;
initializeDeck();
}
}
else
//if(same)
{
// background(0);
if(complete.length<20)
{
if(count<=dl)
{
complete.push(count);
pows.push(dl);
mySound.play();
}
col=1;
row=20;
for(var i=0; i<complete.length; i++)
{
if(row==1340)
{
row=20;
col+=5;
}
else
{
row+=20
}
text(pows[i]+ ": " + complete[i], col*20, row);
//text(i+1 + ": " + complete[i], col*20, row);
}
dl*=2;
initializeDeck();
count=0;
}
}
}
function newDeck(n) // Creates a new deck of 52 cards.
{
var val=1;
tdeck=[]; // Temporary Deck used for builing
for(var i=0; i<n; i++)
{
tdeck.push(new Card(val));
val++;
}
return tdeck;
}
function shuffleDeck (inDeck){
var unshuffled=inDeck;
var temp=0;
for(var i=0; i<unshuffled.length; i++)
{
temp=unshuffled[i];
swap=floor(random(unshuffled.length));
unshuffled[i]=unshuffled[swap];
unshuffled[swap]=temp;
}
return unshuffled;
}
function showCard(x,y,incard)
{
fill('white');
stroke('black');
textSize(20);
push();
translate(x,y);
rect(0,0, cw,ch);
fill(0);
text(incard.value, 0+5,0+20);
rotate(radians(180));
//rect(0,0, cw,ch);
text(incard.value, -cw+5, 25-ch);
//text(incard.pic, -cw+25, 25-ch);
pop();
}
function drawBoard()
{
background(0);
}
// This function takes the deck and
// deals one down, then one to the back
// of the deck until the entire deck has been
// distributed to the new deck
function execute()
{
var ct=0; // Number of cards we have cycled;
var temp; // Temporary card
ndeck.length=0;
do{
temp==undefined;
ndeck.push(deck.pop());
ct++;
if(deck.length)
{
temp=deck[deck.length-1];
}
for(var d=deck.length-1; d>0; d--)
{
deck[d]=deck[d-1];
}
if(deck.length)
{
deck[0]=temp;
ct++;
}
}while(deck.length);
for(var e=0; e<ndeck.length; e++)
{
deck[e]=ndeck[e];
}
}
function keyPressed()
{
// frameRate(1);
//console.log(keyCode);
switch(keyCode)
{
case 38:dl++;
initializeDeck();
count=0;
break;
case 40: dl--;
initializeDeck();
count=0;
break;
}
}