Top Trumps - Card Dealing Issue


#1


I've been working on a Bionicle-based Top Trumps game where you play against a computer but I can't get this cardDeal function (in the SetupCode section) to evenly distribute cards.
In other words, since I've added six cards to the game, the player and the computer should get three each, but this doesn't always happen.

Key (for convenience):

  • totCardsCompL: how many cards have currently been dealt to the computer
  • totCardsPlayL: how many cards have currently been dealt to the player
  • belongsTo: a way of defining which 'deck' the card is in - the player's (1) or the computer's (0). I used a random number generator to define which card goes to which deck.
  • Once either totCards variable reaches half the total number of cards (in this case 3), the relevant if statement should stop running and distribute the remaining cards into the other 'underpopulated' deck.

If you run the code, you'll see what I mean: the live number of cards in the computer's deck is displayed ("tCCL =___"), followed by the deck each card goes to.
I'm pretty sure it's not working because the totCards variables aren't being recognised as updating by the if loop - hence the first if statement (the && one) is always true. However I've wasted too many hours trying to understand how to fix it, so I'd appreciate any help anyone can provide on how to fix it.


function Toa(N,T,S,A,I,B,U,E) {
    this.Name = N;
    this.Team = T;
    this.Strength = S;
    this.Agility = A;
    this.Intellect = I;
    this.Boldness = B;
    this.Unity = U;
    this.Experience = E;
    this.belongsTo = "None";
}
var allCards = [
    
Kalama = new Toa
("Kalama, Toa of Fire","Toa Valda",6,5,8,6,8,3),
Vhisola = new Toa
("Vhisola, Toa of Water","Toa Valda",5,8,5,7,4,2),
Hafu = new Toa
("Hafu, Toa of Stone","Toa Valda",8,4,5,8,7,2),
Tamaru = new Toa
("Tamaru, Toa of Air","Toa Valda",4,9,6,6,4,2),
Ehrye = new Toa
("Ehrye, Toa of Ice","Toa Valda",6,4,9,6,5,1),
Kaj = new Toa
("Kaj, Toa of Earth","Toa Valda",7,6,5,7,2,4)

];


//SetupCode


var cardDeal = function() {
    
    totCardsCompL = 0;
    totCardsPlayL = 0;
    
    for (i=0;i<allCards.length;i++) {
        
        
        if ((totCardsCompL && totCardsPlayL) <=
        allCards.length /2) {
            allCards[i]["belongsTo"] =
            Math.floor(2* Math.random());
        }
        else if (totCardsCompL <= allCards.Length /2){
            allCards[i]["belongsTo"] = 0;
        }
        else if (totCardsPlayL <= allCards.Length /2){
            allCards[i]["belongsTo"] = 1;
        }
        
        if (allCards[i]["belongsTo"] == 0) {
                totCardsCompL++;
            }
        else if (allCards[i]["belongsTo"] == 1) {
                totCardsPlayL++;
            }
    
    console.log("tCCL =" + totCardsCompL);
    }
};

//PlayCode

cardDeal();

//TestCode
//Test properties of all cards
for (i=0;i<allCards.length;i++) {
        console.log(allCards[i]["Name"] + ": " + allCards[i]["belongsTo"]);
}