first ever program - need help =(

This is a discussion on first ever program - need help =( within the C++ Programming forums, part of the General Programming Boards category; I've decided to dive into the world of C++, and decided as my first attempt i would try making a ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    21

    first ever program - need help =(

    I've decided to dive into the world of C++, and decided as my first attempt i would try making a simple tic tac toe/noughts and crosses game. Basic i know, but im just starting =(

    The game works fine except one bug that I just can't find; it draws in a cross or circle in the bottom right for no apparent reason in the first 2 turns, and then from then on it treats it as though the square doesn't exist.

    I would be really grateful if someone could look through the code and find out where the problem is, and maybe while your at it to suggest ways of making my code more efficient as im sure there are simpler functions/ways of doing things that i don't know about.

    Anyway thanks for any help you can give me, heres the code:

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int loopcountrow;
    int loopcountcol;
    int loopcount;
    int board[9];
    int move;
    int win=0;
    int endc;
    int player=1;
    int playerc;
    int i;
    //////////////////////////////////////////////
    /////////////////                  //////////////
    /////////////////   -=0 X=1 O=2    ////////////////
    /////////////////                  /////////////////
    /////////////////////////////////////////////////////
    void drawboard()
    {
         cout << endl;
             for (loopcountrow = 0; loopcountrow < 3; loopcountrow = loopcountrow + 1)
            {
                          
             for (loopcountcol = 1; loopcountcol < 4; loopcountcol = loopcountcol + 1)
                 {
                 loopcount = (loopcountrow*3)+loopcountcol;
               
                 switch(board[loopcount])
    {
    case 0:
         cout << "-";
         break;
    case 1:
    cout << "X";
    break;
    case 2:
    cout << "O";
    break;
    default:
            cout << "-";
    }
                 }
             cout << endl;
             
             }
    }
    //////////////////////////////////////////////////////////////////////////
    void inputmove1()
    {
         cout << endl;
         cout << "Player 1" << endl;
         cout << "Where would you like to move? >> ";
         cin >> move;
              if (move>9){
                          cout << "Invalid Move" << endl;
         inputmove1();
         }
         switch(board[move]){
                             case 0:
                        board[move]=1;  
                        player=2;                   
         break;
         case 1:
         cout << "Invalid Move" << endl;
         inputmove1();
         break;
         case 2:
         cout << "Invalid Move" << endl;
         inputmove1();
         break;
         }
         
       }
    //////////////////////////////////////////////////////////////////////////
    void inputmove2()
    {
         cout << endl;
         cout << "Player 2" << endl;
         cout << "Where would you like to move? >> ";
         cin >> move;
         if (move>9){
                          cout << "Invalid Move" << endl;
         inputmove2();
         }
         switch(board[move]){
                             case 0:
                        board[move]=2;       
                        player=1;              
         break;
         case 1:
         cout << "Invalid Move" << endl;
         inputmove2();
         break;
         case 2:
         cout << "Invalid Move" << endl;
         inputmove2();
         break;
         }
         
       }
    /////////////////////////////////////////////////////////////////////////
    void checkwin1() 
    {
         for (i=1; i<3; i=i+1){
    if (board[1]==i & board[4]==i & board[7]==i){
                   win=i;
                   }
                   if (board[1]==i & board[4]==i & board[7]==i){
                   win=i;
                   }
                   if (board[2]==i & board[5]==i & board[8]==i){
                   win=i;
                   }
                   if (board[3]==i & board[6]==i & board[9]==i){
                   win=i;
                   }
                   if (board[1]==i & board[2]==i & board[3]==i){
                   win=i;
                   }
                   if (board[4]==i & board[5]==i & board[6]==i){
                   win=i;
                   }
                   if (board[7]==i & board[8]==i & board[9]==i){
                   win=i;
                   }    
                   if (board[1]==i & board[5]==i & board[9]==i){
                   win=i;
                   }
                     if (board[3]==i & board[5]==i & board[7]==i){
                   win=i;
                   }
                   }
     }
    /////////////////////////////////////////////////////////////////////////
    void checkwin2() 
    {
     }
    
    /////////////////////////////////////////////////////////////////////////
       void chooseplayer() 
    {
            if (player==1){
                           playerc=1;
                           }else{
                                 playerc=2;
                                 }
     }
    
    /////////////////////////////////////////////////////////////////////////
         
    int main()
    {
        do{
        drawboard();
        checkwin1();
        checkwin2();
        chooseplayer();
      if (playerc==1){
      inputmove1();
    }else{
       inputmove2();
    }
        checkwin1();
        checkwin2();
       }while(win==0);
        drawboard();
         switch(win){
                   case 1:
                        cout << "Player 1 Wins!";
                        break;
                         case 2:
                        cout << "Player 2 Wins!";
                        break;
                         case 3:
                        cout << "Draw =(";
                        break;
                        default:
                                cout << "QQ";
                   }
                  cin >> endc;
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,536
    It seems all of your code assumes arrays are indexed from 1 to 9.

    They aren't, board[9] has valid indexes 0 to 8
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    21
    Thanks a lot =)
    Knew it was something simple, just ha dno idea what...

    Updated code: Any other tips?

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    int loopcountrow;
    int loopcountcol;
    int loopcount;
    int board[9];
    int movex;
    int move;
    int win=0;
    int endc;
    int player=1;
    int playerc;
    int i;
    //////////////////////////////////////////////
    /////////////////                  //////////////
    /////////////////   -=0 X=1 O=2    ////////////////
    /////////////////                  /////////////////
    /////////////////////////////////////////////////////
    void drawboard()
    {
         cout << endl;
             for (loopcountrow = 0; loopcountrow < 3; loopcountrow = loopcountrow + 1)
            {
                          
             for (loopcountcol = 0; loopcountcol < 3; loopcountcol = loopcountcol + 1)
                 {
                 loopcount = (loopcountrow*3)+loopcountcol;
               
                 switch(board[loopcount])
    {
    case 0:
         cout << "-";
         break;
    case 1:
    cout << "X";
    break;
    case 2:
    cout << "O";
    break;
    default:
            cout << "-";
    }
                 }
             cout << endl;
             
             }
    }
    //////////////////////////////////////////////////////////////////////////
    void inputmove1()
    {
         cout << endl;
         cout << "Player 1" << endl;
         cout << "Where would you like to move? >> ";
         cin >> movex;
              if (movex>9){
                          cout << "Invalid Move" << endl;
         inputmove1();
         }
         move=movex-1;
         switch(board[move]){
                             case 0:
                        board[move]=1;  
                        player=2;                   
         break;
         case 1:
         cout << "Invalid Move" << endl;
         inputmove1();
         break;
         case 2:
         cout << "Invalid Move" << endl;
         inputmove1();
         break;
         }
         
       }
    //////////////////////////////////////////////////////////////////////////
    void inputmove2()
    {
         cout << endl;
         cout << "Player 2" << endl;
         cout << "Where would you like to move? >> ";
         cin >> movex;
         if (movex>9){
                          cout << "Invalid Move" << endl;
         inputmove2();
         }
         move=movex-1;
         switch(board[move]){
                             case 0:
                        board[move]=2;       
                        player=1;              
         break;
         case 1:
         cout << "Invalid Move" << endl;
         inputmove2();
         break;
         case 2:
         cout << "Invalid Move" << endl;
         inputmove2();
         break;
         }
         
       }
    /////////////////////////////////////////////////////////////////////////
    void checkwin1() 
    {
         for (i=1; i<3; i=i+1){
    if (board[0]==i & board[3]==i & board[6]==i){
                   win=i;
                   }
                   if (board[0]==i & board[3]==i & board[6]==i){
                   win=i;
                   }
                   if (board[1]==i & board[4]==i & board[7]==i){
                   win=i;
                   }
                   if (board[2]==i & board[5]==i & board[8]==i){
                   win=i;
                   }
                   if (board[0]==i & board[1]==i & board[2]==i){
                   win=i;
                   }
                   if (board[3]==i & board[4]==i & board[5]==i){
                   win=i;
                   }
                   if (board[6]==i & board[7]==i & board[8]==i){
                   win=i;
                   }    
                   if (board[0]==i & board[4]==i & board[8]==i){
                   win=i;
                   }
                     if (board[2]==i & board[4]==i & board[6]==i){
                   win=i;
                   }
                   }
     }
    /////////////////////////////////////////////////////////////////////////
    void checkwin2() 
    {
     }
    
    /////////////////////////////////////////////////////////////////////////
       void chooseplayer() 
    {
            if (player==1){
                           playerc=1;
                           }else{
                                 playerc=2;
                                 }
     }
    
    /////////////////////////////////////////////////////////////////////////
         
    int main()
    {
        do{
        drawboard();
        checkwin1();
        checkwin2();
        chooseplayer();
      if (playerc==1){
      inputmove1();
    }else{
       inputmove2();
    }
        checkwin1();
        checkwin2();
       }while(win==0);
        drawboard();
         switch(win){
                   case 1:
                        cout << "Player 1 Wins!";
                        break;
                         case 2:
                        cout << "Player 2 Wins!";
                        break;
                         case 3:
                        cout << "Draw =(";
                        break;
                        default:
                                cout << "QQ";
                   }
                  cin >> endc;
    return 0;
    }

  4. #4
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Firstly, you could make better use of indenting. For example, a random part of your code:
    Code:
        if(movex > 9)
        {
            cout << "Invalid Move" << endl;
            inputmove2();
        }
        move--;
        switch(board[move])
        {
        case 0:
            board[move] = 2;       
            player = 1;              
            break;
        case 1:
            cout << "Invalid Move" << endl;
            inputmove2();
            break;
        case 2:
            cout << "Invalid Move" << endl;
            inputmove2();
            break;
        }
    }
    /////////////////////////////////////////////////////////////////////////
    void checkwin1() 
    {
        for (i=1; i<3; i++)
        {
            if (board[0]==i && board[3]==i && board[6]==i)
            {
                win = i;
            }
            if (board[0]==i && board[3]==i && board[6]==i)
            {
                win = i;
            }
            if (board[1]==i && board[4]==i && board[7]==i)
            {
                win = i;
            }
            if (board[2]==i && board[5]==i && board[8]==i)
            {
                win = i;
            }
            if (board[0]==i && board[1]==i && board[2]==i)
            {
                win = i;
            }
    Already a lot more readable, isn't it?

    Secondly, some small mistake that doesn't make a big difference - for logical AND use &&. & is the binary operator. Also, you can add 1 to a variable by writing this:
    i = i + 1;
    but this does the same:
    i++;
    and this:
    ++i;
    The latter is preferable.

    Finally, let me say the I think you'll be a great programmer. You seem very talented compared to most beginners that posted on this forum

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    21
    Yeah, I see what you mean, im guessing its something i'm just going to have to do and will eventually become automatic.

    Anyway, thanks for the other advice and the compliment. Talking of compliments, im seriously surprised by how fast this community has helped me.

    =)

  6. #6
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Your inputmove() functions are almost the same. You could transform them into one:

    Code:
    void inputmove()  
    {                           
        cout << endl;
        cout << "Player " << player << endl;
        cout << "Where would you like to move? >> ";
        cin >> movex;
        if (movex>9)
        {
            cout << "Invalid Move" << endl;
            inputmove(); 
        }
        --move;
        switch(board[move])
        {
        case 0:
            board[move] = player;  
            if(player==1)
            {
                player = 2;
            }
            else
            {
                player = 1;
            }                     
            break;
        case 1:
            cout << "Invalid Move" << endl;
            inputmove();
            break;
        case 2:
            cout << "Invalid Move" << endl;
            inputmove();
            break;
        }
    }

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    21
    What's the exact difference between ++i and i++? I know it's something like changing the variable before or after its used in the statement, but only ++i works while i++ makes everything explode

  8. #8
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    This is best explained with an example:
    Code:
    int n = 0;
    int a = 3, b = 7;
    
    n = ++a;   // n is 4, a is 4
    n = b++;   // n is 7, b is 8

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    21
    Ok, cleaned up the code, merged some functions, deleted others and enabled it to detect a draw...

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;
    
    //  Declare Variables
          int loopcountrow;
          int loopcountcol;
          int loopcount;
          int board[9];
          int movex;
          int move;
          int win=0;
          int endc;
          int player=1;
          int playerc;
          int i;
          int numove=0;
          
    //////////////////////////////////////////////
    /////////////////                  //////////////
    /////////////////   -=0 X=1 O=2    ////////////////
    /////////////////                  /////////////////
    /////////////////////////////////////////////////////
    void drawboard()
    {
         cout << endl;
             
         for (loopcountrow = 0; loopcountrow < 3; loopcountrow = loopcountrow + 1)
            {  
                                           
             for (loopcountcol = 0; loopcountcol < 3; loopcountcol = loopcountcol + 1)
                 {
                 loopcount = (loopcountrow*3)+loopcountcol;
                 switch(board[loopcount])
                 {
                 case 0:
                      cout << "-";
                      break;
                 case 1:
                      cout << "X";
                      break;
                 case 2:
                      cout << "O";
                      break;
                 default:
                      cout << "-";
                  }
             
              }
          cout << endl;
             
           }
    }
    ///////////////////////////////////////////////////////////////////////////
       void inputmove()
    {
         cout << endl;
         if(player==1)
         {
              cout << "Player 1" << endl;
         }else{
              cout << "Player 2" << endl;
         }
         cout << "Where would you like to move? >> ";
         cin >> movex;
         if (movex>9)
         {
             cout << "Invalid Move" << endl;
             inputmove();
         }
         move=movex-1;
         switch(board[move])
         {
         case 0:
              if(player==1)
              {
                   board[move]=1;       
                   player=2; 
              }else{
                   board[move]=2;       
                   player=1; 
              }
              numove++;             
              break;
         case 1:
              cout << "Invalid Move" << endl;
              inputmove();
              break;
         case 2:
              cout << "Invalid Move" << endl;
              inputmove();
              break;
         }
         
       }
    /////////////////////////////////////////////////////////////////////////
    void checkwin() 
    {
    
         for (i=1; i<3; i=++i)
         {
                   if (board[0]==i && board[3]==i && board[6]==i)
                   {
                      win=i;
                   }
                   if (board[0]==i && board[3]==i && board[6]==i)
                   {
                     win=i;
                   }
                   if (board[1]==i && board[4]==i && board[7]==i)
                   {
                      win=i;
                   }
                   if (board[2]==i && board[5]==i && board[8]==i)
                   {
                      win=i;
                   }
                   if (board[0]==i && board[1]==i && board[2]==i)
                   {
                      win=i;
                   }
                   if (board[3]==i && board[4]==i && board[5]==i)
                   {
                      win=i;
                   }
                   if (board[6]==i && board[7]==i && board[8]==i)
                   {
                      win=i;
                   }    
                   if (board[0]==i && board[4]==i && board[8]==i)
                   {
                      win=i;
                   }
                   if (board[2]==i && board[4]==i && board[6]==i)
                   {
                      win=i;
                   }
         }
         if(numove==9)
         {
         win=3;
         }
     }
    
    /////////////////////////////////////////////////////////////////////////
         
    int main()
    {
        do
        {
              drawboard();
              inputmove();
              checkwin();
       }while(win==0);
       
        drawboard();
         cout << endl;
         switch(win)
         {
           case 1:
               cout << "Player 1 Wins!";
               break;
           case 2:
                cout << "Player 2 Wins!";
                break;
           case 3:
                cout << "Draw =(";
                break;
           default:
                cout << "QQ";
           }
           
           cin >> endc;
    
    return 0;
    }

  10. #10
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    Compare this:
    Code:
         if(player==1)
         {
              cout << "Player 1" << endl;
         }else{
              cout << "Player 2" << endl;
         }
    and this (from my code)
    Code:
        cout << "Player " << player << endl;
    See?;P
    Last edited by pronecracker; 05-27-2007 at 11:39 AM.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    21
    dam i suck

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    21
    Also another question, not related but I didn't think it deserved a thread of it's own(too many already).

    I have both:
    Dev-C++(4.9.9.2) - Currently Using
    Visual C++ 2005 Express Edition

    Which one should I use, or is there another, even better one?

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,536
    Use them both - it'll keep you honest.
    Using two compilers is a good way of making sure you're actually learning something close to standard C++, and not merely some 'extended' version which one compiler lets you get away with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    21
    Decided to now try and add intelligent ai to the game, and realised it will probably make my life a lot easier if I changed the array into 3x3, are there any complications to this, or anyway that mean my current code can be used?

    Thanks for the help and advice so far though everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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