Thread: some help needed :/

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    some help needed :/

    This little game almost works good , but i dont know how to make it visually better and another problem is my program doesn't show who wins, the game never ends:/

    Hope for your help
    This is my code so far:
    Code:
    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    bool rez(char koord[3][3])
    {
    for(int i=0;i<3;i++)
    {
    if (koord[i][0]==koord[i][1] && koord[i][1]==koord[i][2] && (koord[i][0]=='X' || koord[i][0]=='O')) return true;
    else return false;
    if (koord[0][i]==koord[1][i] && koord[1][i]==koord[2][i] && (koord[i][0]=='X' || koord[i][0]=='O')) return true;
    else return false;
    if (koord[0][0]==koord[1][1] && koord[1][1]==koord[2][2] && (koord[1][1]=='X' || koord[1][1]=='O')) return true;
    else return false;
    }
    }
    
    void print (char koord[3][3])
    {
    for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
    {
    cout<<koord[i][j];
    }
    cout<<endl;
    }
    }
    
    
    
    int main()
    {
    char koord[3][3]={{0,0}};
    char simb,user,pc;
    int rin,kon,pcrin,pckon;
    
    
    srand(time(NULL));
    cout<<"Please choose the simbol - X vai O!"<<endl;
    cin>>simb;
    if (simb=='O')
    {
        user='O';
        pc='X';
    }
    else
    {
        user='X';
        pc='O';
    }
    do
    {
    cout<<"Choose the row and column!"<<endl;
    cin>>rin;
    cin>>kon;
    kon--;
    rin--;
    if ((rin>=3 || rin<0) || (kon>=3 || kon<0)) cout<<"You made a mistake when entered row/column!\n";
    
    pcrin=rand()%3;
    pckon=rand()%3;
    
    if (koord[rin][kon]!=pc) koord[rin][kon]=user;
    
    if  (koord[pcrin][pckon]!=user) koord[pcrin][pckon]=pc;
    
    print(koord);
    
    if (rez(koord)==true)
    {
        cout<<"Game over!";
        break;
    }
    
    }
    while(koord[3][3]!='X');
    
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Keep on top of the indentation -> SourceForge.net: Indentation - cpwiki

    > while(koord[3][3]!='X');
    What is this?
    It's an array overrun, there is no [3][3] element of your array.
    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.

  3. #3
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    1.ok i will check that.

    i dont get it, didn't i inicialized array like this?:
    Code:
    char koord[3][3]={{0,0}};

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Arrays in C/C++ start at zero and stop at size - 1. So with an array of array[3] the valid indexes are 0,1,2 but not 3.

    Jim

  5. #5
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    How I could not imagine that.thank u both!

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I'd also suggest making a game board with the coords nearby otherwise it will be hard for the user to figure what is it all about :P

  7. #7
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Sounds good, i am not sure how to do that.any suggestions?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Presumably, the game is over when
    - there is a line of 'X' or 'O'
    - 9 moves have been made.
    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.

  9. #9
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    it might be too hard to change, but i have another question.-How to determine who will start to play first by random number generator?

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Use srand(time(NULL)) to seed the PRNG with the current time (so the numbers will be different each time you run the program). Then call rand() to generate a random number. Finally, find a way to make this random number either 0 or 1 (look into the modulos operator, or come up with your own algorithm, modulos introduces a slight bias), then you can pick a starting player based upon this value.

    Also, use Google more, this question has been asked and answered each and every day since the beginning of time. Google is your best friend, if you need to solve a common problem but forgot how? Google. If you can't remember the exact syntax for some library you're using? Google. If the compiler is giving you incomprehensible errors? Google.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  11. #11
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    This could be the way:
    Code:
    rand() % 2;
    but i dont know how to mix it with this program.:/

  12. #12
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Well now you have either 0 or 1, randomly chosen each time the program runs. And you want either 1 or the other player to start. So obviously you should map 0 and 1 to your 2 players and make the chosen one start.

    At the moment both players take a turn for each iteration of your loop. I would redesign the program so that either one or the other (Depending on who's turn it is) takes their turn, during the loop, then on the next iteration the other one goes, and so forth. Also if you split your program into smaller functions the code becomes easier to read and write. The turn system could then work something like this:

    Code:
    if(CurrentTurn == "Player")
    {
        PlayerTurn();
        CurrentTurn = "Computer";
    }
    else
    {
        ComputerTurn();
        CurrentTurn = "Player";
    }
    With something like the above code, inside your main game loop, it would be trivial to make either one or the other player start off, just set CurrentTurn to whoever is starting, before entering the loop. Also, having one turn per iteration makes more sense i think, but you'll have to redesign it yourself.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  13. #13
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Thanks for ur explanation but i dont get it really i tried smth like
    Code:
    currentTurn = rand() % 2;
        if(1 == "user")
    {
        PlayerTurn();
        0 = "pc";
    }
    else
    {
        ComputerTurn();
        1 = "user";
    }

  14. #14
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Well first of all, as i said, my example needs to be placed in a loop. Second of all, it was just an example, you cannot just copy paste it and expect it to work. You have to declare the variables and implement the functions. Did you redesign your loop to only handle 1 turn per iteration?

    There will be no handouts, not from me atleast. Giving you the code doesn't help and you'll be back in a day or two with similar questions, and before i know it, i'll be working full time for you as a programmer. Show some effort, then i'll help.

    Also, what did you intend with this line:
    Code:
    0 = "pc";
    Did you write the original program yourself? Because if you are making mistakes like this it seems you need to take a step back and learn some more basic stuff before attempting this game.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  15. #15
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    No, i didnt write by myself thats why i dont know how to change this. If it wouldnt be my homework i wouldnt do anything so advance for myself at the moment. I am reading Alans book and trying to understand and learn as much as i can, but this is too hard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed please.
    By TaktaK in forum C++ Programming
    Replies: 7
    Last Post: 10-04-2005, 09:32 PM
  2. Help Needed !!
    By vodlum in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2005, 02:33 PM
  3. Help needed.
    By hyrule in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2005, 09:51 AM