Created
November 14, 2024 23:46
-
-
Save maksverver/5a9046e9db2617d451eb4466665392e5 to your computer and use it in GitHub Desktop.
Codecup Box Javascript player for Caia (compatible with Node.js and d8)
This file contains 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
#!/usr/bin/d8 --single-threaded | |
// d8 defines print() and printErr() which write to stdout and stderr. | |
// In Node.js, we can use console.log() and console.error() for this. | |
const print = globalThis.print || console.log; | |
const printErr = globalThis.printErr || console.error; | |
// Global player initialization | |
let myColor = undefined; | |
let firstMove = undefined; | |
let myTurn = false; | |
// TODO: other stuff to keep track of game state | |
function executeMove(move) { | |
// TODO | |
} | |
function placeTile(tile) { | |
// TODO | |
return 'Fjv'; | |
} | |
function processLine(line) { | |
if (myColor === undefined) { | |
myColor = Number(line); | |
printErr('My secret color:', myColor); | |
} else if (firstMove === undefined) { | |
firstMove = line; | |
printErr('First move:', firstMove); | |
executeMove(firstMove); | |
} else if (line === 'Start') { | |
myTurn = true; | |
} else if (myTurn) { | |
printErr('Tile received:', line); | |
print(placeTile(line)); | |
myTurn = false; | |
} else { | |
printErr("Opponent's move:", line); | |
executeMove(line); | |
myTurn = true; | |
} | |
} | |
// Caia player identification: | |
// - 'D' for a deterministic player | |
// - 'R' for a random player | |
printErr('D', 'playername'); | |
// Main loop. Reads lines until "Quit". | |
if (typeof readline === 'function') { | |
// Running in d8 | |
for (let line; (line = readline()) !== 'Quit'; ) { | |
processLine(line); | |
} | |
} else { | |
// Running in Node.js | |
const readline = require('node:readline'); | |
const rl = readline.createInterface(process.stdin); | |
rl.on('line', (line) => { | |
if (line === 'Quit') { | |
rl.close(); | |
} else { | |
processLine(line); | |
} | |
}); | |
} |
This file contains 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
REM Uncomment one of these lines (updating the paths) to execute the player script: | |
REM @C:\dev\v8\v8\out\x64.release\d8.exe --single-threaded c:\dev\demo\player-hybrid.js | |
REM @C:\dev\node-v23.2.0-win-x64\node.exe c:\dev\demo\player-hybrid.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment