Assignment No: 15
Title/Problem Statement:
Design and develop the Tic-Tac-Toe Game using C++.
Theory:
Introduction about Tic-Tac-Toe game:
Tic-tac-toe is a fun game that you can play anytime and anywhere as long as you
have a piece of paper, a pencil, and an opponent. Tic-tac-toe is a zero sum game, which
means that if both players are playing their best, that neither player will win. However, if
you learn how to play tic-tac-toe and master some simple strategies, then you'll be able to
not only play, but to win the majority of the time.
If you want to know how to play tic-tac-toe, then see Step 1 to get started.
1
1) Draw the board. First, you have to draw the board, which is made up of a 3 x 3
grid of squares. This means it has three rows of three squares. Some people play
with a 4 x 4 grid, but that is for more advanced players, and we will focus on the
3 x 3 grid here.
2
2) Have the first player go first. Though traditionally, the first player goes with
"X", you can allow the first player to decide whether he wants to go with "X"s or
"O"s. These symbols will be placed on the table, in the attempt to have three of
them in a row. If you're going first, then the best move you can make is to move
into the center. This will maximize your chances of winning, since you'll be able
to create a row of three "X"s or "O"s in more combinations (4) this way than if
you chose a different square.
3
3) Have the second player go second. After the first player goes, then the second
player should put down his symbol, which will be different from the symbol of
the first player. The second player can either try to block the first player from
creating a row of three, or focus on creating his or her own row of three. Ideally,
the player can do both.
4
4) Keep alternating moves until one of the players has drawn a row of three
symbols or until no one can win. The first player to draw three of his or her
symbols in a row, whether it is horizontal, vertical, or diagonal, has won tic-tac-
toe. However, if both players are playing with optimal strategy, then there's a
good chance that no one will win because you will have blocked all of each
other's opportunities to create a row of three.
5
5) Keep practicing. Contrary to popular belief, tic-tac-toe isn't purely a game of
chance. There are some strategies that can help you optimize your skills and to
become an expert tic-tac-toe player. If you keep playing, you'll soon learn all of
the tricks to making sure you win every time -- or, at least, you'll learn the tricks
to make sure you never lose. Its like 0's and x's.
Algorithm:
Algorithm main()
//this is used to create a TIC-TAC-TOE game using matrix
{
1. Display a board whenever player make marking of X or O
2. Select a player only once initially player 1 is selected
player=(player%2)?1:2;
3. Ask player to select his/her square from 3×3 grid
4. If player 1 is playing then select X either select O
mark=(player == 1) ? 'X' : 'O'
5. Fill that square with marked notation
a. If all enter square is not <=9 then print invalid move
6
6. Check for the winner
7. Repeat step 1 to 7 still 3×3 grid become full
8. Print whether player 1 or player 2 are winner or game draw
}
Algorithm checkwin()
//this is used to check winner in tic tac toe game
{
1. if (square[1] == square[2] && square[2] == square[3])
1.1 return 1;
2. else if (square[4] == square[5] && square[5] == square[6])
2.1 return 1;
3. else if (square[7] == square[8] && square[8] == square[9])
3.1 return 1;
4. else if (square[1] == square[4] && square[4] == square[7])
4.1 return 1;
5. else if (square[2] == square[5] && square[5] == square[8])
5.1 return 1;
6. else if (square[3] == square[6] && square[6] == square[9])
6.1 return 1;
7. else if (square[1] == square[5] && square[5] == square[9])
7.1 return 1;
8. else if (square[3] == square[5] && square[5] == square[7])
8.1 return 1;
9. else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] !=
'4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] !=
'8' && square[9] != '9')
9.1 return 0;
10. else
10.1 return -1;
}
Conclusion: Thus we have implemented Tic-Tac-Toe game using C++ concepts.