Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const readline = require("readline").createInterface({
- input: process.stdin,
- output: process.stdout
- })
- let PLAYER = "X"
- let CPU = "O"
- // cpu is with max
- const decr = x => x - 1
- const parseWinner = w => {
- if (w === PLAYER) {
- return "PLAYER wins"
- }
- if (w === CPU) {
- return "CPU wins"
- }
- return "Tie."
- }
- const generateBoard = () => {
- const brd = []
- for (let i = 0; i < 3; i++) {
- brd.push(Array.from({ length: 3 }, () => "_"))
- }
- return brd
- }
- const printBoard = board => {
- board.forEach(row => {
- console.log(row.join(" "))
- })
- console.log()
- }
- const isValid = (board, row, col) =>
- 0 <= row && row <= 2 && 0 <= col && col <= 2 && board[row][col] === "_"
- const getWinner = board => {
- for (let i = 0; i < 3; i++) {
- if (
- board[0][i] !== "_" &&
- board[0][i] === board[1][i] &&
- board[1][i] === board[2][i]
- ) {
- return board[0][i]
- }
- }
- for (let i = 0; i < 3; i++) {
- if (
- board[i][0] !== "_" &&
- board[i][0] === board[i][1] &&
- board[i][1] === board[i][2]
- ) {
- return board[i][0]
- }
- }
- if (
- board[0][0] !== "_" &&
- board[0][0] === board[1][1] &&
- board[1][1] === board[2][2]
- ) {
- return board[0][0]
- }
- if (
- board[0][2] !== "_" &&
- board[0][2] === board[1][1] &&
- board[1][1] === board[2][0]
- ) {
- return board[0][2]
- }
- // _ if tie, undefined if still no winner
- return board.every(row => row.every(el => el !== "_")) ? "_" : undefined
- }
- function min(board, level = 0, alphaIn = -Infinity, betaIn = Infinity) {
- let minValue = Infinity
- let minRow = undefined
- let minCol = undefined
- let alpha = alphaIn
- let beta = betaIn
- const winner = getWinner(board)
- if (winner === PLAYER) {
- return [-1 * (10 - level)]
- }
- if (winner === CPU) {
- return [1 * (10 - level)]
- }
- if (winner === "_") {
- return [0]
- }
- for (let i = 0; i < 3; i++) {
- for (let j = 0; j < 3; j++) {
- if (board[i][j] === "_") {
- board[i][j] = PLAYER
- let [maxValue] = max(board, level + 1, alpha, beta)
- if (maxValue < minValue) {
- minValue = maxValue
- minRow = i
- minCol = j
- }
- board[i][j] = "_"
- if (minValue <= alpha) {
- return [minValue, minRow, minCol]
- }
- if (minValue < beta) {
- beta = minValue
- }
- }
- }
- }
- return [minValue, minRow, minCol]
- }
- function max(board, level = 0, alphaIn = -Infinity, betaIn = Infinity) {
- let maxValue = -Infinity
- let maxRow = undefined
- let maxCol = undefined
- let alpha = alphaIn
- let beta = betaIn
- const winner = getWinner(board)
- if (winner === PLAYER) {
- return [-1 * (10 - level)]
- }
- if (winner === CPU) {
- return [1 * (10 - level)]
- }
- if (winner === "_") {
- return [0]
- }
- for (let i = 0; i < 3; i++) {
- for (let j = 0; j < 3; j++) {
- if (board[i][j] === "_") {
- board[i][j] = CPU
- // const newBoard = setCell(board, i, j, CPU)
- let [minValue] = min(board, level + 1, alpha, beta)
- if (minValue > maxValue) {
- maxValue = minValue
- maxRow = i
- maxCol = j
- }
- board[i][j] = "_"
- if (maxValue >= beta) {
- return [maxValue, maxRow, maxCol]
- }
- if (maxValue > alpha) {
- alpha = maxValue
- }
- }
- }
- }
- return [maxValue, maxRow, maxCol]
- }
- // main
- const board = generateBoard()
- printBoard(board)
- let linesRead = 0
- let firstPlayer = "PLAYER"
- readline.on("line", line => {
- if (linesRead++ === 0) {
- firstPlayer = line
- if (firstPlayer === "CPU") {
- // CPU = "X"
- // PLAYER = "O"
- } else {
- const [_, i, j] = min(board)
- console.log(`The algorithm suggests you pick ${i + 1} ${j + 1}`)
- return
- }
- }
- // player
- if (linesRead > 1 || firstPlayer !== "CPU") {
- const [row, col] = line
- .split(" ")
- .map(Number)
- .map(decr)
- const valid = isValid(board, row, col)
- if (!valid) {
- console.log("Invalid move, try again.")
- return
- }
- board[row][col] = PLAYER
- printBoard(board)
- }
- const winner = getWinner(board)
- if (winner) {
- console.log(parseWinner(winner))
- readline.close()
- return
- } else {
- // CPU
- const [_, i, j] = max(board)
- board[i][j] = CPU
- printBoard(board)
- const winnerAfterCpu = getWinner(board)
- if (winnerAfterCpu) {
- console.log(parseWinner(winnerAfterCpu))
- readline.close()
- return
- }
- }
- const [_, i, j] = min(board)
- console.log(`The algorithm suggests you pick ${i + 1} ${j + 1}`)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement