Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!