JavaScript: War

Last update: 11 Feb 23:20
0
Students

"War" is a card game to win all of the cards. Our challenge based on a modification of a two-player game. Players are dealt an equal number of cards. Players do not look at their cards and put them in a pile nearby. Then each player removes the top card and shows it to the opponent. The player whose card turned out to be of a higher denomination takes both cards and puts them in his deck from below (so that his own card goes first). If the cards have the same value, they are thrown out of the game. There are three possible game outcomes:

  • Both players have no cards left
  • The game continues infinitely
  • One of the players won

war.js

Implement and export as default a class with the run() method that takes two numbers arrays representing the first and second player cards.

  • If the first player wins, the method should return First player. Round: <round number>
  • If the second player wins, the method should return Second player. Round: <round number>
  • If the players have no cards left, then the method should return Peace!
  • If it was not possible to identify the winner in 100 rounds, then Peace! is also returned

Check the actual examples in the tests.

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests