Difficulty determining a winner in tic tac toe.

Good evening,

I am working on a simple tic tac toe game. I have written code that prints out a menu, asks the players to start the game, prints a playing board, and allows the players to enter X's and O's. When a player enters three X's the game does not stop and asks the next player to enter a character. I have a function that is supposed to check to see if a player has won the game, but that function is not working. I have looked into the archives but have not been able to apply any of the other posts to my problem. Below is the code with a few descriptions of whats going on within it. I am using CodeBlocks and am on a Windows device. Thank you in advance.


//***** compiler directives and function/variable declarations ***** //

#include <iostream>
using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int menuChoice;
int winCheck();
int a=winCheck();
int player=1,i,choice;
void board();

//***** Main function *****//

int main(){

//***** menu printout *****//

cout << "welcome to tic tac toe\n" << endl;
cout << "player one is X's and player two is O's" << endl;
cout << "enter 1 to play and 2 to exit" << endl;

int menuChoice;
cin >> menuChoice;

if (menuChoice == 1){
int player = 1,i,choice;
int a=winCheck();

//***** Do/while statement for the input of choices while playing *****//

do{
board();
player=(player%2)?1:2;
cout << "player " << player << "pick a number " << endl;
int choice;
cin >> choice;
char 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<<"Invalid move " << endl;
player--;
};
player++;
}while(a==-1);
board();

//***** If/else statement to call the winCheck function *****//

if(a==1){
cout << "player " << player << "you win!" << endl;
}else{
cout << "Game draw!" << endl;
};
};
return 0;
};

//***** function to determine if three squares have been marked by the same character *****//

int winCheck(){
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[7] == square[5] && square[5] == square[3]){
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;
}

// ***** Function to print out the playing board *****//

void board(){
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;
cout << " --------+-------+------- " << endl;
}
You need to write a = WinCheck(); inside your do-while loop so that the value of a gets updated ;P
Grime,

Thank you very much that did the trick. I was not thinking that I had to update the value of A.
Topic archived. No new replies allowed.