Tic Tac Toe Task

Feb 1, 2017 at 8:57pm
closed account (3vX4LyTq)
I'm writing a program that plays tic tac toe. However I am having some trouble with taking in position. I want it to ask for position again if the user inputs a bad position. However, now my board prints twice and the check position only runs once.

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
  #include<iostream>

using namespace std;

bool checkwin(char t[]);
void board(char t[]);

int main(){
char t[]={'1','2','3','4','5','6','7','8','9'}, turn='x';
int x = 0;
int o = 0;
int pos = 0;///Where on the board to move
bool good = true;/// IS it a valid move?
cout << "Welcome to Tic Tac Toe! Game is over after 9 moves. Press enter to start\n";
cin.ignore();///makes you press enter
cout << "Player one goes first! You are X! Choose a space\n";
board(t);

while(o<9) {
  board(t);///prints the board

  int pos;
  cin >> pos;
  if(t[pos-1] =='x' || t[pos-1]=='o'){/// I have to subtract one because t[0] = 1 and t[8] = 9
    cout << "\nThat space was taken! Try again.\n";
    good = false;
  }
  if(pos<=0 || pos>9){
    cout << "\nInvalid number! Try again.\n";
    good = false;
  }
  else{
  t[pos-1]=turn;
  }
  if(good==false){
    pos = -1;
    cin >> pos;
  }



  if (checkwin(t)){
    break;
  }


  if(turn=='x'){
    cout << "\nPlayer 2: ";
    turn='o';
  }
  else{
    cout << "\nPlayer 1:";
    turn='x';
  }
   //decides where to put the "x" or "o"


  o++;

}
if(o==9){
cout << "It's been nine turns! Game over.";
}
}


//FUNCTIONS
void board(char t[]){

  ///prints and updates the board
  cout << t[0] << " | " << t[1] << " | " << t[2] << "\n";
  cout << "----------" << endl;
  cout << t[3] << " | " << t[4] << " | " << t[5] << "\n";
  cout << "----------" << endl;
  cout << t[6] << " | " << t[7] << " | " << t[8] << "\n";
}

int play(char t[], char turn){
  //decides where to put the "x" or "o"
  int pos;
  cin >> pos;
  if(t[pos-1] =='x' || t[pos-1]=='o'){/// I have to subtract one because t[0] = 1 and t[8] = 9
    cout << "\nThat space was taken! Try again.\n";
    return 1;
  }
  if(pos<=0 || pos>9){
    cout << "\nInvalid number! Try again.\n";
    return 1;
  }
  t[pos-1]=turn;
  return 2;
}
bool checkwin(char t[]) {
    ///X win condition
    if(t[0] == 'x' && t[1] == 'x' && t[2] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[2] == 'x' && t[5] == 'x' && t[8] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[0] == 'x' && t[3] == 'x' && t[6] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[6] == 'x' && t[7] == 'x' && t[8] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[0] == 'x' && t[4] == 'x' && t[8] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[6] == 'x' && t[4] == 'x' && t[2] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    /// o win condition
    else if(t[0] == 'o' && t[1] == 'o' && t[2] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[2] == 'o' && t[5] == 'o' && t[8] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[0] == 'o' && t[3] == 'o' && t[6] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[6] == 'o' && t[7] == 'o' && t[8] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[0] == 'o' && t[4] == 'o' && t[8] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[6] == 'o' && t[4] == 'o' && t[2] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }


    return false;
}
Feb 1, 2017 at 9:54pm
Line 78: You never call your play() function.

Line 17,20: You print the board twice the first time.

Line 17-38: Isn't that what play() is supposed to be doing?

Lines 93-147: You could cut this function in half by checking for one player at a time. Pass the player in as an argument.

Lines 93-147: You're missing several winning combinations.
1
2
3
4
5
  //  Row 1
  if (t[3] == 'x' && t[4] == 'x' && t[5] =='x')

  //  COL 1
  if (t[1] == 'x' && t[4] == 'x' && t[7] =='x')

Feb 1, 2017 at 10:20pm
closed account (3vX4LyTq)
Alright, so I fixed it up

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
#include<iostream>

using namespace std;

bool checkwin(char t[]);
void board(char t[]);
void play(char t[], char turn);

int main(){
char t[]={'1','2','3','4','5','6','7','8','9'}, turn='x';
int x = 0;
int o = 0;
cout << "Welcome to Tic Tac Toe! Game is over after 9 moves. Press enter to start\n";
cin.ignore();///makes you press enter
cout << "Player one goes first! You are X! Choose a space\n";
board(t);

while(o<9) {
  play(t, turn);
  board(t);
  if (checkwin(t)){
    break;
  }
  if(turn=='x'){
    cout << "\nPlayer 2: ";
    turn='o';
  }
  else{
    cout << "\nPlayer 1:";
    turn='x';
  }

  o++;

}
if(o==9){
cout << "It's been nine turns! Game over.";
}
}


//FUNCTIONS
void board(char t[]){

  ///prints and updates the board
  cout << t[0] << " | " << t[1] << " | " << t[2] << "\n";
  cout << "----------" << endl;
  cout << t[3] << " | " << t[4] << " | " << t[5] << "\n";
  cout << "----------" << endl;
  cout << t[6] << " | " << t[7] << " | " << t[8] << "\n";
}

void play(char t[], char turn){
  //decides where to put the "x" or "o"
  int pos;
  cin >> pos;
  if(t[pos-1] =='x' || t[pos-1]=='o'){/// I have to subtract one because t[0] = 1 and t[8] = 9
    cout << "\nThat space was taken! You lose your turn!\n";
    return;
  }
  if(pos<=0 || pos>9){
    cout << "\nInvalid number! Gril. You lose your turn!\n";
    return;
  }
  t[pos-1]=turn;
}
bool checkwin(char t[]) {
    ///X win condition
    if(t[0] == 'x' && t[1] == 'x' && t[2] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[2] == 'x' && t[5] == 'x' && t[8] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[0] == 'x' && t[3] == 'x' && t[6] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[6] == 'x' && t[7] == 'x' && t[8] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[0] == 'x' && t[4] == 'x' && t[8] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    else if(t[6] == 'x' && t[4] == 'x' && t[2] =='x'){
        cout << "\n X wins! GG! \n";
        return true;
    }
    /// o win condition
    else if(t[0] == 'o' && t[1] == 'o' && t[2] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[2] == 'o' && t[5] == 'o' && t[8] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[0] == 'o' && t[3] == 'o' && t[6] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[6] == 'o' && t[7] == 'o' && t[8] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[0] == 'o' && t[4] == 'o' && t[8] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }
    else if(t[6] == 'o' && t[4] == 'o' && t[2] =='o'){
        cout << "\n O wins! GG! \n";
        return true;
    }


    return false;
}


Now, when someone does a "bad move" i.e. - choosing an integer below 1 or above 9, and trying to move where someone has already moved, they lose their turn.

I want to make it so it stays their turn. How do I do that?
Feb 2, 2017 at 2:42am
Use a dowhile loop ?
Topic archived. No new replies allowed.