I am fairly new to Groovy and I have been trying to make a simple tic tac toe game. Since there aren't of resources relating to groovy out there, I have been following a Java project and trying to convert it into groovy. So far it's is going okay but the data in my switch statement isn't replacing the space in the double array with an X. I tried to debug it but I can't find the issue. Below is the code:
`def gameBoard = [[' ','|',' ','|',' '],
['-','+','-','+','-'],
[' ','|',' ','|',' '],
['-','+','-','+','-'],
[' ','|',' ','|',' ']] as char[][]
//user needs to put a piece in one of the spaces, can do this my doing indexes but easier to number boxes 1-9
println "Enter your placement 1-9"
def input = System.in.newReader().readLine()
println input
//changing a symbol (changing the space characters with our own symbol)
switch(input){
//first row, first char
case 1:
gameBoard[0][0] = 'X'
break
case 2:
gameBoard[0][2] = 'X'
break
case 3:
gameBoard[0][4] = 'X'
break
case 4:
gameBoard[2][0] = 'X'
break
case 5:
gameBoard[2][2] = 'X'
break
case 6:
gameBoard[2][4] = 'X'
break
case 7:
gameBoard[4][0] = 'X'
break
case 8:
gameBoard[4][2] = 'X'
break
case 9:
gameBoard[4][4] = 'X'
break
}
printGameBoard(gameBoard)
//print gameBoard[0][1]
static def printGameBoard( gameBoard){
//print out the game board using 2 for loops
//for each array(row) inside of the game board, for each char (c) inside of the row, we are going to print out the symbol
for(char[] row : gameBoard){
for(char c : row){
print c
}
println()
}
}
`
System.in.newReader().readLine() returns string and in switch/case you try to compare it to int.
simple add as int should solve your issue
def input = System.in.newReader().readLine() as int