You are here:

C++/A tic tac toe game

Advertisement


Question
Hello,

I am making a basic Tic tac toe game, but it does not end when a player wins. Here is my program:

#include <iostream>
#include <windows.h>
using namespace std;

void printBoard(char slots[]);
char playerMark(int playerTurn);
bool checkValid(char slots[], int playerMove);
bool winGame(char slots[]);

int main(){
   char slots[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
   int playerTurn = 1;
   bool endGame = false;
   int playerMove;
   cout << "\tWelcome to TIC TAC TOE\n";
   printBoard(slots);

   do{
       bool valid = false;
   while(!valid){          //Enter and test if the move is valid
   cout << "\nEnter your move (1-9): ";
   cin >> playerMove;
   if(checkValid(slots, playerMove)){
       slots[--playerMove] = playerMark(playerTurn);
       valid = true;
   }else
       cout << "Invalid move" << endl;
   }
   system("CLS");
   printBoard(slots);
         //Checking if the game ended
   if(!winGame(slots)){          //Switching player turn
       if(playerTurn==1)
         playerTurn = 2;
       else
         playerTurn = 1;
   }
   }while(!winGame(slots));

   cout << "\n\t Game Over\n";
   if(slots[0]!=' ' && slots[1]!=' ' && slots[2]!=' ' && slots[3]!=' '
         && slots[4]!=' ' && slots[5]!=' ' && slots[6]!=' ' && slots[7]!=' ' && slots[8]!=' ' && !winGame(slots)){
       cout << "Tie!\n";}
   else{
       cout << "Player " << playerTurn << " wins!\n";}

   system("PAUSE");
   return 0;
}


void printBoard(char slots[]){          //Printing the board
   cout << " " << slots[0] << " | " << slots[1] << " | " << slots[2] << " \n";
   cout << "---+---+---\n";
   cout << " " << slots[3] << " | " << slots[4] << " | " << slots[5] << " \n";
   cout << "---+---+---\n";
   cout << " " << slots[6] << " | " << slots[7] << " | " << slots[8] << " \n";
}

char playerMark(int playerTurn){        //Setting player matk
   if(playerTurn==1)
       return 'X';
   else
       return 'O';
}

bool checkValid(char slots[], int playerMove){      //Checking for valid move
   if(slots[--playerMove]== ' ')
       return true;
   else
       return false;
}

bool winGame(char slots[]){          //Checking for end game
   return false;
   if(slots[0] != ' '){
       if(slots[1]==slots[0] && slots[2]==slots[0])
         return true;
       else if(slots[3]==slots[0] && slots[6]==slots[0])
         return true;
   }
   if(slots[4] != ' '){
       if(slots[0]==slots[4] && slots[8]==slots[4]){
         cout << "yes\n";;}
       else if(slots[2]==slots[4] && slots[6]==slots[4]){
         return true;}
       else if(slots[1]==slots[4] && slots[7]==slots[4]){
         return true;}
       else if(slots[3]==slots[4] && slots[5]==slots[4]){
         return true;}
   }
   if(slots[8] != ' '){
       if(slots[6]==slots[8] && slots[7]==slots[8])
         return true;
       else if(slots[2]==slots[8] && slots[5]==slots[8])
         return true;
   }
   if(slots[0]!=' ' && slots[1]!=' ' && slots[2]!=' ' && slots[3]!=' '
         && slots[4]!=' ' && slots[5]!=' ' && slots[6]!=' ' && slots[7]!=' ' && slots[8]!=' ' && !winGame(slots))
         return true;
}

Answer
Hi,

sorry for replying late. I have been abit busy with other things and as such couldn't respond on time.

the reason why it is not printing the winner of the game is because of you had a return false statment in the winGame() function..simply replace your winGame(slots) with this one pasted below and I am positive your code will print out the winner as required. Also, I added afew else if statements to conditions (if slots[0] and if slots[8]). check with your previous code to see what I have added and deleted. stay blessed.

bool winGame(char slots[]){          //Checking for end game
   //return false;
   if(slots[0] != ' '){
       if(slots[1]==slots[0] && slots[2]==slots[0])
         return true;
       else if(slots[3]==slots[0] && slots[6]==slots[0])
         return true;
     else if(slots[4]==slots[0] && slots[8]==slots[0])
         return true;
   }
   if(slots[4] != ' '){
       if(slots[0]==slots[4] && slots[8]==slots[4]){
         cout << "yes\n";;}
       else if(slots[2]==slots[4] && slots[6]==slots[4]){
         return true;}
       else if(slots[1]==slots[4] && slots[7]==slots[4]){
         return true;}
       else if(slots[3]==slots[4] && slots[5]==slots[4]){
         return true;}
   }
   if(slots[8] != ' '){
       if(slots[6]==slots[8] && slots[7]==slots[8])
         return true;
       else if(slots[2]==slots[8] && slots[5]==slots[8])
         return true;
     else if(slots[4]==slots[8] && slots[0]==slots[8])
         return true;
   }
  
  return false;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


henry

Expertise

I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.

Experience

I have a 3- year- work experience in software Engineering.

Education/Credentials
I am a bachelor of Engineering graduate

©2016 About.com. All rights reserved.