Thread: comparing array contents

  1. #1
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66

    comparing array contents

    i was bored so i tried making a tic-tac-toe game in dos using c++ and i am having trouble finding when the person wins the game.


    Code:
    //Dan Kemper     ([email protected])
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    const int MAXROW = 3;
    const int MAXCOL = 3;
    
    int main()
    {
         //declare variables
         int k=0, j=0, z=1, row=0, col=0, win=0;
         int input=0;
         char p1[40],p2[40];
         //initialize array
         char arr[MAXROW][MAXCOL] =
                        {
                             { '-', '-', '-', },
                             { '-', '-', '-', },
                             { '-', '-', '-', }
                        };
         
         cout << "This is a tic-tac-toe game that I made.\n\n"; //program decription
         cout << "Press a ket to continue...";
         getch();
         system("cls");
         //user input for names
         cout << "Player 1, enter your name: ";
         cin.get(p1,40);
         cin.ignore(80,'\n');
         cout << "\n\nPlayer 2, enter your name: ";
         cin.get(p2,40);
         cin.ignore(80,'\n');
         cout << "\n\nPress a key to continue...";
         getch();
         system("cls");
         
         while(1) //main loop; looks like infinate but is later broken using break;
         {
         while(z>=2) //internal loop
         {
          //prints out array
          for (k = 0; k < MAXROW; k++)
          {
                for (j = 0; j < MAXCOL; j++)
                {
                      cout << arr[k][j] << " ";
                }
                cout << endl;
          }
          
          if(z=1) //if its player 1's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row][col]='X'; //puts an X in the spot the user wants
          }
          else //if player 2's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row][col]='O'; //puts an O in the spot the user wants
          }
          
          z++; //increments variable which controld which persons turn it is
          
          
          /*         decides if player 1 wins          */
          
          if(arr[1][1]='X' && arr[1][2]='X' && arr[1][3]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][1]='X' && arr[2][2]='X' && arr[2][3]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[3][1]='X' && arr[3][2]='X' && arr[3][3]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][1]='X' && arr[2][2]='X' && arr[3][3]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][3]='X' && arr[2][2]='X' && arr[3][1]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          /*         decides if player 2 wins          */
          
          
          if(arr[1][1]='O' && arr[1][2]='O' && arr[1][3]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][1]='O' && arr[2][2]='O' && arr[2][3]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[3][1]='O' && arr[3][2]='O' && arr[3][3]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][1]='O' && arr[2][2]='O' && arr[3][3]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][3]='O' && arr[2][2]='O' && arr[3][1]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          
          }
          if(z=-1)
          {
                  break;
          }
          
          z=1;
          }
    
          cout << endl << endl << "Press ESC to exit...";
          
          while(input!=27)
          {
                          input=getch();
          }
          
          return 0;
    }
    i dont know if this makes me dumb for not knowing this but it seams that you cannot use the
    Code:
    if(arr[1][1]='X' && arr[1][2]='X' && arr[1][3]='X')
    to make the comparison and find the winner. maybe i should make that whole section a comment and debugg the rest of the program and then find an alternative way to do this afterward?

    any help is much appreciated. thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    An array of size 3 may be indexed from 0-2 (not 1-3).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    wow. i cant believe i missed that. i have been away from my compiler much too long. thanks.
    im sure ill be back...

  4. #4
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    okay so now i got this:


    Code:
    //Dan Kemper     ([email protected])
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    const int MAXROW = 3;
    const int MAXCOL = 3;
    
    int main()
    {
         //declare variables
         int k=0, j=0, z=1, row=0, col=0, win=0;
         int input=0;
         char p1[40],p2[40];
         //initialize array
         char arr[MAXROW][MAXCOL] =
                        {
                             { '-', '-', '-', },
                             { '-', '-', '-', },
                             { '-', '-', '-', }
                        };
         
         cout << "This is a tic-tac-toe game that I made.\n\n"; //program decription
         cout << "Press a ket to continue...";
         getch();
         system("cls");
         //user input for names
         cout << "Player 1, enter your name: ";
         cin.get(p1,40);
         cin.ignore(80,'\n');
         cout << "\n\nPlayer 2, enter your name: ";
         cin.get(p2,40);
         cin.ignore(80,'\n');
         cout << "\n\nPress a key to continue...";
         getch();
         system("cls");
         
         while(1) //main loop; looks like infinate but is later broken using break;
         {
         while(z>=2) //internal loop
         {
          //prints out array
          for (k = 0; k < MAXROW; k++)
          {
                for (j = 0; j < MAXCOL; j++)
                {
                      cout << arr[k][j] << " ";
                }
                cout << endl;
          }
          
          if(z=1) //if its player 1's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='X'; //puts an X in the spot the user wants
          }
          else //if player 2's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='O'; //puts an O in the spot the user wants
          }
          
          z++; //increments variable which controld which persons turn it is
          
          
          /*         decides if player 1 wins          */
          
          if(arr[0][0]='X' && arr[0][1]='X' && arr[0][2]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]='X' && arr[1][1]='X' && arr[1][2]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]='X' && arr[2][1]='X' && arr[2][2]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]='X' && arr[1][1]='X' && arr[2][2]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]='X' && arr[1][1]='X' && arr[2][0]='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          /*         decides if player 2 wins          */
          
          
          if(arr[0][0]='O' && arr[0][1]='O' && arr[0][2]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]='O' && arr[1][1]='O' && arr[1][2]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]='O' && arr[2][1]='O' && arr[2][2]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]='O' && arr[1][1]='O' && arr[2][2]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]='O' && arr[1][1]='O' && arr[2][0]='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          
          }
          if(z=-1)
          {
                  break;
          }
          
          z=1;
          }
    
          cout << endl << endl << "Press ESC to exit...";
          
          while(input!=27)
          {
                          input=getch();
          }
          
          return 0;
    }
    but the thing is i just keep gettin errors and the program will not run.
    Code:
    136 C:\Dev-Cpp\bin\main.cpp non-lvalue in assignment

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    = is assignment, == is comparison.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    omg, thats horrible i didnt realize that.
    thanks.

  7. #7
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    okay now it ran, but for some reason, the entire loop was completely skipped over. that cannot be since while(1) and the only way to get out of the whole thing is if the if statements inside are true which have break; in them to get out of the loop. i never get asked for the row or column at all. it asks for names and goes to press ESC to exit...

    Code:
    //Dan Kemper     ([email protected])
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    const int MAXROW = 3;
    const int MAXCOL = 3;
    
    int main()
    {
         //declare variables
         int k=0, j=0, z=1, row=0, col=0, win=0;
         int input=0;
         char p1[40],p2[40];
         //initialize array
         char arr[MAXROW][MAXCOL] =
                        {
                             { '-', '-', '-', },
                             { '-', '-', '-', },
                             { '-', '-', '-', }
                        };
         
         cout << "This is a tic-tac-toe game that I made.\n\n"; //program decription
         cout << "Press a key to continue...";
         getch();
         system("cls");
         //user input for names
         cout << "Player 1, enter your name: ";
         cin.get(p1,40);
         cin.ignore(80,'\n');
         cout << "\n\nPlayer 2, enter your name: ";
         cin.get(p2,40);
         cin.ignore(80,'\n');
         cout << "\n\nPress a key to continue...";
         getch();
         system("cls");
         
         while(1) //main loop; looks like infinate but is later broken using break;
         {
         while(z>=2) //internal loop
         {
          //prints out array
          for (k = 0; k < MAXROW; k++)
          {
                for (j = 0; j < MAXCOL; j++)
                {
                      cout << arr[k][j] << " ";
                }
                cout << endl;
          }
          
          if(z=1) //if its player 1's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='X'; //puts an X in the spot the user wants
          }
          else //if player 2's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='O'; //puts an O in the spot the user wants
          }
          
          z++; //increments variable which controld which persons turn it is
          
          
          /*         decides if player 1 wins          */
          
          if(arr[0][0]=='X' && arr[0][1]=='X' && arr[0][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]=='X' && arr[1][1]=='X' && arr[1][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]=='X' && arr[2][1]=='X' && arr[2][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]=='X' && arr[1][1]=='X' && arr[2][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]=='X' && arr[1][1]=='X' && arr[2][0]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          /*         decides if player 2 wins          */
          
          
          if(arr[0][0]=='O' && arr[0][1]=='O' && arr[0][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]=='O' && arr[1][1]=='O' && arr[1][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]=='O' && arr[2][1]=='O' && arr[2][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          
          }
          if(z=-1)
          {
                  break;
          }
          
          z=1;
          }
          
          system("cls");
          cout << "Press ESC to exit...";
          
          while(input!=27)
          {
                          input=getch();
          }
          
          return 0;
    }
    thanks for your help.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    More PIAs:
    Code:
          if(z=1) //if its player 1's turn
    Code:
          if(z=-1)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    ohhhhhhh forgot about those, i really shud have not been a noob and looked for stupid mistakes before posting and wasting your time :/
    thanks for the help

  10. #10
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    got an infanent loop after input of names.. darn blinking white bar...

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       int k=0, j=0, z=1, row=0, col=0, win=0;
       /* ... */
       while ( 1 ) //main loop; looks like infinate but is later broken using break;
       {
          while ( z>=2 ) //internal loop
          /* ... */
          if ( z==-1 )
          {
             break;
          }
          z=1;
       }
    Is the loop entered?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66

    Posts merged -- there is an edit button.

    i guess i wanted while ( z>=2 ) //internal loop to be while ( z<=2 ) //internal loop


    less than or equal

    hmm its always player 1... lets see here

    i see the problem there...

  13. #13
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    okay so the problem is that for some reason, z never becomes anything other than 1 because it keeps saying it is player ones turn.
    also,
    Code:
    if(z==1) //if its player 1's turn
          {
                 cout << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='X'; //puts an X in the spot the user wants
          }
    shouldnt that change that row and column to X, all it does is reprints the
    - - -
    - - -
    - - -

    each time and it never changes




    this is the latest version at this point:
    Code:
    //Dan Kemper     ([email protected])
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    
    const int MAXROW = 3;
    const int MAXCOL = 3;
    
    void intro();
    
    int main()
    {
         system("color 0f");
         //declare variables
         int k=0, j=0, z=1, row=0, col=0, win=0;
         int input=0;
         char p1[40],p2[40];
         //initialize array
         char arr[MAXROW][MAXCOL] =
                        {
                             { '-', '-', '-', },
                             { '-', '-', '-', },
                             { '-', '-', '-', }
                        };
         
         intro();
         cout << "Press a key to continue...";
         getch();
         system("cls");
         //user input for names
         cout << "Player 1, enter your name: ";
         cin.get(p1,40);
         cin.ignore(80,'\n');
         cout << "\n\nPlayer 2, enter your name: ";
         cin.get(p2,40);
         cin.ignore(80,'\n');
         cout << "\n\nPress a key to continue...";
         getch();
         system("cls");
         
         while(1) //main loop; looks like infinate but is later broken using break;
         {
         while(z<=2) //internal loop
         {
          //prints out array
          for (k = 0; k < MAXROW; k++)
          {
                for (j = 0; j < MAXCOL; j++)
                {
                      cout << "\t\t" << arr[k][j] << " ";
                }
                cout << endl << endl;
          }
          
          if(z==1) //if its player 1's turn
          {
                 cout << endl << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='X'; //puts an X in the spot the user wants
          }
          else //if player 2's turn
          {
                 cout << endl << p1 << ", enter your choice:" << endl << endl;
                 cout << "Row: ";
                 cin >> row; //input for row
                 cout << endl << "Coulumn: ";
                 cin >> col; //input for column
                 arr[row+1][col+1]='O'; //puts an O in the spot the user wants
          }
          
          z++; //increments variable which control which persons turn it is
          system("cls");
          
          
          /*         decides if player 1 wins          */
          
          if(arr[0][0]=='X' && arr[0][1]=='X' && arr[0][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]=='X' && arr[1][1]=='X' && arr[1][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]=='X' && arr[2][1]=='X' && arr[2][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]=='X' && arr[1][1]=='X' && arr[2][2]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]=='X' && arr[1][1]=='X' && arr[2][0]=='X')
          {
                           cout << endl << p1 << " wins!!!";
                           z=-1;
                           break;
          }
          
          
          /*         decides if player 2 wins          */
          
          
          if(arr[0][0]=='O' && arr[0][1]=='O' && arr[0][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[1][0]=='O' && arr[1][1]=='O' && arr[1][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[2][0]=='O' && arr[2][1]=='O' && arr[2][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O')
          {
                           cout << endl << p2 << " wins!!!";
                           z=-1;
                           break;
          }
          }
          
          if(z==-1)
          {
                  break;
          }
          
          z=1;
          }
          
          system("cls");
          cout << "Press ESC to exit...";
          
          while(input!=27)
          {
                          input=getch();
          }
          
          return 0;
    }
    
    void intro()
    {
         int i=1;
        
        while(i<=11)
        {
                   switch(i)
                   {
                            case 1: cout << "T"; break;
                            case 2: cout << "i"; break;
                            case 3: cout << "c"; break;
                            case 4: cout << "-"; break;
                            case 5: cout << "T"; break;
                            case 6: cout << "a"; break;
                            case 7: cout << "c"; break;
                            case 8: cout << "-"; break;
                            case 9: cout << "T"; break;
                            case 10: cout << "o"; break;
                            case 11: cout << "e"; break;
                   }
                   Sleep(500);
                   i++;
        }
        cout << endl << endl;
    }
    after i get these things working i will douser proofing on what they can enter for rows and colums and add some colors like make the x's red and o's blue or sumthin...
    Last edited by ReLiEnThAwK; 08-01-2006 at 10:31 PM.

  14. #14
    C++No0b!!!
    Join Date
    Jul 2005
    Location
    penn
    Posts
    66
    so i guess the question is why that particular code wouldnt do anything. why doesnt the X get put into the row and column that was given and then display it when the loop comes around again. and why doesnt it switch to player 2 after 1's turn is over
    Last edited by ReLiEnThAwK; 08-02-2006 at 09:55 AM.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
       z = 1;       //z will be -1 if win found
       numTurns = 0; 
    
       while(#numTurns != 9 && z != -1)  //while less than nine turns taken so far and no win found
       {
           //obtain players choice here
           //increment numTurns here
           //check for wins here
     
           if(z != -1)  //no win found
           {
              if(z == 1)  //if z equals 1
                 z = 2;    //change z to 2
              else          //else
                 z = 1;    //change z to 1
           }   
       }
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 4
    Last Post: 03-18-2009, 05:01 PM
  3. Using the debugger to see array contents
    By JohnnyCat in forum C++ Programming
    Replies: 2
    Last Post: 06-23-2005, 02:17 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM