samedi 16 mai 2020

Mini textual game (if(odd length of array))

I'm struggling with the last step of my mini game where each player gets assigned a random character, each character have a certain amout of power that decreases each fight by the amount of the power of the character he defeated all untill there is just 1 player left now i didn't considered that at some point there will be an odd amount of players (cuz if at the round 1 there are 10 players at the round 2 there are 5) now i want in the rounds where there are an odd amount of player to have the first player "taking a break" so in case there are 5 only 4 fight

here is the main function

matches = function(){
        while(new_arr[0].length>1){      
            round += 1;     
            var x = $("#r1");
            var h2 = $('<h1>').html("----------Round---------- "+round);
            x.append(h2);
            if(new_arr[0].length %2 == 0){
                console.log('iF LENGTH '+ (new_arr[0].length))
                for(i = 0; i<new_arr[0].length;i= i+2){    
                    var h1 = $('<h1>').html(new_arr[0][i] +" (Power Left) = " +new_arr[2][i] + " VS " + new_arr[0][i+1]+" (Power Left) = " +new_arr[2][i+1]);
                    x.append(h1);
                    if(new_arr[2][i]>=new_arr[2][i+1]){  
                        new_arr[2][i] =new_arr[2][i] -new_arr[2][i+1];
                        new_arr[0][i+1] = "defeated";
                        new_arr[1][i+1] = "defeated";
                        new_arr[2][i+1] = "defeated";
                        var h1 = $('<h1>').html(new_arr[0][i] +" wins, Power left after this fight " +new_arr[2][i]);
                        x.append(h1);
                    }else{
                        new_arr[2][i+1] =new_arr[2][i+1] -new_arr[2][i];  
                        new_arr[0][i] = "defeated";
                        new_arr[1][i] = "defeated";
                        new_arr[2][i] = "defeated";
                        var h1 = $('<h1>').html(new_arr[0][i+1] +" wins, Power left after this fight " +new_arr[2][i+1]);
                        x.append(h1);
                        }   
                }
                for(var i = new_arr[0].length -1; i >= 0; i--){
                    var x = new_arr[0][i];                          
                    if(x=="defeated"){
                        new_arr[0].splice(i,1);
                        new_arr[1].splice(i,1);
                        new_arr[2].splice(i,1);
                        }
                }

                shuffle2(new_arr);
            }else{
                console.log('ELSE LENGTH '+ (new_arr[0].length))  
                for(i = 0; i<(new_arr[0].length)-1;i= i+2){

                    var x = $("#r1");
                    var h3 = $('<h1>').html(new_arr[0][i] +" (Power Left) = " +new_arr[2][i] + " VS " + new_arr[0][i+1]+" (Power Left) = " +new_arr[2][i+1])
                    x.append(h3);
                    if(new_arr[2][i]>=new_arr[2][i+1]){
                        new_arr[2][i] =new_arr[2][i] -new_arr[2][i+1];
                        new_arr[0][i+1] = "defeated";
                        new_arr[1][i+1] = "defeated";
                        new_arr[2][i+1] = "defeated";
                        var h1 = $('<h1>').html(new_arr[0][i] +" wins, Power left after this fight " +new_arr[2][i]);
                        x.append(h1);
                        }else{
                            new_arr[2][i+1] =new_arr[2][i+1] -new_arr[2][i];

                            new_arr[0][i] = "defeated";
                            new_arr[1][i] = "defeated";
                            new_arr[2][i] = "defeated";
                            var h1 = $('<h1>').html(new_arr[0][i+1] +" wins, Power left after this fight " +new_arr[2][i+1]);
                            x.append(h1);
                            }       
                        for(var i = new_arr[0].length -1; i >= 0; i--){
                            var x = new_arr[0][i];
                            if(x=="defeated"){
                                new_arr[0].splice(i,1);
                                new_arr[1].splice(i,1);
                                new_arr[2].splice(i,1);
                                }
                            }
                         }

                        shuffle2(new_arr);


            }
            if(new_arr[0].length==1){
                var x = $("#r1");
                var h1 = $('<h1>').html(new_arr[0][0]+" wins this tournament with "+new_arr[2][0]+ " Power Left")
                x.append(h1);
            }   
        }

    }   
    matches();

The new_arr is made like this

new_arr =
[
 [PLayer 1......player N]
 [Character 1.....character N]
 [poerCharacter1 ......powercharacterN]

]

i want to do in a way that if there are an odd amount of players in one round, then the first or last player in the array so

new_arr[0][0]
new_arr[1][0]
new_arr[2][0]

is temporarily removed and then added at the end of the "fights" here is my test with 10 players at first 5 fights happen but then in round 2 character 3 fights 2 times enter image description here

enter image description here

enter image description here

Aucun commentaire:

Enregistrer un commentaire