CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Ned to optimize the code

    simple xox game but i kinda got a problem, that if statement works but it's too long, any ideas how to change into something better? and is there any better way to say the user won without having that "break;"?

    thanks for any help =)

    Code:
    #include <iostream>
    #include <string>
    
    void main()
    {
    char grid[3][3] = {'.','.','.','.','.','.','.','.','.'};
    int row = 0;
    int col = 0;
    char input;
    std::string msg = "";
    
    
    std::cout<< "  0 1 2"<<"\n"<<"0 . . ."<<"\n"<<"1 . . ."<<"\n"<<"2 . . ."<<std::endl; 
    
    for(int k=0;k<9;k++)
    {
    	std::cout<< "Enter row: ";
    	std::cin>> row;
    	std::cout<< "Enter collum: ";
    	std::cin>> col;
    	std::cout<< "Enter X or O: ";
    	std::cin>> input;
    	grid[row][col]= input;
    
    for(int i=0;i<3;i++)
    {
    	for(int j=0;j<3;j++)
    	{
    		std::cout<< grid[i][j];
    	}
    	std::cout<< "\n";
    }
    if(((((((((grid[0][0]=='x')&&(grid[0][1]=='x')&&(grid[0][2]=='x')||
    (grid[1][0]=='x')&&(grid[1][1] == 'x')&&(grid[1][2]=='x')||(grid[2][0]=='x')&&
    (grid[2][1]=='x')&&(grid[1][2]=='x')))))))))
    {
    	msg = "you won";
    	break;
    }
    else
    {
    	msg = "You lost";
    }
    }
    std::cout<< msg;
    }
    the output
    Last edited by Mariusmssj; November 30th, 2009 at 01:34 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Ned to optimize the code

    Quote Originally Posted by Mariusmssj
    simple xox game but i kinda got a problem, that if statement works but it's too long, any ideas how to change into something better?
    Move the expression into a function, then call that function in the if statement. It is not an optimisation, but a refactoring of code to make it more readable.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Ned to optimize the code

    You can store the position of all X's in the lower 9 bits of an int (or a short if you want) by assigning each field a bit from 0-8. Likewise you can store the position of all O's in another int.

    Using the same encoding, each three-in-a-row can be expressed as an int value, e.g. the value 7 represents the field 0, 1 and 2, 273 the fields 0, 4 and 8. Iterate over the list of all these values, and check for X's and O's if all bits of the value are set (use binary &). If they are, then the X's or O's have won.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Ned to optimize the code

    Quote Originally Posted by laserlight View Post
    Move the expression into a function, then call that function in the if statement. It is not an optimisation, but a refactoring of code to make it more readable.
    i got no idea about functions xD

  5. #5
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Ned to optimize the code

    Quote Originally Posted by treuss View Post
    You can store the position of all X's in the lower 9 bits of an int (or a short if you want) by assigning each field a bit from 0-8. Likewise you can store the position of all O's in another int.

    Using the same encoding, each three-in-a-row can be expressed as an int value, e.g. the value 7 represents the field 0, 1 and 2, 273 the fields 0, 4 and 8. Iterate over the list of all these values, and check for X's and O's if all bits of the value are set (use binary &). If they are, then the X's or O's have won.

    9 lower bits on an int?

    so i would have int 1; and it would have some numbers which would be the directions on my 3 by 3 array grid?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Ned to optimize the code

    Quote Originally Posted by Mariusmssj View Post
    i got no idea about functions xD
    Now's a good time to learn. You can't program without them.

    A nested if statement may be easier to read.
    Code:
    if(a)
       if(b)
            if(c)
    //..etc

  7. #7
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Ned to optimize the code

    Quote Originally Posted by GCDEF View Post
    Now's a good time to learn. You can't program without them.

    A nested if statement may be easier to read.
    Code:
    if(a)
       if(b)
            if(c)
    //..etc
    well i can't use functions because i will get marked down since the teacher haven't toughs us functions.

    i read more about the nested if statements thank you ^_^

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Ned to optimize the code

    Quote Originally Posted by Mariusmssj View Post
    9 lower bits on an int?

    so i would have int 1; and it would have some numbers which would be the directions on my 3 by 3 array grid?
    Number the board like this:
    Code:
               
      0      1      2
    -----|------|-----
      3  |   4  |  5
    -----|------|-----
      6      7      8
    Each position on the board represents a bit in an integer. So if you have X's in 0,1,2, then bits 0, 1, and 2 of the integer are "on" (set to 1). The bits are numbered from right to left most, where bit 0 is the rightmost bit

    000000111 == top row has all X's == 7

    So 7 is a winning set of bits.

    Another one is 0, 4, and 8 (the diagonal).

    0100010001 = 273

    Do the same with the other combinations, and you now have a list of integers that represent winning combinations.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 30th, 2009 at 02:57 PM.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Ned to optimize the code

    Another way to improve readability would be to set values of several bools and just writ them in your if statement.
    Code:
    bool bGrid0 = grid[0][0] == 'x' && grid[0][1] == 'x';
    bool bGrid1 = grid[1][0] == 'x' && grid[1][1] == 'x';
    //etc.
    //then
    
    if(bGrid0 || bGrid1 || bGrid2)
    {
    //do stuff
    }
    Get in the habit of using white space too. Not sure why beginners are so reluctant to put spaces between variable and operators.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Ned to optimize the code

    Quote Originally Posted by Mariusmssj View Post
    well i can't use functions because i will get marked down since the teacher haven't toughs us functions.
    You asked how to "optimize" the code, and you can't really start optimizing until you learn to write functions to easily see what you should optimize.

    Also, you have "void main()" instead of "int main()". The main() function returns an int, not void. So either your teacher is not teaching you C++ correctly and/or you're using books that are obsolete/out of date/wrong when it comes to teaching proper C++.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Nov 2009
    Location
    UK
    Posts
    166

    Re: Ned to optimize the code

    Quote Originally Posted by GCDEF View Post
    Another way to improve readability would be to set values of several bools and just writ them in your if statement.
    Code:
    bool bGrid0 = grid[0][0] == 'x' && grid[0][1] == 'x';
    bool bGrid1 = grid[1][0] == 'x' && grid[1][1] == 'x';
    //etc.
    //then
    
    if(bGrid0 || bGrid1 || bGrid2)
    {
    //do stuff
    }
    Get in the habit of using white space too. Not sure why beginners are so reluctant to put spaces between variable and operators.
    OMG it worked, THANKS i will need to read in more about the "bool"

    Quote Originally Posted by Paul McKenzie View Post
    You asked how to "optimize" the code, and you can't really start optimizing until you learn to write functions to easily see what you should optimize.

    Also, you have "void main()" instead of "int main()". The main() function returns an int, not void. So either your teacher is not teaching you C++ correctly and/or you're using books that are obsolete/out of date/wrong when it comes to teaching proper C++.

    Regards,

    Paul McKenzie
    well he said he will teach us c then will advance further he wants us to know all the history not sure why though :S

    but thank you for your help ^_^ what up to date websites could i use to learn c++?

  12. #12
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Ned to optimize the code

    This may help.

    http://www.learncpp.com/

    well i can't use functions because i will get marked down since the teacher haven't taught us functions.
    It make me sad to see students being marked down for showing some initiative.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Ned to optimize the code

    Quote Originally Posted by Mariusmssj
    OMG it worked, THANKS i will need to read in more about the "bool"
    hmm... won't you get marked down for using bool since your teacher has not taught you about that?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Ned to optimize the code

    Quote Originally Posted by laserlight View Post
    hmm... won't you get marked down for using bool since your teacher has not taught you about that?
    I know, it can get really ridiculous as to what a teacher says can or can't be used. When I was a C++ teacher, I didn't care how a student solved the problem, as long as they can explain to me how they did it (even if what they did wasn't taught yet). If there is a general "you can't use this" construct, then it better be well-defined as to what can't be used.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 1st, 2009 at 06:20 AM.

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Ned to optimize the code

    If he's really learning C, bool shouldn't even compile.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured