-
November 30th, 2009, 01:30 PM
#1
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.
-
November 30th, 2009, 01:51 PM
#2
Re: Ned to optimize the code
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.
-
November 30th, 2009, 02:02 PM
#3
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.
-
November 30th, 2009, 02:13 PM
#4
Re: Ned to optimize the code
Originally Posted by laserlight
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
-
November 30th, 2009, 02:21 PM
#5
Re: Ned to optimize the code
Originally Posted by treuss
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?
-
November 30th, 2009, 02:26 PM
#6
Re: Ned to optimize the code
Originally Posted by Mariusmssj
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
-
November 30th, 2009, 02:37 PM
#7
Re: Ned to optimize the code
Originally Posted by GCDEF
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 ^_^
-
November 30th, 2009, 02:49 PM
#8
Re: Ned to optimize the code
Originally Posted by Mariusmssj
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.
-
November 30th, 2009, 02:52 PM
#9
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.
-
November 30th, 2009, 02:56 PM
#10
Re: Ned to optimize the code
Originally Posted by Mariusmssj
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
-
November 30th, 2009, 03:19 PM
#11
Re: Ned to optimize the code
Originally Posted by GCDEF
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"
Originally Posted by Paul McKenzie
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++?
-
December 1st, 2009, 05:46 AM
#12
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
-
December 1st, 2009, 05:53 AM
#13
Re: Ned to optimize the code
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?
-
December 1st, 2009, 06:16 AM
#14
Re: Ned to optimize the code
Originally Posted by laserlight
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.
-
December 1st, 2009, 08:06 AM
#15
Re: Ned to optimize the code
If he's really learning C, bool shouldn't even compile.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|