Tic Tac Toe Help

Hello,

I am trying to revise my code, however, I am unsure of what to do for these last two things. First, how can I get my program to represent the board with dashes instead of numbers? I made my checker to a '-' however my program closes instantly. Second, what function do I need to write in order to check if someone has already placed a letter in the same spot. And it needs to say position taken, illegal move. Here is the assignment:
TIC-TAC-TOE

For this assignment you are to write a program to simulate the famous game of Tic-Tac-Toe. You must represent the board using a string. Input will consist of a number (1 through 9) and a character ('X' or 'O'), on a single line. The numbers 1 through 9, represent a position on the board. e.g.:

1 2 3
4 5 6
7 8 9

An empty board location will be represented with a dash ('-'). You only need to check if a board position is already occupied. If a board position is not already occupied the print a new board with the new character. If the board position is occupied, then print a message stating that the position is occupied, don't print a new board and ask the user to enter another input. You need not check for illegal inputs, such as numbers outside of the range 1-9 or characters other than 'X' and 'O'. Also, you need not check for a stalemate, the game will always end with a winner. e.g.

Welcome to TIC-TAC-TOE
- - -
- - -
- - -

Enter a move: 1 X
X - -
- - -
- - -

Enter a move: 5 O
X - -
- O -
- - -

Enter a move: 9 X
X - -
- O -
- - X

Enter a move: 1 O
Illegal move. Position occupied.
Enter a move: 6 O
X - -
- O O
- - X

Enter a move: 3 X
X - X
- O O
- - X

Enter a move: 4 O
X - X
O O O
- - X

Congtaulations! O won!!!

Here is my code:



#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class ticTacToe
{
public:
	void printBoard() const;
	void getMove(string playerList[], int number, char& playMove, int& playPosition);
	void setMove(char move, int position);
	void checkMove(char& move, int& position, int num);
	int numOfMoves() const; //keeps track of the number of moves made
	bool isWinner() const;
	ticTacToe();
private:
	char board[3][3];
	int totalMoves;
};

void setIntro(string playerList[]);
void checkPlayer(int& number);
void congratPlayer(string playerList[], int num);

int main()
{
	cout << "Welcome to TIC-TAC-TOE" << endl;

	ticTacToe myGame; //object to hold the board
	string player[2]; //variables to hold the names of the two players
	char move; //variable to hold wither X or O
	int position; //variable to hold the position on the board
	int index=0; //variable to hold the index of the player array

	setIntro(player);
	myGame.printBoard();

	

	while(!myGame.isWinner() && myGame.numOfMoves()<9) //checks that there is no winner and totalMoves<9
	{
		checkPlayer(index); //if index==0, its player[0] turn, otherwise player[1] turn.
		myGame.getMove(player,index,move,position);
		myGame.checkMove(move,position,index);
		myGame.setMove(move,position);

		if(myGame.isWinner()) //structure to end the program after someone wins
		{
			myGame.printBoard();
			congratPlayer(player, index);
			return 0;
		}

		cout << endl;
		myGame.printBoard(); //print board after each move

		index++;
	}//end while
	
	cout << "Cats game!" << endl; //executes if myGame.isWinner() is false and totalMoves==8

	return 0;
}//end main

void ticTacToe::printBoard() const
{
	for(int row=0;row<3;row++)
	{
		for(int col=0;col<3;col++)
			cout << board[row][col] << "   ";
		cout << endl << endl << endl;
	}
}

void ticTacToe::getMove(string playerList[], int number, char& playMove, int& playPosition)
{
	if(number==0)
	{
		cout << playerList[number] << "Enter a move: ";
		cin >> playPosition >> playMove;
	}
	else
	{
		cout << playerList[number] << "Enter a move: ";
		cin >> playPosition >> playMove;
	}
}

void ticTacToe::checkMove(char& move, int& position, int num)
{
	if(num==0)
	{
		while(move!='X' && move!='x')
		{
			cout << "You must enter X as your move. Please reenter your move: ";
			cin >> move;
		} 
	} //end if
	else
	{
		while(move!='O' && move!='o')
		{
			cout << "You  must enter O as your move. Please reenter your move: ";
			cin >> move;
		}
	} //end else


	while(position<=0 || position>=10)
	{
		cout << "Invalid position. Position must be between 1 and 9 inclusive. Reenter position: ";
		cin >> position;
		
	} //end while
}

void ticTacToe::setMove(char move, int position)
{
	if(position<=3)
		board[0][position-1]=static_cast<char>(toupper(static_cast<int>(move)));
	else if(position<=6)
	{
		switch(position)
		{
		case 4:
			board[1][0]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 5:
			board[1][1]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 6:
			board[1][2]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		}
	}
	else if(position<=9)
	{
		switch(position)
		{
		case 7:
			board[2][0]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 8:
			board[2][1]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		case 9:
			board[2][2]=static_cast<char>(toupper(static_cast<int>(move)));
			break;
		}
	}

	totalMoves++;
}

int ticTacToe::numOfMoves() const
{
	return totalMoves;
}

bool ticTacToe::isWinner() const
{
	if(board[0][0]==board[1][1] && board [0][0]==board[2][2])
			return true;
	else if(board[2][0]==board[1][1] && board [2][0]==board[0][2])
			return true;
	else if(board[0][0]==board[0][1] && board [0][0]==board[0][2])
			return true;
	else if(board[1][0]==board[1][1] && board [1][0]==board[1][2])
			return true;
	else if(board[2][0]==board[2][1] && board [2][0]==board[2][2])
			return true;
	else if(board[0][0]==board[1][0] && board [0][0]==board[2][0])
			return true;
	else if(board[0][1]==board[1][1] && board [0][1]==board[2][1])
			return true;
	else if(board[0][2]==board[1][2] && board [0][2]==board[2][2])
			return true;
	
	return false;

}

ticTacToe::ticTacToe()
{
	
	for(int row=0;row<3;row++)
		for(int col=0;col<3;col++)
		{
			board[row][col]='-';  // Closing instantly problem occurs here
			
		}
		
	totalMoves=0;
}

void setIntro(string playerList[])
{
	playerList[0]='X';
    playerList[1]='O';
}

void checkPlayer(int& number)
{
	if(number>1)
		number=0;
}



void congratPlayer(string playerList[], int num)
{
	cout << "Congratulations, " << playerList[num] << "! You have won!" << endl;

	system("pause");
}



Thanks for any help
Topic archived. No new replies allowed.