basic C++ question:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buddyjuf
    • Jul 2026

    #1

    basic C++ question:

    So I have this 2 dimensional Array (3X3)

    and I want to make it so

    "if there is an "X" in slot 2-2, return a "YES"

    tried doing

    if ( board[2][2] hahahaha 'X' )
    {
    cout << "Yes";
    }
    else
    {
    cout << "No";
    }

    and I keep on getting "No"s at every turn


    can anybody help me out on this one?


    what I am actually trying to do, is that it's a TIC TAC TOE game, and I need to stop the program once someone wins...

    help me out yooo!!!

    178340550
  • BuggyG
    Confirmed User
    • Oct 2002
    • 4179

    #2
    ah shit.. haven't done any C++ in over 10years

    damn.. now I wanna know too
    and I know it's a fuckin simple thing

    Comment

    • skinnay
      Confirmed User
      • Apr 2004
      • 2274

      #3
      post the code somewhere where it wont go hahahaha
      Make Real Green with ORGANIC SEO | Blog post exchange | Non-index page trades | A-B C-D Trades [icq: 194-215-962] [mail: [email protected]]

      Comment

      • buddyjuf

        #4
        Originally posted by skinnay
        post the code somewhere where it wont go hahahaha
        its just 2 "=" next to eachother

        Comment

        • rickholio
          Confirmed User
          • Jan 2004
          • 1914

          #5
          Originally posted by bdjuf
          So I have this 2 dimensional Array (3X3)

          and I want to make it so

          "if there is an "X" in slot 2-2, return a "YES"

          tried doing

          if ( board[2][2] hahahaha 'X' )
          {
          cout << "Yes";
          }
          else
          {
          cout << "No";
          }

          and I keep on getting "No"s at every turn


          can anybody help me out on this one?


          what I am actually trying to do, is that it's a TIC TAC TOE game, and I need to stop the program once someone wins...

          help me out yooo!!!

          178340550
          Because [2][2] is actually referencing the 'bottom right' element of a 3x3 array. Arrays in C/C++ are referenced from 0. If you're trying to check the MIDDLE square, you're checking the wrong spot... and if you reference [3][3], you're overflowing and begging for a h4x0r 2 r0x0r j00r b0x0rz.

          Try this:
          Code:
            if(board[1][1]hahahaha'X') {       // Is there an X in the middle square?
              cout << "Yes";
            } else {
              cout << "No";
            }
          BTW - The best way to make a TTT game is to have the program 'learn'. I did the same thing back in 80s on an atari 400... it's possible to make a damn-near unbeatable computerized opponent.
          Last edited by rickholio; 04-05-2005, 08:54 PM.
          ~

          Comment

          • rickholio
            Confirmed User
            • Jan 2004
            • 1914

            #6
            A 'win' state checker:

            Code:
            char *checkwin(char board[2][2])  // Prototype might be a bit off, adjust accordingly ;)
            {
              // Check horizontal
              for(int y=0;y<3;y++) {
                int xs, os;
                for(int x=0;x<3;x++) {
                  xs += (board[x][y] = = 'X');
                  os += (board[x][y] = = 'O');
                }
              }
              if(xs= =3) return("X Wins");
              if(os= =3) return("O Wins");
            
              // Check vertical
              for(int x=0;x<3;x++) {
                int xs, os;
                for(int y=0;y<3;y++) {
                  xs += (board[x][y] = = 'X');
                  os += (board[x][y] = = 'O');
                }
              }
              if(xs= =3) return("X Wins");
              if(os= =3) return("O Wins");
            
              // Check left-right diag
              for(int a=0,xs=0,os=0;a<3;a++) {
                xs += (board[a][a] = = 'X');
                os += (board[a][a] = = 'O');
              }
              if(xs= =3) return("X Wins");
              if(os= =3) return("O Wins");
            
              // Check right-left diag
              for(int a=0,xs=0,os=0;a<3;a++) {
                xs += (board[a][2-a] = = 'X');
                os += (board[a][2-a] = = 'O');
              }
              if(xs= =3) return("X Wins");
              if(os= =3) return("O Wins");
            
            // No winnar.
              return;
            }
            There is almost certainly better ways of doing this (making a string of 3 characters in the array and matching that against a string of 'XXX' or 'OOO', for instance). This was just a 60 second slap together that'll do the job and demonstrate the logic cleanly.
            ~

            Comment

            • wdsguy
              Ryde or Die
              • Dec 2002
              • 19568

              #7
              Originally posted by bdjuf
              So I have this 2 dimensional Array (3X3)

              and I want to make it so

              "if there is an "X" in slot 2-2, return a "YES"

              tried doing

              if ( board[2][2] hahahaha 'X' )
              {
              cout << "Yes";
              }
              else
              {
              cout << "No";
              }

              and I keep on getting "No"s at every turn


              can anybody help me out on this one?


              what I am actually trying to do, is that it's a TIC TAC TOE game, and I need to stop the program once someone wins...

              help me out yooo!!!

              178340550

              Doing a assignment for cs class?

              Comment

              • vas
                Confirmed User
                • Dec 2003
                • 562

                #8
                use the min max algo, i coded a tic tac toe game for school a couple years ago using this algo, and it was undefeatable..

                http://personal.vsnl.com/erwin/tictactoe.htm
                icq: 375956791

                Comment

                • nastyking
                  • Nov 2002
                  • 2174

                  #9
                  array element numbering starts with 0 not 1

                  Comment

                  • Slacker
                    Confirmed User
                    • Feb 2003
                    • 331

                    #10
                    Nerds








                    just kidding

                    Comment

                    • darnit
                      Confirmed User
                      • Jul 2001
                      • 2439

                      #11
                      Nerds


                      Comment

                      • buddyjuf

                        #12
                        got it! thanx guys

                        now another question, how do I reset an array? make it all empty again?

                        Comment

                        • tranza
                          ICQ: 197-556-237
                          • Jun 2003
                          • 57559

                          #13
                          Damn! I used to know C++, now I don't remember shit!
                          I'm just a newbie.

                          Comment

                          • rickholio
                            Confirmed User
                            • Jan 2004
                            • 1914

                            #14
                            Originally posted by bdjuf
                            got it! thanx guys

                            now another question, how do I reset an array? make it all empty again?
                            Assuming it's a contiguous area of memory and a simple array:

                            Code:
                            memset((void *)array_ptr, number_of_elements * sizeof(array_type), 0);
                            Alternately, you can request it be zeroed while allocating:

                            Code:
                            array_ptr = (array_type *)calloc(number_of_elements, sizeof(array_type));
                            You're doing c++, so you can always just destroy the old object and create a new one, which should set all values to their prototypes as well... if not, you can do the memset() thing in the object constructor.

                            HTH. HAND.

                            PS - If you malloc()'d the array originally, make sure you free() it before doing a new malloc(), otherwise you'll leak ram.
                            Last edited by rickholio; 04-06-2005, 01:29 PM.
                            ~

                            Comment

                            • buddyjuf

                              #15
                              everything works!!! thanx alot for your help guys

                              Comment

                              Working...