Tic Tac Toe CLI Game

The popular game made in C for the terminal.

: C

Concepts Used

  • Structural Programming
  • Minimax Algorithm
  • Ascii Art

Features

  • Player Vs Player Mode
  • Player Vs Computer Mode
  • CLI with ascii art

Link


Introduction

I had once heard that Bill Gates’ first program was a TicTacToe game..

This was my 2nd C program project. I wanted to try to make this game by myself and I will say that it was tough.

Working

I had learned to use ASCII art to make CLI to look attractive, so I used it as much as possible.

Each position on the grid is numbered, in order from 0.

Basic implementation was an multi dimentional array of the winning positions and check after every move.

unsigned wins[8][3]={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};

This worked pretty well for Player Vs Player. But I wanted to try to make Player Vs Computer mode.

After alooot of research and understanding things I sat down to code and after considerable effort modified my whole program to accomodate that.

This was when the tough part came in, I had to make or find an algorithm and a system to predict the best way to figure out which moves had the highest rate of success, that is getting the board layout having atleast one match with the winnig positions.

Then I found that I needed to use the Minimax algorithm(A tree search algorithm) and set a score for every move and iterate through possibilities in terms of a tree.

Moving back a branch, the ones with the least future moves which leads to success will be chosen.

It was a huge deal for me back then and it took some time to get my head around the concept and then implement it.

This was the first time I used a recursion in my code…

I think(I grid[9], I player) { // grid is the present grid condition and I player is 
    int winner = won(grid);   // won checks if success position exists, 0 means no.
    if(winner != 0) {
            return winner*player;
            }
    int move = -1;
    int score = -2;
    int i=0;
    for(i = 0; i <= 8 ; i++) {
        if(grid[i] == 0) {   // iterate through grid
            grid[i] = player; // check by placing marker at grid[i]
            int thisScore = -think(grid, player*-1); //recursion, `*-1` to switch between players. thisScore is the score of the lowest node at the bottom. 
            if(thisScore > score) {
                score = thisScore;
                move = i;
            } // pick the one that's worst for the opponent
            grid[i] = 0; // reset board after try
        }
    }
    if(move == -1){ return 0;} 
    return score;
}

The computer will keep on checking till the best postion is found and this function think is called on every position move by player.

The best the player can do is draw against the computer.

Features

The features can be summarised below:

  • CLI mode
  • 2 Modes of play:
    • Player Vs Player
    • Player Vs Computer
  • Unwinnable against Computer
  • ASCII art splash screens for welcome, winning and exit
  • Very quick and responsive

Conclusion

The end product was pretty cool, I can challenge people to win against the computer, offering a reward.

No one did. Thats the gimmick. Computers are dumb, but boy, can they calculate quickly.