Code Monkey home page Code Monkey logo

tic-tac-toe's Introduction

tic-tac-toe

Unbeatable Tic Tac Toe. Single-page HTML, CSS & JavaScript.

tic-tac-toe

Implementation of Minimax algorithm wrapped up in a neat chalkboard UI. The chalk effect is achieved using SVG turbulence filters.

Minimax algorithm:

tic-tac-toe/index.html

Lines 438 to 482 in 3e10e64

function getComputerMoveData(tempPlaygroundState, depth) {
const winCombinationIndex = getWinCombinationIndex(tempPlaygroundState);
if (winCombinationIndex !== -1) {
// we've got a winner in this hypothetical game!
const wonMover = tempPlaygroundState[winCombinations[winCombinationIndex][0]];
return {
index: -1,
score: wonMover === 1
? -1 // user hypothetically won (bad)
: 1 // computer hypothetically won (good)
};
} else if (!tempPlaygroundState.some((mover) => mover === -1)) {
// a tie
return { index: -1, score: 0 };
}
depth = (depth || 0) + 1;
// 1 - user's hypothetical turn; 0 - computer's hypothetical turn.
const mover = +!(depth % 2);
const availableMoves = tempPlaygroundState
.map((_, i) => i)
.filter((boardIndex) => tempPlaygroundState[boardIndex] === -1);
const scores = [];
const moves = [];
for (let i = 0; i < availableMoves.length; i++) {
const move = availableMoves[i];
const iterationPlayground = [ ...tempPlaygroundState ];
iterationPlayground[move] = mover;
const hypotheticalMoveData = getComputerMoveData(iterationPlayground, depth);
scores.push(hypotheticalMoveData.score);
moves.push(move);
// rollback the hypothetical move
tempPlaygroundState[move] = -1;
}
const func = mover === 1
? Math.min // we're looking for the min score if it's user's turn
: Math.max; // and for the max score if it's computer's score
const moveScore = func.apply(null, scores);
const moveScoreIndex = scores.indexOf(moveScore);
return { index: moves[moveScoreIndex], score: moveScore };
}

DEMO

tic-tac-toe's People

Contributors

g-30 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.