c++ tic tac toe command proamt help
I recently trying to make a tic tac toe game in c++. I almost made it. everything works fine just I can't figure out how to make the winning system. I have tried it in so many ways but I keep falling out I use for loops in many ways I even at one point hard code everything but nothing work out so please can anyone help me to do it I will share my code with you all. can anyone fix it up? I really need help. Here is my code :
#include<iostream>
using namespace std;
class board
{ //variables and functions that are private
private:
// this is the board
char _brd[3][3];
//this determines that what will be the state if its a symbol that the
//player is using then that player wins
// if it's a \0 or NULL then it's a tie or if it's free space then the game isn't over.
char _state;
// this is the char or the symbol that player 1 will be using
char _s1;
//this is the char or the symbol that player 2 will be using
char _s2;
// this is the char that is used as free space.
char _fs;
// this is responsible for making it turn-based
bool _trn;
// um I used it so I have not to use 3 all the time also I will try to figure it out for making it n X n grid.
int _size;
// its job is simple it resets the board to make it playable. no one wants to play on a sketchy board right.
void reset(){
// 2 nested loops i for y, j for x
for (int i=0;i<_size;i++){
for (int j=0;j<_size ;j++ ){
//setting them to free space so they are playable
_brd[i][j]=_fs;
}
}
}
// handle if the state is changed
void handleState(){
// if player 1 wins
if(_state==_s1){
cout<<"THE OWNER OF "<<_s1<<" WINS!!!"<<endl;
}
//if player 2 wins
else if(_state==_s2){
cout<<"THE OWNER OF "<<_s2<<" WINS!!!"<<endl;
}
//if its a tie
else if(_state=='\0'){
cout<<"!!!!TIE!!!!"<<endl;
}
}
//print`s the tic tac toe board
void printB(){
// same as the reset but extra cout statment
for (int i=0;i<_size ;i++ )
{cout<<"---------------"<<endl;
for (int j=0;j<_size ;j++)
{
cout<<"| "<<_brd[i][j]<<" |";
}
cout<<endl<<"---------------"<<endl;
}
}
// change any point of it to the given char.
void tweekB(int x,int y, char s){
// some validation so it would not become out of range and stuck to the border
// make a copy of the given variable so they can be on the range
int a=x;
int b=y;
// see the code and figure it out
if(a>_size){
a=_size;
}
else if(b>_size){
b=_size;
}
else if(a<1){
a=1;
}
else if(b<1){
b=1;
}
// well all people will use 1 to 3 so minus it out by 1 to fit them perfectly
// any way note that the first one is y and the second one is x
_brd[b-1][a-1]=s;
}
// extra input validation so you cant do anything what ever you want
int validatePos(int x,int y){
// check if the position is already taken
if(_brd[y-1][x-1]!=_fs){
return 1;
}
// check if they are out of range. the tweek board has extra input validation so nothing went wrong for a bug.
else if(x<1||x>_size||y<1||y>_size){
return 2;
}
// if nothing is wrong
else{
return 0;
}
}
char checkB(char s){
// count the amont of free space
int fsc=0;
for(int m=0;m<_size;m++){
for(int n=0;n<_size;n++){
if(_brd[m][n]==_fs){
fsc++;
}
}
}
// if its a tie or all the position is taken
if(fsc<1){
return '\0';
}
// the problem is now is HOW CAN I DETERMINE IF ANYONE HAS WON THE GAME PLEASE HELP MEEEEEEE!!!!!!
// if they won return the char that has given to the function as it will later set on the _state variable.
// if nothing is wrong
else{
return _fs;
}
}
// the functions that are public and used to run the game
public:
// set the varibles and calls reset().
board(char s1,char s2,char fs){
_fs=fs;
_s1=s1;
_s2=s2;
_state=_fs;
_size=3;
_trn=true;
reset();
}
// play the game
void Playbrd(){
// game loop
while (_state==_fs)
{
//Turn based system code.
if(_trn){
int x,y;
printB();
cout<<"PLAYER 1 THE OWNER OF "<<_s1<<" PLEASE ENTER A X Y POSITION ( IN THE FORMATE OF X Y ) \n ";
cin>>x>>y;
// based of what validate board outputs it prints your error or problems
switch(validatePos(x,y)){
case 0:
tweekB(x,y,_s1);
_state=checkB(_s1);
_trn=!_trn;
break;
case 1:
cout<<"The position is already taken"<<endl;
break;
case 2:
cout<<"OUT OF RANGE!!!"<<endl;
break;
}
}
//basically a cut and paste with comments
else{
int x,y;
printB();
cout<<"PLAYER 2 THE OWNER OF "<<_s2<<" PLEASE ENTER A X Y POSITION ( IN THE FORMATE OF X Y ) \n ";
cin>>x>>y;
// based of what validate board outputs it prints your error or problems
switch(validatePos(x,y)){
case 0:
tweekB(x,y,_s2);
_state=checkB(_s2);
_trn=!_trn;
break;
case 1:
cout<<"The position is already taken"<<endl;
break;
case 2:
cout<<"OUT OF RANGE!!!"<<endl;
break;
}
}
}
//handle the change
handleState();
}
};
int main(){
// define a board and play it simple.
board plg('X','Y',' ');
plg.Playbrd();
}
now you know it so can anyone help me please
Yorum yazmak için lütfen oturum açın.