No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.idea
css
images
js
README.md
cartoon-landscape1.svg
index.html

README.md

Connect-4


Description

This code is a single page desktop and mobile friendly app for playing Connect-4, a popular two player game where each player tries to connect four tokens in a row (horizontally, vertically, or diagonally). As Connect-4 is a game of so-called "perfect information," the game engine is implemented via application of the Minimax Algorithm. A brief description of the app can be found on my personal website in the link below.

Author: Chad McKee

Languages and Technologies used

  • JavaScript
  • jQuery
  • HTML
  • CSS

Dependencies and Plugins

  • Bootstrap
  • Google Fonts

App Landing Page

Code Snippets

The Minimax Algorithm with alpha-beta pruning

The following code snippet demonstrates the game's implementation of the Minimax algorithm with alpha-beta pruning.

    function minimax(board, availableMoves, depth, alpha, beta, maximizer) {
        
        var bestValue;
        //Check if the player or cpu has won the game and assign
        //a point value to the game state accordingly.
        var currentValue = evaluateState(board);
        if (currentValue === 1000 || currentValue === -1000) {
            return currentValue;
        }

        //If no moves remain, and no one has won, the game a tie.
        if (availableMoves.length === 0) {
            return 0;
        }
        //If the maximum recursion depth has been reached, call a heuristic
        //function to assign a score to the game state based on who is ahead.
        if(depth === 0){
            return heuristic(board);
        }

        //Find the best move available from the perspective of the cpu.  This
        //corresponds to locating the game state with the highest score.
        if (maximizer) {
            bestValue = -10000;
            for (var i = 0; i < availableMoves.length; i++) {
                var boardCopy = copyBoard(board);
                var availableMovesCopy = copyAvailableMoves(availableMoves);
                updateBoard(boardCopy, availableMovesCopy[i], availableMovesCopy, false);
                bestValue = Math.max(minimax(boardCopy, availableMovesCopy, depth - 1, alpha, beta, !maximizer), bestValue);
                alpha = Math.max(bestValue, alpha);
                if(beta <= alpha){
                    break;
                }

            }
            return bestValue;
        }
        
        //Find the best move available from the perspective of the player.  This
        //corresponds to locating the game state with the lowest score.
        else{
            bestValue = 10000;
            for (var i = 0; i < availableMoves.length; i++) {
                var boardCopy = copyBoard(board);
                var availableMovesCopy = copyAvailableMoves(availableMoves);
                updateBoard(boardCopy, availableMovesCopy[i], availableMovesCopy, true);
                bestValue = Math.min(minimax(boardCopy, availableMovesCopy, depth - 1, alpha, beta, !maximizer), bestValue);
                beta = Math.min(bestValue, beta);
                if(beta <= alpha){
                    break;
                }
            }
            return bestValue;
        }

    }