Thread: 3D Tic Tac Toe How to Reset Boards?

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    3

    3D Tic Tac Toe How to Reset Boards?

    Okay so I have a completely different code now and for the most part it works. The thing is, I want the game to run 10 total times after a winner is declared. And I have that, only when the new game starts, the boards doesn't reset.

    How do I reset the boards?
    Code:
    #include<iostream>
    #include<stdio.h>
    #include<string>
    #include<stdlib.h>
    
    
    using namespace std;
    //---------------------------------------------------------------------Global Scope-------------------------------------------------------------------
    char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
    char board2[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
    char board3[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
    char user = 'O';
    int d;
    
    
    void DisplayBoard()
    { 	
    		printf("\nBoard 1\n");	
    		for (int a = 0; a < 3; a++)
    	{
    		for (int b = 0; b < 3; b++)
    		{	
    			cout << board[a][b] << " ";	
    		}
    		cout << endl;
    	}
    		printf ("\nBaord 2\n");
    		for (int f = 0; f < 3; f++)
    	{
    		for (int g = 0; g < 3; g++)
    		{	
    			cout << board2[f][g] << " ";
    		}
    		cout << endl;
    	}
    		printf ("\nBoard 3\n");
    		for (int h = 0; h < 3; h++)
    	{
    		for (int j = 0; j < 3; j++)
    		{	
    			cout << board3[h][j] << " ";
    		}
    		cout << endl;
    	}
    }
    //-----------------------------------------------------------------------CLASS----------------------------------------------------------------------------------
    class Game{
    public:
    //Class Funtion 1:
    void BoardDisplay()
    { 	
    		printf("\nBoard 1\n");	
    		for (int a = 0; a < 3; a++)
    	{
    		for (int b = 0; b < 3; b++)
    		{	
    			cout << board[a][b] << " ";	
    		}
    		cout << endl;
    	}
    		printf ("\nBaord 2\n");
    		for (int f = 0; f < 3; f++)
    	{
    		for (int g = 0; g < 3; g++)
    		{	
    			cout << board2[f][g] << " ";
    		}
    		cout << endl;
    	}
    		printf ("\nBoard 3\n");
    		for (int h = 0; h < 3; h++)
    	{
    		for (int j = 0; j < 3; j++)
    		{	
    			cout << board3[h][j] << " ";
    		}
    		cout << endl;
    	}
    }
    //Class Funtion 2:
    void user_input()
    {
    	int c = (rand () % 27) + 1;
    	cout << "\nIt's\t " << user << "\tTurn! Please Choose A Space: \n";
    	
    	if (c == 0)
    	{
    		cout << "This Is Not A Valid Input!" << endl;
    		user_input();
    	}	
    	if (c == 1)
    	{
    		if (board[0][0] == '1'){
    			board[0][0] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    	 	else
            {
                cout << "This Spot Is Already Taken" << endl;
                user_input();
            }	
    	}
    	else if (c == 2)
    	{
    		if (board[0][1] == '2'){
    			board[0][1] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 3)
    	{
    		if (board[0][2] == '3'){
    			board[0][2] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 4)
    	{
    		if (board[1][0] == '4'){
    			board[1][0] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 5)
    	{
    		if (board[1][1] == '5'){
    			board[1][1] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 6)
    	{
    		if (board[1][2] == '6'){
    			board[1][2] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 7)
    	{
    		if (board[2][0] == '7'){
    			board[2][0] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 8)
    	{
    		if (board[2][1] == '8'){
    			board[2][1] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();
    		}	
    	}
    	else if (c == 9)
    	{
    		if (board[2][2] == '9'){
    			board[2][2] = user;
    			printf ("\n%c chose Board 1 Space %d\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	//BOARD TWO
    	else if (c == 10)
    	{
    		if (board2[0][0] == '1'){
    			board2[0][0] = user;
    			printf ("\n%c chose Board 2 Space 1\n", user);
    		}
    	 	else
            {
                cout << "This Spot Is Already Taken" << endl;
                user_input();
            }	
    	}
    	else if (c == 11)
    	{
    		if (board2[0][1] == '2'){
    			board2[0][1] = user;
    			printf ("\n%c chose Board 2 Space 2\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 12)
    	{
    		if (board2[0][2] == '3'){
    			board2[0][2] = user;
    			printf ("\n%c chose Board2 Space 3\n", user);
    			}		
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 13)
    	{
    		if (board2[1][0] == '4'){
    			board2[1][0] = user;
    			printf ("\n%c chose Board 2 Space 4\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 14)
    	{
    		if (board2[1][1] == '5'){
    			board2[1][1] = user;
    			printf ("\n%c chose Board 2 Space 5\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 15)
    	{
    		if (board2[1][2] == '6'){
    			board2[1][2] = user;
    			printf ("\n%c chose Board 2 Space 6\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 16)
    	{
    		if (board2[2][0] == '7'){
    			board2[2][0] = user;
    			printf ("\n%c chose Board 2 Space 7\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 17)
    	{
    		if (board2[2][1] == '8'){
    			board2[2][1] = user;
    			printf ("\n%c chose Board 2 Space 8\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();
    		}	
    	}
    	else if (c == 18)
    	{
    		if (board2[2][2] == '9'){
    			board2[2][2] = user;
    			printf ("\n%c chose Board 2 Space 9\n", user, c);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	//BOARD THREE
    	else if (c == 19)
    	{
    		if (board3[0][0] == '1'){
    			board3[0][0] = user;
    			printf ("\n%c chose Board 3 Space 1\n", user);
    		}
    	 	else
            {
                cout << "This Spot Is Already Taken" << endl;
                user_input();
            }	
    	}
    	else if (c == 20)
    	{
    		if (board3[0][1] == '2'){
    			board3[0][1] = user;
    			printf ("\n%c chose Board 3 Space 2\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 21)
    	{
    		if (board3[0][2] == '3'){
    			board3[0][2] = user;
    			printf ("\n%c chose Board 3 Space 3\n", user);
    	}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 22)
    	{
    		if (board3[1][0] == '4'){
    			board3[1][0] = user;
    			printf ("\n%c chose Board 3 Space 4\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 23)
    	{
    		if (board3[1][1] == '5'){
    			board3[1][1] = user;
    			printf ("\n%c chose Board 3 Space 5\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 24)
    	{
    		if (board3[1][2] == '6'){
    			board3[1][2] = user;
    			printf ("\n%c chose Board 3 Space 6\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 25)
    	{
    		if (board3[2][0] == '7'){
    			board3[2][0] = user;
    			printf ("\n%c chose Board 3 Space 7\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}
    	else if (c == 26)
    	{
    		if (board3[2][1] == '8'){
    			board3[2][1] = user;
    			printf ("\n%c chose Board 3 Space 8\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();
    		}	
    	}
    	else if (c == 27)
    	{
    		if (board3[2][2] == '9'){
    			board3[2][2] = user;
    			printf ("\n%c chose Board 3 Space 9\n", user);
    		}
    		else
    		{
    			cout << "This Spot Is Already Taken" << endl;
                user_input();	
    		}	
    	}	
    }
    //------------------------------------------------------------------------------------
    void switch_user()
    {
    	
    	int rand(user);
    	if (user =='O')
    		user = 'X';
    	else
    		user = 'O';
    }
    //-------------------------------------------------------------------------------------
    char checkWin()
    {
    	//BOARD ONE
    	//Player X:
    	if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') //Row 1
    		return 'X';
    	else if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') //Row 2
    		return 'X';
    	else if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') //Row 3
    		return 'X';
    	else if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') //Column 1
    		return 'X';
    	else if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') //Column 2
    		return 'X';
    	else if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') //Column 3
    		return 'X';
    	else if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') //Diagonal Top Left to Bottom Right
    		return 'X';
    	else if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X') //Diagnoal Bottom Left to Top Right
    		return 'X';
    	//Player O:
    	else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') //Row 1
    		return 'O';
    	else if (board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') //Row 2
    		return 'O';
    	else if (board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O') //Row 3
    		return 'O';
    	else if (board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') //Column 1
    		return 'O';
    	else if (board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') //Column 2
    		return 'O';
    	else if (board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') //Column 3
    		return 'O';
    	else if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') //Diagonal Top Left to Bottom Right
    		return 'O';
    	else if (board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O') //Diagnoal Bottom Left to Top Right
    		return 'O';
    //BOARD TWO
    	//Player X:
    	if (board2[0][0] == 'X' && board2[0][1] == 'X' && board2[0][2] == 'X') //Row 1
    		return 'X';
    	else if (board2[1][0] == 'X' && board2[1][1] == 'X' && board2[1][2] == 'X') //Row 2
    		return 'X';
    	else if (board2[2][0] == 'X' && board2[2][1] == 'X' && board2[2][2] == 'X') //Row 3
    		return 'X';
    	else if (board2[0][0] == 'X' && board2[1][0] == 'X' && board2[2][0] == 'X') //Column 1
    		return 'X';
    	else if (board2[0][1] == 'X' && board2[1][1] == 'X' && board2[2][1] == 'X') //Column 2
    		return 'X';
    	else if (board2[0][2] == 'X' && board2[1][2] == 'X' && board2[2][2] == 'X') //Column 3
    		return 'X';
    	else if (board2[0][0] == 'X' && board2[1][1] == 'X' && board2[2][2] == 'X') //Diagonal Top Left to Bottom Right
    		return 'X';
    	else if (board2[2][0] == 'X' && board2[1][1] == 'X' && board2[0][2] == 'X') //Diagnoal Bottom Left to Top Right
    		return 'X';
    	//Player O:
    	else if (board2[0][0] == 'O' && board2[0][1] == 'O' && board2[0][2] == 'O') //Row 1
    		return 'O';
    	else if (board2[1][0] == 'O' && board2[1][1] == 'O' && board2[1][2] == 'O') //Row 2
    		return 'O';
    	else if (board2[2][0] == 'O' && board2[2][1] == 'O' && board2[2][2] == 'O') //Row 3
    		return 'O';
    	else if (board2[0][0] == 'O' && board2[1][0] == 'O' && board2[2][0] == 'O') //Column 1
    		return 'O';
    	else if (board2[0][1] == 'O' && board2[1][1] == 'O' && board2[2][1] == 'O') //Column 2
    		return 'O';
    	else if (board2[0][2] == 'O' && board2[1][2] == 'O' && board2[2][2] == 'O') //Column 3
    		return 'O';
    	else if (board2[0][0] == 'O' && board2[1][1] == 'O' && board2[2][2] == 'O') //Diagonal Top Left to Bottom Right
    		return 'O';
    	else if (board2[2][0] == 'O' && board2[1][1] == 'O' && board2[0][2] == 'O') //Diagnoal Bottom Left to Top Right
    		return 'O';
    //BOARD THREE
    	//Player X:
    	if (board3[0][0] == 'X' && board3[0][1] == 'X' && board3[0][2] == 'X') //Row 1
    		return 'X';
    	else if (board3[1][0] == 'X' && board3[1][1] == 'X' && board3[1][2] == 'X') //Row 2
    		return 'X';
    	else if (board3[2][0] == 'X' && board3[2][1] == 'X' && board3[2][2] == 'X') //Row 3
    		return 'X';
    	else if (board3[0][0] == 'X' && board3[1][0] == 'X' && board3[2][0] == 'X') //Column 1
    		return 'X';
    	else if (board3[0][1] == 'X' && board3[1][1] == 'X' && board3[2][1] == 'X') //Column 2
    		return 'X';
    	else if (board3[0][2] == 'X' && board3[1][2] == 'X' && board3[2][2] == 'X') //Column 3
    		return 'X';
    	else if (board3[0][0] == 'X' && board3[1][1] == 'X' && board3[2][2] == 'X') //Diagonal Top Left to Bottom Right
    		return 'X';
    	else if (board3[2][0] == 'X' && board3[1][1] == 'X' && board3[0][2] == 'X') //Diagnoal Bottom Left to Top Right
    		return 'X';
    	//Player O:
    	else if (board3[0][0] == 'O' && board3[0][1] == 'O' && board3[0][2] == 'O') //Row 1
    		return 'O';
    	else if (board3[1][0] == 'O' && board3[1][1] == 'O' && board3[1][2] == 'O') //Row 2
    		return 'O';
    	else if (board3[2][0] == 'O' && board3[2][1] == 'O' && board3[2][2] == 'O') //Row 3
    		return 'O';
    	else if (board3[0][0] == 'O' && board3[1][0] == 'O' && board3[2][0] == 'O') //Column 1
    		return 'O';
    	else if (board3[0][1] == 'O' && board3[1][1] == 'O' && board3[2][1] == 'O') //Column 2
    		return 'O';
    	else if (board3[0][2] == 'O' && board3[1][2] == 'O' && board3[2][2] == 'O') //Column 3
    		return 'O';
    	else if (board3[0][0] == 'O' && board3[1][1] == 'O' && board3[2][2] == 'O') //Diagonal Top Left to Bottom Right
    		return 'O';
    	else if (board3[2][0] == 'O' && board3[1][1] == 'O' && board3[0][2] == 'O') //Diagnoal Bottom Left to Top Right
    		return 'O';
    //RAW BOARD
    	//Player X
    	if (board[0][0] == 'X' && board2[0][0] == 'X' && board3[0][0] == 'X') //Raw 1
    		return 'X';
    	else if (board[0][1] == 'X' && board2[0][1] == 'X' && board3[0][1] == 'X') //Raw 2
    		return 'X';
    	else if (board[0][2] == 'X' && board2[0][2] == 'X' && board3[0][2] == 'X') //Raw 3
    		return 'X';
    	else if (board[1][0] == 'X' && board2[1][0] == 'X' && board3[1][0] == 'X') //Raw 4
    		return 'X';
    	else if (board[1][1] == 'X' && board2[1][1] == 'X' && board3[1][1] == 'X') //Raw 5
    		return 'X';
    	else if (board[1][2] == 'X' && board2[1][2] == 'X' && board3[1][2] == 'X') //Raw 6
    		return 'X';
    	else if (board[2][0] == 'X' && board2[2][0] == 'X' && board3[2][0] == 'X') //Raw 7
    		return 'X';
    	else if (board[2][1] == 'X' && board2[2][1] == 'X' && board3[2][1] == 'X') //Raw 8
    		return 'X';
    	else if (board[2][2] == 'X' && board2[2][2] == 'X' && board3[2][2] == 'X') //Raw 9
    		return 'X';
    	//Player O
    	if (board[0][0] == 'O' && board2[0][0] == 'O' && board3[0][0] == 'O') //Raw 1
    		return 'O';
    	else if (board[0][1] == 'O' && board2[0][1] == 'O' && board3[0][1] == 'O') //Raw 2
    		return 'O';
    	else if (board[0][2] == 'O' && board2[0][2] == 'O' && board3[0][2] == 'O') //Raw 3
    		return 'O';
    	else if (board[1][0] == 'O' && board2[1][0] == 'O' && board3[1][0] == 'O') //Raw 4
    		return 'O';
    	else if (board[1][1] == 'O' && board2[1][1] == 'O' && board3[1][1] == 'O') //Raw 5
    		return 'O';
    	else if (board[1][2] == 'O' && board2[1][2] == 'O' && board3[1][2] == 'O') //Raw 6
    		return 'O';
    	else if (board[2][0] == 'O' && board2[2][0] == 'O' && board3[2][0] == 'O') //Raw 7
    		return 'O';
    	else if (board[2][1] == 'O' && board2[2][1] == 'O' && board3[2][1] == 'O') //Raw 8
    		return 'O';
    	else if (board[2][2] == 'O' && board2[2][2] == 'O' && board3[2][2] == 'O') //Raw 9
    		return 'O';
    }
    
    
    Game ()
    {
    	d = 0;
    	while (1)
    	{
    		d++;
    		user_input();
    		BoardDisplay();
    		system ("PAUSE");
    		if (checkWin() == 'O')
    		{
    			cout << "O Wins!" << endl;
    			break;
    		}
    		else if (checkWin() == 'X')
    		{
    			cout << "X Wins!" << endl;
    			break;
    		}
    		else if (checkWin() == '/' && d == 9)
    		{
    			cout<< "It's A Tie!" << endl;
    			break;
    		}
    		system ("CLS");
    		switch_user();	
    	}
    }
    
    
    };
    //-------------------------------------------------------------------------------BEGIN MAIN---------------------------------------------------------
    int main()
    {	
    	cout << "\n\tWelcome To Tic Tac Toe!\n" << endl;
    	DisplayBoard();
    	int i = 0;
    	Game obj;
    	while(i<10){
    	ClearBoard();
    	Game game;
    	i++;
    	}	
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    So, while you're copy/pasting your way to 600+ lines, I made a sampler.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    class Board {
    private:
        char board[3][3][3];
    public:
        Board();
        void Display();
        void Turn(int pos, char player);
    };
    
    Board::Board() {
        for ( int z = 0 ; z < 3 ; z++ ) {
            for ( int y = 0 ; y < 3 ; y++ ) {
                for ( int x = 0 ; x < 3 ; x++ ) {
                    board[z][y][x] = y*3 + x + 1 + '0';
                }
            }
        }
    }
    
    void Board::Display() {
        for ( int z = 0 ; z < 3 ; z++ ) {
            cout << "Board " << z+1 << endl;
            for ( int y = 0 ; y < 3 ; y++ ) {
                for ( int x = 0 ; x < 3 ; x++ ) {
                    cout << board[z][y][x] << " ";
                }
                cout << endl;
            }
        }
    }
    
    void Board::Turn(int pos, char player) {
        int z = pos / 9;
        int y = ( pos / 3 ) % 3;
        int x = pos % 3;
        board[z][y][x] = player;
    }
    
    int main ( ) {
        Board board;
        cout << "Initial Board" << endl;
        board.Display();
        board.Turn(12,'X');
        board.Turn(22,'O');
        cout << endl << "After one round" << endl;
        board.Display();
    }
    Every time you create a global variable, you need to stop and think - like think about what class it should be in.

    Every time you put numeric suffixes on your variables like board, board2, board3 - you're doing something wrong.
    Because what you really want is an array of those things.

    Every time you ctrl-c/ctrl-v/edit, you need to examine the edit carefully.
    Because your edit is a prime candidate to be a parameter in some generalised function of the code you just copied.

    Oh, and you're STILL using recursion to implement a while loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    3
    Okay can you please relax with the rude replies? I'm new to C++ and I'm not familiar with all the rules yet. I don't know where you see the "copy/pasted" code except with the "display board" and "Board display function". Also, your reply didn't help at all because it STILL doesn't tell me how to reset the boards.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,727
    Quote Originally Posted by pearwasp View Post
    Okay can you please relax with the rude replies? I'm new to C++ and I'm not familiar with all the rules yet. I don't know where you see the "copy/pasted" code except with the "display board" and "Board display function". Also, your reply didn't help at all because it STILL doesn't tell me how to reset the boards.
    This is a website that helps you learn to program in C and C++; if you do not wish to learn this is likely a bad website for you.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    229
    I don't see how you think he's being rude in any way at all. He's helping and implying sarcasm is not being rude. If someone takes the time to help you in learning to code better, what you should do is to try putting in effort into fixing your code. Trust me, if Salem can't help you, then probably no one else can.

    > I'm new to C++ and I'm not familiar with all the rules yet

    Well, it's time to learn some of them then. And clearly you should start at post #2 and make changes to your code and post again.

    > I don't know where you see the "copy/pasted" code except with the "display board" and "Board display function".

    All your if-else statements are essentially just copy pastes. From first glance, you can do everything you are doing pretty much with just for loops and switch conditions. Not sure as I haven't look closely enough, but Salem's post hints to doing exactly that. You have if conditions like these:


    Code:
    if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') //Row 1
            return 'X';
        else if (board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') //Row 2
            return 'X';
        else if (board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X') //Row 3
            return 'X';
        else if (board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') //Column 1
            return 'X';
        else if (board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') //Column 2
            return 'X';
        else if (board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') //Column 3
            return 'X';
        else if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') //Diagonal Top Left to Bottom Right
            return 'X';
        else if (board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X') //Diagnoal Bottom Left to Top Right
            return 'X';
    I used to write code like that not a long time back (Refer to a thread called 2048 in the Game Programming section, that code is crap but it's written by me and when I finished with it I thought I'd learnt a lot of programming and I'm getting really good but the truth was that I was a void main'er and stupid af and many many thanks to Salem for helping me change that). This kind of code is horrendous to write and even more stupid to read through. If you're writing code this way, take a step back, and realise that you are doing it all wrong. Games aren't programmed this way. Loops and jump statements were created for a reason and so were arrays and functions. Implementing a while loop using recursion is just stupid. There's heavy memory penalty and a lot more that isn't gonna be doing you any good. Again, as Salem mentions, what do you think about this:

    Code:
    char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
    char board2[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
    char board3[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
    
    // No?
    char Board [3][3][3] = {{{} , {} , {}} , {{} , {} , {}} , {{} , {} , {}}};
    It's in global scope which is just bad enough but the fact that you know arrays but still don't know arrays is worse. You are trying to implement a 3D game with 3 different boards? Kind of defeats the point of it being 3D then when all you're doing is writing code for a 2D game to simulate 3D. People do that sometimes and there are times when you actually need to do things this way but here, you're doing it wrong. ALSO, you're learning C++, which means you should first practice a tonne of Object Oriented Programming. Don't you think you're missing the whole point of your Board not being part of TicTacToeGame? Before jumping in to writing code for Projects, it would be useful and better to learn and master the basics of each simple concept.

    Your rand () calls are useless as they are going to give you the same values every program run. Instead what you need to do is seed the random number generator with a truly varying value like system time. I'll leave it up to you to search how you should seed rand using srand.

    > using namespace std;

    Stop using this. It is bad practice for reasons I don't want to explain now but eventually down the line, you'll know yourself.

    ????? printf and cout together ?????

    WHAT are you trying to do? Does printf not do what cout can and does cout not do what printf can?

    [EDIT]
    A more C++ way for random number generation using the STL:

    Code:
    int random_int (void)
    {
        static std::default_random_engine e;
        static std::uniform_real_distribution<> dis(0, 8); // in the range 0-8
        return dis(e);
    }
    https://isocpp.org/files/papers/n3551.pdf
    rand() Considered Harmful | GoingNative 2013 | Channel 9
    [/EDIT]
    Last edited by Zeus_; 12-06-2019 at 02:22 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,602
    I rather suspect OP has been getting overly generous marks from their tutor up until this point.

    This code needs an 'F', regardless of whether it finally produces the right answer or not. The whole approach needs fixing - badly.

    If you're aspiring to be a professional programmer, you're not going to make it based on this evidence.

    And no, I'm not being mean or rude - blunt and to the point yes.
    How To Ask Questions The Smart Way
    If I wanted to be be rude, there would be no mistake in interpretation - you would know it.

    > Also, your reply didn't help at all because it STILL doesn't tell me how to reset the boards.
    Code:
    Board::Board() {
        for ( int z = 0 ; z < 3 ; z++ ) {
            for ( int y = 0 ; y < 3 ; y++ ) {
                for ( int x = 0 ; x < 3 ; x++ ) {
                    board[z][y][x] = y*3 + x + 1 + '0';
                }
            }
        }
    }
    What do you think this does?


    Too busy looking for slights for something to whine about, and not paying attention to coding.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why are these boards having so many problems?
    By funkydude9 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 11-12-2002, 01:41 AM
  2. New boards?
    By Eibro in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-18-2002, 11:14 PM
  3. Boards...
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 02-05-2002, 11:40 PM

Tags for this Thread