• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question regarding Arrays  RSS feed

 
Brian Blackburn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm currently working my way through the Head First book and it's going well, or atleast I thought so and decided over the weekend to take a break and have a little challenge at creating the classic tic tac toe. I think I'm ok with most of the stuff that is required for the game, my plan was just to try individual bits out on their own, get them working then start piecing them together, however I can't for the life of me figure this out. Maybe because I've not done anything like this just yet, or my brain is just refusing to work, either way until I can get it figured out in my head I won't be happy Now for the question, and it maybe very a very stupid one.

I have created a 3x3 array. From what I have learnt so far I know that to access each element you would use [0][0], [0][1] and so on for the first and second elements. What I can't seem to understand though is how to put the users guess into the array. I think part of my problem is thinking that the elements are set out like above, so if if the user inputs "9" to go into grid 9, how exactly does that get translated for being inserted into the corresponding element of the array? Which I think would be [2][2] ?

I know how to iterate through an array, to give values to the elements using for loops, but so far that was just to create a blank playing board really. I've tried looking over other peoples code from the many examples around, along with trying to look through array information to see if I can spot anything but I had no luck, and is obviously due to me looking for the wrong thing entirely.

Would you please be able to shed some light on how this works, I apologise if this really is a stupid question, I generally avoid asking for help and prefer to figure these things out on my own but it seems the "easiest" thing is often the first stumbling block for me

Regards

Brian
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I think your question is this:

You have a tic-tac-toe board set up as below:

1 | 2 | 3
--|---|---
4 | 5 | 6
--|---|---
7 | 8 | 9


The user enters a number from 1 to 9 in order to select a square to make their move. You need to figure out how to go from the 1 to 9 value into the double index required to select into your arrays. whereas I won't give you the answer (I want you to give it a try) here is a hint: you will have to calculate the two indexes from your value: You can do so with a few basic calculations (division, subtraction, and the modulus operator). You will need to know the size of the row. Things to consider:
a) The displayed grid above is 1 indexed, and Java arrays are 0 indexed, so you will have to convert from one to the other.
b) The row index is simple integer division
c) The column index is what is left over after the division
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian, welcome to CodeRanch!

This is a math problem. Somehow you need to map "1" to [0][0], "2" to [1][0], ..., "8" to [1][2] and "9" to [2][2] (Assuming [column][row]).

We need to break this into two parts. How do we get the column from the number, and how do we get the row?

You can use simple integer division to get the row. Hint: integer division chops off the remainder, so 5/3 == 1.

So how about the column? How do we map "1", "4", "7" to column 0? The trick is that these values minus one have the same remainder when divided by three. As luck would have it, Java has an operator that determines the remainder of integer division, also known as the modulus operator. Java uses % as the modulus operator. It works the same way as division, except you get the remainder instead of the quotient.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
Brian Blackburn wrote: . . . I have created a 3x3 array. . . .
No, you haven’t. There is no such thing as an m × n array. You have created a 3-element array which contains several 3-element arrays. The two are different; the Java™ solution is better than an m × n array.
As Stephan has hinted, the integer operators and the indexing of arrays are designed to work with each other. What you may need is the distance you are from the very first square, so try renumbering your squares 0 to 8. you should find it easy to convert back to 1 to 9 later.
 
Brian Blackburn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

Well after what has seemed like an eternity I think I've finally gotten it, just been testing some values out that would represent the elements and its working, now to get this up and running properly.

Thank you for all your help regarding this.

Regards

Brian
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!