To wrap my head around objects and arrays/arraylists, I decided to make a Tic-Tac-Toe app.
Here's the want to do:
The board is composed of a char array[3][3] which looks like/corresponds to:
codeindent - - - 0,0 1,0 2,0
c - - - 0,1 1,1 2,1
c - - - 0,2 1,2 2,2
I have the players input their move choice by entering a digit 1-9
1 2 3
4 5 6
7 8 9
Now I'd like an array where if I called index 4, it would return [0,1] in such a way I could point it directly to the char[][] array so I could do something along the lines of (I know that's not how its implemented but I'm putting var types for my own mental clarity)
char[][] boardArray[??? refArray[int playerMove]] = char currentPlayer
However, I just can't seem to wrap my head around what I need to do. I don't know what type of array it should be or why.
While typing this/looking stuff I just realized arrays are objects and can only store primitives in java. So I couldn't use an array to call an array...is this where I'd use Arraylists?
Thanks for all the answers! The purpose of this little exercise was to use arrays in an overly complicated and ridiculous fashion for the sole purpose of getting me practice with them, but it does seem that java as a language isn't capable of what I want to do in the fashion I want to do it. Definitely gonna go through and try out these other answers too
Implementing the multi-array is a perfectly valid way of going about this problem. Accessing the array and storing objects in the array seems to be where your problem lies.
Firstly, arrays can store anything! You can store both primitives and objects (or references to objects) inside them, you just have to change the definition of the array.
For example this will store an array of primitive chars:
char boardArray[][] = new char[3][3];
And this will store an array of Objects:
Object boardArray[][] = new Object[3][3];
With this in mind, let's have a go at your problem:
We start with the multi-array boardArray, we can make it a character array if we want to use characters for our reference ('X' and 'O') and the empty string if it's unused.
//by default this will implement 3x3 matrix with empty strings
char boardArray[3][3] = new char[3][3];
Now we want to go ahead and take some user input:
// get the value
int value = Integer.parseInt(br.readLine().toString());
Then you can calculate the row and column from that field and modify:
// find the row
int row = (value - 1) / 3;
// get the column value (minus one as we want it from 1->9 instead of 0-8)
int column = (value - 1) % 3;
Then you can change values in the array!
if (round % 2 == 0)
boardArray[row][column] = playerA;
else
boardArray[row][column] = playerB;
Full code can be seen here. It's not elegant and kind of hacky. There are plenty of ways of going about this problem. I'd recommend creating your own Board class that abstracts the code away from your main class. I'll leave this up to you!
I'd also recommend taking a look at the Java docs and researching tutorials on objects and arrays.
It can seem confusing and daunting, but remember arrays are just blocks of memory that set aside room for you to store a type of object that you want to put into them.
Enjoy my friend!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With