not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4 KiB

  1. function LOLGame(size) {
  2. // Set initial
  3. var length = Math.max(size,0);
  4. var player = 0;
  5. // Accessor
  6. this.getLength = function() {
  7. return length;
  8. };
  9. this.getPlayer = function() {
  10. return player;
  11. };
  12. // Change player, only at start
  13. this.reverse = function() {
  14. player = (player + 1) % 2;
  15. return true;
  16. };
  17. // Play
  18. this.play = function(number) {
  19. if (number === undefined || number == null || number < 1 || number > 3 || number > length)
  20. return length;
  21. length = length - number;
  22. if (length > 0)
  23. player = (player + 1) % 2;
  24. return length;
  25. };
  26. // Test end of game
  27. this.endOfGame = function() {
  28. return length == 0;
  29. };
  30. // Think to the better next shot
  31. this.think = function(level) {
  32. // Level 1: try to leave at least one at end of game
  33. if (level >= 1) {
  34. if (length >= 2 && length <= 4)
  35. return length-1;
  36. }
  37. // Level 2: try to leave five
  38. if (level >= 2) {
  39. if (length >= 6 && length <= 8)
  40. return length-5;
  41. }
  42. // Level 3: always try to left the nearest multiple of 4+1
  43. if (level >= 3) {
  44. var attemptsup = length-(Math.floor(length/4)*4+1);
  45. if (attemptsup >=1 && attemptsup <= 3)
  46. return attemptsup;
  47. var attemptinf = length-((Math.floor(length/4)-1)*4+1);
  48. if (attemptinf >=1 && attemptinf <= 3)
  49. return attemptinf;
  50. }
  51. // Simple case: randomly choose a number
  52. return Math.floor(Math.random()*Math.min(3,length-1))+1;
  53. };
  54. }