Guest User

Untitled

a guest
Feb 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. /*
  2. Libs included:
  3. underscore lodash chai sinon sinon-chai mocha async request q bluebird jsdom
  4. */
  5.  
  6. function makeMove(move, currentPosition) {
  7.  
  8. let newPosition = [...currentPosition];
  9.  
  10. switch(move) {
  11.  
  12. case 'r': {
  13. newPosition[0]++;
  14. break;
  15. }
  16.  
  17. case 'l': {
  18. newPosition[0]--;
  19. break;
  20. }
  21.  
  22. case 'u': {
  23. newPosition[1]--;
  24. break;
  25. }
  26.  
  27. case 'd': {
  28. newPosition[1]++;
  29. break;
  30. }
  31. }
  32.  
  33. return newPosition;
  34.  
  35.  
  36. }
  37.  
  38. function isOutOfBounds(position) {
  39. return position.join('').length > 2;
  40. }
  41.  
  42. function convertPosition(pos) {
  43. return pos.join('');
  44. }
  45.  
  46. function runGame(player1Moves, player2Moves) {
  47. let PlayerOnePosition = [0,0];
  48. let PlayerTwoPosition = [9,9];
  49.  
  50. let takenPositions = [];
  51.  
  52.  
  53. for(let i = 0; i < player1Moves.length; i++) {
  54. takenPositions.push(
  55. PlayerOnePosition.join(''),
  56. PlayerTwoPosition.join('')
  57. );
  58.  
  59. const firstPlayerMove = player1Moves[i];
  60. const secondPlayerMove = player2Moves[i];
  61.  
  62.  
  63. // Player enters a free space
  64. // Player enters a taken space
  65.  
  66.  
  67.  
  68.  
  69. // Logic to see if player is out of bounds
  70. PlayerOnePosition = makeMove(firstPlayerMove, PlayerOnePosition);
  71. PlayerTwoPosition = makeMove(secondPlayerMove, PlayerTwoPosition);
  72.  
  73. // Both players have entered the same space or out of bounds
  74. if(
  75. convertPosition(PlayerOnePosition) === convertPosition(PlayerTwoPosition) || isOutOfBounds(PlayerOnePosition) && isOutOfBounds(PlayerTwoPosition) || takenPositions.includes(convertPosition(PlayerOnePosition)) && takenPositions.includes(convertPosition(PlayerTwoPosition))) {
  76. console.log('Draw!');
  77. break;
  78. }
  79.  
  80. // Player is out of bounds
  81. if(isOutOfBounds(PlayerOnePosition) || takenPositions.includes(convertPosition(PlayerOnePosition))) {
  82. console.log('Player 2 wins!');
  83. break;
  84. }
  85.  
  86. if(isOutOfBounds(PlayerTwoPosition) || takenPositions.includes(convertPosition(PlayerTwoPosition))) {
  87. console.log('Player 1 wins!');
  88. break;
  89. }
  90.  
  91.  
  92. }
  93.  
  94. }
  95.  
  96. runGame(
  97. ['d','d','r','r','r','u','r','d','d','d','d','l','d','r','r','r','u','u'],
  98. ['l','l','l','u','u','l','u','u','u','r','r','u','l','l','l','l','u','r']
  99. );
  100.  
  101. /*
  102. Old Content below(Python 2):
  103.  
  104. # Libraries Included:
  105. # Numpy, Scipy, Scikit, Pandas
  106. # # The Tron Problem
  107. #
  108. # '''
  109. # Greetings future XXXXXXXXXX employee! You have been "randomly" selected to participate in the 11th annual Tron games!
  110. # Don't worry, you won't be actually playing the games.
  111. # You'll be judging the battles after the fact! Let me take a quick second to brief you on the Tron Standard Rules (TSRs).
  112. #
  113. # 1) The game is played on a standard 10x10 board
  114. # 2) Player 1 starts on position 0x0. Player 2 starts on position 9x9
  115. # 3) At each turn, a player may move up, down, left, or right on the board. These steps are held in an array and take the form 'u','d','l', and 'r' respectively.
  116. # 4) If a player crosses a previous path of another player, including themselves, they are eliminated
  117. # 5) If a player lands on the same space as another player on the same turn, both players are eliminated and the match is declared a draw
  118. # 6) If a player moves off the board, into the vast cyber nothingness, they are eliminated
  119. # 7) If there is only one player left at the end of a turn, that player wins no matter if they have more moves or not
  120. # 8) If the match has ended and there is more than one player still active, the match is declared a draw
  121. # '''
  122.  
  123. #Player1 = ['r','d','d','r','r','r','l','l','l','d','d','d','l','d','d','d','d','r']
  124. #Player2 = ['u','l','l','u','l','l','u','l','l','d','d','l','l','u','u','r','u','l']
  125. */
Add Comment
Please, Sign In to add comment