Followers 0

# more tic tac toe

## 31 posts in this topic

[quote name='alvaro' timestamp='1350915126' post='4992774']
No, that's not how it works. Array indices do start at zero, so in an array of size 8, the last valid entry has index 7.
[/quote]

When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
0

##### Share on other sites
[quote name='littletray26' timestamp='1350965907' post='4992977']
When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
[/quote]
That's because memory is allocated in pages and you got lucky with the last element having been allocated as a side effect, especially in debug mode, or some other reason. But it is technically undefined behaviour and using it is a big no-no, as it is (quite literally) not supposed to work.

It's like saying "I need 9 elements in my array but I'm only going to allocate 8 because I like taking risks and playing with factors which are out of my control." [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

An array[n] only has valid indices between 0 inclusive and n [color=#ff0000][b]exclusive[/b][/color]. Edited by Bacterius
2

##### Share on other sites
[quote name='Bacterius' timestamp='1350966220' post='4992978']
[quote name='littletray26' timestamp='1350965907' post='4992977']
When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
[/quote]
That's because memory is allocated in pages and you got lucky with the last element having been allocated as a side effect, especially in debug mode, or some other reason. But it is technically undefined behaviour and using it is a big no-no, as it is (quite literally) not supposed to work.

It's like saying "I need 9 elements in my array but I'm only going to allocate 8 because I like taking risks and playing with factors which are out of my control." [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

An array[n] only has valid indices between 0 inclusive and n [color=#ff0000][b]exclusive[/b][/color].
[/quote]

Well you learn something new everyday I'm not sure why this wasn't mentioned in my C++ book but thank you
0

##### Share on other sites
[quote name='littletray26' timestamp='1350971976' post='4992994']
[quote name='Bacterius' timestamp='1350966220' post='4992978']
[quote name='littletray26' timestamp='1350965907' post='4992977']
When I made my TicTacToe game my board was an array[8] and I never had a problem with using all 9 spaces in it, so I'm not sure what you mean...
I'm confused
[/quote]
That's because memory is allocated in pages and you got lucky with the last element having been allocated as a side effect, especially in debug mode, or some other reason. But it is technically undefined behaviour and using it is a big no-no, as it is (quite literally) not supposed to work.

It's like saying "I need 9 elements in my array but I'm only going to allocate 8 because I like taking risks and playing with factors which are out of my control." [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

An array[n] only has valid indices between 0 inclusive and n [color=#ff0000][b]exclusive[/b][/color].
[/quote]

Well you learn something new everyday [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] I'm not sure why this wasn't mentioned in my C++ book but thank you [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

Get a better C++ book then, because whatever one you have probably should just be burned.

It should also be noted that the valid range of pointers to an array A with a size n is A to A + n [color=#ff0000]inclusive[/color]. The main thing is that the dereferencable range of A is A to A + n [color=#ff0000]exclusive[/color].
1

##### Share on other sites
[quote name='Washu' timestamp='1350972358' post='4992996']
Get a better C++ book then, because whatever one you have probably should just be burned.
[/quote]

Beginning C++ Game Programming by Michael Dawson. Burn away
0

##### Share on other sites
[quote name='phil67rpg' timestamp='1350866639' post='4992635']
I have tried fisherYates shuffle but it still does not work.here is my code
void Computer::move_player_O()
{
srand(time(NULL));
const int length=10;
int array[length];
for(int t=1;t<=9;t++)
array[t]=t;
fisherYates(array,length);
player_O=array[t];
t++;
}
void Computer::check_player_O()
{
board[player_O]='O';
}
[/quote]

int array[length]; this will not work try

int *array=new int[length];

and after you have done working

delete[] array;
-1

##### Share on other sites
[quote name='giobs111' timestamp='1350979495' post='4993021']
[quote name='phil67rpg' timestamp='1350866639' post='4992635']
I have tried fisherYates shuffle but it still does not work.here is my code
[...]
const int length=10;
int array[length];
[...]
[/quote]

int array[length]; this will not work try[/quote]

Why wouldn't it work? length' is a compile-time constant and using it as an array length is perfectly kosher.

[quote]int *array=new int[length];

and after you have done working

delete[] array;
[/quote]
You only need to do that if length' is not known at compile time. Even in that case, some compilers (e.g., gcc) do support [url="http://en.wikipedia.org/wiki/Variable-length_array"]variable-length arrays[/url]. VLAs are part of the C99 standard and they almost made it to the C++11 standard.
0

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account