Jump to content
  • Advertisement
Sign in to follow this  
PennstateLion

tic tac toe with double scripted array query

This topic is 4864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to do the exercise in my C++ book by Deitel. The exercise is to create a tic tac toe class and make is a functional game, this is fine. Question is, he asks you to create a double scripted array and init it to all zeroes.. Then he wants the player to choose the pos of their X or Y and have us update the array.. If the double scripted array is set to all Zeroes, how would I know what part of the array to update, i have no way of searching the array or anything... any insight is appreciated, thanks

Share this post


Link to post
Share on other sites
Advertisement
You have the X and Y position to update, that should be enough to find the position in your array. There you will just drop a mark for the active player, if the field is still 0, otherwise you should ask the active player for a new position...

For more help, give me some code. For example definition of your array and the loop, with which you initialize it.

Share this post


Link to post
Share on other sites
right, that is fine, but i guess i should have clearified, a player is going to have to give me the x,y coord of the move they want to make, which is fine, just not very functional imho...
wanted to know if there was something else i was missing...

Share this post


Link to post
Share on other sites
I thought your question was how to place the marker in your array, with just given x and y coordinate?

If you would give me your array definition. I can help you, but for one thing I can't imagine what you mean with a "scripted" array...

Share this post


Link to post
Share on other sites
ok,

so the excerise wants us to create an array

int tictactoe[2][2]; // a 3x3 array

then i will init it to zeroes...

problem is how to functionally ask a a player to place their X or an O into the array without having to have them specify a direct x,y coordinate... since the array is init to zeros , do i have any other way to ask them to place their X or O? I thought well why not init the array with a seq of numbers to 9. so the array would like this

1 2 3
4 5 6
7 8 9

then the player could pinpoint and choose where to place their X or O
but since they want me to init to all zeroes, i dont have that option.


sorry if ive confused

Share this post


Link to post
Share on other sites
You could do that if you liked. I'd recommend some changes to the original code: make it an array of chars instead of an array of ints. Then init it to the char values '1' through '9'. Then when you have the number from the player where they want to place it (say you stored it in an int called "where"), use this:

tictactoe[(int)((where - 1) / 3)][(where - 1) % 3] = 'X';

The first index operates by division, making use of the fact that casting to int always rounds downward. The second index operates by modulus. Both have to subtract 1 to account for C++'s zero-based indexing.

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
You could do that if you liked. I'd recommend some changes to the original code: make it an array of chars instead of an array of ints. Then init it to the char values '1' through '9'. Then when you have the number from the player where they want to place it (say you stored it in an int called "where"), use this:

tictactoe[(int)((where - 1) / 3)][(where - 1) % 3] = 'X';

The first index operates by division, making use of the fact that casting to int always rounds downward. The second index operates by modulus. Both have to subtract 1 to account for C++'s zero-based indexing.




ahh, thats nice, clean, and quick.....
question tho, why modulus in the 2nd index, wouldnt division still work?

Share this post


Link to post
Share on other sites
Eh...well, no. You see, you divide to get the row, and take the remainder of that division to get the column (modulus == remainder). I don't really know how to truly explain it. Say, for instance, the user chose 8. Subtract one to get 7, and divide by 3 to get 2 with a remainder of 1. So you know to use row [2], column [1]. (Which, with zero-based indexing, actually means the 3rd row and the 2nd column, as we all know.) If you were to divide for the 2nd (column) index, you'd get 2 again, which would put the user's x in column [2] as if they'd chosen 9 instead of 8.

Hope that helps,
Twilight Dragon

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You array is indexed like this, if you substract one:

0 1 2
3 4 5
6 7 8

Now if you divide these indexes by 3 you get( integer div.)

0 0 0
1 1 1
2 2 2

And if you apply modulus 3 to the initial table

0 1 2
0 1 2
0 1 2

With division and modulus you get all combination of columns and lines subscipts

If you use division again you would get these couple of subscripts

0,0 0,0 0,0
1,1 1,1 1,1
2,2 2,2 2,2

Which doesn't cover all your board (only a diagonal)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!