Skip to content

Instantly share code, notes, and snippets.

@chasingmaxwell
Created July 8, 2020 21:15
Show Gist options
  • Save chasingmaxwell/8266044abbec1376207ebd6a39c046c8 to your computer and use it in GitHub Desktop.
Save chasingmaxwell/8266044abbec1376207ebd6a39c046c8 to your computer and use it in GitHub Desktop.
Tron
// The Tron Problem
// Greetings future Episource 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 (top left corner). Player 2 starts on position 9x9 (bottom right corner).
// 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.
// 9) If both players are eliminated on the same turn, the match is declared a draw.
const SIZE = 10;
const STARTA = { x: 0, y: 0 };
const STARTB = { x: 9, y: 9 };
const board = [];
for (let i = 0; i < SIZE; i++) {
const row = [];
for (let s = 0; s < SIZE; s++) {
row.push('-1');
}
board.push(row);
}
function doMove(currentPos, move) {
const newPos = {...currentPos};
if (move === 'u') {
newPos.y = newPos.y - 1;
}
if (move === 'd') {
newPos.y = newPos.y + 1;
}
if (move === 'l') {
newPos.x = newPos.x - 1;
}
if (move === 'r') {
newPos.x = newPos.x + 1;
}
console.log(newPos);
return newPos;
}
function evaluate(pos) {
if (typeof board[pos.y] === 'undefined' || typeof board[pos.y][pos.x] === 'undefined' || board[pos.y][pos.x] !== '-1') {
return false;
}
return true;
}
function theGame(startA, startB, aMoves, bMoves) {
let currentA = startA;
let currentB = startB;
board[currentA.y][currentA.x] = 'A';
board[currentB.y][currentB.x] = 'B';
for (let move = 0; move < aMoves.length; move++) {
currentA = doMove(currentA, aMoves[move]);
currentB = doMove(currentB, bMoves[move]);
if (currentA.x === currentB.x && currentA.y === currentB.y) {
return 'Draw!';
}
const aState = evaluate(currentA);
const bState = evaluate(currentB);
if (!aState && !bState) {
return 'Draw!';
}
if (!aState) {
return 'Player B wins!';
}
if (!bState) {
return 'Player A wins!';
}
board[currentA.y][currentA.x] = 'A';
board[currentB.y][currentB.x] = 'B';
}
return 'Draw!';
}
// player 2 should win (player 1 runs into his own path)
// const player1 = ['r', 'd', 'd', 'r', 'r', 'r', 'l', 'l', 'l', 'd', 'd', 'd', 'l', 'd', 'd', 'd', 'd', 'r'];
// const player2 = ['u', 'l', 'l', 'u', 'l', 'l', 'u', 'l', 'l', 'd', 'd', 'l', 'l', 'u', 'u', 'r', 'u', 'l'];
// draw (both eliminated on same turn)
// const player1 = ['d', 'd', 'r', 'r', 'r', 'u', 'r', 'd', 'd', 'd', 'd', 'l', 'd', 'r', 'r', 'r', 'u', 'u'];
// const player2 = ['l', 'l', 'l', 'u', 'u', 'l', 'u', 'u', 'u', 'r', 'r', 'u', 'l', 'l', 'l', 'l', 'u', 'r'];
// draw (both alive)
// const player1 = ['d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'r', 'r', 'r', 'r', 'r', 'u', 'u', 'u', 'u', 'u'];
// const player2 = ['u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'l', 'l', 'l', 'l', 'l', 'd', 'd', 'd', 'd', 'd'];
// draw (same space on same turn)
// const player1 = ['d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'r', 'r', 'r', 'r', 'r', 'u', 'u', 'u', 'u', 'u'];
// const player2 = ['u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'l', 'l', 'l', 'l', 'l', 'd', 'd', 'd', 'r', 'd'];
// // draw (eliminated on 5th round)
// const player1 = ['d', 'd', 'd', 'd', 'u', 'u', 'r', 'd', 'd', 'd', 'd', 'l', 'd', 'r', 'r', 'r', 'u', 'u'];
// const player2 = ['l', 'l', 'l', 'l', 'r', 'l', 'u', 'u', 'u', 'r', 'r', 'u', 'l', 'l', 'l', 'l', 'u', 'r'];
// player 2 should win
// const player1 = ['r', 'd', 'd', 'r', 'r', 'r', 'd', 'r', 'r', 'd', 'd', 'd', 'l', 'd', 'd', 'd', 'd', 'r'];
// const player2 = ['u', 'l', 'l', 'u', 'l', 'l', 'u', 'l', 'l', 'd', 'd', 'l', 'l', 'u', 'u', 'r', 'u', 'l'];
// player 1 should win
// const player1 = ['r', 'd', 'd', 'r', 'r', 'r', 'd', 'r', 'r', 'd', 'd', 'd', 'r', 'u', 'u', 'u', 'd', 'r'];
// const player2 = ['u', 'l', 'l', 'u', 'l', 'l', 'u', 'u', 'u', 'u', 'u', 'l', 'l', 'u', 'u', 'r', 'u', 'l'];
//player 2 should win (player 1 goes out of bounds)
// const player1 = ['r', 'd', 'r', 'r', 'u', 'r', 'u', 'u', 'u', 'd', 'd', 'd', 'r', 'u', 'u', 'u', 'd', 'r'];
// const player2 = ['u', 'l', 'l', 'u', 'l', 'l', 'u', 'l', 'l', 'd', 'd', 'l', 'l', 'u', 'u', 'r', 'u', 'l'];
console.log('result: ', theGame(STARTA, STARTB, player1, player2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment