Jump to content

Need help with C++ (X and O game)

Richard Stark

here is the program : 

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

#include <iostream>

using namespace std;

int main()
{
    int A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
    int row,col,crow,ccol,w=0;
    int xo=0;
    int yo=8;
   for(int coin=0;coin<=1;coin){
   //Display Module

   for(int p1=0;p1<3;p1++){
        cout<<"\n \n "<<endl;

    for(int p2=0;p2<3;p2++){

        cout<<A[p1][p2]<<"            ";
    }

   }

   //Game Engine(input Module)
   cout<<"\n \n \nEnter the Row number"<<endl;
   cin>>row;
   crow=row-1;
   cout<<"Enter the Column Number"<<endl;
   cin>>col;
   ccol=col-1;
   w++;
   if(w%2==0){
    A[crow][ccol]=xo;

   }
   else{
    A[crow][ccol]=yo;

   }

  //Winning Algorithm
if(yo==A[0][0]==A[0][1]==A[0][1]){
        cout<<"Player 1 Wins !!!"<<endl;


}


   }

    return 0;
}
 

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

well i am trying to do it step by step but when i type enter value row=1 and column=1 then it automatically shows up Player 1 wins although i it has to make a row of it = each other ........Also i know the winning algorithm is incomplete but it is not working in it base .....

 

Aslo pls tell me what i am doing wrong and feel free to suggest better way of handling it....

 

Also i am programming on Code Blocks

 

 

Also i am a newbie to C++ so keep the stuff basic(it is an incomplete program)

Link to comment
Share on other sites

Link to post
Share on other sites

int main()
{
    int A[3][3] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
    int row, col, crow, ccol, w = 0;
    int xo = 0;
    int yo = 8;
    
    for(int coin=0; coin <= 1; coin){ //This won't end, last loop parameter doesn't change anything
        //Display Module
        for(int p1 = 0; p1 < 3; p1++){
            cout << "\n \n " << endl;
            for(int p2 = 0; p2 < 3; p2++){
                cout << A[p1][p2] << "            ";
            }
        }
        
        //Game Engine(input Module)
        cout << "\n \n \nEnter the Row number" << endl;
        cin >> row;
        crow = row - 1;
        
        cout << "Enter the Column Number" << endl;
        cin >> col;
        ccol = col - 1;
        w++;
        
        if(w % 2 == 0){
            A[crow][ccol] = xo;
        } else {
            A[crow][ccol] = yo;
        }
        
        //Winning Algorithm
        std::cout << yo << ", " << A[0][0] << ", " << A[0][1] << ", " << A[0][1] << std::endl;
        if(yo == A[0][0] == A[0][1] == A[0][1]){ // You cannot do that. Use && or loop through
            cout << "Player 1 Wins !!!" << endl;
        }

    }
    
    return 0;
}

Your code is messy. To read It I had to redo all white spaces. Pasting it without code tags did't help.

 

The first for loop is equal to while(true) right now, as you don't iterate coin (I guess it is TODO, but mentioning anyway)

Your winning algorithm, to simplify, I will use a, b, c, d, instead of your yo, A[0][0], and A[0][1] x 2.

//So when you have
int a = 8;
int b = 8;
int c = 1;
int d = 1;

//then

if(a == b == c == d)
// int  int  int  int
  
//My guess is it will evaluate into  
if((a == b) == c == d)
//then
if(true == (c == d))
// bool     int  int

//then  
if(true == true)
// bool    bool

But I'm just guessing, I don't know how it is evaluated and what is order of it, I just found that it will return true if a == b and c == d

What you have to do is loop through rows and cols and compare those with your variable or use && and, your line would look like this:

if(yo == A[0][0] && yo == A[0][1] && yo == A[0][2]) //Notice that I did A[0][2] where you have A[0][1] second time.


And one more notice about "\n" and endl

//You do:
cout << "\n \n" << endl;
//and it is ~ equal to
cout << "\n \n\n";
//or
cout << endl << " " << endl << endl;

P.S.: I don't say that "\n" == std::endl

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, Mr_KoKa said:

int main()
{
    int A[3][3] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
    int row, col, crow, ccol, w = 0;
    int xo = 0;
    int yo = 8;
    
    for(int coin=0; coin <= 1; coin){ //This won't end, last loop parameter doesn't change anything
        //Display Module
        for(int p1 = 0; p1 < 3; p1++){
            cout << "\n \n " << endl;
            for(int p2 = 0; p2 < 3; p2++){
                cout << A[p1][p2] << "            ";
            }
        }
        
        //Game Engine(input Module)
        cout << "\n \n \nEnter the Row number" << endl;
        cin >> row;
        crow = row - 1;
        
        cout << "Enter the Column Number" << endl;
        cin >> col;
        ccol = col - 1;
        w++;
        
        if(w % 2 == 0){
            A[crow][ccol] = xo;
        } else {
            A[crow][ccol] = yo;
        }
        
        //Winning Algorithm
        std::cout << yo << ", " << A[0][0] << ", " << A[0][1] << ", " << A[0][1] << std::endl;
        if(yo == A[0][0] == A[0][1] == A[0][1]){ // You cannot do that. Use && or loop through
            cout << "Player 1 Wins !!!" << endl;
        }

    }
    
    return 0;
}

Your code is messy. To read It I had to redo all white spaces. Pasting it without code tags did't help.

 

The first for loop is equal to while(true) right now, as you don't iterate coin (I guess it is TODO, but mentioning anyway)

Your winning algorithm, to simplify, I will use a, b, c, d, instead of your yo, A[0][0], and A[0][1] x 2.


//So when you have
int a = 8;
int b = 8;
int c = 1;
int d = 1;

//then

if(a == b == c == d)
// int  int  int  int
  
//My guess is it will evaluate into  
if((a == b) == c == d)
//then
if(true == (c == d))
// bool     int  int

//then  
if(true == true)
// bool    bool

But I'm just guessing, I don't know how it is evaluated and what is order of it, I just found that it will return true if a == b and c == d

What you have to do is loop through rows and cols and compare those with your variable or use && and, your line would look like this:


if(yo == A[0][0] && yo == A[0][1] && yo == A[0][2]) //Notice that I did A[0][2] where you have A[0][1] second time.


And one more notice about "\n" and endl


//You do:
cout << "\n \n" << endl;
//and it is ~ equal to
cout << "\n \n\n";
//or
cout << endl << " " << endl << endl;

P.S.: I don't say that "\n" == std::endl

Yep . I will try to fix it .BTW,thank you for your kind help

Link to comment
Share on other sites

Link to post
Share on other sites

Is it tic tac toe? Just to be sure of what you are trying to make.

 

I don't know much of c++, but I've figured out to make an AI for tic tac toe using Java and JavaFX, if you need to, I can help with the logic, not the syntax though...

Intel i7 6700k @4.6 1.330v  cooled with H110i GT // 16Gb Kingston Fury DDR4 ram @2133MHz // Rx 480 Nitro+ // 2TB + 1 TB Hdd  // 250 GbSSD // on an Asus Z170-A // powered with Corsair RM750i // all  inside a Corsair 600C

Link to comment
Share on other sites

Link to post
Share on other sites

On 2.6.2016 at 9:46 PM, Mr_KoKa said:

 


//So when you have
int a = 8;
int b = 8;
int c = 1;
int d = 1;

//then

if(a == b == c == d)
// int  int  int  int
  
//My guess is it will evaluate into  
if((a == b) == c == d)
//then
if(true == (c == d))
// bool     int  int

//then  
if(true == true)
// bool    bool

But I'm just guessing, I don't know how it is evaluated and what is order of it, I just found that it will return true if a == b and c == d.


Just for informative purposes, the actual evaluation order goes as follows:

//So when you have
int a = 8;
int b = 8;
int c = 1;
int d = 1;

a == b => true
true == c => true
true == d => true

 

Link to comment
Share on other sites

Link to post
Share on other sites

if(yo==A[0][0]==A[0][1]==A[0][1]

 

That last one should be A[0][2] I believe.

 

Thing is, true / false is turned into an integer, and true is something that is not (since that is for false) 0, and is usually 1. So when you do y0 == A[0][0] and that returns true, it becomes 1. Then you are NOT comparing y0 or (A[0][0] for that matter) to A[0][1], but instead, you are comparing 1 == A[0][1]. So what you should instead do is to use the && operator, as in,

if (yo==A[0][0] && yo==A[0][1] && yo==A[0][2])

and then the results start making more sense.

 

What they said about the code is true though, putting everything inside int main() doesn't get you anywhere in the long run, and using more descriptive variable names isn't a problem with autofill - unless going for the minimum possible charcount is the end goal.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×