14 Replies - 5668 Views - Last Post: 11 December 2009 - 10:51 AM Rate Topic: -----

#1 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:10 PM

I am doing a c++ problem with tic tac toe an multidimensional arrays.


#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class TicTacToe{
private:
	char theBoard[3][3]; //variable
	int board[3][3];



public:
	TicTacToe(void);//constructor
	void playOneGame(void);//member function
	void switchPlayer(char &);//member function
	void showBoard(void);// member function 
	void postMove(int, int, char);// member function
	char determineWinner(void);// member function


};

int main(void){
	//test the class by playing one game
	TicTacToe Game1;
	Game1.playOneGame();
}//end main




void TicTacToe::playOneGame(void){
	//start a game and play until someone wins or a draw occurs
	const int MaxMoves = 9;
	char currentPlayer = 'O';
	int row = 0;
	int clmn = 0;
	char theWinner = ' ';
	int nmbrOfMoves = 0; //kep track of the number

	do{
		switchPlayer(currentPlayer);//change player from x to o or vice versa
		showBoard();
		cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
		cout << "Enter your row (0, 1, 2): ";
		cin >> row;
		cout << "Enter your column (0, 1, 2): ";
		cin >> clmn;

		postMove(row, clmn, currentPlayer); //post the move to the board

		theWinner = determineWinner(); //see if anyone won the game

		nmbrOfMoves++;//keeptrack of the number of moves
	}while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));

	showBoard();//show the ending board

	if(theWinner = ' 1') //declare a winner
		cout << "\n\nThe Winner is player " << theWinner << endl << endl;
	else
		cout << "\n\nThe Game was a Draw" << endl << endl;
}

TicTacToe::TicTacToe(void){
	//intialize the array contents
	theBoard[3][3]=' ';


}


void TicTacToe::switchPlayer(char &currentPlayer){
	//switches the current player


	if (currentPlayer == 'X')
		currentPlayer = 'O';
	else
		currentPlayer = 'X';
}

void TicTacToe::showBoard(){
	//displays the board
	
	cout << endl;
	for (int row=0; row<3; row++)
	{
		for (int clmn=0; clmn<3; clmn++)
		{
			cout << "["<< theBoard[row][clmn] << "]";
		}
		cout << endl;
	}
	cout << endl;

}

void TicTacToe::postMove(int row, int col, char value){
	//gets the users move and post it to the board//showXO
	char currentPlayer = ' ';
	//it's not doing what I want... 
	// the spaces are all full.. 

	if (theBoard[row][col]== 1|| theBoard[row][col]==2)
	{
		cout << "Space already used. Try again. " << endl;
	}
	else 
	{
		theBoard[row][col]=currentPlayer;
	}

	for(row = 0; row <3; row ++)
	{
		if(row > 3)
		{// should I say(row != 0) (row != 1) (row != 2)??
			cout << "Invalid choice" << endl;
		}

		for(col = 0; col <3; col++)
		{
			if(col > 3)
			{
				cout << "Invalid choice" << endl;
			}

			if(theBoard[row][col]==0)
			{
				theBoard[row][col]=' ';
			}
			if (theBoard[row][col]== 1)
			{
				theBoard[row][col] = 'X';
			}
			if (theBoard[row][col]== 2)
			{
				theBoard[row][col] = 'O';
			}

		}
	}
}








char TicTacToe::determineWinner(void){
	//analyzes the board to see if there is a winner
	//returns a X, O indicating the winner
	//if the game is a draw then D is returned

	for (int i = 0; i < 3; i++){
		if (theBoard[i][0] == theBoard[i][1]
		&& theBoard[i][1] == theBoard[i][2]
		&& theBoard[i][0] != ' '){
			return theBoard[i][0];
		}
	}
	//check the clmns
	for (int i = 0; i < 3; i++){
		if (theBoard[0][i] == theBoard[1][i]
		&& theBoard[1][i] == theBoard[2][i]
		&& theBoard[0][i] != ' '){
			return theBoard[0][i];
		}
	}
	//check the diagnals
	if (theBoard[0][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[2][2]
	&& theBoard[0][0] != ' ') {
		return theBoard[0][0];
	}
	if (theBoard[2][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[0][2]
	&& theBoard[2][0] != ' ') {
		return theBoard[2][0];
	}
	return 'D';
}




It shows no errors but it isn't working. It stops after you do the first move

Is This A Good Question/Topic? 0
  • +

Replies To: Tic Tac Toe with multidimensional arrays

#2 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:19 PM

theBoard[3][3]=' '; doesn't do what you want it to do. this sets the value at 3,3 (which isn't in the array) to space. you need nested for loops to initialize theBoard. inside these loops you should also be initializing the board array.
for (int x = 0; x < 3; x++)
	for (int y = 0; y < 3; y++)
	{
		theBoard[x][y] = ' ';
		board[x][y] = 0;
	}

This post has been edited by piman314: 10 December 2009 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:28 PM

That helped but i'm not sure what to do next there are no error and one warning
C4309: '=' : truncation of constant value




// Tic Tac Toe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class TicTacToe{
private:
	char theBoard[3][3]; //variable
	int board[3][3];



public:
	TicTacToe(void);//constructor
	void playOneGame(void);//member function
	void switchPlayer(char &);//member function
	void showBoard(void);// member function 
	void postMove(int, int, char);// member function
	char determineWinner(void);// member function


};

int main(void){
	//test the class by playing one game
	TicTacToe Game1;
	Game1.playOneGame();
}//end main




void TicTacToe::playOneGame(void){
	//start a game and play until someone wins or a draw occurs
	const int MaxMoves = 9;
	char currentPlayer = 'O';
	int row = 0;
	int clmn = 0;
	char theWinner = ' ';
	int nmbrOfMoves = 0; //kep track of the number

	do{
		switchPlayer(currentPlayer);//change player from x to o or vice versa
		showBoard();
		cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
		cout << "Enter your row (0, 1, 2): ";
		cin >> row;
		cout << "Enter your column (0, 1, 2): ";
		cin >> clmn;

		postMove(row, clmn, currentPlayer); //post the move to the board

		theWinner = determineWinner(); //see if anyone won the game

		nmbrOfMoves++;//keeptrack of the number of moves
	}while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));

	showBoard();//show the ending board

	if(theWinner = ' 1') //declare a winner
		cout << "\n\nThe Winner is player " << theWinner << endl << endl;
	else
		cout << "\n\nThe Game was a Draw" << endl << endl;
}

TicTacToe::TicTacToe(void){
	//intialize the array contents
	theBoard[3][3]=' ';


}


void TicTacToe::switchPlayer(char &currentPlayer){
	//switches the current player


	if (currentPlayer == 'X')
		currentPlayer = 'O';
	else
		currentPlayer = 'X';
}

void TicTacToe::showBoard(){
	//displays the board
	
	cout << endl;
	for (int row=0; row<3; row++)
	{
		for (int clmn=0; clmn<3; clmn++)
		{
			cout << "["<< theBoard[row][clmn] << "]";
		}
		cout << endl;
	}
	cout << endl;

}

void TicTacToe::postMove(int row, int col, char value){
	//gets the users move and post it to the board//showXO
	char currentPlayer = ' ';
	//it's not doing what I want... 
	// the spaces are all full.. 

	if (theBoard[row][col]== 1|| theBoard[row][col]==2)
	{
		cout << "Space already used. Try again. " << endl;
	}
	else 
	{
		theBoard[row][col]=currentPlayer;
	}

	for(row = 0; row <3; row ++)
	{
		if(row > 3)
		{// should I say(row != 0) (row != 1) (row != 2)??
			cout << "Invalid choice" << endl;
		}

		for(col = 0; col <3; col++)
		{
			if(col > 3)
			{
				cout << "Invalid choice" << endl;
			}

			if(theBoard[row][col]==0)
			{
				theBoard[row][col]=' ';
			}
			if (theBoard[row][col]== 1)
			{
				theBoard[row][col] = 'X';
			}
			if (theBoard[row][col]== 2)
			{
				theBoard[row][col] = 'O';
			}

		}
	}
}








char TicTacToe::determineWinner(void){
	//analyzes the board to see if there is a winner
	//returns a X, O indicating the winner
	//if the game is a draw then D is returned

	for (int i = 0; i < 3; i++){
		if (theBoard[i][0] == theBoard[i][1]
		&& theBoard[i][1] == theBoard[i][2]
		&& theBoard[i][0] != ' '){
			return theBoard[i][0];
		}
	}
	//check the clmns
	for (int i = 0; i < 3; i++){
		if (theBoard[0][i] == theBoard[1][i]
		&& theBoard[1][i] == theBoard[2][i]
		&& theBoard[0][i] != ' '){
			return theBoard[0][i];
		}
	}
	//check the diagnals
	if (theBoard[0][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[2][2]
	&& theBoard[0][0] != ' ') {
		return theBoard[0][0];
	}
	if (theBoard[2][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[0][2]
	&& theBoard[2][0] != ' ') {
		return theBoard[2][0];
	}
	return 'D';
}





Was This Post Helpful? 0
  • +
  • -

#4 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:35 PM

what line is it on? on the line if(theWinner = ' 1') you are setting theWinner to '1' instead of comparing it.
Was This Post Helpful? 0
  • +
  • -

#5 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:42 PM

That exact line.
Was This Post Helpful? 0
  • +
  • -

#6 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:45 PM

just change it to if(theWinner == ' 1') and it should work fine.
Was This Post Helpful? 1
  • +
  • -

#7 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:51 PM

It also doesn't post O's or X's


// Tic Tac Toe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class TicTacToe{
private:
	char theBoard[3][3]; //variable
	int board[3][3];



public:
	TicTacToe(void);//constructor
	void playOneGame(void);//member function
	void switchPlayer(char &);//member function
	void showBoard(void);// member function 
	void postMove(int, int, char);// member function
	char determineWinner(void);// member function


};

int main(void){
	//test the class by playing one game
	TicTacToe Game1;
	Game1.playOneGame();
}//end main




void TicTacToe::playOneGame(void){
	//start a game and play until someone wins or a draw occurs
	const int MaxMoves = 9;
	char currentPlayer = 'O';
	int row = 0;
	int clmn = 0;
	char theWinner = ' ';
	int nmbrOfMoves = 0; //kep track of the number

	do{
		switchPlayer(currentPlayer);//change player from x to o or vice versa
		showBoard();
		cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
		cout << "Enter your row (0, 1, 2): ";
		cin >> row;
		cout << "Enter your column (0, 1, 2): ";
		cin >> clmn;

		postMove(row, clmn, currentPlayer); //post the move to the board

		theWinner = determineWinner(); //see if anyone won the game

		nmbrOfMoves++;//keeptrack of the number of moves
	}while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));

	showBoard();//show the ending board

	if(theWinner == ' 1') //declare a winner
		cout << "\n\nThe Winner is player " << theWinner << endl << endl;
	else
		cout << "\n\nThe Game was a Draw" << endl << endl;
}

TicTacToe::TicTacToe(void){
	//intialize the array contents
	for (int x = 0; x < 3; x++)
	for (int y = 0; y < 3; y++)
	{
		theBoard[x][y] = ' ';
		board[x][y] = 0;
	}


}


void TicTacToe::switchPlayer(char &currentPlayer){
	//switches the current player


	if (currentPlayer == 'X')
		currentPlayer = 'O';
	else
		currentPlayer = 'X';
}

void TicTacToe::showBoard(){
	//displays the board
	
	cout << endl;
	for (int row=0; row<3; row++)
	{
		for (int clmn=0; clmn<3; clmn++)
		{
			cout << "["<< theBoard[row][clmn] << "]";
		}
		cout << endl;
	}
	cout << endl;

}

void TicTacToe::postMove(int row, int col, char value){
	//gets the users move and post it to the board//showXO
	char currentPlayer = ' ';
	//it's not doing what I want... 
	// the spaces are all full.. 

	if (theBoard[row][col]== 1|| theBoard[row][col]==2)
	{
		cout << "Space already used. Try again. " << endl;
	}
	else 
	{
		theBoard[row][col]=currentPlayer;
	}

	for(row = 0; row <3; row ++)
	{
		if(row > 3)
		{// should I say(row != 0) (row != 1) (row != 2)??
			cout << "Invalid choice" << endl;
		}

		for(col = 0; col <3; col++)
		{
			if(col > 3)
			{
				cout << "Invalid choice" << endl;
			}

			if(theBoard[row][col]==0)
			{
				theBoard[row][col]=' ';
			}
			if (theBoard[row][col]== 1)
			{
				theBoard[row][col] = 'X';
			}
			if (theBoard[row][col]== 2)
			{
				theBoard[row][col] = 'O';
			}

		}
	}
}








char TicTacToe::determineWinner(void){
	//analyzes the board to see if there is a winner
	//returns a X, O indicating the winner
	//if the game is a draw then D is returned

	for (int i = 0; i < 3; i++){
		if (theBoard[i][0] == theBoard[i][1]
		&& theBoard[i][1] == theBoard[i][2]
		&& theBoard[i][0] != ' '){
			return theBoard[i][0];
		}
	}
	//check the clmns
	for (int i = 0; i < 3; i++){
		if (theBoard[0][i] == theBoard[1][i]
		&& theBoard[1][i] == theBoard[2][i]
		&& theBoard[0][i] != ' '){
			return theBoard[0][i];
		}
	}
	//check the diagnals
	if (theBoard[0][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[2][2]
	&& theBoard[0][0] != ' ') {
		return theBoard[0][0];
	}
	if (theBoard[2][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[0][2]
	&& theBoard[2][0] != ' ') {
		return theBoard[2][0];
	}
	return 'D';
}




Was This Post Helpful? 0
  • +
  • -

#8 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 05:58 PM

in postMove you have char currentPlayer = ' '; so it looks at that local variable instead of the one in the class. Take that line out and it should work.
Also in that function you have
if (theBoard[row][col]== 1|| theBoard[row][col]==2).
you probably want
if (theBoard[row][col]== 'X'|| theBoard[row][col]=='O')
Was This Post Helpful? 0
  • +
  • -

#9 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 06:07 PM

Once I did what you said I had to declare current player as a variable and then it says that it is uninitialized.








// Tic Tac Toe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class TicTacToe{
private:
	char theBoard[3][3]; //variable
	int board[3][3];



public:
	TicTacToe(void);//constructor
	void playOneGame(void);//member function
	void switchPlayer(char &);//member function
	void showBoard(void);// member function 
	void postMove(int, int, char);// member function
	char determineWinner(void);// member function


};

int main(void){
	//test the class by playing one game
	TicTacToe Game1;
	Game1.playOneGame();
}//end main




void TicTacToe::playOneGame(void){
	//start a game and play until someone wins or a draw occurs
	const int MaxMoves = 9;
	char currentPlayer = 'O';
	int row = 0;
	int clmn = 0;
	char theWinner = ' ';
	int nmbrOfMoves = 0; //kep track of the number

	do{
		switchPlayer(currentPlayer);//change player from x to o or vice versa
		showBoard();
		cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
		cout << "Enter your row (0, 1, 2): ";
		cin >> row;
		cout << "Enter your column (0, 1, 2): ";
		cin >> clmn;

		postMove(row, clmn, currentPlayer); //post the move to the board

		theWinner = determineWinner(); //see if anyone won the game

		nmbrOfMoves++;//keeptrack of the number of moves
	}while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));

	showBoard();//show the ending board

	if(theWinner == ' 1') //declare a winner
		cout << "\n\nThe Winner is player " << theWinner << endl << endl;
	else
		cout << "\n\nThe Game was a Draw" << endl << endl;
}

TicTacToe::TicTacToe(void){
	//intialize the array contents
	for (int x = 0; x < 3; x++)
	for (int y = 0; y < 3; y++)
	{
		theBoard[x][y] = ' ';
		board[x][y] = 0;
	}


}


void TicTacToe::switchPlayer(char &currentPlayer){
	//switches the current player


	if (currentPlayer == 'X')
		currentPlayer = 'O';
	else
		currentPlayer = 'X';
}

void TicTacToe::showBoard(){
	//displays the board
	
	cout << endl;
	for (int row=0; row<3; row++)
	{
		for (int clmn=0; clmn<3; clmn++)
		{
			cout << "["<< theBoard[row][clmn] << "]";
		}
		cout << endl;
	}
	cout << endl;

}

void TicTacToe::postMove(int row, int col, char value){
	
	int currentPlayer;

	if (theBoard[row][col]== 'X'|| theBoard[row][col]=='O')
	{
		cout << "Space already used. Try again. " << endl;
	}
	else 
	{
		theBoard[row][col]=currentPlayer;
	}

	for(row = 0; row <3; row ++)
	{
		if(row > 3)
		{// should I say(row != 0) (row != 1) (row != 2)??
			cout << "Invalid choice" << endl;
		}

		for(col = 0; col <3; col++)
		{
			if(col > 3)
			{
				cout << "Invalid choice" << endl;
			}

			if(theBoard[row][col]==0)
			{
				theBoard[row][col]=' ';
			}
			if (theBoard[row][col]== 1)
			{
				theBoard[row][col] = 'X';
			}
			if (theBoard[row][col]== 2)
			{
				theBoard[row][col] = 'O';
			}

		}
	}
}
char TicTacToe::determineWinner(void){
	//analyzes the board to see if there is a winner
	//returns a X, O indicating the winner
	//if the game is a draw then D is returned

	for (int i = 0; i < 3; i++){
		if (theBoard[i][0] == theBoard[i][1]
		&& theBoard[i][1] == theBoard[i][2]
		&& theBoard[i][0] != ' '){
			return theBoard[i][0];
		}
	}
	//check the clmns
	for (int i = 0; i < 3; i++){
		if (theBoard[0][i] == theBoard[1][i]
		&& theBoard[1][i] == theBoard[2][i]
		&& theBoard[0][i] != ' '){
			return theBoard[0][i];
		}
	}
	//check the diagnals
	if (theBoard[0][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[2][2]
	&& theBoard[0][0] != ' ') {
		return theBoard[0][0];
	}
	if (theBoard[2][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[0][2]
	&& theBoard[2][0] != ' ') {
		return theBoard[2][0];
	}
	return 'D';
}








Was This Post Helpful? 1
  • +
  • -

#10 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 06:11 PM

you need a currentPlayer variable in the class, not in each function.
char currentPlayer = 'X';
Was This Post Helpful? 0
  • +
  • -

#11 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 06:27 PM

error C2864 only static const integral data member can be initialized in a class
Was This Post Helpful? 0
  • +
  • -

#12 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 06:30 PM

sorry it should be char currentPlayer; and in your constructor currentPlayer = 'X';
Was This Post Helpful? 0
  • +
  • -

#13 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 06:41 PM

Should that be public or private and still not showing O's and X's. It comes up as a | and two small lines.



// Tic Tac Toe.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;

class TicTacToe{
private:
	char theBoard[3][3]; //variable
	int board[3][3];
	char currentPlayer;


public:
	TicTacToe(void);//constructor
	void playOneGame(void);//member function
	void switchPlayer(char &);//member function
	void showBoard(void);// member function 
	void postMove(int, int, char);// member function
	char determineWinner(void);// member function


};

int main(void){
	//test the class by playing one game
	TicTacToe Game1;
	Game1.playOneGame();
}//end main




void TicTacToe::playOneGame(void){
	//start a game and play until someone wins or a draw occurs
	const int MaxMoves = 9;
	char currentPlayer = 'X';
	int row = 0;
	int clmn = 0;
	char theWinner = ' ';
	int nmbrOfMoves = 0; //kep track of the number

	do{
		switchPlayer(currentPlayer);//change player from x to o or vice versa
		showBoard();
		cout << "\n\nPlayer " << currentPlayer << endl; //get the players move
		cout << "Enter your row (0, 1, 2): ";
		cin >> row;
		cout << "Enter your column (0, 1, 2): ";
		cin >> clmn;

		postMove(row, clmn, currentPlayer); //post the move to the board

		theWinner = determineWinner(); //see if anyone won the game

		nmbrOfMoves++;//keeptrack of the number of moves
	}while((theWinner=='D')&&(nmbrOfMoves < MaxMoves));

	showBoard();//show the ending board

	if(theWinner == ' 1') //declare a winner
		cout << "\n\nThe Winner is player " << theWinner << endl << endl;
	else
		cout << "\n\nThe Game was a Draw" << endl << endl;
}

TicTacToe::TicTacToe(void){
	//intialize the array contents
	for (int x = 0; x < 3; x++)
	for (int y = 0; y < 3; y++)
	{
		theBoard[x][y] = ' ';
		board[x][y] = 0;
	}


}


void TicTacToe::switchPlayer(char &currentPlayer){
	//switches the current player

	if (currentPlayer == 'X')
		currentPlayer = 'O';
	else
		currentPlayer = 'X';
}

void TicTacToe::showBoard(){
	//displays the board
	
	cout << endl;
	for (int row=0; row<3; row++)
	{
		for (int clmn=0; clmn<3; clmn++)
		{
			cout << "["<< theBoard[row][clmn] << "]";
		}
		cout << endl;
	}
	cout << endl;

}

void TicTacToe::postMove(int row, int col, char value){
	
	

	if (theBoard[row][col]== 'X'|| theBoard[row][col]=='O')
	{
		cout << "Space already used. Try again. " << endl;
	}
	else 
	{
		theBoard[row][col]=currentPlayer;
	}

	for(row = 0; row <3; row ++)
	{
		if(row > 3)
		{// should I say(row != 0) (row != 1) (row != 2)??
			cout << "Invalid choice" << endl;
		}

		for(col = 0; col <3; col++)
		{
			if(col > 3)
			{
				cout << "Invalid choice" << endl;
			}

			if(theBoard[row][col]==0)
			{
				theBoard[row][col]=' ';
			}
			if (theBoard[row][col]== 1)
			{
				theBoard[row][col] = 'X';
			}
			if (theBoard[row][col]== 2)
			{
				theBoard[row][col] = 'O';
			}

		}
	}
}
char TicTacToe::determineWinner(void){
	//analyzes the board to see if there is a winner
	//returns a X, O indicating the winner
	//if the game is a draw then D is returned

	for (int i = 0; i < 3; i++){
		if (theBoard[i][0] == theBoard[i][1]
		&& theBoard[i][1] == theBoard[i][2]
		&& theBoard[i][0] != ' '){
			return theBoard[i][0];
		}
	}
	//check the clmns
	for (int i = 0; i < 3; i++){
		if (theBoard[0][i] == theBoard[1][i]
		&& theBoard[1][i] == theBoard[2][i]
		&& theBoard[0][i] != ' '){
			return theBoard[0][i];
		}
	}
	//check the diagnals
	if (theBoard[0][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[2][2]
	&& theBoard[0][0] != ' ') {
		return theBoard[0][0];
	}
	if (theBoard[2][0] == theBoard[1][1]
	&& theBoard[1][1] == theBoard[0][2]
	&& theBoard[2][0] != ' ') {
		return theBoard[2][0];
	}
	return 'D';
}



Was This Post Helpful? 0
  • +
  • -

#14 piman314  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 169
  • Joined: 07-August 09

Re: Tic Tac Toe with multidimensional arrays

Posted 10 December 2009 - 06:46 PM

the switchPlayer method shouldn't take a parameter, and in playOneGame you shouldn't have char currentPlayer = 'X';
Was This Post Helpful? 1
  • +
  • -

#15 hash018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 10-December 09

Re: Tic Tac Toe with multidimensional arrays

Posted 11 December 2009 - 10:51 AM

It works now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1