But now can't finish it.. I can't figure out how to make the board display after every turn. Can anyone help?

#include <iostream>
using namespace std;
void showBoard (char t[3][3]); 

int main ()
{
    char player;
    char t[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
    int place = 0;

    for (int i = 0; i < 9; i++)
    {
        if ( i%2 == 0)
    { cout << "Place X (1-9): "; player = 'X';}
    else
    { cout << "Place O (1-9): "; player = 'O';}

    cin >> place;

    }
switch (place)

{ case 1: t[0][0] = player;
    break;
  case 2: t[0][1] = player;
    break;
  case 3: t[0][2] = player;
    break;
  case 4: t[1][0] = player;
    break;
  case 5: t[1][1] = player;
    break;
  case 6: t[1][2] = player;
    break;
  case 7: t[2][0] = player;
    break;
  case 8: t[2][1] = player;
    break;
  case 9: t[2][2] = player;
    break;
}

  for (int i = 0; i < 3; i++)
    { for (int j = 0; j < 3; j++)
    cout << t[i][j];
    cout << endl;
  }

return 0;
}

Recommended Answers

All 3 Replies

First define the showBoard. Maybe this will get you started :

void showBoard (char t[3][3])
{	
	for(int i = 0; i < 3; i++)
	{
		cout<<"---------------------\n";

		for(int j = 0; j < 3; j++)
		{
			cout << t[i][j] << "   |   ";
		}
		cout<<endl;
	}
	cout<<"---------------------\n";
}

Then after you get the position where the user wants to put a X or an 0, insert it into the 2d array and call the print
function again. This all should go until the game is over.

well i defined the function, but still when i use it in the body of my code it only displays the board after the last case in the switch. how do i make it display after every move?

#include <iostream>
using namespace std;
void showBoard (char t[3][3]);

int main ()
{
	char player;
	char t[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
	int place = 0;

	for (int i = 0; i < 9; i++)
	{	if ( i%2 == 0)
	{ cout << "Place X (1-9): "; player = 'X';}
	else
	{ cout << "Place O (1-9): "; player = 'O';}
	cin >> place;

	}
	

switch (place)

{ case 1: t[0][0] = player;
	break;
  case 2: t[0][1] = player;
    break;
  case 3: t[0][2] = player;
    break;
  case 4: t[1][0] = player;
	break;
  case 5: t[1][1] = player;
    break;
  case 6: t[1][2] = player;
	break;
  case 7: t[2][0] = player;
	break;
  case 8: t[2][1] = player;
    break;
  case 9: t[2][2] = player;
	break;
	
}
showBoard (t);
return 0;
}
void showBoard (char t[3][3])
{
for (int i = 0; i < 3; i++)
{ for (int j = 0; j < 3; j++)
	cout << t[i][j];
	cout << endl;
 }
}

Do something like this, in psuedo code

initAllVariables
bool play = true;

while( play)
{
    //print board;
    //get user input
    //if input is invalid, try again
   // insert the input into the gameBoardArray
    // print board
   // while valid : generate computer move
  // print board
  //check if game is over
 // if over ask user to play again ?
 // if yes call resetFunction that resets everything.
 //else return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.