I was trying to make a program for Tic-tac-toe between 2 players, using a class. But my is_victory function is always returning false. Why so?
//header file : tic_tac_toe.h
class Game
{
public :
Game();
void insert(int , char);
void print() const;
bool is_empty(int) const;
bool is_victory() const;
private :
char *pntr;
};
codefile:
// codefile for the class tic_tac_toe.cpp
#include"tic_tac_toe.h"
#include<iostream>
using namespace std;
Game::Game()
{
pntr = new char[9] ;
for(int i=0; i<9; i )
{
pntr[i] = 48 i;
}
}
void Game::insert(int location, char val)
{
pntr[location] = val;
}
void Game::print() const
{
for(int i=1; i<=9; i )
{
cout<<pntr[i-1]<<"|";
if(i%3 == 0) cout<<endl;
}
}
bool Game::is_empty(int location) const
{
if(pntr[location] == location 48 ) return true;
else return false;
}
bool Game::is_victory() const
{
if(pntr[0] == pntr[1] == pntr[2]) return true;
if(pntr[3] == pntr[4] == pntr[5]) return true;
if(pntr[6] == pntr[7] == pntr[8]) return true;
if(pntr[0] == pntr[3] == pntr[6]) return true;
if(pntr[1] == pntr[4] == pntr[7]) return true;
if(pntr[2] == pntr[5] == pntr[8]) return true;
if(pntr[0] == pntr[4] == pntr[8]) return true;
if(pntr[2] == pntr[4] == pntr[6]) return true;
return false;
}
Main program :
//tic_tac_toe_try1.cpp
#include"tic_tac_toe.h"
#include<iostream>
using namespace std;
int main()
{
char player1;
char player2;
Game gameobj;
cout<<"choose between X or O"<<endl;
cin>> player1;
if(player1 == 'X') player2='O';
else player2 = 'X';
for(int i=0; i<4; i )
{
int location;
gameobj.print();
cout<<"enter location to enter "<< player1<<"between 0-8 as shown above : ";
cin>> location;
while(! (location>=0 && location<9) )
{
cout<<"invalid location , please renter location between 0-8: ";
cin>>location;
}
while(!gameobj.is_empty(location) )
{
cout<<"invalid location , already filled please renter : ";
cin>>location;
}
gameobj.insert(location, player1);
gameobj.print();
if(gameobj.is_victory())
{
cout<<"congrats player1 victorious";
return 0;
}
cout<<"enter location to enter "<< player2<<"between 0-8 as shown above : ";
cin>> location;
while(! (location>=0 && location<9) )
{
cout<<"invalid location , please renter location between 0-8: ";
cin>>location;
}
while(!gameobj.is_empty(location) )
{
cout<<"invalid location , already filled please renter : ";
cin>>location;
}
gameobj.insert(location, player2);
if(gameobj.is_victory())
{
cout<<"congrats player2 victorious";
return 0;
}
}
return 0;
}
Sample trial in terminal of vsc :
PS D:\c > g tic_tac_toe.cpp tic_tac_toe_try1.cpp
PS D:\c > ./a.exe
choose between X or O
X
0|1|2|
3|4|5|
6|7|8|
enter location to enter Xbetween 0-8 as shown above : 0
X|1|2|
3|4|5|
6|7|8|
enter location to enter Obetween 0-8 as shown above : 3
X|1|2|
O|4|5|
6|7|8|
enter location to enter Xbetween 0-8 as shown above : 2
X|1|X|
O|4|5|
6|7|8|
enter location to enter Obetween 0-8 as shown above : 4
X|1|X|
O|O|5|
6|7|8|
enter location to enter Xbetween 0-8 as shown above : 1
X|X|X|
O|O|5|
6|7|8|
enter location to enter Obetween 0-8 as shown above :
As above even after getting 3 X's in 0,1,2 pntr's is_victory still doesn't give true.
I also tried checking the values of pntr[0], pntr[1], pntr[2] by manually printing them and the value of expression (pntr[0] == pntr[1] == pntr[2]) while the earlier all print X, but the comparison results in 0.
CodePudding user response:
if(pntr[0] == pntr[1] == pntr[2]) return true;
The condition A == B == C will be evaluated as (A == B) == C. The expression (A == B) will result in false (0) or true (1), and from the initialization of pntr we can see that C will never be equal to 0 nor 1. Therefore, the expression as a whole always evaluates to false.
What you probably meant to do is, e.g., this:
if(pntr[0] == pntr[1] && pntr[1] == pntr[2]) return true;
