Need help with checking string vallues

This is a discussion on Need help with checking string vallues within the C++ Programming forums, part of the General Programming Boards category; I'm writing a Tic-Tac-Toe game, but I'm having trouble with my checkwin() function at the bottom. It's supposed to compare ...

  1. #1
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166

    Need help with checking string vallues

    I'm writing a Tic-Tac-Toe game, but I'm having trouble with my checkwin() function at the bottom.

    It's supposed to compare the values of each board[] but If i just press enter once, and it assigns X to board 1, it automatically says that I won.

    Code:
    //Tic-Tac-Toe
    
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    #include <string>
    
    enum
    {
      W = 119,
      A = 97,
      S = 115,
      D = 100,
      ENTER = 13
    };
    int ch;
    using namespace std;
    
    int X;//for the coordinates
    int Y;
    int cell, player; 
    short int turn;
    char pice, *pice_ptr = &pice;
    int moveto( int moveX, int moveY );
    static int get_code();
    int XO();
    int checkwin();
    int checkcell();
    void checkplayer();
    void getcell();
    
    int main(){
        cout<<"\t\t\t   Tic-Tac-Toe";
          
        cout<<"\n\n\n\n\n\n\n\n";
        cout<<"\t\t\t     |     |\n";
        cout<<"\t\t\t     |     |\n";
        cout<<"\t\t\t_____|_____|_____\n";
        cout<<"\t\t\t     |     |\n";      
        cout<<"\t\t\t     |     |\n";
        cout<<"\t\t\t_____|_____|_____\n";
        cout<<"\t\t\t     |     |\n";
        cout<<"\t\t\t     |     |\n";
        cout<<"\t\t\t     |     |\n";
        
        X = 26;
        Y = 9;
        moveto(X,Y);  //start the game in cell 1
        turn = 0;
        while ( turn <= 12 || !checkwin() == 1 ){
              ch = getch();  //get a keypress (W,A,S,D, or ENTER)
    //------------up------------------------     
              switch (ch) {
              case W:
              if ( Y == 9 ){
                   break;
              }
              
              else{
                   
                   Y = Y - 3;
                   moveto(X,Y);
                   break;
              }
    //-----------left----------------------
              case A:
              if ( X <= 26 ){
                   break;
              }
              else{
                   
                   X = X - 6;
                   moveto(X,Y);
                   break;
              }
    //------------down----------------------     
              case S:
              if ( Y == 15 ){
                   break;
              }
              
              else{
                   Y = Y + 3;
                   moveto(X,Y);
                   break;
              }
    //------------right--------------------------
              case D:
              if ( X >= 38 ){
                   break;
              }
              
              else{
                   X = X + 6;
                   moveto(X,Y);
                   break;
              }
    //------------enter---------------------
              case ENTER:
              XO();
              checkwin();
              break;
         }
    }
    }
    
    
    //mechanism for moving the cursor
    int moveto( int moveX, int moveY )
    {
        HANDLE hOut;
        COORD Position;
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
        Position.X = moveX;
        Position.Y = moveY;  
        SetConsoleCursorPosition(hOut,Position);
    }
    
    static int get_code( void )
    {
      int ch = getch();
      return ch;
    }
    
    void checkplayer_and_pice(){
        if ( turn % 2 == 0 ){ //if there is a remainder, it is an odd number
             player = 1;
             pice = 88; //88 = letter O 
        }
        else{
             player = 2;
             pice = 79;//79 = letter X
        }
    }
    
    void getcell(){                             //check's coordinate of cell and
         if ( X == 26 && Y == 9 ){ cell = 1; }   //give's cell a value according to
         else if ( X == 26 && Y == 12 ){ cell = 4; }     //the matching coordinates
         else if ( X == 26 && Y == 15 ){ cell = 7; }
         else if ( X == 32 && Y == 9 ){ cell = 2; }    
         else if ( X == 32 && Y == 12 ){ cell = 5; }
         else if ( X == 32 && Y == 15 ){ cell = 8; }
         else if ( X == 38 && Y == 9 ){ cell = 3; }
         else if ( X == 38 && Y == 12){ cell = 6; }
         else if ( X == 38 && Y == 15){ cell = 9; }
              
    }
    
    int checkcell(){                            //checks the given board[] if it
        if ( board[cell] == 0 ){                //already contains a value
             return 0;                          // returns 1 if it does 0 otherwise
        }
        if ( !(board[cell] == 0) ){
             return 1;
        }
    }
    
    int XO()                                    //mechanism for printing x or o to                         
    {                                           //the screen
         checkplayer_and_pice();
         getcell();
         checkcell();
         
         if ( checkcell() == 0 ){
              board[cell] = pice;
              cout<<*pice_ptr;    
              turn++;              //having a pointer is pointless -_-
              return 0;
         }
         
         else if ( checkcell() == 1 ){
              return 0;
         }
    }
    
    int checkwin()
    {
         if ( board[1] == board[4] == board[7] ){
              cout<<"\n\n\nPlayer "<<player<<" wins!";
              return 1;
         }
         
         else{
         return 0;
         }
    }

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    24
    Code:
    if ( board[1] == board[4] == board[7] ){
    that expression evaluates "board[1] == board[4]" first, then takes the boolean result
    (1 or 0) and compares it to the cell at board[7]. you need to compare the elements separately.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
              turn++;              //having a pointer is pointless -_-
    Yeah I suppose when everything in your entire program is done with global variables, playing with would be pointless.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by legend
    Code:
    if ( board[1] == board[4] == board[7] ){
    that expression evaluates "board[1] == board[4]" first, then takes the boolean result
    (1 or 0) and compares it to the cell at board[7]. you need to compare the elements separately.

    Thanks. I knew there was something wrong there.

    I solved it with board[1] == board[4] && board[1] == board[7]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:59 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21