Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a simple "paper and pencil" game. Two players alternate turns, with the first player marking an X on a 3-by-3 game board and the second marking an O. The goal is to make three of your marks in a row, blocking and preventing your opponent from doing the same.
You are watching: Tic tac toe game in c++ using array
Tic-tac-toe was one of the first (possibly the first) games used to explore artificial intelligence. As suggested by the Wikipedia link, writing a computer program to play a perfect game is relatively simple (see the section on strategy). However, at this point in our study of C++, we use tic-tac-toe only as a way of demonstrating two-dimensional arrays and how to pass them as function arguments.
The Tic-Tac-Toe Problem
Write a C++ program that allows two players to play tic-tac-toe.
Program Requirements:Use an array to represent the playing boardInitialize the array to represent an empty boardDisplay the game board prior to every move and at the end of the gameprompt each player to move in their turnThe player moves by entering a row and column number and the program marks the corresponding array element with either an X or an OThe program validates each move by insuring that the row and column are inbounds and that the selected space is empty - if the player enters an invalid move, the program loops until a valid move is enteredAs a special case, if the player enters -1 for either the row or the column, the game endsAfter each move, test to see if there is a winnerIf there is a winner, the program displays the board, announces the winner, and endsDeclare a draw and end when no moves remain
Even when a program is small and simple, programmers often have a great deal of flexibility in how they choose to implement its various parts. As the size and complexity increase, so do the number of options. The tic-tac-toe example focuses on just four of those choices to illustrate that some choices only work on some operating systems, and some involve tradeoffs between different parts of the program.
One of the purposes of the tic-tac-toe example is to demonstrate how to pass two-dimensional arrays to functions. That implies that the program must define at least one function beyond main. But functions have value far beyond demonstrating syntax and behavior. Functions help to focus our attention on what needs to be done rather than on how to do it. Functions help us manage the complexity of increasingly large programs: they allow us to focus on one small sub-problem - one that we are more likely to understand in its entirety, from beginning to end - while temporarily ignoring the full problem. Our first choice is how to decompose the tic-tac-toe problem into a set of mutually interoperable functions.
The second choice that we explore is how we draw the game board. We play the tic-tac-toe game on a 3×3 board. As we have done all semester, we will make the board using ASCII or text characters. But even within this constraint, we still have a choice to make. As a first choice, we may use the characters "|" and "-" to represent the vertical and horizontal lines and the "+" character to represent the intersection or cross lines. Although this approach works, it doesn"t result in smooth, unbroken lines. A second approach is to use the drawing symbols that are available on some systems as extended ASCII codes.
const char VERT = "|";const char HORIZ = "-";const char CROSS = "+";
|const char VERT = (char)179;const char HORIZ = (char)196;const char CROSS = (char)197;|
See more: Play Battle Gear 3 Hacked - Battle Gear 3 Hacked (Cheats)
The fourth and final choice only applies to the display function needed by the version of the program represented by Figure 3(b), and just shows that a given sub-problem can of often be solved in many ways. (The display function for version (a) is simple and structured very much like the multiplication table introduced in chapter 3.) The fence post problem, also introduced in chapter 3, describes a general problem where a loop must print a repeating pattern. In the tic-tac-toe program, the pattern is ar,0|ar,1|ar,2, where each ar,c is one element of row r of the array, and | is the vertical line that is part of the playing board. Using pseudo code, we can demonstrate three of the possible ways structuring the inner loop of the display function:
for col = 0, 1, and 2 print a
|print a||for col = 0 and 1 print a|
|ttt1.cpp||ttt1.cpp||moves and board characters together in the array (Figure 3(a))|
|ttt2.cpp||ttt2.cpp||moves only in the array (Figure 3(b)); has three versions of the display function:Does not use the fence post problem solutionUsed the fence post solutionUses the fact that the game board has exactly three rows and three columns to eliminate the for-loops altogether|