How can i do that? | SoloLearn: Learn to code for FREE!

+1

How can i do that?

code: https://code.sololearn.com/cSSyEtqUZn0X what im missing in the game is that i need to somehow convert the values of the hashmap into a 2D array. there are explanations all over the code. you can read everything, run the code and see what im missing in your own eyes.. thanks in advanced.

10/23/2020 9:30:41 AM

Yahel

14 Answers

New Answer

+2

Well, you are storing what is currently in the tictactoe array in the hashmap. What you need to store are the coordinates. You could use an enumeration of the coordinates and match input against the names of the enumeration. Just one idea. The translation process will likely be a bit cumbersome, I can imagine. Or store coordinate objects in the hashmap: HashMap<String, Coordinate> for which you would have to create a Coordinate class of course, that has a constructor taking both row and column and attributes to query these values.

+2

For example as a class: class Coordinate { public final int row; public final int column; public Coordinate(int r, int c) { row = r; column = c; } } hm.add("top left", new Coordinate(0, 0));

+2

Well, when taking input, and it is "top left", the hash map gives you the coordinates of it. Those you can use to set the field in the array.

+2

No no no ... you initialise the hash map only once. There is no dynamic binding of some sort. The hash map only associates a reference string to coordinates into the game board. To update the board, you need to: - take input - map input to coordinates - use coordinates to update the game board

+2

You don't need to use hashmap, the coordinate type you need is a 2D vector with two component, create a vector class like this ignoring the "z" component https://code.sololearn.com/cQ71W9oiX1SA/?ref=app

+1

Coder Kitten , how can i save coordinates of 2d array - as a value in a hashmap?

+1

Coder Kitten thanks! I added a bit more things, and the game is great know! I would never think of that solution without u... but now I know what I need to do in these situations. Thanks.

+1

You're welcome.

0

You cannot run these type of games in sololearn, because it doesn't use an interactive ide.

0

QTWizard I know, run the game on another platform..

0

Coder Kitten , ok. but how is it going to connect with the 2d array placement? i dont get how its going to connect with the rest of the code...

0

Coder Kitten yes, I get that part. But how the: new Coordinate(0,0) gonna help me? Is it going in the 2d array? hm.put("top left", tictactoe[new Coordinate(0,0)][new Coordinate(0,0)]);

0

How is it possible?

0

Coder Kitten I don't understand how the code would look.. sorry for my misunderstanding..