1 Replies - 723 Views - Last Post: 13 May 2013 - 11:41 PM Rate Topic: -----

#1 vidkid  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 06-April 13

Help With Tic Tac Toe Program

Posted 13 May 2013 - 10:14 PM

Please Help,

I am having trouble finishing this Tic Tac Toe program. I am having trouble primarily with my functions for if there are winning rows, columns, or diagonals. When I build my solution I get this:

visual studio 2010\projects\tictactoe\tictactoe\tictactoe.cpp(120): warning C4700: uninitialized local variable 'sym1' used
visual studio 2010\projects\tictactoe\tictactoe\tictactoe.cpp(122): warning C4700: uninitialized local variable 'sym2' used
1>TicTacToe.obj : error LNK2019: unresolved external symbol "bool __cdecl playerWin(char (* const)[3])" (?playerWin@@YA_NQAY02D@Z) referenced in function _main
1>TicTacToe.obj : error LNK2019: unresolved external symbol "bool __cdecl validPosition(char (* const)[3],int,int)" (?validPosition@@YA_NQAY02DHH@Z) referenced in function _main
1>TicTacToe.obj : error LNK2019: unresolved external symbol "void __cdecl displayboard(char (* const)[3],int)" (?displayboard@@YAXQAY02DH@Z) referenced in function _main
\visual studio 2010\Projects\TicTacToe\Debug\TicTacToe.exe : fatal error LNK1120: 3 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========



#include <iostream>
#include <string>
using namespace std;
//function prototypes
void initboard(char [][3], int);
void displayboard(char[][3], int);
bool validPosition (char board [][3],int row, int col);
bool winRow (char board[][3], int Row);
bool winCol (char board[][3], int Col);
bool winDiagonal(char board[][3]);
bool playerWin(char board[][3]);

void main()
{
	int round=0, r=0, c=0, player=0;
	char board [3][3];
	string player1, player2;
	bool done = false;
	char sym1, sym2;

	cout<<" Welcome " <<endl<<"**************"<<endl;

	cout<<"\n Player 1: what is your name and what symbol do you want to use? ";
	cin>>player1>>sym1;
	cout<<"\n Player 2: what is your name and what symbol do you want to use? ";
	cin>>player2>>sym2;
	cout<<"**************"<<endl;

	//Function call - to initialize the tic tac toe board
	initboard(board,3);
	//Function call - to display the board
	displayboard(board,3);

	//let's play!!
	do
	{
		//get & validate the coordinates for the position
		do
		{
			if(player==0)
				cout<<player1;
			else
				cout<<player2;

			cout<<" , please enter the coordinates for your position (Row,Col): ";
			cin>>r>>c;
			cout<<endl;
			//validate the input and make sure the location is not taken
		}while(!validPosition(board,r,c));

		//Update the board by placing the player's symbol at the requested position

		if(player==0)
			board[r][c]=sym1;
		else
			board[r][c]=sym2;

		displayboard(board,3);

		//check if there was a win

		if(playerWin(board))
		{
			if(player=0)
				cout<<player1<<" won! congratulations!!\n";
			else
				cout<<player2<<" won! congratulation!!\n";

			done=true;
		}else
			round++;

		player=(player+1)%2;
		
	}while((round<9)&& !done);
}//end of main function

//this function initializes the tic tac toe board
void initboard(char b[][3], int size)
{
	for(int row=0; row<3; row++)
	{
		for(int col=0; col<3; col++)
			b[row][col]=' ';
	}
}

//this function displays the tic tac toe board
void displayboard(const char b[][3], int size)
{
	char i;

	for(i=0; i<3; i++)
	{
		cout<<"|"<<b[i][0]<<"|"<<b[i][1]<<"|"<<b[i][2]<<endl;
		cout<<"--------"<<endl;
	}
}
//this function returns true is the requested position is valid and available; otherwise, it returns false

bool validPosition(const char board[][3], int row, int col)
{
	if((row<0)||(col<0)||(row>=3)||(col>=3))
	{
		cout<<"Wrong!"<<endl;
		return false;
	}
	else
		return true;
	
}
//this function returns true if all the symbols in row "Row" are the same and are not blank, otherwise it returns false
bool winRow(const char board[][3], int Row)
{
	char sym1;
	char sym2;

	for(int i=0; i<3; i++)
	{
		if(Row == sym1)
			return true;
		else if (Row == sym2)
			return true;
		else if (Row != sym1 || Row != sym2)
			return false;
	}
	return false;

}
//this function returns true if all the symbols in col "Col" are the same and are not blank, otherwise it returns false
bool winCol(const char board[][3], int Col)
{
	for(int i=0; i<3; i++)
	{
		if(winCol(board,i))
			return true;
	}
	return false;
}
//this function returns true if there was a diagonal win, otherwise, it returns false
bool winDiagonal(const char board[][3])
{
	for(int i=0; i<3; i++)
	{
		if(winDiagonal(board))
			return true;
	}
	return false;
}
//this function returns true if there was a win, otherwise, it returns false
bool playerWin(const char board [][3])
{
	for(int i=0; i<3; i++)
	{
		if(winRow(board,i))
			return true;
	}
	for(int i=0; i<3; i++)
	{
		if(winCol(board,i))
			return true;
	}
	if(winDiagonal(board))
	{
		return true;
	}
	return false;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help With Tic Tac Toe Program

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Help With Tic Tac Toe Program

Posted 13 May 2013 - 11:41 PM

There are differences between your function prototypes and there definitions.

This post has been edited by jjl: 13 May 2013 - 11:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1