Skip to content

Commit

Permalink
Added files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Perrishnikov committed Feb 20, 2016
1 parent 4e1c0d8 commit 1377f85
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 180 deletions.
12 changes: 11 additions & 1 deletion cards.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Another edit
.card {
background-image: url("graphics/cardback.gif");
border-color: #808080 #000000 #000000 #808080;
border-width: 2px;
border-width: 1px;
border-style: solid;
font-size: 20pt;
position: absolute;
Expand All @@ -25,6 +25,16 @@ Another edit
height: 5.00em;
}

.suitOptions {
font-size: 20pt;
float: top;
margin-right: 1em;
margin-left: 1em;
position: relative;
width: 1.00em;
height: 1.00em;
}

.front {
background-color: #ffffff;
color: #000000;
Expand Down
127 changes: 0 additions & 127 deletions cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,131 +253,4 @@ function cardCreateNode() {
// Return the card node.

return cardNode;
}

//=============================================================================
// Stack Object
//=============================================================================

//-----------------------------------------------------------------------------
// Stack constructor function.
//-----------------------------------------------------------------------------

function Stack() {

// Create an empty array of cards.

this.cards = new Array();

this.makeDeck = stackMakeDeck;
this.shuffle = stackShuffle;
this.deal = stackDeal;
this.draw = stackDraw;
this.addCard = stackAddCard;
this.combine = stackCombine;
this.cardCount = stackCardCount;
}

//-----------------------------------------------------------------------------
// stackMakeDeck(n): Initializes a stack using 'n' packs of cards.
//-----------------------------------------------------------------------------

function stackMakeDeck(n) {

var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K");
var suits = new Array("C", "D", "H", "S");
var i, j, k;
var m;

m = ranks.length * suits.length;

// Set array of cards.

this.cards = new Array(n * m);

// Fill the array with 'n' packs of cards.

for (i = 0; i < n; i++)
for (j = 0; j < suits.length; j++)
for (k = 0; k < ranks.length; k++)
this.cards[i * m + j * ranks.length + k] = new Card(ranks[k], suits[j]);
}

//-----------------------------------------------------------------------------
// stackShuffle(n): Shuffles a stack of cards 'n' times.
//-----------------------------------------------------------------------------

function stackShuffle(n) {

var i, j, k;
var temp;

// Shuffle the stack 'n' times.

for (i = 0; i < n; i++)
for (j = 0; j < this.cards.length; j++) {
k = Math.floor(Math.random() * this.cards.length);
temp = this.cards[j];
this.cards[j] = this.cards[k];
this.cards[k] = temp;
}
}

//-----------------------------------------------------------------------------
// stackDeal(): Removes the first card in the stack and returns it.
//-----------------------------------------------------------------------------

function stackDeal() {

if (this.cards.length > 0)
return this.cards.shift();
else
return null;
}

//-----------------------------------------------------------------------------
// stackDraw(n): Removes the indicated card from the stack and returns it.
//-----------------------------------------------------------------------------

function stackDraw(n) {

var card;

if (n >= 0 && n < this.cards.length) {
card = this.cards[n];
this.cards.splice(n, 1);
}
else
card = null;

return card;
}

//-----------------------------------------------------------------------------
// stackAdd(card): Adds the given card to the stack.
//-----------------------------------------------------------------------------

function stackAddCard(card) {

this.cards.push(card);
}

//-----------------------------------------------------------------------------
// stackCombine(stack): Adds the cards in the given stack to the current one.
// The given stack is emptied.
//-----------------------------------------------------------------------------

function stackCombine(stack) {

this.cards = this.cards.concat(stack.cards);
stack.cards = new Array();
}

//-----------------------------------------------------------------------------
// stackCardCount(): Returns the number of cards currently in the stack.
//-----------------------------------------------------------------------------

function stackCardCount() {

return this.cards.length;
}
60 changes: 42 additions & 18 deletions deck.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
// Deck functions on HTML

var deck, hand, discards;

window.onload = init;
var numDecks = 1; //updated in deck.js reset()
var numCards; //set in cards.js stackMakeDeck()
var numInDeck;
var numInDiscard = 0;

window.onload = init;
function init() {

deck = new Stack();
hand = new Stack();
discards = new Stack();
deck.makeDeck(1); //set initial # of decks

// console.log(numInDiscard);
deck.makeDeck(numDecks); //set initial # of decks
displayNumCards(); //info.js
numInDiscard = 0;
displayNumInDiscard(); //info.js

display();
}

function shuffle() {
if (deck == null) return;
deck.shuffle(1);
deck.shuffle(1); //increase for better shuffling?
console.log("Deck shuffled");
display();
}

function deal() {
var i;
var e = document.getElementById("dealNum");
var numDealt= e.value;
var numDealt= e.value; //updates

if (deck == null) return;

if (deck.cardCount() < numDealt)
if (deck.cardCount() < numDealt) //run cardCount
alert("Not enough cards.");
else {
discard();
for (i = 0; i < numDealt; i++)
hand.addCard(deck.deal());
for (var i = 0; i < numDealt; i++)
hand.addCard(deck.deal()); //run addCard, deal from deck
}

display();
Expand All @@ -47,13 +56,15 @@

function reset() {

var el;

if (deck == null) return;

discards.combine(hand);
deck.combine(discards);
display();
//New code to rerun init() with # of decks
var e = document.getElementById("deckNum");
numDecks = e.value
init();
//Old code to combine other stacks
// discards.combine(hand);
// deck.combine(discards);
// display();
}

function display() {
Expand All @@ -64,48 +75,61 @@
// Note: only a fraction of the cards in the deck and discard pile are
// displayed, just enough to get an idea of the number of cards in each.

// deck
left = 0;
top = 0;
el = document.getElementById("deck");
while (el.firstChild != null)
el.removeChild(el.firstChild);
n = deck.cardCount();
for (i = 0; i < Math.round(n / 5); i++) {
for (var i = 0; i < Math.round(n / 5); i++) {
node = deck.cards[i].createNode();
node.firstChild.style.visibility = "hidden";
node.style.left = left + "em";
node.style.top = top + "em";
el.appendChild(node);
left += 0.10;
top += 0.05;

displayNumInDeck(); //info.js

}

//hand
left = 0;
top = 0;
el = document.getElementById("hand");
while (el.firstChild != null)
el.removeChild(el.firstChild);
for (i = 0; i < hand.cardCount(); i++) {
for (var i = 0; i < hand.cardCount(); i++) {
node = hand.cards[i].createNode();
node.style.left = left + "em";
node.style.top = top + "em";
el.appendChild(node);
left += 1.00;
top += 0.25;
//This logs 7//
//console.log(hand.cards.length);
}

//discards
left = 0;
top = 0;
el = document.getElementById("discards");
while (el.firstChild != null)
el.removeChild(el.firstChild);
n = discards.cardCount();
for (i = n - Math.round(n / 5); i < n; i++) {
for (var i = n - Math.round(n / 5); i < n; i++) {
node = discards.cards[i].createNode();
node.style.left = left + "em";
node.style.top = top + "em";
el.appendChild(node);
left += 0.10;
top += 0.05;

numInDiscard = discards.cards.length; //info.js
// console.log(discards.cards.length);
displayNumInDiscard()

}
}
Loading

0 comments on commit 1377f85

Please sign in to comment.