War - Puzzle Discussion

Thank you, I ended up figuring out the solution.

Hello,

At the moment I am at a loss here.
I’ve read basically the whole forum thread and I am still confused as why my long game terminates after 1162 rounds with PAT:

An excerpt from my log

P1 startdeck AH,4H,5D,6D,QC,JS,8S,2D,7D,JD,JC,6C,KS,QS,9D,2C,5S,9S,6S,8H,AD,4D,2H,2S,7S,8C
P2 startdeck 10H,4C,6H,3C,KC,JH,10C,AS,5H,KH,10S,9H,9C,8D,5C,AC,3H,4S,KD,7C,3S,QH,10D,3D,7H,QD
Current battle cards P1:AH P2: 10H
Turn:0=>AH 10H P1 WINS
P1 deck after winning battle 4H,5D,6D,QC,JS,8S,2D,7D,JD,JC,6C,KS,QS,9D,2C,5S,9S,6S,8H,AD,4D,2H,2S,7S,8C,AH,10H
Current battle cards P1:4H P2: 4C
Turn:1=>4H 4C !!!WAR!!!
P1 Wardeck  4H,5D,6D,QC
P2 Wardeck  4C,6H,3C,KC
Deck counts: 23 21
Current battle cards P1:JS P2: JH
Turn:1=>JS JH !!!WAR!!!
P1 Wardeck  4H,5D,6D,QC,JS,8S,2D,7D
P2 Wardeck  4C,6H,3C,KC,JH,10C,AS,5H
Deck counts: 19 17
Current battle cards P1:JD P2: KH
Turn:1=>JD KH P2 WINS
P2 deck after winning war 10S,9H,9C,8D,5C,AC,3H,4S,KD,7C,3S,QH,10D,3D,7H,QD,4H,5D,6D,QC,JS,8S,2D,7D,KH,4H,5D,6D,QC,JS,8S,2D,7D,JD

If I understood correctly, at the moment P2 wins above battle, the cards are added to his deck (queue-wise) like this:

[battlecardP1+3warcardsP1][battlecardP1+3warcardsP1][winningBattleCard**][battlecardP2+3warcardsP2][battlecardP2+3warcardsP2][losingBattleCard**]

** regardless the player.

As some of the posts contradicts other posts I am not sure what the correct order should be.
Anyone out there that’s able to help me out?

Besides that: cases 7-8-9 fails me.
Whereas when I make some changes sometimes 9 will succeed and 8 keeps failing with the dreaded timeout (e.g. it crashes vscode on my local machine if I try to run it there)

Thanks in advance for any reply.

Regarding your log, which test case is it?

The long game

Bonjour ,
Si les administrateurs l’accepte, ci-joint les commentaires de ma gestion des piles de cartes (ps: mon programme fonctionne :slight_smile: )

Bon courage.

Hello. I’m getting desperate.
I manage to go past a battle and 2 chain battles but 1 game 1 battle for me is 2 - 212 instead of 1 - 52

Langugage : JavaScript
Concepts:

  1. I use same notation replacing J with 11, Q with 12 etc… A is 14. I store them as objects of {v:, n:} for tracking

  2. Get 1st card of each array and shift() the array. In case of a war i will always use the 4-th of the array to compare so it is handy

  3. The war is handled through recursion:

function war(pp1, pp2, recursionCounter = 1) { //pp1 = Pile Player 1 et
  let rCounter = recursionCounter;
  //check if the war is possible
  if (pp1.length < 4 || pp2.length < 4) {
    return "PAT";
  }
  //war is possible
  //take the 4th card, compare
  let comp1 = pp1[rCounter * 4 - 1];
  let comp2 = pp2[rCounter * 4 - 1];
  //if same
  if (comp1.v === comp2.v) {
    console.error(`consecutive war ${rCounter}`);
    rCounter++;
    return war(pp1, pp2, rCounter);
  } else {
    console.error(
      `war winner :${
        pp1.v > pp2.v ? 1 : 2
      } with counter of consequity ${rCounter}`
    );
    return { winner: pp1.v > pp2.v ? 1 : 2, times: rCounter };
  }
}
  1. Then at the end of the war I assemble the winning pot. The order for it is according to the rules: first the cp1 (1st card player 1 ), followed by n cards from player 1 he used in the war, then cp2 then n cards from war from player 2:
      let temp1 = p1.slice(0, warRes.times * 4);
      let temp2 = p2.slice(0, warRes.times * 4);
      let winPot = [cp1].concat(temp1).concat([cp2]).concat(temp2);
      parseInt(warRes.winner) === 1
        ? (p1 = p1.concat(winPot))
        : (p2 = p2.concat(winPot));
  1. Then the amount of cards used in the battle are deleted from the beginning of the each player’s pile:
function erase(a, n) {
  for (let i = 0; i < n; i++) {
    a.shift();
  }
}
...
    //p1 and p2 represent initial pile. As i mentioned, the 1st card for comparison before the battle is already shifted.
    erase(p1, warRes.times * 4);
    erase(p2, warRes.times * 4);

In the case of 1 game 1 battle, I actually somehow have 6 individual battles.

Can somebody help me with

  • Advice
  • Log for card amounts after each turn for this particular case
  • antidepressants (?)
  • Alternative test case where there are less data?

Thanks :slight_smile:

Can you share here the cards used in the first 30 battle(s)/war(s)? I’ll compare my results with yours.

Hello ! Thank you so much !
here are the logs:

How to read:
In a line starting with :“comparing” you have the cards that are compared
counter represents turns, p1, p2 represent the amount of cards that player has at the end of the turn

comparing 10 / 2
counter : 1, p1: 27, p2: 25

comparing 13 / 9
counter : 2, p1: 28, p2: 24

comparing 6 / 8
counter : 3, p1: 27, p2: 25

comparing 10 / 4
counter : 4, p1: 28, p2: 24

comparing 8 / 5
counter : 5, p1: 29, p2: 23

comparing 14 / 14
let the war begin. cards posessed : 29 / 23
war winner :2 with counter of consequuity 1
war result : {"winner":2,"times":1}
[{"v":12,"n":"Q"},{"v":3,"n":"3"},{"v":7,"n":"7"},{"v":13,"n":"K"}]
p1 b4 assembly & b4 drop : 28
p2 b4 assembly & b4 drop : 22
p1 after assembly & b4 drop : 28
p2 after assembly & b4 drop : 32
p1 after assembly & after drop : 24
p2 after assembly & after drop :28
counter : 6, p1: 24, p2: 28

comparing 9 / 4
counter : 7, p1: 25, p2: 27

comparing 2 / 6
counter : 8, p1: 24, p2: 28

comparing 11 / 6
counter : 9, p1: 25, p2: 27

comparing 13 / 12
counter : 10, p1: 26, p2: 26

comparing 3 / 9
counter : 11, p1: 25, p2: 27

comparing 2 / 10
counter : 12, p1: 24, p2: 28

comparing 12 / 4
counter : 13, p1: 25, p2: 27

comparing 14 / 11
counter : 14, p1: 26, p2: 26

comparing 11 / 6
counter : 15, p1: 27, p2: 25

comparing 7 / 3
counter : 16, p1: 28, p2: 24

comparing 13 / 8
counter : 17, p1: 29, p2: 23

comparing 10 / 3
counter : 18, p1: 30, p2: 22

comparing 4 / 7
counter : 19, p1: 29, p2: 23

comparing 14 / 9
counter : 20, p1: 30, p2: 22

comparing 5 / 8
counter : 21, p1: 29, p2: 23

comparing 5 / 2
counter : 22, p1: 30, p2: 22

comparing 10 / 6
counter : 23, p1: 31, p2: 21

comparing 2 / 8
counter : 24, p1: 30, p2: 22

comparing 13 / 14
counter : 25, p1: 29, p2: 23

comparing 9 / 12
counter : 26, p1: 28, p2: 24

comparing 10 / 3
counter : 27, p1: 29, p2: 23

comparing 4 / 7
counter : 28, p1: 28, p2: 24

comparing 8 / 13
counter : 29, p1: 27, p2: 25

comparing 5 / 14
counter : 30, p1: 26, p2: 26

Hello!

This is where things start to become wrong. Mine is comparing 13 (KD) / 2 (2D) instead of 13 / 14. KD and 2D were involved in an ordinary battle instead of a war, before they are compared in “counter 25”. So, you may check what’s gone wrong much earlier in the simulation (“counter 3” and “counter 8”).

You realy save me.

@StepBack13 Thank you soooo much!! =D I was with the same problem… tried three/four ways to optimize my code (changing between Lists, Queues, Arrays and Strings) without success. After seeing your comment, I realized that I also was using 10 to Jack!! I fixed the values and all the tests passed!! Thank you!!!

1 Like

Thank you so much for providing me a comparison!

It took me till today to figure things out…and i mean reeeeeeally trying hard.
You know what it was? a battle! in the piece of code I provided earlier

  if (comp1.v === comp2.v) {
    console.error(`consecutive war ${rCounter}`);
    rCounter++;
    return war(pp1, pp2, rCounter);
  } else {
    console.error(
      `war winner :${
        pp1.v > pp2.v ? 1 : 2
      } with counter of consequity ${rCounter}`
    );
    return { winner: pp1.v > pp2.v ? 1 : 2, times: rCounter };
  }
}

pp1 and pp2 are arrays and my mind thought it were selected values in question.
Damn…took me simply to export the 1 game 1 battle + my solution into VS code, establish an fs- stream to log to file and parallel print both arrays… Xd

Thank you for advice again))!

1 Like