< Previous | Contents | Next >

Setting Up the Program

You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 6 folder; the filename is tic-tac-toe.cpp. Ill go over the code here, section by section.

The first thing I do in the program is include the files I need, define some global constants, and write my function prototypes.

// Tic-Tac-Toe

// Plays the game of tic-tac-toe against a human opponent


#include <iostream> #include <string> #include <vector> #include <algorithm>


using namespace std;

206 Chapter 6 n References: Tic-Tac-Toe


image

Table 6.1 Tic-Tac-Toe Functions

Function Description


void instructions() Displays the game instructions.

char askYesNo(string question) Asks a yes or no question. Receives a question. Returns

either a ’y’ or an ’n’.

int askNumber(string question, int

high, int low = 0)

Asks for a number within a range. Receives a question, a low number, and a high number. Returns a number in the range from low to high.

char humanPiece() Determines the human’s piece. Returns either an ’X’ or an ’O’.

char opponent(char piece) Calculates the opposing piece given a piece. Receives

either an ’X’ or an ’O’. Returns either an ’X’ or an

’O’.

void displayBoard(const vector<char>& board)

char winner(const vector<char>&

board)


bool isLegal(const vector<char>&

board, int move)

int humanMove(const vector<char>&

board, char human)

int computerMove(vector<char> board, char computer)

void announceWinner(char winner, char computer, char human)

Displays the board on the screen. Receives a board.


Determines the game winner. Receives a board. Returns an ’X’, ’O’, ’T’ (to indicate a tie), or ’N’ (to indicate that no one has won yet).

Determines whether a move is legal. Receives a board and a move. Returns either true or false.

Gets the human’s move. Receives a board and the human’s piece. Returns the human’s move.

Calculates the computer’s move. Receives a board and the computer’s piece. Returns the computer’s move. Congratulates the winner or declares a tie. Receives the

winning side, the computer’s piece, and the human’s

piece.


// global constants const char X = ’X’; const char O = ’O’; const char EMPTY = ’ ’; const char TIE = ’T’; const char NO_ONE = ’N’;


// function prototypes void instructions();

char askYesNo(string question);

Introducing the Tic-Tac-Toe Game 207


int askNumber(string question, int high, int low = 0); char humanPiece();

char opponent(char piece);

void displayBoard(const vector<char>& board); char winner(const vector<char>& board);

bool isLegal(const vector<char>& board, int move); int humanMove(const vector<char>& board, char human); int computerMove(vector<char> board, char computer);

void announceWinner(char winner, char computer, char human);

In the global constants section, X is shorthand for the char ’X’, one of the two pieces in the game. O represents the char ’O’, the other piece in the game. EMPTY, also a char, represents an empty square on the board. Its a space because when its displayed, it will look like an empty square. TIE is a char that represents a tie game. And NO_ONE is a char used to represent neither side of the game, which I use to indicate that no one has won yet.