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.
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.
Re: Ned to optimize the code
Quote:
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
Re: Ned to optimize the code
Quote:
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?
Re: Ned to optimize the code
Quote:
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
Re: Ned to optimize the code
Quote:
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 ^_^
Re: Ned to optimize the code
Quote:
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
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.
Re: Ned to optimize the code
Quote:
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
Re: Ned to optimize the code
Quote:
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"
Quote:
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++?
Re: Ned to optimize the code
This may help.
http://www.learncpp.com/
Quote:
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.
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?
Re: Ned to optimize the code
Quote:
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
Re: Ned to optimize the code
If he's really learning C, bool shouldn't even compile.
Re: Ned to optimize the code
Quote:
Originally Posted by
JohnW@Wessex
thanks i just bookmarked it
well i am still going to learn about the functions before he actually will teach us it.
Quote:
Originally Posted by
laserlight
hmm... won't you get marked down for using bool since your teacher has not taught you about that?
that is a really good point
Quote:
Originally Posted by
Paul McKenzie
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
i will have to ask him that tomorrow, it;s really stupid of how it's being done
Quote:
Originally Posted by
GCDEF
If he's really learning C, bool shouldn't even compile.
but it did xD and it works just fine
and i just want say thanks for all of you, and all the help you gave me ^_^
Re: Ned to optimize the code
Quote:
Originally Posted by
GCDEF
If he's really learning C, bool shouldn't even compile.
It would compile if the compiler supports C99 and <stdbool.h> is #included
Kurt