Tic Tac Toe Game Using Without Arrays Please Help

It's working fine but the Win and Loss Result Is not Correct Please help

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
#include<iostream>
#include <string>
using namespace std;
int main()
{
  int j=0,n=0;
  string a,b,c,d,e,f,g,h,i;
  int opt1,opt2;
  
  
  //PLAYER 1
  do
  {
  cout<<"123\n456\n789\n";
  if (n==0 || n==2 ||n==4 ||n==6 ||n==8)
  { 
  do
  {
  cout<<"Player 1 Turn:";
  cin>>opt1;
  if(opt1!=opt2)
  {
   switch(opt1)
   {
    case 1 : a="O";      
    break;
    case 2 : b="O";
    break;  
    case 3 : c="O";
    break; 
     case 4 : d="O";
     break;
      case 5 : e="O";
      break;
       case 6: f="O";
       break;
        case 7: g="O";
        break;
         case 8: h="O";
         break;
          case 9: i="O";
          break; 
    default : cout<<"Error\n";
    }
    }
    else if (opt1==opt2)
    {
    cout<<"Place is Already Taken\n";
    }       
    }while(opt2==opt1);
   }
   
   //PLAYER 2:
   else if(n==1 || n==3 ||n==5 ||n==7 ||n==9)
   {
     do
     {
     cout<<"Player 2 Turn:";
     cin>>opt2;
     if(opt2!=opt1)
     {
     switch(opt2)
   {
    case 1 : a="X";      
    break;
    case 2 : b="X";
    break;  
    case 3 : c="X";
    break; 
     case 4 : d="X";
     break;
      case 5 : e="X";
      break;
       case 6: f="X";
       break;
        case 7: g="X";
        break;
         case 8: h="X";
         break;
          case 9: i="X";
          break; 
          default : cout<<"Error\n";
    }
    }
    else if(opt2==opt1)
    {
      cout<<"Place is already Taken\n";
    }     
    
   }while(opt1==opt2);
   }
   j++;
   n++;
 
   cout<<a<<b<<c<<endl<<d<<e<<f<<endl<<g<<h<<i<<endl;
   }while(j<9);
   
   
    //WIN AND LOSS 
     if (a=="X" && b=="X" && c=="X" || a=="X" && e=="X" && i=="X" || c=="X" && e=="X" && g=="X" || d=="X" && e=="X" && f=="X" || g=="X" && h=="X" && i=="X")
   cout<<"Player 2 Wins\n";
   else if(a=="O" && b=="O" && c=="O" || a=="O" && e=="O" && i=="O" || c=="O" && e=="O" && g=="O" || d=="O" && e=="O" && f=="O" || g=="O" && h=="O" && i=="O")
   cout<<"Player 1 Wins\n";
   else cout<<"It's A Draw\n";
   
   
      
   
    
  return 0;  
    
    
    
 }
Last edited on
It's working fine but
^^^ which is it? Is it working fine, or not :P

as far as I can tell, you are not checking for a win after every move. so if the game ends after 5 moves, it won't work. you need to check for a win after every move.

whats with the creeping right indents on the cases?
Last edited on
how do i do it ....please help :(
copy 99-104 and paste it twice, after each turn, remove it from where it is.
If ever there were a case for decent variable names (not alphabet soup) and indentation, then this program would be it.

Just because you're not allowed to use arrays, it doesn't stop you making a whole bunch of variables that resemble an array.
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
#include<iostream>
#include <string>
using namespace std;
int main()
{
  int j = 0, n = 0;
  string r1c1, r1c2, r1c3, r2c1, r2c2, r2c3, r3c1, r3c2, r3c3;
  int opt1, opt2;

  //PLAYER 1
  do {
    cout << "123\n456\n789\n";
    if (n == 0 || n == 2 || n == 4 || n == 6 || n == 8) {
      do {
        cout << "Player 1 Turn:";
        cin >> opt1;
        if (opt1 != opt2) {
          switch (opt1) {
          case 1:
            r1c1 = "O";
            break;
          case 2:
            r1c2 = "O";
            break;
          case 3:
            r1c3 = "O";
            break;
          case 4:
            r2c1 = "O";
            break;
          case 5:
            r2c2 = "O";
            break;
          case 6:
            r2c3 = "O";
            break;
          case 7:
            r3c1 = "O";
            break;
          case 8:
            r3c2 = "O";
            break;
          case 9:
            r3c3 = "O";
            break;
          default:
            cout << "Error\n";
          }
        } else if (opt1 == opt2) {
          cout << "Place is Already Taken\n";
        }
      } while (opt2 == opt1);
    }
    //PLAYER 2:
    else if (n == 1 || n == 3 || n == 5 || n == 7 || n == 9) {
      do {
        cout << "Player 2 Turn:";
        cin >> opt2;
        if (opt2 != opt1) {
          switch (opt2) {
          case 1:
            r1c1 = "X";
            break;
          case 2:
            r1c2 = "X";
            break;
          case 3:
            r1c3 = "X";
            break;
          case 4:
            r2c1 = "X";
            break;
          case 5:
            r2c2 = "X";
            break;
          case 6:
            r2c3 = "X";
            break;
          case 7:
            r3c1 = "X";
            break;
          case 8:
            r3c2 = "X";
            break;
          case 9:
            r3c3 = "X";
            break;
          default:
            cout << "Error\n";
          }
        } else if (opt2 == opt1) {
          cout << "Place is already Taken\n";
        }
      } while (opt1 == opt2);
    }
    j++;
    n++;
    cout << r1c1 << r1c2 << r1c3 << endl
         << r2c1 << r2c2 << r2c3 << endl
         << r3c1 << r3c2 << r3c3 << endl;
  } while (j < 9);


  //WIN AND LOSS
  if (r1c1 == "X" && r1c2 == "X" && r1c3 == "X" || // row1
      r1c1 == "X" && r2c2 == "X" && r3c3 == "X" || // \ diagonal
      r1c3 == "X" && r2c2 == "X" && r3c1 == "X" || // / diagonal
      r2c1 == "X" && r2c2 == "X" && r2c3 == "X" || // row2
      r3c1 == "X" && r3c2 == "X" && r3c3 == "X")   // row3
    cout << "Player 2 Wins\n";
  else
  if (r1c1 == "O" && r1c2 == "O" && r1c3 == "O" ||
      r1c1 == "O" && r2c2 == "O" && r3c3 == "O" ||
      r1c3 == "O" && r2c2 == "O" && r3c1 == "O" ||
      r2c1 == "O" && r2c2 == "O" && r2c3 == "O" ||
      r3c1 == "O" && r3c2 == "O" && r3c3 == "O")
    cout << "Player 1 Wins\n";
  else
    cout << "It's A Draw\n";

  return 0;
}
@salem I run it and it still doesn't give right winning or losing result , it prints player 1 wins when it should print that its a draw



e.g:
OXO
XOX
OXO
Player 1 Wins

It should say that its a draw
Last edited on
Why? O has won because of the left to right diagonal and the right to left diagonal. r1c1 r2c2 r3c3 & r1c3 r2c2 r3c1
Oh sorry my bad ....ThankYou ...Love this community <3
> @salem I run it and it still doesn't give right winning or losing result
I just reformatted YOUR code for readability.
It's up to you to fix what is now rather more obviously wrong with it.

Consider that TTT has
- 3 winning rows
- 3 winning columns
- 2 winning diagonals

I suppose you could use a bitmapped board to avoid an array. You would need to use 2 bits per position since there are 3 states: empty, O, and X. So you need 18 bits, which will fit in a 32-bit unsigned int.

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
#include <iostream>
using namespace std;

// row and col are 0-based

void set(unsigned& board, int row, int col, char sym)
{
    unsigned val = 3u;
    switch (sym)    {
    case ' ': val = 0u; break;
    case 'O': val = 1u; break;
    case 'X': val = 2u; break;
    }
    board |= val << ((row * 3 + col) * 2);
}

char get(unsigned board, int row, int col)
{
    return " OX?"[(board >> ((row * 3 + col) * 2)) & 3u];
}

void draw_board(unsigned board)
{
    for (int row = 0; row < 3; ++row)
    {
        cout << ' ';
        for (int col = 0; col < 2; ++col)
            cout << get(board, row, col) << " | ";
        cout << get(board, row, 2) << '\n';
        if (row < 2) cout << "-----------\n";
    }
}

int main()
{
    unsigned board = 0;
    set(board, 1, 2, 'O');
    set(board, 2, 0, 'X');
    set(board, 1, 1, 'O');
    draw_board(board);
}

   |   |  
-----------
   | O | O
-----------
 X |   |  

Topic archived. No new replies allowed.