Hi guys, I hope all of you are having a good one! ...
# getting-started
p
Hi guys, I hope all of you are having a good one! I am doing some projects from Hyperskill, concretely the minesweeper. I managed to create a 2D array which randomly locates mines. After this, I created another array to shows in each position how many mines are around the coordinate. As the array is char, I need to convert the number of mines around the coordinate into a char and here I have the issue. The program runs without any problem but it does not show what it should. Is this some issue I cause?
X is mine
. there are not mines around
m
can you share a snippet of code that seems to be problematic? Probably the conversion?
ðŸ™Œ 1
p
Sorry, I forgot to paste the code. https://pastebin.com/jkXn38KP This are the functions involved in this, but as you say I believe the problem is in the conversion.
Copy code
``````private fun calMines
private fun showMines``````
In kotlin playground, if I type 1.toChar() it prints a weird icon. Might it have some relation?
m
Yes, Int.toChar() will convert the int to the specific char on the table. You can just do 1.toString()[0]
âž• 2
p
Indeed, this has been fixed. Is there a link where I can read about this situation? To have an understanding on WHY happens. Thank you very much @Michael de Kaste & @Milan Hruban for answering.
m
http://www.asciitable.com/ its quite simple. What is 12.toChar() supposed to do in your situation? there isn't a singular
``12``
character. Characters are represented by a number. So if you ask for for 12.toChar(), you'll get the 12th character on the ascii table. Now if you would say .toString(), you're saying you want to integer exactly as it is shown, but in a string, so 1.toString() becomes "1". Since "1" is a string, and not a character, the first character of that string is the character you're looking for. And thus 1.toChar() gives you a weird character, and 1.toString()[0] gives you the actual character
K 1
p
I kind of expect this answer but my knowledge is not yet there.
Copy code
``So if you ask for for 12.toChar(), you'll get the 12th character on the ascii table.``
My mind dealed with this but at the same time I was thinking that if the number was <10 (two units) it should convert it in a number char. Thanks again!
m
Well, how would you get the 3th character on the table then? ðŸ˜‰
ðŸ˜… 1