Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: c++

  1. #1
    Join Date
    Jan 2012
    Beans
    160

    c++

    I know this probably isnt the forum for this, but i need some c++ help. I have been working on this "tic tac toe" program for a few days, and i cannot get it to run. I am unsure of what to do. Some guidance would be helpful.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame();
    void taketurn (int x);
    int checkwin ();
    
    int z;
    int player1;
    int player2;
    int slot_filled;
    
    int a, b, c, d, e, f, g, h, i;
    
    enum slotfill 
    {
        X , O, U
    };
    
    int main()
    {
        cout << "  ------------------\n ";
        cout << " Welcome to tic tac toe\n";
        cout << " -------------------\n";
        
        cout << " Menu selection.\n1. Play\n 2. Exit\n Your choice\n?";
        cin >> z ;
    
        switch (z)
        {
            case 1:
            playgame();
            break;
            
            case 2: 
            return 0;
            
            default : 
            return 0;
        }
        
            
    
    }    
        
    void playgame()
    {
        do{
            
        taketurn(player1);
        checkwin();
        taketurn(player2);
        checkwin();
    } while (checkwin != 1);
    }
    
    void taketurn (int x)
    {
        cout << "which slot would you like to fill?" << endl;
        cin >> slot_filled;
        
        switch (x)
        {
            case 0: 
            slot_filled = 0;
            break;
            
            case 1:
            slot_filled = 1;
            break;
            
        }
        
    }
    
    int checkwin ()
    {
    if ( a == 0 && b == 0 && c == 0 || d == 0 && e == 0 && f == 0 || g == 0 && h == 0 && i == 0)
    {
        cout << " Player 1 wins!!!! ";
        return 1;
    }
    
    if ( a == 0 && d== 0 && g == 0 || b == 0 && e == 0 && h == 0 || c == 0 && f == 0 && i == 0)
    {
        cout << " Player 1 wins!!!! ";
        return 1;
    }
    
    if ( a == 0 && e == 0 && i == 0 || c == 0 && e == 0 && g == 0)
    {
        cout << " Player 1 wins!!!";
        return 1;
    }
    // break for reading between player1 and player 2
    
    if ( a == 1 && b == 1 && c == 1 || d == 1 && e == 1 && f == 1 || g == 1 && h == 1 && i == 1)
    {
        cout << " Player 2 wins!!!! ";
        return 1;
    }
    
    if ( a == 1 && d== 1 && g == 1 || b == 1 && e == 1 && h == 1 || c == 1 && f == 1 && i == 1)
    {
        cout << " Player 2 wins!!!! ";
        return 1;
    }
    
    if ( a == 1 && e == 1 && i == 1 || c == 1 && e == 1 && g == 1)
    {
        cout << " Player 2 wins!!!";
        return 1;
    }
    }

  2. #2
    Join Date
    Feb 2009
    Beans
    86

    Re: need some c++ help

    line 55 while (checkwin != 1);
    checkwin isn't a variable, so you're simply missing the () after the name!

  3. #3
    Join Date
    Jan 2012
    Beans
    160

    Re: need some c++ help

    ok im a bit dense. shouldnt checkwin return "1" when someone wins?

  4. #4
    Join Date
    Feb 2009
    Beans
    86

    Re: need some c++ help

    when you type checkwin it looks for a variable named checkwin. when you type checkwin() it looks for a function named checkwin(). you have checkwin(); so you need to type checkwin(); which will return 1.

    it's a common mistake i make myself to forget to add the () at the end. basically, replace it with:
    while (checkwin() != 1)

    and it'll work.

  5. #5
    Join Date
    Jan 2012
    Beans
    160

    Re: need some c++ help

    Thanks

  6. #6
    Join Date
    Jan 2012
    Beans
    160

    Re: need some c++ help

    Ok, so ive modded my code to read this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame();
    void taketurn (int x);
    int checkwin ();
    
    int z;
    int player1 = 1;
    int player2 = 2;
    int slot_filled;
    
    int a, b, c, d, e, f, g, h, i;
    
    enum slotfill 
    {
        X , O, U
    };
    
    int main()
    {
        cout << "  ------------------\n ";
        cout << " Welcome to tic tac toe\n";
        cout << " -------------------\n";
        
        cout << " Menu selection.\n1. Play\n 2. Exit\n Your choice\n?";
        cin >> z ;
    
        switch (z)
        {
            case 1:
            playgame();
            break;
            
            case 2: 
            return 0;
            
            default : 
            return 0;
        }
        
            
    
    }    
        
    void playgame()
    {
        do{
            
        taketurn(player1);
        checkwin();
        taketurn(player2);
        checkwin();
    } while (checkwin() != 1);
    }
    
    void taketurn (int x)
    {
        cout << "which slot would you like to fill?" << endl;
        cin >> slot_filled;
        
        switch (x)
        {
            case 1: 
            slot_filled = 1;
            break;
            
            case 2:
            slot_filled = 2;
            break;
            
        }
        
    }
    
    int checkwin ()
    {
    if ( a == 1 && b == 1 && c == 1 || d == 1 && e == 1 && f == 1 || g == 1 && h == 1 && i == 1)
    {
        cout << " Player 1 wins!!!! ";
        return 1;
    }
    
    if ( a == 1 && d== 1 && g == 1 || b == 1 && e == 1 && h == 1 || c == 1 && f == 1 && i == 1)
    {
        cout << " Player 1 wins!!!! ";
        return 1;
    }
    
    if ( a == 1 && e == 1 && i == 1 || c == 1 && e == 1 && g == 1)
    {
        cout << " Player 1 wins!!!";
        return 1;
    }
    // break for reading between player1 and player 2
    
    if ( a == 2 && b == 2 && c == 2 || d == 2 && e == 2 && f == 2 || g == 2 && h == 2 && i == 2)
    {
        cout << " Player 2 wins!!!! ";
        return 1;
    }
    
    if ( a == 2 && d== 2 && g == 2 || b == 2 && e == 2 && h == 2 || c == 2 && f == 2 && i == 2)
    {
        cout << " Player 2 wins!!!! ";
        return 1;
    }
    
    if ( a == 2 && e == 2 && i == 2 || c == 2 && e == 2 && g == 2)
    {
        cout << " Player 2 wins!!!";
        return 1;
    }
    }
    and all im getting is the initial question " Which slot would you like to fill?

    When i input a slot, the question goes on as an endless loop. What have i done wrong?

  7. #7
    Join Date
    Feb 2009
    Beans
    86

    Re: need some c++ help

    I don't get an endless loop; I get a Player 1 winsWhat slot would you like to fill? prompt

    Mostly because: int a~i are not inited; but on my system started off at 0. this means the board already has player 1 as the winner. I suggest either initializing your a~i variables as some value such as... -1. or 3. or 9. a value that means clear. The way I'd go is probably make 1 = player 1, and 2 = player 2, rather than 0 = player 1, 1 = player 2.

  8. #8
    Join Date
    Jul 2007
    Location
    Magic City of the Plains
    Beans
    Hidden!
    Distro
    Xubuntu 14.10 Utopic Unicorn

  9. #9
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: need some c++ help

    OK, some suggestions for getting out of the mire ...

    a. Run indent on your program so that you can see the structure. Or indent it consistently by hand.

    b. Look at the taketurn() function. It doesn't really do anything. I think you intend it to fill one of the variables a .. i with a player token, but it doesn't. It just overwrites slot_filled with 1 and 2 alternately. This is the endless loop, after pressing a key for a slot to fill.

    c. Try adding some debug output in taketurn() and playgame().

    d. Consider a more structured way of modelling the board so that you can check rows and columns in loops rather than just brute forcing everything through if statements, e.g. have a board which is a 3x3 array of char -- space means no token, then use X and O for player tokens.

    Code:
    // Check for winning line on each row
    for (int row = 0; row < 3; row++) {
        if (board[row][0] == board[row][1] && ... etc, etc) {
        }
    }
    e. Make use of true/false for clarity when dealing with booleans. Use clearer values for the players, rather then declaring player1 = 1 and then testing for something == 1.

    f. Consider having some test code that populates the board so that you can test your functions that check for winning lines.

    g. Start with an AI that just picks a random square -- but you need to know it's an empty square and when the board is full.

    h. Draw a visual representation of the board at each move so that you can see what is going on.

  10. #10
    Join Date
    Mar 2010
    Location
    Metro-ATL; PM free zone.
    Beans
    Hidden!
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: need some c++ help

    I was a professional C/C++ developer for over a decade on systems that could not fail - EVER. We had to handle any software issue that could possibly happen - even from a flipped bit due to cosmic ray interference.

    This is C code run thru a C++ compiler. It is not C++ code. I did that myself when I was starting out and didn't understand OO. Sure, a C++ compiler is required to compile it, but without classes ... er ... nope, not C++.

    A few tips that will save you hundreds of hours looking for errors that have nothing to do with why the program doesn't behave the way you like:

    1. Initialize all variables to known values - all of them. PERIOD. int z=0;
    2. Avoid the use of global variables. A variable's scope should be no more than what is required.
    3. Use const always for variables that should never change - ALWAYS.
    4. Use static for any functions that do not need to access class instance variables.
    5. Use variable names that mean something. Longer is better than shorter unless there is an extremely good reason. In 20 yrs of programming, I've only used short variables in production code once and it was for complex Boolean logic that was manually reduced through a Karnaugh map https://en.wikipedia.org/wiki/Karnaugh_map.
    6. On comparisons, avoid using numbers, unless it is absolutely clear what the number means. Use True/False when that is the real intent, not 0 and non-zero.
    7. On comparisons, always place the static/const part on the left hand side. This will catch common errors at compile time. Accidental assignment, for example.
    8. Do not trust operator precedence. Use parentheses if there is any doubt by you OR could be any doubt by someone else.
    9. Use whitespace and indentation to make your code more readable. Find a style that you and your team like, setup indent++ to force that style and stick to it.
    10. Always use {} for 1 statement ifs. This common mistake is easily prevented due to 1 statement runs after an if without {}.
    11. Always use a version control system. ALWAYS. Even when working on a project by yourself I've been saved many, many times thanks to version control.
    12. Write Unit tests which verify each function for expected results AND boundary condition failures.
    13. No function should be larger than fits onto 1 computer screen. Really, anything over 20 lines is getting too long.
    14. Verify input data into every class and never, ever trust user input to be anything other than funky binary data trying to break your code.
    15. On larger projects, group the code into libraries that make sense and avoid circular dependencies. Don't get me started on this. I've had to clean up too many project where all the files were dumped into a single directory and statically linked into a massive program. Using libs and shared libraries can make code much quicker to compile and link, but it helps with reuse later and across multiple projects.

    Unrelated, but use as few I/O statements as possible. More couts mean the program runs slower. A multi-line cout doesn't need multiple "cout" statements - just use the newline \n where you want it.


    You don't have any pointers in the code, but if you did, I'd suggest pointer safety methods like setting to NULL before allocation, checking that the allocation succeeds and resetting back to NULL after the memory is delete/free'd. Trying to find where a wild pointer got used is very hard, but if the pointer is reset back to NULL, at the next attempted use, the program should crash immediately. That will tell you exactly where the problem lies.



    Most of these tips will let the compiler find your programming mistakes, thus preventing them from happening at run-time.


    Anyway, I hope this helps in some small way. Take what is useful for you today and use it.

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •