0

I want if after using the loop function, the board will be clean, without saving the previous game data.

I have tried using various methods as shown on YouTube, but I still don't want to lose the previous game data. I hope someone will tell me how how to delete the previous data.

I have tried various ways, have searched YouTube, the web and others but it still doesn't work.

This is my code:

#include<iostream>
using namespace std;
char data1[3][3];
char pp='X';

void draw()//muncul data1
{
    cout<<endl;
    for(int i=0; i<3; i++)
    {
    cout<<" ----- ----- -----\n";
    cout<<"|     |     |     |\n";
    cout<<"|  "<<data1[i][0]<<"  |  "<<data1[i][1]<<"  |  "<<data1[i][2]<<"  |"<<endl;
    cout<<"|     |     |     |\n";
    }
    cout<<" ----- ----- -----\n";
    cout<<endl;
    cout<<" --- --- ---\n";
    cout<<"| 1 | 2 | 3 |"<<"  Silahkan"<<endl;
    cout<<" --- --- ---\n";
    cout<<"| 4 | 5 | 6 |"<<"    Pilih"<<endl;
    cout<<" --- --- ---\n";
    cout<<"| 7 | 8 | 9 |"<<"  Koordinat"<<endl;
    cout<<" --- --- ---"<<endl;
    cout<<endl;


}
void clear()//hapus data1
{
    for(int i=0; i<3; i++)
    {
        (data1[0][i] == ' ') && (data1[0][i] == data1[1][i]) && (data1[1][i] == data1[2][i]);
    }
}
void input()
{
    int a;
    cout<<"Angka yang anda ingin kan : ";
    cin>>a;
    
    if(a==1)
    {
        data1[0][0] = pp;
    }
    else if(a==2)
    {
        data1[0][1] = pp;
    }
    else if(a==3)
    {
        data1[0][2] = pp;
    }
    else if(a==4)
    {
        data1[1][0] = pp;
    }
    else if(a==5)
    {
        data1[1][1] = pp;
    }
    else if(a==6)
    {
        data1[1][2] = pp;
    }
    else if(a==7)
    {
        data1[2][0] = pp;
    }
    else if(a==8)
    {
        data1[2][1] = pp;
    }
    else if(a==9)
    {
        data1[2][2] = pp;
    }
}
void pla()
{
    if(pp=='X')
        pp = 'O';
    else
        pp = 'X';
    
}
char win()
{
    //player 1
    //horizontal
    if (data1[0][0]=='X' && data1[0][1]=='X' && data1[0][2]=='X')
        return 'X';
    if (data1[1][0]=='X' && data1[1][1]=='X' && data1[1][2]=='X')
        return 'X';
    if (data1[2][0]=='X' && data1[2][1]=='X' && data1[2][2]=='X')
        return 'X';
    //vertikal
    if (data1[0][0]=='X' && data1[1][0]=='X' && data1[2][0]=='X')
        return 'X';
    if (data1[0][1]=='X' && data1[1][1]=='X' && data1[2][1]=='X')
        return 'X';
    if (data1[0][2]=='X' && data1[1][2]=='X' && data1[2][2]=='X')
        return 'X';
    //diagonal
    if (data1[0][0]=='X' && data1[1][1]=='X' && data1[2][2]=='X')
        return 'X';
    if (data1[2][0]=='X' && data1[1][1]=='X' && data1[0][2]=='X')
        return 'X';
    //player 2
    //horizontal
    if (data1[0][0]=='O' && data1[0][1]=='O' && data1[0][2]=='O')
        return 'O';
    if (data1[1][0]=='O' && data1[1][1]=='O' && data1[1][2]=='O')
        return 'O';
    if (data1[2][0]=='O' && data1[2][1]=='O' && data1[2][2]=='O')
        return 'O';
    //vertikal
    if (data1[0][0]=='O' && data1[1][0]=='O' && data1[2][0]=='O')
        return 'O';
    if (data1[0][1]=='O' && data1[1][1]=='O' && data1[2][1]=='O')
        return 'O';
    if (data1[0][2]=='O' && data1[1][2]=='O' && data1[2][2]=='O')
        return 'O';
    //diagonal
    if (data1[0][0]=='O' && data1[1][1]=='O' && data1[2][2]=='O')
        return 'O';
    if (data1[2][0]=='O' && data1[1][1]=='O' && data1[0][2]=='O')
        return 'O';
    return '>';
}
int main()
{
    char num1,num2,y,Y,N,n;
    int num3;
    do
    {
        cout<<"TIC TAC TOE"<<endl;
        cout<<" 1. Start"<<endl;
        cout<<" 2. Exit"<<endl;
        cin>>num3;
        system("cls");
        if(num3==1)
        {
            do
            {
                clear();
                while (1)
                {
                    
                    draw();
                    input();
                    if (win()=='X')
                    {
                        system("cls");
                        cout<<endl<<"X wins!"<<endl<<endl;

                        cout<<" Main lagi ? (y/n) ";
                        cin>>num1;
                    }
                    else if(win()=='O')
                    {
                        system("cls");
                        cout<<endl<<"O wins!"<<endl<<endl;
                        cout<<" Main lagi ? (y/n) ";
                        cin>>num1;
                    }
                    pla();
                    system("cls");
                }
                system("pause");
            } while (num1=='y' || num1=='Y');
            
                cout<<" Ingin kembali ke menu? (y/n)";
                cin>>num2;
            
        }
        else
        {
            cout<<"Terimakasih telah bermain!";
        }
                
    } while (num2=='y' || num2=='Y');
    

}
rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 4
    So you want to delete the previous data but you don't want to lose the previous game data? Deleting data will lead to loss of data. – MikeCAT Jan 08 '21 at 16:34
  • 1
    Assignments or something like `memset()` should be used for initialization of boards. – MikeCAT Jan 08 '21 at 16:35
  • @MikeCAT ah sorry for the bad explanation, my english is bad so I just want to remove the symbol on the board when I enter the y input, so it can be played back – Reza Pahlevi Jan 08 '21 at 16:39
  • *I have tried using various methods as shown on YouTube* -- You can't learn C++ properly by cherry picking videos from YouTube. – PaulMcKenzie Jan 08 '21 at 16:55
  • 1
    @RezaPahlevi How about just rewriting the board all over again from scratch? What you want to do, where you are going to the screen and just writing over cells on the screen -- there is no standard way in C++ to do this. – PaulMcKenzie Jan 08 '21 at 16:59
  • @PaulMcKenzie I know sir that I can't learn only from YouTube, but this is my first semester in collage, and during the pandemic the lecturer only taught through zoom meetings, and he only taught about if else and suddenly gave the task to create a program that contained it. void, array, looping, if else, and my lecturer did zoom the meeting only 2 times during one semester so my only way to divide was via youtube, w3school and several other websites, once again apologize for my bad English, thank you – Reza Pahlevi Jan 08 '21 at 17:13
  • 1
    Are you perhaps asking to write over the terminal ([Linux Example](https://stackoverflow.com/q/29039603/10957435))? –  Jan 08 '21 at 17:23
  • 2
    Good books and practice, man. Even teachers at universities are questionable if you really want to learn C++. Too many are still teaching from C lesson plans that were ported decades ago from Pascal. – user4581301 Jan 08 '21 at 17:30
  • Try this clear(): `void clear(){memset(&data1[0][0], ' ', size * sizeof(data1[0][0]));}` – Soleil Jan 08 '21 at 17:31
  • @Chipster no sir, only want to clear the board – Reza Pahlevi Jan 08 '21 at 17:45
  • @user4581301 thank you sir, any good suggestions for books i can read? maybe for a real beginner like me – Reza Pahlevi Jan 08 '21 at 17:45
  • 2
    @RezaPahlevi That's in theory off-topic for SO, but we do actually have a thread for it: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – underscore_d Jan 08 '21 at 17:46
  • @Soleil-MathieuPrévot thank you so much sir let me try it – Reza Pahlevi Jan 08 '21 at 17:47
  • @underscore_d thank you very much sir, i am very grateful for your advice – Reza Pahlevi Jan 08 '21 at 17:49
  • @RezaPahlevi -- OK, I understand that it's hard to find teachers at this time. What you can do is try to obtain good books. If you're going to use YouTube, use the videos that teach you certain aspects of C++ properly, for example correct usage of smart pointers, STL, etc. Don't go to the videos that attempt to show you how to create a video game in C++, as those videos (and even websites) are *not* there to teach basic C++. – PaulMcKenzie Jan 08 '21 at 18:58
  • Try this input: `void input() { int a; cout << "Angka yang anda ingin kan : "; cin >> a; set(a, pp); } void set(int a, char newValue) { a--; int w = 3; auto y = a / w; auto x = a % w; data1[y][x] = newValue; }` – Soleil Jan 08 '21 at 21:29
  • This pla: `void pla() { pp = pp == 'X' ? 'O' : 'X'; }` – Soleil Jan 08 '21 at 21:31

1 Answers1

3
(data1[0][i] == ' ') && (data1[0][i] == data1[1][i]) && (data1[1][i] == data1[2][i]);

This doesn't do anything! It's just a string of conditions.

I suspect you meant to perform a series of assignments:

data1[0][i] = ' ';
data1[0][i] = data1[1][i];
data1[1][i] = data1[2][i];

Next, note that two of these are back to front, so:

data1[0][i] = ' ';
data1[1][i] = data1[0][i];
data1[2][i] = data1[1][i];

Though you could also write:

data1[0][i] = data1[1][i] = data1[2][i] = '';

Or, for the best code (in my opinion):

data1[0][i] = ' ';
data1[1][i] = ' ';
data1[2][i] = ' ';
Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • A nested loop `for ( int i = 0; i < 3; i++ ) for ( int j = 0; j < 3; j++ ) data[i][j] = ' ';` would be even better, in my opinion. Note that I can't write newlines in a comment. This code should be written in 3 lines. Feel free to add this to your answer, if you want. – Andreas Wenzel Jan 08 '21 at 17:59
  • @AndreasWenzel I thought about it, but to be honest with only three elements I prefer writing it out. – Asteroids With Wings Jan 08 '21 at 18:36
  • Another issue is that it seems like a bat habit to fill entire arrays in [column-major](https://en.wikipedia.org/wiki/Row-_and_column-major_order) order instead of row-major order, because that results in worse cache performance. However, you were probably correct not to mention this, because such advanced topics are likely to confuse the OP rather than help them. – Andreas Wenzel Jan 08 '21 at 19:59
  • @AndreasWenzel That's a good point to be fair that I hadn't thought of – Asteroids With Wings Jan 08 '21 at 23:28