tic tac toe.

Pages: 12
I CREATED this tic tac toe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include<cstdlib>
using namespace std;




char square[11]={'0','1','2','3','4','5','6','7','8','9','\0'};
void board();
int win();



main()
{
   system("color 9a");
   char playagain='y';
   while(playagain=='y')
   {
    cout<<"Welcome in my Tic Tac Toe!!!\n\n\n"<<endl;
    int player=1;
    int i,choice;
    char mark;

    do
    {

     board();
     player=(player%2)?1:2;
     cout<<"Player "<<player<<" enter a number: ";
     cin>>choice;
     mark=(player==1)?'X':'O';

     if(choice==1 && square[1]=='1')
       square[1]=mark;

     else if (choice == 2 && square[2] == '2')

            square[2] = mark;
        else if (choice == 3 && square[3] == '3')

            square[3] = mark;
        else if (choice == 4 && square[4] == '4')

            square[4] = mark;
        else if (choice == 5 && square[5] == '5')

            square[5] = mark;
        else if (choice == 6 && square[6] == '6')

            square[6] = mark;
        else if (choice == 7 && square[7] == '7')

            square[7] = mark;
        else if (choice == 8 && square[8] == '8')

            square[8] = mark;
        else if (choice == 9 && square[9] == '9')

            square[9] = mark;
        else
        {
            cout<<"\nINVALID MOVE \n\n";
            player--;

        }
        i=win();
        player++;
    }while(i==-1);
    board();
    if(i==1)
    cout<<"Player"<<--player<<" win";
    if(i==0)
    cout<<"Game draw";



cout<<"\n\nWanna play again(y/n)?:";
cin>>playagain;
cout<<endl;

if(playagain=='y')
  {
      cout<<"You choice to play again\n\n";
  }
  else if(playagain=='n')
  {
      cout<<"You choice not to play again\n\n";
  }
else
{
    cout<<"You didnt put an valid character so i will take it like(n),so the program will close.\n\n";
}

for(int c=0;c<40;c++)
{
cout<<"****";
}
cout<<endl;
}

}

int win()
{
    if(square[1]==square[2]&&square[2]==square[3])
    return 1;

    else if(square[4]==square[5]&&square[5]==square[6])
    return 1;

    else if(square[7]==square[8]&&square[8]==square[9])
    return 1;

    else if(square[1]==square[4]&&square[4]==square[7])
    return 1;

    else if(square[2]==square[5]&&square[5]==square[8])
    return 1;

    else if(square[3]==square[6]&&square[6]==square[9])
    return 1;

    else if(square[1]==square[5]&&square[5]==square[9])
    return 1;

    else if(square[3]==square[5]&&square[5]==square[7])
    return 1;

   else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'&&square[5] != '5'
    && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
    return 0;

else
return -1;

}

void board()
{


    cout<<"\tTic Tac Toe\t\t\n\n";
    cout<<"PLAYER 1(X)  ,  PLAYER 2(O)\n\n";
    cout<<square[1]<<"|"<<square[2]<<"|"<<square[3]<<endl;
    cout<<"-----"<<endl;
    cout<<square[4]<<"|"<<square[5]<<"|"<<square[6]<<endl;
    cout<<"-----"<<endl;
    cout<<square[7]<<"|"<<square[8]<<"|"<<square[9]<<endl;


}


but if i put to play again,it has the game that was played instead to create a new game.
how can clear it from the game before?

thanks(BECAUSE i am just a noob,only 20 days learning(without any experience) , 1,5 hour per day),show me your solution into in my code,if you could.

also tell me your opinion.
Your square array seems to hold the board status. So if you want to reset the board, just reset that array to its initial value.
can show it to me ,to understand what you mean?

if square[1] == '1', then that means that square is empty, right?

so to make the board empty, set all squares to their 'empty' value.
i understood what you mean,but didnt understand how to do it.
can show me.
i will be happy .
thanks in advance.
closed account (o3hC5Di1)
Hi there,

Here's an example

1
2
3
4
for (int i = 1; i <=9; i++)  //for every square
{
    square[i] = '1';  //set value to 1 ("empty")
}


Hope that helps.

All the best,
NwN
Last edited on
please put it into in my code.
i am beginner yet.
closed account (o3hC5Di1)
Where do you think it should go in your code? :)

NwN
man now will help me or will ask me strange things?

could put it in my code yes or not.?
if you could i will be very happy.
If you can't do things on your own, don't try to be a programmer. Not sound harsh, but it's a field of problem solving where hand holding rarely happens. Think logically on where that snippet should go.
closed account (o3hC5Di1)
Wouldn't you be even happier if you actually learnt where it goes and why?

It's really not that hard... just think about what the code does and then look at where in your code you need that to happen.

I do want to help, but I will not spoonfeed you.

All the best.
NwN
guys.
i am learning almost 21 hours c++ and programming and i created a tic tac toe.
other guys need 60-70 hours to do it if they are beginners.
i did a big try to do it,i always try to find it alone but dont believe that i am good yet,i am just noob yet.

i believe that it will go in the end of main,but i am not sure.
please give a tip ,show me at least some things.
to understand better.
guys i think that i found it.
look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream>
#include<cstdlib>
using namespace std;




char square[11]={'0','1','2','3','4','5','6','7','8','9','\0'};
void board();
int win();



main()
{
   system("color 9a");
   char playagain='y';
   while(playagain=='y')
   {
    cout<<"Welcome in my Tic Tac Toe!!!\n\n\n"<<endl;
    int player=1;
    int i,choice;
    char mark;

    do
    {

     board();
     player=(player%2)?1:2;
     cout<<"Player "<<player<<" enter a number: ";
     cin>>choice;
     mark=(player==1)?'X':'O';

     if(choice==1 && square[1]=='1')
       square[1]=mark;

     else if (choice == 2 && square[2] == '2')

			square[2] = mark;
		else if (choice == 3 && square[3] == '3')

			square[3] = mark;
		else if (choice == 4 && square[4] == '4')

			square[4] = mark;
		else if (choice == 5 && square[5] == '5')

			square[5] = mark;
		else if (choice == 6 && square[6] == '6')

			square[6] = mark;
		else if (choice == 7 && square[7] == '7')

			square[7] = mark;
		else if (choice == 8 && square[8] == '8')

			square[8] = mark;
		else if (choice == 9 && square[9] == '9')

			square[9] = mark;
        else
        {
            cout<<"\nINVALID MOVE \n\n";
            player--;

        }
        i=win();
        player++;
    }while(i==-1);
    board();
    if(i==1)
    cout<<"Player"<<--player<<" win";
    if(i==0)
    cout<<"Game draw";



cout<<"\n\nWanna play again(y/n)?:";
cin>>playagain;
cout<<endl;

if(playagain=='y')
  {
      cout<<"You choice to play again\n\n";
  }
  else if(playagain=='n')
  {
      cout<<"You choice not to play again\n\n";
  }
else
{
    cout<<"You didnt put an valid character so i will take it like(n),so the program will close.\n\n";
}

for(int c=0;c<40;c++)
{
cout<<"****";
}
cout<<endl;

    square[1] = '1';
    square[2] = '2';
    square[3] = '3';
    square[4] = '4';
    square[5] = '5';
    square[6] = '6';
    square[7] = '7';
    square[8] = '8';
    square[9] = '9';
}
}

int win()
{
    if(square[1]==square[2]&&square[2]==square[3])
    return 1;

    else if(square[4]==square[5]&&square[5]==square[6])
    return 1;

    else if(square[7]==square[8]&&square[8]==square[9])
    return 1;

    else if(square[1]==square[4]&&square[4]==square[7])
    return 1;

    else if(square[2]==square[5]&&square[5]==square[8])
    return 1;

    else if(square[3]==square[6]&&square[6]==square[9])
    return 1;

    else if(square[1]==square[5]&&square[5]==square[9])
    return 1;

    else if(square[3]==square[5]&&square[5]==square[7])
    return 1;

   else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'&&square[5] != '5'
    && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
    return 0;

else
 return -1;

}

void board()
{


    cout<<"\tTic Tac Toe\t\t\n\n";
    cout<<"PLAYER 1(X)  ,  PLAYER 2(O)\n\n";
    cout<<square[1]<<"|"<<square[2]<<"|"<<square[3]<<endl;
    cout<<"-----"<<endl;
    cout<<square[4]<<"|"<<square[5]<<"|"<<square[6]<<endl;
    cout<<"-----"<<endl;
    cout<<square[7]<<"|"<<square[8]<<"|"<<square[9]<<endl;


}


right?
right?


When you ran the program, did it work the way you wanted?

The answer to that question = the answer to your question.
yes ,it works.
thank you guys to helped me to think with this way.

also which is your opinion for my program?
dont forget that i am noob yet.
which is your opinion?
For a beginner program it's great. :)
@Skarla

Good job.

Take a look at http://www.cplusplus.com/forum/articles/12974/ for some beginner exercises.

Good luck and keep working!
Was going to post my ugly code for reference, but since you got it I won't torture the other guys with it :).
thank you guys.
Pages: 12