[ create a new paste ] login | about

Link: http://codepad.org/fuC4JxBE    [ raw code | output | fork ]

C++, pasted on Mar 8:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// Global Variables
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
int sum;

// Prototypes

/******************************************************************************
* InitBoard
* This function initializes each spot in the board to a space ' '.
*
* RETURNS: Board initialized with all spaces
*****************************************************************************/
void InitBoard(char board[][3]); // tic tac toe board

/******************************************************************************
* DisplayBoard
* This function outputs the tic tac toe board including the tokens
*
* 1 2 3
* [1][1] | [1][2] | [1][3]
* | |
* 1 | |
* | |
* --------------------------
* [2][1] | [2][2] | [2][3]
* | |
* 2 | |
* | |
* --------------------------
* [3][1] | [3][2] | [3][3]
* | |
* 3 | |
* | |
*
* RETURNS: nothing
* Outputs the current state of the board
*****************************************************************************/
void DisplayBoard(const char board[][3]); // tic tac toe board

/******************************************************************************
* GetPlayers
* This function prompts the user and gets the input for the players’ names.
* player1 contains the name of the player that is using the X token.
* player2 contains the name of the player that is using the O token.
*
* RETURNS: the token opposite of the one in which it receives.
*****************************************************************************/
void GetPlayers(string& player1,	//player X’s name
		string& player2);	//player O’x name


void GetAndCheckInp(char board[][3], char token, string player1, string player2);

/******************************************************************************
* SwitchToken
*
* RETURNS: the token opposite of the one in which it receives.
*****************************************************************************/
char SwitchToken(char token); // current player’s token ('X' or 'O') - IN

/******************************************************************************
* CheckWin
* This function checks to see if either player has won.
*
* RETURNS the character value of the player that won or a value that
* indicates a tie.
*****************************************************************************/
char CheckWin(const char board[][3]); // tic tac toe board - IN

/******************************************************************************
* OutputWinner
*
* RETURNS: nothing
* Displays the winner’s name
*****************************************************************************/
void OutputWinner(char whoWon
		  string player1,
		  string player2);








int main()
{
	const int NUM_ROWS = 3;
	const int NUM_COLS = 3;

	// Variables
	bool again;
	bool won;
	char board[NUM_ROWS][NUM_COLS];
	char token;
	string player1;
	string player2;

	won = false;
	again = true;

	InitBoard(board[][NUM_COLS]);

	OutputInstruct();

	GetPlayers(player1, player2);

	token = 'X';

	do {

		while(!won)
		{
			GetAndCheckInp(board[][NUM_COLS], token, player1, player2);

			if (CheckWin(board[][NUM_COLS]) != 'N')
			{
				won = true;
			}

			DisplayBoard(board[][NUM_COLS]);

			SwitchToken(token);
		}

	} while(again);




	return 0;
}





void OutputInstruct()
{
	cout << "Play the game";
}

/******************************************************************************
* InitBoard
* This function initializes each spot in the board to a space ' '.
*
* RETURNS: Board initialized with all spaces
*****************************************************************************/
void InitBoard(char board[][numCols])	// tic tac toe board – OUT
{
	for(int i = 0; i <= NUM_ROWS; i++)
		for(int j = 0; j <= numCols; j++)
			board[i][j] = " ";
}

/******************************************************************************
* DisplayBoard
* This function outputs the tic tac toe board including the tokens
* played in the proper format (as described below).
*
* 1 2 3
* [1][1] | [1][2] | [1][3]
* | |
* 1 | |
* | |
* --------------------------
* [2][1] | [2][2] | [2][3]
* | |
* 2 | |
* | |
* --------------------------
* [3][1] | [3][2] | [3][3]
* | |
* 3 | |
* | |
*
* RETURNS: nothing
* Outputs the current state of the board
*****************************************************************************/
void DisplayBoard(const char board[][3]) // tic tac toe board
{
	cout << setw(10) << "1" << setw(8) << "2" << setw(9) << "3\n";
	for (int i=0; i < 3; i++)
	{
		cout << setw(7)<< "[" << i+1 << "][1] | " << "[" << i+1;
		cout << "][2] | " << "[" << i+1 << "][3]" << endl;
		cout << setw(14)<< "|" << setw(9) << "|" << endl;
		for (int j = 0; j < 3; j++)
		{
			switch(j)
			{
				case 0: cout << i + 1 << setw(9) << board[i][j];
				cout << setw(4) << "|";
				break;
				case 1: cout << setw(4) << board[i][j];
				cout << setw(5) << "|";
				break;
				case 2: cout << setw(4) << board[i][j] << endl;
				break;
				default: cout <<"ERROR!\n\n";
			}
		}
		cout << setw(14)<< "|" << setw(10) << "|\n";
		if (i != 2)
		{
			cout << setw(32) << "--------------------------\n";
		}
	}
	cout << endl << endl;
}

/******************************************************************************
* GetPlayers
* This function prompts the user and gets the input for the players’ names.
* player1 contains the name of the player that is using the X token.
* player2 contains the name of the player that is using the O token.
*
* RETURNS: the token opposite of the one in which it receives.
*****************************************************************************/
void GetPlayers(string& player1,
		string& player2)
{
	cout << "Enter the name of player 1(X):";
	getline (cin, player1);
	cout << "Enter the name of player 2(O)";
	getline (cin, player2);
}

void GetAndCheckInp(char board[][numCols],
		    char token,
		    string player1,
		    string player2)
{
	int row;
	int col;
	bool valid;
	do
	{
		if (token == 'X')
		{
			cout << player1 << "'s turn: What's your play?";
			cout << "Row?";
			cin >> row;
			cout << "Column?";
			cin >> col;
		}
		else if (token == 'O')
		{
			cout << player2 << "'s turn: What's your play?";
			cout << "Row?";
			cin >> row;
			cout << "Column?";
			cin >> col;
		}
		row--;
		col--;
		if ((row > NUM_ROWS || row < 0) || (col > numCols || col < 0))
		{
			valid = false;
			cout << "Please chose a space on the board." << endl;
		}
		else if (board[row][col] != ' ')
		{
			valid = false;
			cout << "That space is already taken, choose another";
		}
		else
		{
			valid = true;
			board[row][col] = token;
			sum++;
		}
	} while (!valid);
}

/******************************************************************************
* SwitchToken
*
* RETURNS: the token opposite of the one in which it receives.
*****************************************************************************/
char SwitchToken(char token) // current player’s token ('X' or 'O') - IN
{
	if (toupper(token) == 'X')
	{
		token = 'O';
	}
	else if (toupper(token) == 'O')
	{
		token = 'X';
	}
}

/******************************************************************************
* CheckWin
* This function checks to see if either player has won.

* RETURNS the character value of the player that won or T if it was a tie
*  or N if no won
*****************************************************************************/
char CheckWin(const char board[][numCols])	// tic tac toe board - IN
{
	char tmp;
	int lcv;

	if (sum == 9)
	{
		return 'T';
	}
	else if (sum != 9)
	{
		if (((tmp = board[1][1]) != ' ' && (board[0][0] == tmp && board[2][2] == tmp) || (board[2][0] == tmp && board[0][2] == tmp)))
		{
			return tmp;
		}

		for (lcv = 0; lcv < 3; lcv++)
		{
			if ((tmp = board[lcv][0]) != ' ' && board[lcv][1] == tmp && board[lcv][2] == tmp)
			{
				return tmp;
			}
			else if ((tmp = board[lcv][0]) != ' ' && board[lcv][1] == tmp && board[lcv][2] == tmp)
			{
				return tmp;
			}
		}
	}
	else
	{
		return 'N';
	}
}

/******************************************************************************
* OutputWinner
*
* RETURNS: nothing
* Displays the winner’s name
*****************************************************************************/
void OutputWinner(char whoWon,
		string player1,
		string player2)
{
	if (whoWon == 'X')
	{
	cout << player1 << " has won the game";
	}
	else if (whoWon == 'O')
	{
		cout << player2 << " has won the game";
	}
	else if (whoWon == 'T')
	{
		cout << player1 << " and " << player2 << " have tied";
	}
}


Output:
1
2
Line 83: error: expected ',' or '...' before 'string'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: