I simply commented away all database entries and put a for loop around doing the whole battle 1000 times. It took 48 seconds to complete.
This is kind of what I expected. It would probably be possible to make it go faster by optimizing the code as much as possible but I doubt that it will be possible to make it go fast enough to not appear as if the game is broken, and with many games going it will take too much resources from the server.
We really need to find another solution to the problem, it might not be exactly the same but it might be good enough to make the game work and be fun. I'll post my javascript code from yesterday and tell you about my tests. Perhaps you will get some ideas.
I tested quite a lot of things and the best results I got was when each unit get 3 decks that it draw card from (the card deck never ran out with 3 decks per unit)
When a unit killed another unit its deck would receive new cards, cards with the value 20 o basically cards that are making it more difficult to hit again. The killed units strength dived by 2 was the amount of cards given per kill so for example if a STR 4 is killed 2 new 20s in placed in the deck of the attacker.
This makes it hard for one unit to kill a lot of other units, like the spider situation. The spider still won 1 of 10.000 making it 99.99% chance to win for Zajoman.
When I used the same system on the two hero armies I got 77% chance to win compared to 73% chance normally.
I'm posting the code, its quite messy but I added some comments in case you feel like trying. Just copy all and put in in a html file and run.
- Code: Select all
<script type="text/javascript">
function WarlordsTest()
{
//Data to show survivors
var defoutline = new Array(0,0,0,0,0,0,0,0,0);
var attoutline = new Array(0,0,0,0,0,0,0,0,0);
var attwincount = 0;
var defwincount = 0;
var attdecklength = 0;
var defdecklength = 0;
var attwinning = 0;
var defwinning = 0;
const DIE = 20;
var HITS_STACK1 = 2;
var HITS_STACK2 = 2;
const CYCLES = 10000;
var strarrStack1 = new Array;
var hitarrStack1 = new Array;
var strarrStack2 = new Array;
var hitarrStack2 = new Array;
var deck1 = new Array;
var deck2 = new Array;
var hitsStack1;
var hitsStack2;
//Strength of the attackers
strarrStack1[0] = 4;
strarrStack1[1] = 4;
strarrStack1[2] = 4;
strarrStack1[3] = 4;
strarrStack1[4] = 4;
strarrStack1[5] = 7;
strarrStack1[6] = 7;
strarrStack1[7] = 8;
//Strength of the defenders
strarrStack2[0] = 9;
/*
strarrStack2[1] = 4;
strarrStack2[2] = 5;
strarrStack2[3] = 5;
strarrStack2[4] = 8;
strarrStack2[5] = 8;
strarrStack2[6] = 6;
strarrStack2[7] = 12;
*/
//Hitpoints of the attackers
hitarrStack1[0] = 2;
hitarrStack1[1] = 2;
hitarrStack1[2] = 2;
hitarrStack1[3] = 2;
hitarrStack1[4] = 2;
hitarrStack1[5] = 2;
hitarrStack1[6] = 2;
hitarrStack1[7] = 2;
//Hitpoints of the defenders
hitarrStack2[0] = 2;
/*
hitarrStack2[1] = 2;
hitarrStack2[2] = 2;
hitarrStack2[3] = 2;
hitarrStack2[4] = 2;
hitarrStack2[5] = 2;
hitarrStack2[6] = 2;
hitarrStack2[7] = 2;
*/
//var defdeck = deck1[3];
//alert(defdeck);
var attnodeck = 1;
var defnodeck = 1;
var r = 0;
var q = 0;
var winsStack1 = 0;
var winsStack2 = 0;
var unitsLeftStack1 = 0;
var unitsLeftStack2 = 0;
for (i = 0; i < CYCLES; i++)
{
//Creating the deck arrys
for(var y=0; y<hitarrStack1.length; y++)
{
deck1[y] = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
deck1[y] = shuffle(deck1[y]);
}
for(var a=0; a<hitarrStack2.length; a++)
{
deck2[a] = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
deck2[a] = shuffle(deck2[a]);
}
var attdeck = deck1[0];
var defdeck = deck2[0];
//hitsStack1 = HITS_STACK1;
//hitsStack2 = HITS_STACK2;
defnodeck = 1;
attnodeck = 1;
r = 0;
q = 0;
for(var p=0; p < 250; p++)
{
//window.document.write(r+","+hitarrStack2.length+"<br />");
while (hitarrStack1[q] > 0 && hitarrStack2[r] > 0)
{
//var strStack2 = strarrStack2[r];
//var hitsStack2 = hitarrStack2[r];
//If the deck only have 1 card left pick it and create a new deck, this is used if you decrese the amount of cards per deck
if(attdeck.length == 1)
{
var rollStack1 = attdeck.shift();
attnodeck = attnodeck+0;
var k=0;
for(var h=20; h>attnodeck; h--)
{
attdeck[k] = h;
k++;
}
attdeck = shuffle(attdeck);
}
if(defdeck.length == 1)
{
var rollStack2 = defdeck.shift();
defnodeck = defnodeck+0;
var k=0;
for(var h=20; h>defnodeck; h--)
{
defdeck[k] = h;
k++;
}
defdeck = shuffle(defdeck);
}
//Draw cards from deck
var rollStack1 = attdeck.shift();
var rollStack2 = defdeck.shift();
if (rollStack1 <= strarrStack1[q] && rollStack2 > strarrStack2[r]) hitarrStack2[r]--;
if (rollStack2 <= strarrStack2[r] && rollStack1 > strarrStack1[q]) hitarrStack1[q]--;
//var rollStack1 = GetRandomInt(1, DIE);
//var rollStack2 = GetRandomInt(1, DIE);
}
//alert (hitsStack1+","+hitarrStack2[r]);
if (hitarrStack1[q] == 0)
{
//If attacker is killed add cards to enemy deck
q++;
var attdeck = deck1[q];
var str = strarrStack1[q];
var str = Math.ceil(str / 2);
for(f=0; f<str; f++)
{
defdeck.push(20);
}
defdeck = shuffle(defdeck);
if(q == hitarrStack1.length) {break;}
//unitsLeftStack2 = unitsLeftStack2 + (hitsStack2 + 1) / 2;
}
if (hitarrStack2[r] == 0)
{
//If defender is killed add cards to enemy deck
r++;
var defdeck = deck2[r];
var str = strarrStack2[r];
var str = Math.ceil(str / 2);
for(f=0; f<str; f++)
{
attdeck.push(20);
}
attdeck = shuffle(attdeck);
if(r == hitarrStack2.length) {break;}
//winsStack1++;
//unitsLeftStack1 = unitsLeftStack1 + (hitsStack1 + 1) / 2;
}
}
//If army is dead give new hitpoints to all units and save a bunch of data
if(r == hitarrStack2.length)
{
winsStack1++;
hitarrStack2[0] = 2;
/*
hitarrStack2[1] = 2;
hitarrStack2[2] = 2;
hitarrStack2[3] = 2;
hitarrStack2[4] = 2;
hitarrStack2[5] = 2;
hitarrStack2[6] = 2;
hitarrStack2[7] = 2;
*/
hitarrStack1[0] = 2;
hitarrStack1[1] = 2;
hitarrStack1[2] = 2;
hitarrStack1[3] = 2;
hitarrStack1[4] = 2;
hitarrStack1[5] = 2;
hitarrStack1[6] = 2;
hitarrStack1[7] = 2;
var Defsurvivors = hitarrStack2.length - r;
var Attsurvivors = hitarrStack1.length - q;
if(Defsurvivors >= 1)
{
defwinning = defwinning + Defsurvivors;
defwincount++;
}
if(Attsurvivors >= 1)
{
attwinning = attwinning + Attsurvivors;
attwincount++;
}
attdecklength = attdecklength+deck1[0].length;
defdecklength = defdecklength+deck2[0].length;
if(Attsurvivors == 0)
{
attoutline[0]++;
}
if(Attsurvivors == 1)
{
attoutline[1]++;
}
if(Attsurvivors == 2)
{
attoutline[2]++;
}
if(Attsurvivors == 3)
{
attoutline[3]++;
}
if(Attsurvivors == 4)
{
attoutline[4]++;
}
if(Attsurvivors == 5)
{
attoutline[5]++;
}
if(Attsurvivors == 6)
{
attoutline[6]++;
}
if(Attsurvivors == 7)
{
attoutline[7]++;
}
if(Attsurvivors == 8)
{
attoutline[8]++;
}
}
//If army is dead give new hitpoints to all units and save a bunch of data
if(q == hitarrStack1.length)
{
winsStack2++;
hitarrStack1[0] = 2;
hitarrStack1[1] = 2;
hitarrStack1[2] = 2;
hitarrStack1[3] = 2;
hitarrStack1[4] = 2;
hitarrStack1[5] = 2;
hitarrStack1[6] = 2;
hitarrStack1[7] = 2;
hitarrStack2[0] = 2;
/*
hitarrStack2[1] = 2;
hitarrStack2[2] = 2;
hitarrStack2[3] = 2;
hitarrStack2[4] = 2;
hitarrStack2[5] = 2;
hitarrStack2[6] = 2;
hitarrStack2[7] = 2;
*/
var Defsurvivors = hitarrStack2.length - r;
var Attsurvivors = hitarrStack1.length - q;
if(Defsurvivors >= 1)
{
defwinning = defwinning + Defsurvivors;
defwincount++;
}
if(Attsurvivors >= 1)
{
attwinning = attwinning + Attsurvivors;
attwincount++;
}
attdecklength = attdecklength+deck1[0].length;
defdecklength = defdecklength+deck2[0].length;
if(Defsurvivors == 0)
{
defoutline[0]++;
}
if(Defsurvivors == 1)
{
defoutline[1]++;
}
if(Defsurvivors == 2)
{
defoutline[2]++;
}
if(Defsurvivors == 3)
{
defoutline[3]++;
}
if(Defsurvivors == 4)
{
defoutline[4]++;
}
if(Defsurvivors == 5)
{
defoutline[5]++;
}
if(Defsurvivors == 6)
{
defoutline[6]++;
}
if(Defsurvivors == 7)
{
defoutline[7]++;
}
if(Defsurvivors == 8)
{
defoutline[8]++;
}
}
}
//alert(attwinning+", "+attwincount);
var Attsurvivors = attwinning / attwincount;
var Defsurvivors = defwinning / defwincount;
var averattCards = attdecklength / CYCLES;
var averdefCards = defdecklength / CYCLES;
// = defwinning / CYCLES;
// = attwinning / CYCLES;
window.document.write("Attacker stack winning percentage is " + winsStack1 / CYCLES * 100 + "%. <br />Def survivors:"+Defsurvivors+" <br />Attack survivors:"+Attsurvivors+"<br />Def winnings:"+winsStack2+"");
window.document.write("<br />Att winnings:"+winsStack1);
window.document.write("<br />Att outline:"+attoutline);
window.document.write("<br />Def outline:"+defoutline);
window.document.write("<br />Attack deck unit 1:"+deck1[0]);
window.document.write("<br />Defend deck unit 1:"+deck2[0]);
window.document.write("<br />Att unit1 cards left:"+deck1[0].length);
window.document.write("<br />Def unit1 cards left:"+deck2[0].length);
window.document.write("<br />Average att cards left:"+averattCards);
window.document.write("<br />Average def cards left:"+averdefCards);
window.document.write("<br />Def unit1 cards left:"+deck2[0].length);
//printf ("6 Strength Stack won %d battles with an average of %f units left\n", winsStack1, unitsLeftStack1/10000.0);
//printf ("4 Strength Stack won %d battles with an average of %f units left\n", winsStack2, unitsLeftStack2/10000.0);
//printf ("The 6 Strength Stack winning percentage is %f\n", (float)winsStack1/10000.0*100.0);
}
function shuffle(list) {
var i, j, t;
for (i = 1; i < list.length; i++) {
j = Math.floor(Math.random()*(1+i));
if (j != i) {
t = list[i];
list[i] = list[j];
list[j] = t;
}
}
return list;
}
function GetRandomInt(min, max)
{
if (min == max || min > max)
{
window.alert("GetRandomInt(): Incorrect arguments!");
return null;
}
return min + Math.floor(Math.random() * (max + 1 - min));
}
</script>
<div onclick="WarlordsTest();" style="width: 200px; height:100px; border:solid 1px;"></div>