Help with tic tac toe program

I have been having problems while building a simple tic tac toe program
my conditions are not being checked for player winning ….line 64?

Here is my code :
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

#include <iostream>

int main()
{
 
  /*

Address positions where the player can  write:

  1|3|5
  - - -
  11|13|15
  - - -
  21|23|25

 */
     
     
    char ch[] = "  | | - - - | | - - - | | ";
    char* p = &ch[0];
    int input_pos;
    char ch0 = '0';
    char ch1 = '1';

 // --------- displaying  tic tac toe page -------------------------
  
    for(int i =0; i< sizeof(ch)-1 ;i++){
        std::cout<<ch[i];
        if(i%5 == 0 ) std::cout<<"\n";
    }
    std::cout<<"\n";
    for(;;){
        
        std::cout<<"Player 1 enter the position(1-9) where you want to enter '0' ";
        std::cin>>input_pos;
        
//------------- mapping positions to actual addresses---------------------

        if(input_pos ==1) p = &ch[1];
        if(input_pos ==2) p = &ch[3];
        if(input_pos ==3) p = &ch[5];
        if(input_pos ==4) p = &ch[11];
        if(input_pos ==5) p = &ch[13];
        if(input_pos ==6) p = &ch[15];
        if(input_pos ==7) p = &ch[21];
        if(input_pos ==8) p = &ch[23];
        if(input_pos ==9) p = &ch[25];
        if(input_pos ==10) exit(0);
    
//----------------copying value to that position ----------------------  
  
    if(*p != 0 || *p != 1) *p ='0';

//----------------displaying result page--------------------------------   
 
    for(int i =0; i< sizeof(ch)-1 ;i++){
        std::cout<<ch[i];
        if(i%5 == 0 ) std::cout<<"\n";
    }
        std::cout<<"\n";

 //--------------------checking if player 1 has won --------------------       
        if(
           (ch[1] == ch[3] == ch[5] ==ch0)    ||
           (ch[11] == ch[13] == ch[15] ==ch0) ||
           (ch[21] == ch[23] == ch[25] ==ch0) ||
           (ch[1] == ch[11] == ch[21] ==ch0)  ||
           (ch[3] == ch[13] == ch[23] ==ch0)  ||
           (ch[5] == ch[15] == ch[25] ==ch0)  ||
           (ch[1] == ch[13] == ch[25] ==ch0)  ||
           (ch[5] == ch[13] == ch[21] ==ch0)
           )
        {std::cout<<"player1 won"; exit(0);}
           
 //-----------repeating same for player 2 ------------------------   

    std::cout<<"Player 2 enter the position(1-9) where you want to enter '1' ";
    std::cin>>input_pos;
    
    if(input_pos ==1) p = &ch[1];
    if(input_pos ==2) p = &ch[3];
    if(input_pos ==3) p = &ch[5];
    if(input_pos ==4) p = &ch[11];
    if(input_pos ==5) p = &ch[13];
    if(input_pos ==6) p = &ch[15];
    if(input_pos ==7) p = &ch[21];
    if(input_pos ==8) p = &ch[23];
    if(input_pos ==9) p = &ch[25];
    if(input_pos ==10) exit(0);
    
    
    if(*p != 0 || *p != 1) *p ='1';
    
    for(int i =0; i< sizeof(ch)-1 ;i++){
        std::cout<<ch[i];
        if(i%5 == 0 ) std::cout<<"\n";
    }
        std::cout<<"\n";
        
        if(
           (ch[1] == ch[3] == ch[5] ==ch1)    ||
           (ch[11] == ch[13] == ch[15] ==ch1) ||
           (ch[21] == ch[23] == ch[25] ==ch1) ||
           (ch[1] == ch[11] == ch[21] ==ch1)  ||
           (ch[3] == ch[13] == ch[23] ==ch1)  ||
           (ch[5] == ch[15] == ch[25] ==ch1)  ||
           (ch[1] == ch[13] == ch[25] ==ch1)  ||
           (ch[5] == ch[13] == ch[21] ==ch1)
           )
        {std::cout<<"player2 won"; exit(0);}
    
    }
}
Last edited on
ch[1] == ch[3] == ch[5] ==ch0 Does not work like you want.

It is calculated as ((ch[1] == ch[3]) == ch[5]) ==ch0
operator== returns bool which can be converted to int. Basically it returns 1 if variables are equal and 0 if they are not. Imagine what would happen if all variables are equal to '0':
(Remember: '1' != 1)
'0' == '0' == '0' =='0'true == '0' == '0'1 == '0' == '0'false == '0'0 == '0'false
so I have to separate all the conditions and check one by one like this :

 
(ch[1] == ch[3] && ch[5] == ch0 && ch[5] == ch[3])


Thanks!
Last edited on
Also:

In lines 53 and 93, the player will be allowed to overwrite existing spaces. for two reasons:

1. You are comparing a literal integer value to char - and C++ treats them differently of course.

2. step through this logic:

Even if you fix that comparison there's another problem - the if statement will always be true:

truth table for char to char comparisons:
1
2
3
4
                 *p != '0'   ||       *p != '1'
*p = ' '           true         not evaluated          //   result = true
*p = '0'          false             true               //  result = true
*p = '1'          true          not evaluated          //  result = true  


same result if the square is empty or either player has the square, the square is changed to the current player's marker.
Last edited on
replaced || by &&
Topic archived. No new replies allowed.