Skip to content

Instantly share code, notes, and snippets.

@pranithan-kang
Created July 24, 2024 03:34
Show Gist options
  • Save pranithan-kang/f7c1a868abbe1387f430933bd6034c20 to your computer and use it in GitHub Desktop.
Save pranithan-kang/f7c1a868abbe1387f430933bd6034c20 to your computer and use it in GitHub Desktop.
scissor-paper-rock

source

สรุปในมุม pseudo code เป็น JavaScript snippet

let condLength = 3;

let winningConditions = Array.from(
    Array(condLength).keys(), 
    (_, i) => i < condLength - 1 ? i + 1 : 0
);
// Generate circular linked list
// (0, 1, 2)  --  array index
// [1, 2, 0]  --  value

function solveScissorsPaperRock(p1, p2) {
    if (p1 === p2) {
        return 0;  // Draw
    }
    return ( 
        winningConditions[p1] === p2 ? // p1 == p2 + 1 ?
        1 // Win
        :
        -1 // Lose
    );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment