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;"?
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
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
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.
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?
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 01:57 PM.
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++.
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++?
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
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 05:20 AM.
Bookmarks