Home > OS >  Why does my Kotlin code fail the test case?
Why does my Kotlin code fail the test case?

Time:02-26

I'm currently learning Kotlin. So in the course I'm doing I wrote this code:

package connectfour
import java.util.*

var rows : Int = 0
var columns : Int = 0

fun main() {
    val scanner = Scanner(System.`in`)
    val regex = Regex("[5-9]\\s?[Xx]\\s?[5-9]")
    //Print program title
    println("Connect Four")
    println("First player's name:")
    val p1 = scanner.next()
    println("Second player's name:")
    val p2 = scanner.next()

    while (true) {
        println("Set the board dimensions (Rows x Columns)")
        println("Press Enter for default (6 x 7)")
        val s = scanner.next()
        when {
            s.isBlank() -> {
                rows = 6
                columns = 7
                break
            }
            s.matches(regex) && s.isNotEmpty() -> {
                rows = s.substring(0, 1).toInt()
                columns = s.substring(s.lastIndex).toInt()
                if (rows !in 5..9) {
                    println("Board rows should be from 5 to 9")
                    continue
                } else if (columns !in 5..9) {
                    println("Board columns should be from 5 to 9")
                    continue
                } else if (rows in 5..9 && columns in 5..9) {
                    break
                }
            }
            else -> {
                println("Invalid input")
                continue
            }
        }
    }
    println("$p1 VS $p2")
    println("$rows X $columns board")
    println("\n")
    println("\n")

And this is the Output my code produces:

Connect Four First player's name:

Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 5x5 Anna VS Joan 5 X 5 board

Connect Four First player's name:

Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 9X9 Anna VS Joan 9 X 9 board

Connect Four First player's name:

Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7)

So my question is. When compared to the following code. Why does my code stop at the point it is stopping. My code fails the "Press Enter" test. I can't seem to find my mistake.

Other Code:

package connectfour

import org.w3c.dom.ranges.Range

const val MIN_DIMENSION = '5'
const val MAX_DIMENSION = '9'

fun main() {
    println("Connect Four")
    println("First player's name:")
    val firstPlayerName = readln()
    println("Second player's name:")
    val secondPlayerName = readln()
    val dimensionRegex = "\\d [Xx]\\d ".toRegex()
    var boardDimension: List<Int>

    while (true) {
        println("Set the board dimensions (Rows x Columns)\n"  
                "Press Enter for default (6 x 7)")
        val dimension = readln().replace("\\s*".toRegex(), "")
        when {
            dimension.isBlank() -> {
                boardDimension = listOf(6, 7)
                break
            }
            dimension.matches(dimensionRegex) -> {
                boardDimension = dimension.split("[Xx]".toRegex()).map { it.toInt() }
                if (boardDimension.first() !in 5..9) {
                    println("Board rows should be from 5 to 9")
                } else if (boardDimension.last() !in 5..9) {
                    println("Board columns should be from 5 to 9")
                } else {
                    break
                }
            }
            else -> println("Invalid input")
        }
    }

    println("$firstPlayerName VS $secondPlayerName")
    println("${boardDimension.first()} X ${boardDimension.last()} board")
}

Desired Output:

Connect Four First player's name:

Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 5x5 Anna VS Joan 5 X 5 board Connect Four First player's name: Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 9X9 Anna VS Joan 9 X 9 board Connect Four First player's name: Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7)

Anna VS Joan 6 X 7 board Connect Four First player's name:

Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 7 x 9
Anna VS Joan 7 X 9 board Connect Four First player's name: Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 8 X 6
Anna VS Joan 8 X 6 board Connect Four First player's name: Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 9 X 5
Anna VS Joan 9 X 5 board

Start test 2 Connect Four First player's name:

Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 4x5 Board rows should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 4X5 Board rows should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 10x6 Board rows should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 12x6 Board rows should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 6x1 Board columns should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 7X4 Board columns should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 8x10 Board columns should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 9x30 Board columns should be from 5 to 9 Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 9X5 Anna VS Joan 9 X 5 board

Start test 3 Connect Four First player's name:

Anna Second player's name: Joan Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 6x Invalid input Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) X5 Invalid input Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 10k6 Invalid input Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 12Z6 Invalid input Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) a 7x9 Invalid input Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 5x8 t Invalid input Set the board dimensions (Rows x Columns) Press Enter for default (6 x 7) 9X5 Anna VS Joan 9 X 5 board

CodePudding user response:

So my question is. When compared to the following code. Why does my code stop at the point it is stopping. My code fails the "Press Enter" test. Please help because i can't find my mistake and it is driving me nuts.

val s = scanner.next() on line 20 is your issue. The next() method looks for the next string you input. When you hit enter with no text it's still waiting. It does not return an empty string but will sit there and wait until you finally do enter something.

Your program isn't moving forward to your when statement (and thus the isBlank() check) because it is still waiting on the user input.

  • Related