Created
September 6, 2025 12:23
-
-
Save UltiRequiem/3667418e59458f155651365a3b09e5eb to your computer and use it in GitHub Desktop.
No tenia internet, me puse a jugar un rato, esta incompleto XD
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
import process from "node:process"; | |
class Game { | |
private state: (0 | 1 | 2)[]; | |
private winningCombinations: number[][]; | |
constructor() { | |
this.state = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | |
this.winningCombinations = [ | |
// Horizontals | |
[0, 1, 2], | |
[3, 4, 5], | |
[6, 7, 8], | |
// Diagonals | |
[0, 4, 8], | |
[2, 4, 6], | |
// Verticals | |
[0, 3, 6], | |
[1, 4, 7], | |
[2, 5, 8], | |
]; | |
} | |
private letter(number: 0 | 1 | 2): "X" | "Y" | "_" { | |
if (number === 0) return "_"; | |
return number === 1 ? "X" : "Y"; | |
} | |
private winner() { | |
for (const winningComb of this.winningCombinations) { | |
const [a, b, c] = winningComb; | |
if (this.state[a] === this.state[b] && this.state[b] === this.state[c]) { | |
if (this.state[a] === 0) continue; | |
return this.state[a]; | |
} | |
} | |
} | |
private print() { | |
console.log(); | |
for (let i = 0; i < this.state.length; i += 3) { | |
console.log(); | |
for (let j = i; j < i + 3; j++) { | |
const player = this.letter(this.state[j]); | |
const text = ` ${player} `; | |
process.stdout.write(text); | |
} | |
console.log(); | |
} | |
console.log(); | |
} | |
private parse(input: string): number { | |
const number = Number.parseInt(input); | |
if (Number.isNaN(number)) { | |
console.log(`Solo ingresar numeros!!`); | |
process.exit(); | |
} | |
if (number > 9 || number < 1) { | |
console.log("Ingresar numeros en el rango!!"); | |
process.exit(); | |
} | |
if (this.state[number - 1] !== 0) { | |
console.log("Elegir luages vacios!"); | |
process.exit(); | |
} | |
return number - 1; | |
} | |
private input(player: 1 | 2) { | |
const playerName = player === 1 ? "primer" : "segundo"; | |
const input = prompt(`Donde marcara el ${playerName} jugador: `) ?? ""; | |
const idx = this.parse(input); | |
return idx; | |
} | |
public play() { | |
console.log( | |
"Solo ingresar numeros del 1-9, indicando la posicion a marcar:", | |
); | |
console.log(); | |
console.log("Estado Incial: "); | |
while (this.winner() === undefined) { | |
this.print(); | |
const fiIdx = this.input(1); | |
const seIdx = this.input(2); | |
this.state[fiIdx] = 1; | |
this.state[seIdx] = 2; | |
} | |
this.print(); | |
console.log(`The winner is ${this.winner()}`); | |
} | |
} | |
const runtime = new Game(); | |
runtime.play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment