Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda