3 Replies - 5372 Views - Last Post: 08 November 2010 - 08:47 AM Rate Topic: -----

#1 Whomper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-November 10

Help with tic tac toe using 2d arrays

Posted 04 November 2010 - 09:10 AM

Hi, I'm a pretty new c++ programmer and I am currently attempting to create a game of tic tac toe using two dimensional arrays.

I'm stuck on the part which I need to get the coordinates from the player, this is what i have for the get position functuion.

//Get the position the player wants to mark on the board
int getPosition(char board [3][3], char playerSymbol){
	int XCoord;
	int yCoord;
	bool checkCoordX;
	bool checkCoordY;
	
	//Get the X coordinate the player wishes to mark and store it in the variable XCoord
	// Check that the X coordinate is between 1 and 3
	while (checkCoordX = false){
		cout << "Please select the X coordinate: " << endl;
		cin >> XCoord;
		if (XCoord < 1 || XCoord > 3){
			checkCoordX = false;
			cout << "Please enter a correct value between 1 and 3" << endl;
		}
		else{
			checkCoordX = true;
		}
	}

	//Get the y coordinate the player wishes to mark and store it in the variable yCoord
	// Check that the y coordinate is between 1 and 3
	
	while (checkCoordY = false){
		cout << "Please select the y coordinate: " << endl;
		cin >> yCoord;
		if (yCoord < 1 || yCoord > 3){
			checkCoordY = false;
			cout << "Please enter a correct value between 1 and 3" << endl;
		}
		else{
			checkCoordY = true;
		}
	}
		board[XCoord][yCoord] == playerSymbol;
	
}


is it possible to pass the variables into the array like I have? Also how would I call this function.

(Sorry if what I'm saying doesn't make sense im very new and I barely know what I'm talking about)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with tic tac toe using 2d arrays

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Help with tic tac toe using 2d arrays

Posted 04 November 2010 - 09:19 AM

while (checkCoordX = false)

That there is an ASSIGNMENT, not a COMPARISON. = is for assigning a value to the value on the left side of the statement; == is for comparing the two sides of the statement.

Also, arrays are indexed starting from 0, not 1. Therefore valid indexes for a 3-element array are 0, 1, and 2.
Was This Post Helpful? 1
  • +
  • -

#3 auggiecc87  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 09-March 09

Re: Help with tic tac toe using 2d arrays

Posted 04 November 2010 - 09:22 AM

Couple things first. Make sure to use a standard naming convention throughout the whole program (kind of nit picky but helps make your code easier to read/edit). By this I mean choose either XCoord or yCoord (note the capitalization difference.

Also, arrays index at 0 not 1 so the user input number must either be 0-2 or you must subtract 1 from xCoord and yCoord in the board edit.

That should get you started. Other than that you are going to want to check if the location has already been set. (Currently your code would allow me to over write the other players choice)
Was This Post Helpful? 1
  • +
  • -

#4 Whomper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-November 10

Re: Help with tic tac toe using 2d arrays

Posted 08 November 2010 - 08:47 AM

hey guys thanks for the replies, i've been working on the code in my spare time and this is what i have at the moment.

The problem I'm having is that everytime it outputs the board, there are no coordinates showed, any ideas?

#include <iostream>
#include <locale>
using namespace std;


//Function prototypes
int checkWin(char board [3][3]);
char displayBoard(char board [3][3]);
int getPosition (char board [3][3], char playerSymbol);




int main() 
{
	char board[3][3] = { {' ',' ',' '},
			     {' ',' ',' '},
			     {' ',' ',' '} };
	char player1Symbol = 0;
	char player2Symbol = 0;
	char playerTurn; //The variable that stores whether it is player ones turn or player two
	bool correctPlayerSymbol = false;
	int turnCounter = 1; // Determines how many turns there has been in the game
	int xCoord;
	int yCoord;
	bool checkCoordX = false;
	bool checkCoordY = false;
	

	//Allows each player to select their symbol
	while (correctPlayerSymbol == false){
		cout << "player 1, please select your symbol (X , O)" << endl;
		cin >> player1Symbol;
		player1Symbol == toupper(player1Symbol); // Converts the input to a capital letter
		if (player1Symbol == 'X' || player1Symbol == 'O' ){ // {'X','\0'}
			correctPlayerSymbol = true;
			cout << "Congratulations, you have selected " << player1Symbol << endl;
		}
		else{
			correctPlayerSymbol = false;
		}
	}
	

	//Assigns player 2 the opposite symbol to player 1
	if (player1Symbol == 'X'){
		player2Symbol = 'O';
		cout << "Player 2, your symbol is O" << endl;
	}
	else if (player1Symbol == 'O'){
		player2Symbol = 'X';
		cout << "Player 2, your symbol is X" << endl;

	}

	// determine which players turn it is
	while (turnCounter <=9 && checkWin(board) != 1){

		if (turnCounter % 2 == 0){
			playerTurn = player1Symbol;
			cout << "Player 1, it is your turn!" << endl;		
			
		}
		else{
			playerTurn = player2Symbol;
			cout << "Player 2, it is your turn!" << endl;
		}

		checkCoordX = false;
		checkCoordY = false;
		//Get the X coordinate the player wishes to mark and store it in the variable XCoord
		// Check that the X coordinate is between 1 and 3
		while (checkCoordX == false){
			cout << "Please select the X coordinate: " << endl;
			cin >> xCoord;
			if (xCoord < 1 || xCoord > 3){
				checkCoordX = false;
				cout << "Please enter a correct value between 1 and 3" << endl;
			}
			else{
				checkCoordX = true;
			}
		}

		//Get the y coordinate the player wishes to mark and store it in the variable yCoord
		// Check that the y coordinate is between 1 and 3
		while (checkCoordY == false){
			cout << "Please select the y coordinate: " << endl;
			cin >> yCoord;
			if (yCoord < 1 || yCoord > 3){
				checkCoordY = false;
				cout << "Please enter a correct value between 1 and 3" << endl;
			}
			else{
				checkCoordY = true;
			}	
		}

		if (board[yCoord-1][xCoord-1] != ' '){
			cout << "This space has already been taken!" << endl;
			continue;
		} 
		else{
			board[yCoord-1][xCoord-1] == playerTurn;
			displayBoard(board);
		}


		turnCounter++;

		
	}

	if (checkWin(board) == 1) { 
		cout << "Player " << playerTurn << " has won!" << endl;
	}
	else{
		cout << "Player " << playerTurn << " has lost!" << endl;
	
	}
	return 0;
}


//function to display the tic tac toe board
char displayBoard(char board[3][3])
{
	cout << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
        cout << " |" << "___________" << "| " << endl;
        cout << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
        cout << " |" << "___________" << "| " << endl;
        cout << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
}



//Check if game has been won

int checkWin(char board [3][3])
{
	int win;
	win = 0;

	//Check verticals for player 1
	if( board [0][0] == 'X' && board [1][0] == 'X' && board [2][0] == 'X' || board [0][1] == 'X' && board [1][1] == 'X' && board [2][1] == 'X' || board [0][2] == 'X' && board [1][2] == 'X' && 		board [2][2] == 'X'){
		win = 1;
	}

	//Check horizontals for player 1
	if( board [0][0] == 'X' && board [0][1] == 'X' && board [0][2] == 'X' || board [1][0] == 'X' && board [1][1] == 'X' && board [1][2] == 'X' || board [2][0] == 'X' && board [2][1] == 'X' && 		board [2][2] == 'X') {
		win = 1;

	}
	//check diagonals for player 1
	if( board [0][0] == 'X' && board [1][1] == 'X' && board [2][2] == 'X' || board [0][2] == 'X' && board [1][1] == 'X' && board [2][0] == 'X' ) {
		win = 1;

	}

	return win;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1