Created
February 7, 2018 19:07
-
-
Save MrBenJ/2599bfdb4fb87631f7c9d6ebb5b976d2 to your computer and use it in GitHub Desktop.
"TRON" game code challenge for live coding interview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Libs included: | |
underscore lodash chai sinon sinon-chai mocha async request q bluebird jsdom | |
*/ | |
function makeMove(move, currentPosition) { | |
let newPosition = [...currentPosition]; | |
switch(move) { | |
case 'r': { | |
newPosition[0]++; | |
break; | |
} | |
case 'l': { | |
newPosition[0]--; | |
break; | |
} | |
case 'u': { | |
newPosition[1]--; | |
break; | |
} | |
case 'd': { | |
newPosition[1]++; | |
break; | |
} | |
} | |
return newPosition; | |
} | |
function isOutOfBounds(position) { | |
return position.join('').length > 2; | |
} | |
function convertPosition(pos) { | |
return pos.join(''); | |
} | |
function runGame(player1Moves, player2Moves) { | |
let PlayerOnePosition = [0,0]; | |
let PlayerTwoPosition = [9,9]; | |
let takenPositions = []; | |
for(let i = 0; i < player1Moves.length; i++) { | |
takenPositions.push( | |
PlayerOnePosition.join(''), | |
PlayerTwoPosition.join('') | |
); | |
const firstPlayerMove = player1Moves[i]; | |
const secondPlayerMove = player2Moves[i]; | |
// Player enters a free space | |
// Player enters a taken space | |
// Logic to see if player is out of bounds | |
PlayerOnePosition = makeMove(firstPlayerMove, PlayerOnePosition); | |
PlayerTwoPosition = makeMove(secondPlayerMove, PlayerTwoPosition); | |
// Both players have entered the same space or out of bounds | |
if( | |
convertPosition(PlayerOnePosition) === convertPosition(PlayerTwoPosition) || isOutOfBounds(PlayerOnePosition) && isOutOfBounds(PlayerTwoPosition) || takenPositions.includes(convertPosition(PlayerOnePosition)) && takenPositions.includes(convertPosition(PlayerTwoPosition))) { | |
console.log('Draw!'); | |
break; | |
} | |
// Player is out of bounds | |
if(isOutOfBounds(PlayerOnePosition) || takenPositions.includes(convertPosition(PlayerOnePosition))) { | |
console.log('Player 2 wins!'); | |
break; | |
} | |
if(isOutOfBounds(PlayerTwoPosition) || takenPositions.includes(convertPosition(PlayerTwoPosition))) { | |
console.log('Player 1 wins!'); | |
break; | |
} | |
} | |
} | |
runGame( | |
['d','d','r','r','r','u','r','d','d','d','d','l','d','r','r','r','u','u'], | |
['l','l','l','u','u','l','u','u','u','r','r','u','l','l','l','l','u','r'] | |
); | |
/* | |
Old Content below(Python 2): | |
# Libraries Included: | |
# Numpy, Scipy, Scikit, Pandas | |
# # The Tron Problem | |
# | |
# ''' | |
# Greetings future XXXXXXXXXX employee! You have been "randomly" selected to participate in the 11th annual Tron games! | |
# Don't worry, you won't be actually playing the games. | |
# You'll be judging the battles after the fact! Let me take a quick second to brief you on the Tron Standard Rules (TSRs). | |
# | |
# 1) The game is played on a standard 10x10 board | |
# 2) Player 1 starts on position 0x0. Player 2 starts on position 9x9 | |
# 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. | |
# 4) If a player crosses a previous path of another player, including themselves, they are eliminated | |
# 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 | |
# 6) If a player moves off the board, into the vast cyber nothingness, they are eliminated | |
# 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 | |
# 8) If the match has ended and there is more than one player still active, the match is declared a draw | |
# ''' | |
#Player1 = ['r','d','d','r','r','r','l','l','l','d','d','d','l','d','d','d','d','r'] | |
#Player2 = ['u','l','l','u','l','l','u','l','l','d','d','l','l','u','u','r','u','l'] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment