-2

I am working on a basic Javascript Tic Tac Toe game that lets a user play against the computer. One of the requirements for this game is that the user can never beat the computer the most they can do is tie. I'm having trouble figuring out the logic for this and I have not seen any examples I can understand on how to implement this. Right now the computer just chooses a random spot to place it's decision on it's turn. If this random spot is the top left corner(randomChoice==0) or the bottom right corner (randomChoice ==9) it changes it to the box next to it. I have posted the code below any tips on this would be helpful. Also this is a link to all the code I have so far on CodePen. http://codepen.io/Android162010/pen/LGZXQa

function playRandom() {    


    randomChoice = Math.round(Math.random() * 10);

    if (randomChoice == 0) {
        randomChoice = 1;
    }
    if (randomChoice == 10) {
        randomChoice = 9;
    }


    if ($('#' + randomChoice).hasClass('hoverable')) {
        makeTic('#' + randomChoice, false);
    }
    else {
        playRandom();
    }
}
Blaine
  • 31
  • 6
  • 1
    Hard code a winning strategy for the computer. Tic tac toe is simple enough of a game that you could get away with a giant set of if statements (perhaps encoded). – ryanyuyu Dec 22 '15 at 21:59
  • 2
    http://neverstopbuilding.com/minimax – azium Dec 22 '15 at 21:59
  • You're definitely in the right place to get help, but this isn't really a question. I suggest editing the question to narrow it down to exactly what you're stuck on. Your program's only job is to pick the square that minimizes its chances of losing. A good first step would be to always pick the center square if it's available. – Joel Anair Dec 22 '15 at 22:08

1 Answers1

5

You can use the minimax algorithm to create an AI for playing Tic Tac Toe.

Here is an article on minimax being used with Tic Tac Toe: http://neverstopbuilding.com/minimax

The idea is that, instead of choosing moves at random you look ahead to possible future states of the game, and rank them by how good they are. The author presents a system where a winning node is assigned a value of +10 and a losing node gets -10.

# @player is the turn taking player
def score(game)
    if game.win?(@player)
        return 10
    elsif game.win?(@opponent)
        return -10
    else
        return 0
    end
end

Once you have a system for ranking future states based on how good they are for you, you can formulate an algorithm for choosing the best states. Here is the author's plain-english explanation of the algorithm:

A description for the algorithm, assuming X is the "turn taking player," would look something like:

  • If the game is over, return the score from X's perspective.
  • Otherwise get a list of new game states for every possible move
  • Create a scores list
  • For each of these states add the minimax result of that state to the scores list
  • If it's X's turn, return the maximum score from the scores list
  • If it's O's turn, return the minimum score from the scores list

And finally, here is the author's solution

def minimax(game, depth)
    return score(game) if game.over?
    depth += 1
    scores = [] # an array of scores
    moves = []  # an array of moves

# Populate the scores array, recursing as needed
game.get_available_moves.each do |move|
    possible_game = game.get_new_state(move)
    scores.push minimax(possible_game, depth)
    moves.push move
end

# Do the min or the max calculation
if game.active_turn == @player
    # This is the max calculation
    max_score_index = scores.each_with_index.max[1]
    @choice = moves[max_score_index]
    return scores[max_score_index]
else
    # This is the min calculation
    min_score_index = scores.each_with_index.min[1]
        @choice = moves[min_score_index]
        return scores[min_score_index]
    end
end
Community
  • 1
  • 1
Matt Geyer
  • 112
  • 5
  • 3
    ["Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline."](http://stackoverflow.com/help/how-to-answer) Could you summarize or quote the important parts of the site? – Mike Cluck Dec 22 '15 at 22:05
  • 1
    @MikeC now the answer contains a pretty good explanation/summary of the linked article. – ryanyuyu Dec 22 '15 at 22:47