lundi 5 décembre 2016

transferring items among several arrays in javascript

I have got a pretty complicated issue, I tried everything and its not working properly. So the conception is (i just copied the interesting part of it, otherwise it would be few hundred more lines) :

The program is a card game and 24 cards (4 different colors, one is always stronger,it is called ADU) are distributed randomly among 4 players (4 arrays). The table where you put down the cards are represented by "asztal" array. First the human player puts a card, then the computers should reach in this order:

  1. If they have same color and higher value - pick that card
  2. If they have same color and any value - pick that card
  3. If they dont have matching color, any car from the special color set (for being simple, its the first card the loop would find in the array)
  4. If they dont have matching color, nor card from special color set, than the first element of the array (player[0]).

If you run my code, you would see it is not grabbing 1/1/1 card from each array, but sometimes more. And those cards do disappear, and not getting into the asztal array. My code: (http://ift.tt/2gYKClN)

function CardA(name,value,adu){
    this.name = name;
    this.value = value;
};
function CardB(name,value,adu){
    this.name = name;
    this.value = value;
};
function CardC(name,value,adu){
    this.name = name;
    this.value = value;
};
function CardD(name,value,adu){
    this.name = name;
    this.value = value;
};
CardA.prototype.adu = false;
CardB.prototype.adu = false;
CardC.prototype.adu = false;
CardD.prototype.adu = false;
var a9 = new CardA("Tök kilenc",0);
var a10 = new CardA("Tök tíz",10);
var aal = new CardA("Tök alsó",2);
var afel = new CardA("Tök felső",3);
var akir = new CardA("Tök király",4);
var aasz = new CardA("Tök ász",11);
var b9 = new CardB("Levél kilenc",0);
var b10 = new CardB("Levél tíz",10);
var bal = new CardB("Levél alsó",2);
var bfel = new CardB("Levél felső",3);
var bkir = new CardB("Levél király",4);
var basz = new CardB("Levél ász",11);
var c9 = new CardC("Makk kilenc",0);
var c10 = new CardC("Makk tíz",10);
var cal = new CardC("Makk alsó",2);
var cfel = new CardC("Makk felső",3);
var ckir = new CardC("Makk király",4);
var casz = new CardC("Makk ász",11);
var d9 = new CardD("Szív kilenc",0);
var d10 = new CardD("Szív tíz",10);
var dal = new CardD("Szív alsó",2);
var dfel = new CardD("Szív felső",3);
var dkir = new CardD("Szív király",4);
var dasz = new CardD("Szív ász",11);
CardC.prototype.adu = true;
var player1 = [c9,b9,b10,d9,a9,d10];
var player2 = [a10,aal,dal,c10,cal,bal];
var player3 = [bfel,bkir,basz,dfel,dkir,dasz];
var player4 = [afel,akir,aasz,cfel,ckir,casz];
var asztal = [];
asztal.push(player1.splice(0,1)[0]);
var player2card1 = function() {
for (i = 0; i < player2.length; i++) {
    if (Object.getPrototypeOf(player2[i]) == Object.getPrototypeOf(asztal[0]) && player2[i].value > asztal[0].value) {
        asztal.push(player2.splice(i,i+1)[0])
        return
    }
}
if (asztal.length == 1) {
for (i = 0; i < player2.length; i++) {
    if (Object.getPrototypeOf(player2[i]) == Object.getPrototypeOf(asztal[0])) {
        asztal.push(player2.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 1){
    for (i = 0; i < player2.length; i++) {
    if (player2[i].adu == true)  {
        asztal.push(player2.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 1) {
    asztal.push(player2.splice(0,1)[0])
    return
}
        };
var player3card1 = function() {
for (i = 0; i < player3.length; i++) {
    if (Object.getPrototypeOf(player3[i]) == Object.getPrototypeOf(asztal[0]) && player3[i].value > asztal[0].value) {
        asztal.push(player3.splice(i,i+1)[0])
        return
    }
}
if (asztal.length == 2) {
for (i = 0; i < player3.length; i++) {
    if (Object.getPrototypeOf(player3[i]) == Object.getPrototypeOf(asztal[0])) {
        asztal.push(player3.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 2){
    for (i = 0; i < player3.length; i++) {
    if (player3[i].adu == true)  {
        asztal.push(player3.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 2) {
    asztal.push(player3.splice(0,1)[0])
    return
}
        };
var player4card1 = function() {
for (i = 0; i < player4.length; i++) {
    if (Object.getPrototypeOf(player4[i]) == Object.getPrototypeOf(asztal[0]) && player4[i].value > asztal[0].value) {
        asztal.push(player4.splice(i,i+1)[0])
        return
    }
}
if (asztal.length == 3) {
for (i = 0; i < player4.length; i++) {
    if (Object.getPrototypeOf(player4[i]) == Object.getPrototypeOf(asztal[0])) {
        asztal.push(player4.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 3){
    for (i = 0; i < player4.length; i++) {
    if (player4[i].adu == true)  {
        asztal.push(player4.splice(i,i+1)[0])
        return
    }
}
}
if (asztal.length == 3) {
    asztal.push(player4.splice(0,1)[0])
    return
}
        };
player2card1();
player3card1();
player4card1();
console.log(player1);
console.log(player2);
console.log(player3);
console.log(player4);
console.log(asztal);

Aucun commentaire:

Enregistrer un commentaire