Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

191
Vistas
Simple Tic Tac Toe using Groovy (Index in switch statement not changing element in double array)

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()

    }

}
 `
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda