The game is only a couple hundred lines of Ruby in a single main.py file, so we'll walk through it in this tutorial step-by-step. (This will be almost exactly the same as my tutorial for the Python version.)

The Board

The nice thing about tic-tac-toe is that the game is easy to model! While we could build out an entire Board class (and that's something you could add if you wanted to fork this REPL and update it), but for simplicity's sake, we'll just model our board is a two-dimensional array:

@board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]

The board-related functions in our game will be empty_cells (which returns open spots on the board), set_move (which places an X or an O in the provided spot, assuming it's available), and render (which draws the board). Let's look at each one in turn.

def empty_cells(state)
[].tap do |cells|
state.each_with_index do |row, x|
row.each_with_index do |cell, y|
cells << [x, y] if cell.zero?
end
end
end
end

Here, we iterate through all the cells on the board and check to see if the value is still 0 (which is how we initialized the board, as you can see above). If so, we add that cell's coordinates to the cells array, then return that array so we know which spots are still available for a player to choose. If you're curious about the [].tap magic, all that does is let us modify our array inside the block, then automatically return the completed array; you can read more about Object#taphere.

This is also pretty straightforward: we check to see if our chosen move is valid (that is, it's in the empty_cells array), and if so, we set that square on the board to X or O (depending on which player is moving), returning true on success and false if the move can't be made because the square is already occupied.

Our last board method just prints out the current state of the board, printing out every cell and showing a blank for free cells, X for cells taken by the X player, and O for cells taken by the O player.

Evaluating Moves

So far, so good! We return 1 if the provided state (which is a two-dimensional array) would result in the computer winning, -1 if the human player would win, and 0 if it would be a tie. There are two important things to note here:

We choose -1 for a human player win because we want the minimax algorithm to run from the computer player's perspective: it's trying to minimize the maximum score the human player can achieve;

This function is run for possible game states, meaning that the computer player uses this function to look at the results of all possible moves, all the possible moves those moves would allow, all the possible moves those moves would allow, and so on, generally limited to a certain depth (at which point the computer chooses the move that leads to the worst "possible futures" for the human player; that is, the branch in the tree of possible states with the smallest possible maximum score).

This might seem a little confusing now, but we'll go through all the details when we get to the minimax function itself.

Here, we simply return true if the player has achieved a set of winning moves. There are eight winning moves in tic-tac-toe: the top row, the middle row, the bottom row, the left column, the middle column, the right column, and the two diagonals (corresponding to the eight entries in our win_state array).

def game_over(state)
wins(state, HUMAN) || wins(state, COMP)
end

This one's pretty easy! If either the human or the computer wins, the game is immediately over.

The Minimax Algorithm

On to the main event: the minimax algorithm! This is how our computer player decides which moves to make.

As mentioned earlier, this algorithm looks at "possible futures" in the game, assigning expected scores to each, and makes the move that maximizes the score (for the computer) and minimizes the score (for the human). (Remember, since the algorithm is being used by the computer player to select a move, it will want to maximize its score and minimize the human opponent's score.) We take the state of the game, the depth to recurse to in our game tree, and a player, and initialize our "best move" to -1, -1, -INFINITY (that is, a non-existent square with worst/lowest possible score) for the computer player and -1, -1, +INFINITY (a non-existent square with the best/highest possible score) for a human player. Then, for as long as we're recursing and the game isn't over, we continually make imaginary moves and check the resulting scores, assigning as the "best" score the one that represents the highest possible (for the computer) or lowest possible (for the human). In the base case, we return the result of evaluate on our game state, and the overall algorithm returns the move with the highest perceived score for the computer and the lowest one for the human.

The computer player uses minimax to make the best possible move given the current state of the board, with the exception of when depth == 9 (that is, every square on the board is empty), in which case since the computer player is moving first, there's no "best" move and the computer selects a random square.

Finally, our main function lets the human player select X or O and whether they want to move first or second, then enters the game loop: as long as there's an empty square on the board, alternate human and computer player turns, displaying the appropriate messages for when the human player wins, loses, or ties with the computer. We kick everything off just by calling main at the very end of the file.

...and that's it! I hope you enjoyed this tutorial, and feel free to fork this REPL to add more functionality.