Just a problem

This is a discussion on Just a problem within the C++ Programming forums, part of the General Programming Boards category; So I know this is a lot but if any one could help me find the pvp problem with this?? ...

  1. #1
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    Question Just a problem

    So I know this is a lot but if any one could help me find the pvp problem with this??


    Code:
    #include <iostream>
    
    char sqr1='1';
    char sqr2='2';
    char sqr3='3';
    char sqr4='4';
    char sqr5='5';
    char sqr6='6';
    char sqr7='7';
    char sqr8='8';
    char sqr9='9';
    int variable;
    int player1;
    int player2;
    int check;
    
    int player1switch ();
    int player2switch ();
    void clrscrn();
    int checker();
    int AI();
    int wincomp();
    
    int board ()
    {
     using namespace std;
    
     cout<<sqr1<<" | "<<sqr2<<" | "<<sqr3<<endl;
     cout<<"---------\n";
     cout<<sqr4<<" | "<<sqr5<<" | "<<sqr6<<endl;
     cout<<"---------\n";
     cout<<sqr7<<" | "<<sqr8<<" | "<<sqr9<<endl;
     return 0;
    }
    
    
    
    
    int main(){
    
    int player;
    
    using namespace std;
    do  {
       cout<<"Agaist Computer(1) or People(2)?";
       cin>>player;
    }while (player!=1 && player!=2);
    
    
    while (1){
          board();
          check=1;
          while (check==1){
    
                cout<<"Player 1 enter your square number: \n\n\n\n\n\n\n\n\n\n\n\n";
                cin>>player1;
                check=checker();
                }
          player1switch();
          clrscrn();
          switch (player){
    
          case 1: AI(); break;
          case 2: {
               board();
               check=1;
               while (check==1){
                     board();
                     cout<<"Player 2 enter your square number: \n\n\n\n\n\n\n\n\n\n\n\n";
                     cin>>player2;
                     check=checker();
                     }
               player2switch();
               clrscrn();
               };
          break;
          }}
    cin>>variable;
    return 0;
    }
    
    
    
    
    
    
    int player1switch (){
    switch (player1)
      {	   
       case 1: sqr1='X';
            break;
       case 2: sqr2='X';
            break;
       case 3: sqr3='X';
            break;
       case 4: sqr4='X';
            break;
       case 5: sqr5='X';
            break;
       case 6: sqr6='X';
            break;
       case 7: sqr7='X';
            break;
       case 8: sqr8='X';
            break;
       case 9: sqr9='X';
            break;
      }
     return 0;
    }
    
    
    int player2switch (){
    switch (player2)
      {	   
       case 1: sqr1='O';
            break;
       case 2: sqr2='O';
            break;
       case 3: sqr3='O';
            break;
       case 4: sqr4='O';
            break;
       case 5: sqr5='O';
            break;
       case 6: sqr6='O';
            break;
       case 7: sqr7='O';
            break;
       case 8: sqr8='O';
            break;
       case 9: sqr9='O';
            break;
      }
     return 0;
    }
    
    
    void clrscrn(){
    std::cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"<<endl;
    }
    
    
    int checker() {
    using namespace std;
        if (player1==1 || player2==1) {
           if (sqr1=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr1=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }else
        if (player1==2 || player2==2){
           if (sqr2=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr2=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }else
        if (player1==3 || player2==3){
           if (sqr3=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr3=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }else
        if (player1==4 || player2==4){
           if (sqr4=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr4=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }else
        if (player1==5 || player2==5){
           if (sqr5=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr5=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }else
        if (player1==6 || player2==6){
           if (sqr6=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr6=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }else
        if (player1==7 || player2==7){
           if (sqr7=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr7=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }else
        if (player1==8 || player2==8){
           if (sqr8=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr8=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           } else
        if (player1==9 || player2==9){
           if (sqr9=='X'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           if (sqr9=='O'){
              cout<<"Sorry, taken.";
              return 1;}
           else
           return 0;
           }
        }
    
    int AI() {
    
    if (sqr1=='X' && sqr2=='X' && sqr3=='3') {
       sqr3='O';return 0;}
    else
    if (sqr1=='X' && sqr2=='2' && sqr3=='X') {
       sqr2='O';return 0;}
    else
    if (sqr1=='1' && sqr2=='X' && sqr3=='X') {
       sqr1='O';return 0;}
    else
    if (sqr4=='X' && sqr5=='5' && sqr6=='X') {
       sqr5='O';return 0;}
    else
    if (sqr4=='4' && sqr5=='X' && sqr6=='X') {
       sqr4='O';return 0;}
    else
    if (sqr4=='X' && sqr5=='X' && sqr6=='6') {
       sqr6='O';return 0;}
    else
    if (sqr7=='X' && sqr8=='8' && sqr9=='X') {
       sqr8='O';return 0;}
    else
    if (sqr7=='7' && sqr8=='X' && sqr9=='X') {
       sqr7='O';return 0;}
    else
    if (sqr7=='X' && sqr8=='X' && sqr9=='9') {
       sqr9='O';return 0;}
    else
    if (sqr1=='X' && sqr4=='4' && sqr7=='X') {
       sqr4='O';return 0;}
    else
    if (sqr1=='1' && sqr4=='X' && sqr7=='X') {
       sqr1='O';return 0;}
    else
    if (sqr1=='X' && sqr4=='X' && sqr7=='7') {
       sqr7='O';return 0;}
    else
    if (sqr2=='X' && sqr5=='5' && sqr8=='X') {
       sqr5='O';return 0;}
    else
    if (sqr2=='2' && sqr5=='X' && sqr8=='X') {
       sqr2='O';return 0;}
    else
    if (sqr2=='X' && sqr5=='X' && sqr8=='8') {
       sqr8='O';return 0;}
    else
    if (sqr3=='X' && sqr6=='6' && sqr9=='X') {
       sqr6='O';return 0;}
    else
    if (sqr3=='3' && sqr6=='X' && sqr9=='X') {
       sqr3='O';return 0;}
    else
    if (sqr3=='X' && sqr6=='X' && sqr9=='9') {
       sqr9='O';return 0;}
    else
    if (sqr1=='X' && sqr5=='5' && sqr9=='X') {
       sqr5='O';return 0;}
    else
    if (sqr1=='1' && sqr5=='X' && sqr9=='X') {
       sqr1='O';return 0;}
    else
    if (sqr1=='X' && sqr5=='X' && sqr9=='9') {
       sqr9='O';return 0;}
    else
    if (sqr7=='X' && sqr5=='5' && sqr3=='X') {
       sqr5='O';return 0;}
    else
    if (sqr7=='7' && sqr5=='X' && sqr3=='X') {
       sqr7='O';return 0;}
    else
    if (sqr7=='X' && sqr5=='X' && sqr3=='3') {
       sqr3='O';return 0;}
    else
    
    if (sqr5=='5') {
       sqr5='O';}
    else
       if (sqr1=='1') {
          sqr1='O';}
       else
          if (sqr9=='9') {
             sqr9='O';}
          else
             if (sqr3=='3') {
                sqr3='O';}
             else
                if (sqr2=='2') {
                   sqr2='O';}
                else
                   if (sqr6=='6') {
                      sqr6='O';}
                   else
                      if (sqr7=='7') {
                         sqr7='O';}
                      else
                         if (sqr8=='8') {
                            sqr8='O';}
                         else
                             if (sqr4=='4') {
                                sqr4='O';}
                             else
                                cout<<"";
    
    return 0;
    }
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    ok...so what is the problem...post the errors, or point to the code that is giving you trouble.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Oh my god, there has got to be a better way to do that.

    I suggest you rethink your design. Consider some loops and functions that could cut down the size of your code.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    Lightbulb reasons

    I know there is a better way to do it, I have thought of many but I was just fooling around and this kind of evolved. The code Im havin trouble with is the function checker. The If statements don't seem to cascade right but I cant figure out why. And sorry about not saying this sooner just was in a hurry to get to dinner.
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: reasons

    Originally posted by zergdeath1
    I know there is a better way to do it, I have thought of many but I was just fooling around and this kind of evolved. The code Im havin trouble with is the function checker. The If statements don't seem to cascade right but I cant figure out why. And sorry about not saying this sooner just was in a hurry to get to dinner.
    Then you should
    1) post the function checker() instead of everything
    2) tell us exactly what the problem is (cascading wrong? how?)
    3) tell us what it's supposed to do when it does work

    The function looks fine to me, bases on what I know it's supposed to do, which is if player 1 & 2 are the same, check a square for X or O.

    But what's it do if player 1 & 2 are not equal?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Just curious!

    Why do you have three of the using namespace std; directives inside a function? Why do you have three of them period? You should only need one and it should be after all your includes. I compiled and ran your program except it doesn't exit very well. When you finish the game it doesn't do anything just sits there. You need a condition for wining the game and for stalemate and for losing the game. Also if you select a block already taken it doesn't redisplay the tic tac toe board it just prints the message. The function checker() doesn't have a return statement, all the if else statements do but the function itself doesn't.
    Last edited by manofsteel972; 03-03-2004 at 03:48 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #7
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    Exclamation correction

    maybe im wrong but doesnt the Checker statement say that

    If player1 is equal to 1 OR player2 is equal to 1 then check.....
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Yes it does say that.

    You are referring to the selection made by the player. If player 1 selects the square number 1 by entering 1 on the keyboard then it checks to see if an X or an O is in the square if it is then it prints the message Sorry Already taken.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    Ok back to the Problem

    I cant seem to get the if statements in checker to work when ever I run this I hit 5 then 6 for player 2 and it always says that 6 is taken??
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The problem is with your statements that look like this:

    if (player1==5 || player2==5)

    When you call checker to check the player 2's choice, the value of player1 is still 5 from the previous play. The if above returns true even though you don't want to check player1's value.

    One solution, which would help your overall design, is instead of having a global variable, just pass the appropriate variable into the function. When you want to check player1's choice, pass in player1 to the checker function. When you want to check player2's choice, pass in player2 to the checker function. Then, inside the checker function, simply check the passed in value instead of both player1 and player2.

    I highly recommend learning how to use the debugger. Stepping through the program would have shown you that it is getting inside the if (player1==5 || player2==5) statement.

  11. #11
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    Talking No idea how

    How do you use the Debuger I am using Dev C++ and dont know how to use it? Thanks for you help!
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  12. #12
    Registered User zergdeath1's Avatar
    Join Date
    Sep 2003
    Posts
    39

    OHH and also.

    As suggested how can I tuen the game off and have it exit when it is over?
    Stolen Quote: Buttered Toast always lands butter side down and cats always land on their feet, what happens when you strap buttered Toast to the back of a cat?
    My Quote: Practice Makes Perfect Nobodys Perfect Why Practice?

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you can throw in a call to exit(); after you detect the game is over---not very elegant but breaks out of the unending loop, or you can change loop so it isn't unending by using a variable rather than a conditional which is constant. A variable used in this manner is commonly called a flag, because when you detect a desired state and change the flag it sets the course of the program in a different direction.
    Code:
    bool gameOver = false;
    
    while(!gameOver)
    {
        //within your code when you detect game is over change gameOver;
        gameOver = true;
    }
    if you detect that the game is over in a function called from within the game loop, then you need to either pass the flag by reference to the function where you detect the game is over (so the change to the flag in the function affects the value of the flag back in the loop) or have the function return an appropriate value to the flag as a return value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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