Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Tic tac toe using AI and C++

, 13 Aug 2013
Rate this:
Please Sign up or sign in to vote.
Using Artifecial Intelligance in the tic tac toe game to make computer never lose. Moreover two humans can play with each other.

Introduction

Tic tac toe is a well know game. Basically there is a 3x3 board and two players: one is X and the other is O, and there are 8 probabilities for winning diagonals, rows, and columns. In this proposal I’m going to explain how I implemented and designed this game as a computer program in two main parts.

  • First part: Two humans play with each other.
  • Second part: A human plays with the computer and the computer never loses.

UI design

I designed the user interface to be flexible in use and to meet the look and feel for users. The game board consists of 9 buttons as in the following figure:

Sample Image

The UI gives the flexibility for users to choose between two options, either a human and computer or two humans.

Sample Image

The UI shows the user who should play to manage the game and make it easier.

Sample Image

Design Architecture

I divided the solution to many parts so the development would be easier.

Two Humans design

Sample Image

Checkwin: Checks if either one of the players connected three blocks in row, column, or diagonal (8 possibilities).

Sample Image

Computer and human design

I designed an algorithm myself as I didn’t want to get it the easy way and search on the internet and get a pre-written code. The algorithm is designed for 3X3 and expected to be scalable and work for an odd number of blocks as 5x5.

As the following figure shows, there are eight probabilities for connecting three blocks with the same symbol.

Sample Image

First step: Fill the center block. By this way I forbid the opponent from four opportunities and he still has another 4 as the following figure shows.

Sample Image

Second step: However the opponent plays, there is no danger till now and I have to decreased his number of opportunities. The best location for playing now is the corners as it decreases his opportunities by two in one turn.

Sample Image

Third step: Check the two lines (1 and 2) as the previous figure shows, and the one which contains two similar symbols; the computer should set the third with the other opposite symbol so I don’t let the opponent win as the following figure shows.

Sample Image

Fourth step: Check the last available line for the opponent to win and check if he has two blocks set with his symbol and then check the third.

Using the code

Sample Image

Fifth step: Check for the last empty place (and it should be one place at this step as the board has only 9 blocks) and set it. In steps 3 and 4 in the above algorithm I first check If I can win and then implement the step.

License

This article, along with any associated source code and files, is licensed under The Mozilla Public License 1.1 (MPL 1.1)

Share

About the Author

Ahmed-Fayed
CartoLogic
Egypt Egypt
Junior Software Engineer at CartoLogic

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 13 Aug 2013
Article Copyright 2013 by Ahmed-Fayed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid