• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# more tic tac toe

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

31 replies to this topic

### #21Bacterius  Members

Posted 21 October 2012 - 07:14 PM

The code doesn't actually make sense. player_O=array[t]; specifically - once the array is shuffled, you want to pick each element one after the other to get a list of numbers which don't repeat. Obviously this means you need to store the shuffled array somewhere, and use it for all 9 numbers (or 10.. why is it 10?) because if you just generate a new array each time and pick one element from this array, well, you've basically done nothing.

Edited by Bacterius, 21 October 2012 - 07:14 PM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #22littletray26  Members

Posted 22 October 2012 - 07:50 AM

How can there be 9 spaces in an array of size 8? That makes no sense.

If we have for example
int banana[5];


There is in fact 6 spaces.

banana[0] = 1;
banana[1] = 2;
banana[2] = 3;
banana[3] = 4;
banana[4] = 5;
banana[5] = 6;


Arrays start off at zero, so in an 8 space array there is 9 spaces.
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

### #23littletray26  Members

Posted 22 October 2012 - 07:52 AM

Otherwise, you can change the < sign to <= .

This was the confusion. Sorry.
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

### #24Álvaro  Members

Posted 22 October 2012 - 08:12 AM

How can there be 9 spaces in an array of size 8? That makes no sense.

If we have for example
int banana[5];


There is in fact 6 spaces.

banana[0] = 1;
banana[1] = 2;
banana[2] = 3;
banana[3] = 4;
banana[4] = 5;
banana[5] = 6;


Arrays start off at zero, so in an 8 space array there is 9 spaces.

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.

### #25dAND3h  Members

Posted 22 October 2012 - 10:01 AM

How can there be 9 spaces in an array of size 8? That makes no sense.

If we have for example
int banana[5];


There is in fact 6 spaces.

banana[0] = 1;
banana[1] = 2;
banana[2] = 3;
banana[3] = 4;
banana[4] = 5;
banana[5] = 6;


Arrays start off at zero, so in an 8 space array there is 9 spaces.

See Alvaro's response. But please try to take advice onboard , or at least make sure you are correct before you end up giving wrong advice. I probably should have explained it when I said it. Alas, No harm done

Edited by dAND3h, 22 October 2012 - 10:03 AM.

### #26littletray26  Members

Posted 22 October 2012 - 10:18 PM

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.

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
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

### #27Bacterius  Members

Posted 22 October 2012 - 10:23 PM

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

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."

An array[n] only has valid indices between 0 inclusive and n exclusive.

Edited by Bacterius, 22 October 2012 - 10:39 PM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #28littletray26  Members

Posted 22 October 2012 - 11:59 PM

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

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."

An array[n] only has valid indices between 0 inclusive and n exclusive.

Well you learn something new everyday I'm not sure why this wasn't mentioned in my C++ book but thank you
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

### #29Washu  Senior Moderators

Posted 23 October 2012 - 12:05 AM

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

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."

An array[n] only has valid indices between 0 inclusive and n exclusive.

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

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 inclusive. The main thing is that the dereferencable range of A is A to A + n exclusive.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX

### #30littletray26  Members

Posted 23 October 2012 - 12:16 AM

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

Beginning C++ Game Programming by Michael Dawson. Burn away
The majority of Internet Explorer users don't understand the concept of a browsing application, or that there are options.
They just see the big blue 'e' and think "Internet". The thought process usually does not get much deeper than that.

Worms are the weirdest and nicest creatures, and will one day prove themselves to the world.

I love the word Clicky

### #31giobs111  Members

Posted 23 October 2012 - 02:04 AM

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';
}

int array[length]; this will not work try

int *array=new int[length];

and after you have done working

delete[] array;

### #32Álvaro  Members

Posted 23 October 2012 - 03:49 AM

I have tried fisherYates shuffle but it still does not work.here is my code
[...]
const int length=10;
int array[length];
[...]

int array[length]; this will not work try

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

int *array=new int[length];

and after you have done working

delete[] array;

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 variable-length arrays. VLAs are part of the C99 standard and they almost made it to the C++11 standard.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.