3 Replies - 8173 Views - Last Post: 22 February 2010 - 08:46 AM Rate Topic: -----

#1 Riskinit   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 174
  • Joined: 14-January 10

Help with 2 dimensional array. TicTacToe

Posted 21 February 2010 - 05:04 PM

I am making a tictactoe program. Our teacher requires us to use a two dimensional array so the code below does that. However, when I go to the function player1() and try to enter a number things get wierd.

#include <iostream>
using namespace std;

//Function Prototype.
void showTable();
void player1();
void player2();
char test[2] = {'X','O'};
char marker[3][3] = {{'7','8','9'},{'4','5','6'},{'1','2','3'}};
char choice;

int main()
{
	int turn;
  /*    marker[0][0] = '7';
        marker[0][1] = '8';
        marker[0][2] = '9';
        marker[1][0] = '4';
        marker[1][1] = '5';
        marker[1][2] = '6';
        marker[2][0] = '1';
        marker[2][1] = '2';
        marker[2][2] = '3';*/
        showTable();
		for(turn = 0; turn < 9; turn++)
		{
			if(turn = 0,2,4,6,8)
				player1();
			else
				player2();
		}
}

void showTable()
{
	    cout << marker[0][0] << " | " << marker[0][1] << " | " << marker[0][2] << endl;
        cout << "__________" << endl << endl;
        cout << marker[1][0] << " | " << marker[1][1] << " | " << marker[1][2] << endl;
        cout << "__________" << endl << endl;
        cout << marker[2][0] << " | " << marker[2][1] << " | " << marker[2][2] << endl << endl;
}

void player1()
{
	cout << "Player 1's turn. Enter a number." << endl;
	cin >> choice;
	switch(choice)
	{
	case '1': if(marker[2][0] == test[1] || marker[2][0] == test[2])
			  {
				  cout << "That spot has already been taken!" << endl;
				  cin >> choice;
			  }
			  else
			  {
				  marker[2][0] = 'X';
				  showTable();
			  }
			  break;
	case '2': if(marker[2][1] != 'X' || marker[2][1] != 'O')
			  {
				  marker[2][1] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	case '3': if(marker[2][2] != 'X' || marker[2][2] != 'O')
			  {
				  marker[2][2] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	case '4': if(marker[1][0] != 'X' || marker[1][0] != 'O')
			  {
				  marker[1][0] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	case '5': if(marker[1][1] != 'X' || marker[1][1] != 'O')
			  {
				  marker[1][1] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	case '6': if(marker[1][2] != 'X' || marker[1][2] != 'O')
			  {
				  marker[2][0] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	case '7': if(marker[3][0] != 'X' || marker[3][0] != 'O')
			  {
				  marker[3][0] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	case '8': if(marker[3][1] != 'X' || marker[3][1] != 'O')
			  {
				  marker[3][1] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	case '9': if(marker[3][2] != 'X' || marker[3][2] != 'O')
			  {
				  marker[3][2] = 'X';
				  showTable();
			  }
			  else
			  {
				  cin >> choice;
			  }
			  break;
	}
}

void player2()
{
	cout << "Player 2's turn. Enter a number." << endl;
	cin >> choice;
}


Basically when I run the program and click '1' it gets stuck in the switch statement, and even though I am constantly clicking 1 it never recognizes that spot has already been taken. So I figured the table wasn't updating. So my question is how do I make it update the table?

Is This A Good Question/Topic? 0
  • +

Replies To: Help with 2 dimensional array. TicTacToe

#2 Paul-   User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Help with 2 dimensional array. TicTacToe

Posted 21 February 2010 - 10:36 PM

This line
	case '1': if(marker[2][0] == test[1] || marker[2][0] == test[2])

should be
	case '1': if(marker[2][0] == test[0] || marker[2][0] == test[1])

Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7505
  • View blog
  • Posts: 15,553
  • Joined: 16-October 07

Re: Help with 2 dimensional array. TicTacToe

Posted 22 February 2010 - 05:11 AM

This is not real C++ code: if(turn = 0,2,4,6,8)

Globals are bad. Do you have to use a 2D array, because clearly you don't need it.

Don't do "player1" and "player2", rather "player(char currentPlayer)". Your goal is to reduce repetition, no increase it.
Was This Post Helpful? 0
  • +
  • -

#4 Pro.Z   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-February 10

Re: Help with 2 dimensional array. TicTacToe

Posted 22 February 2010 - 08:46 AM

Hey,

I changed the whole source code , sorry , but in the same time I use 2D array as you wanted

And I added a feature which is asking you to play again and again



/*
Coded by Pro.Z 
[email protected]
*/
#include<iostream>
using namespace std;

void ShowTable(char [][3]);
void ResetTable(char[] [3]);
char Marker[][3]={'1','2','3','4','5','6','7','8','9'};

void main(void)
{
	char player_symbol = 'X';
	char player_turn = '1';
	char player_move;
	char play_again = 'Y';
	int R,C,times_played = 0; // just for FOR Loop
	ShowTable(Marker);
	do{

		cout << "Player No." << player_turn << " turn" << endl;
		cin >> player_move;
		for ( R = 0 ; R<3 ; R++)
		{
			for(C = 0 ; C<3 ; C++)
			{
				if(Marker[R][C] == player_move)
				{
					Marker[R][C] = player_symbol;
				}
			}
		}

		for (R=0 ; R<3 ; R++)
		{
			if((Marker[R][0] == Marker[R][1] && Marker[R][1] == Marker[R][2]) || (Marker[0][R] == Marker[1][R]&& Marker[1][R] == Marker[2][R]))
			{
				ShowTable(Marker);
				cout << "Player No." << player_turn << " wins\nCongratulations\n" << endl;
				cout << "Do you wnat to player again\nEnter Y for yes ->" << endl;
				cin >> play_again;
				if(play_again == 'Y' || play_again == 'y')
				{
					ResetTable(Marker);
					play_again = 'Y';
					player_turn = '2';
					times_played = 0;
				}
			}
		}
		
		if((Marker[0][0] == Marker[1][1] && Marker[1][1] == Marker[2][2]) || (Marker[0][2] == Marker[1][1]&& Marker[1][1] == Marker[2][0]))
		{
			ShowTable(Marker);
			cout << "Player No." << player_turn << " wins\nCongratulations" << endl;
			cout << "Do you wnat to player again\nEnter Y for yes ->" << endl;
			cin >> play_again;
			if(play_again == 'Y' || play_again == 'y')
			{
				ResetTable(Marker);
				play_again = 'Y';
				player_turn = '2';
				times_played = 0;
			}
		}

		if( play_again == 'Y')
		{
			if(player_turn == '1')
			{
				player_turn = '2';
				player_symbol = 'O';
				ShowTable(Marker);
			}
			else 
			{
				player_turn = '1';
				player_symbol = 'X';
				ShowTable(Marker);
			}
		}
		times_played++;
		if(times_played == 9)
		{
			cout << "There's no winner\n\nDo you want to play again?\nEnter Y for yes->\n";
			cin >> play_again;
			if(play_again == 'Y' || play_again == 'y')
			{
				ResetTable(Marker);
				play_again = 'Y';
				player_turn = '1';
				times_played = 0;
				ShowTable(Marker);
			}
		}

	}while(play_again == 'Y');

}

void ShowTable(char M[][3])
{
	int i,j; 
	for(i=0;i<3;i++)
	{
		cout << "|  ";
		for(j=0;j<3;j++)
		{
			cout << M[i][j] << "  |  ";
		}
		cout << endl << endl;
	}
}

void ResetTable(char M[][3])
{
	char cChar = '1';
	int R,C;
	for(R=0 ; R<3 ; R++)
	{
		for(C=0 ; C<3 ; C++)
		{
			M[R][C] = cChar;
			cChar++;
		}
	}
}



Peace from Saudi Arabia

Good luck buddy

This post has been edited by Pro.Z: 22 February 2010 - 10:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1