This one is on how to write a tic tac toe c++ game that plays against the computer. When the blackjack assignment was shared on StumbleUpon, it generated a large amount of traffic towards the C++ Better Explained site with over 2500 new visitors to the site. Due to popular demand, here is another article on game development with C++.
Tic Tac Toe Code using Classes
Just like the previous example of implementing blackjack in C++, there are several elements of the Tic Tac toe game that we need to break down. The following code example shows just how that is being done by creating a class of the game and creating a separate function for the needed piece of the game that is required.
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− //Tic Tac Toe Class //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− #ifndef TIC_TAC_TOE #define TIC_TAC_TOE #include <time.h> #include <stdlib.h> #include <stdio.h> #include <iostream> using std::cout; class TicTacToe { private: int pos[9]; // board position int player; // current player int totalTurns; // how many turns so far? int numberPlayers; // 1 to 20 bool playerType[20]; // player #, 0 = comp, 1 = human public: TicTacToe(); void printTurn(); bool playerHuman(); void humanMove(); void computerMove(); void drawBoard(); bool Winner(); bool fullBoard(); void nextTurn(); }; #endif
Tic_Tac_Toe.cpp file – drawBoard() and other main functions
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // Tic Tac Toe constructor //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− TicTacToe::TicTacToe() { srand (time(0)); player = 1; // who starts first? totalTurns = 0; // new player setup numberPlayers = 2; playerType[1] = 1; // playerType[player] is human (1) playerType[2] = 0; // playerType[player] is computer (0) for (int i = 0; i < 9; i++) { pos[i] = 0; } } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // Draw the Tic Tac Toe Board and seperate the board // with the help of | //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− void TicTacToe::drawBoard() { std::cout << std::endl<< pos[0] << " | " << pos[1] << " | " << pos[2]<< "\n?\n"<< pos[3] << " | " << pos[4] << " | " << pos[5]<< "\n?\n"<< pos[6] << " | " << pos[7] << " | " << pos[8]<< std::endl;} void TicTacToe::printTurn() { std::cout << "\nPlayer " << player << "'s turn.\n"; } void TicTacToe::nextTurn() { totalTurns++; if (++player > numberPlayers) player = 1; } bool TicTacToe::playerHuman() { return playerType[player]; } void TicTacToe::humanMove() { std::cout << "\nEnter your move: "; int move; do { std::cin >> move; move−−; // so user can enter 1−9 instead of 0−8 } while (move < 0 || move > 8 || pos[move] != 0); pos[move] = player; } void TicTacToe::computerMove() { int move; do { // Pick a random number from 1 − 9 move = rand() % 9; // just pick a random number } while (move < 0 || move > 8 || pos[move] != 0); pos[move] = player; } bool TicTacToe::Winner() { int board[8][3] = {{0,1,2}, // winning possibilities {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}}; // Go through the list of possibilities for (int i = 0; i < 8; i++) { if ((pos[board[i][0]] == pos[board[i][1]]) && (pos[board[i][1]] == pos[board[i][2]]) && pos[board[i][0]] != 0) { cout << "\nPlayer " << pos[board[i][0]] << " wins!\n\n"; return 1; // return winner true } } return 0; // winner false } bool TicTacToe::fullBoard() { if (totalTurns == 9) { cout << "\nTie game!\n\n"; return 1; } else return 0; }
Main.cpp file
#include "Tic_Tac_Toe.h" //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // Main function of the game //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− int main() { TicTacToe Game; Game.drawBoard(); do { Game.printTurn(); if (Game.playerHuman()) // human turn? Game.humanMove(); else Game.computerMove(); Game.drawBoard(); Game.nextTurn(); } // Keep running the game until there is a winner and the game board is full while (!Game.Winner() && !Game.fullBoard()); return 0; }