로그인 바로가기 하위 메뉴 바로가기 본문 바로가기
난이도
입문

코틀린 프로그래밍 기본 1

임시 이미지 황영덕
http://www.boostcourse.org/mo132/forum/28017
좋아요 647 수강생 4341

package FinalProject

fun main() {
    var board = arrayOf(arrayOf(' ',' ',' '), arrayOf(' ',' ',' '), arrayOf(' ',' ',' '))
    val names = arrayOf("Player 1", "Player 2")
    val marks = arrayOf('O', 'X')

    play(board, names, marks)

}

fun play(board:Array<Array<Char>>, names:Array<String>, marks:Array<Char>){
    var turn = 0
    var win_check: Int = 0
    while(win_check == 0 && turn != 9) {
        printBoard(turn, board, names, marks)
        win_check = winCheck(board)
        if(win_check == 1){
            println("  0 1 2")
            for (i in 0..2) {
                print("$i ")
                for (j in 0..2) {
                    print("${board[i][j]}")
                    if(j!=2) {
                        print("|")
                    }
                }
                println("")
                if(i!=2) {
                    println("  -+-+-")
                }
            }
            println("${names[turn%2]} 승리!! ")
            return
        }
        turn = turn+1

    }
    println("  0 1 2")
    for (i in 0..2) {
        print("$i ")
        for (j in 0..2) {
            print("${board[i][j]}")
            if(j!=2) {
                print("|")
            }
        }
        println("")
        if(i!=2) {
            println("  -+-+-")
        }
    }
    println("무승부!!")
    return
}

fun printBoard(turn:Int, board:Array<Array<Char>>, names:Array<String>, marks:Array<Char>){
    println("\n${turn+1}번째 턴\n")
    println("  0 1 2")
    for (i in 0..2) {
        print("$i ")
        for (j in 0..2) {
            print("${board[i][j]}")
            if(j!=2) {
                print("|")
            }
        }
        println("")
        if(i!=2) {
            println("  -+-+-")
        }
    }
    if(turn%2 == 0){
        print("${names[turn%2]} 입력(줄,칸): ")
    } else{
        print("${names[1]} 입력(줄,칸): ")
    }
    val input: String? = readLine()
    val xy = input_check(input, board)
    val x_error:Char = xy[0]
    if (x_error != 'E') {
        val x = xy[0].toInt()
        val y = xy[1].toInt()
        if (turn % 2 == 0) {
            board[y][x] = marks[0]
        } else {
            board[y][x] = marks[1]
        }
    }else{
        printBoard(turn, board, names, marks)
    }
}

fun input_check(input:String?, board:Array<Array<Char>>):Array<Char>{
    val input_arr = input!!.split(",")
    val y = input_arr[0].toInt()
    val x = input_arr[1].toInt()
    if((x !in 0..2) || (y !in 0..2) ){
        println("Error!! 숫자 넘침")
        return arrayOf('E','E')
    }else if(board[y][x] != ' '){
        println("Error!! 빈공간 아님")
        return arrayOf('E','E')
    }else{
        return arrayOf(x.toChar(),y.toChar())
    }
}

fun winCheck(board:Array<Array<Char>>) : Int {
    for (i in 0..2) {
        if ((board[i][0] != ' ') && (board[i][0] == board[i][1]) && (board[i][0] == board[i][2])){
            return 1
        }
    }
    for (i in 0..2) {
        if ((board[0][i] != ' ') && (board[0][i] == board[1][i]) && (board[0][i] == board[2][i])){
            return 1
        }
    }
    if ((board[0][0] != ' ') && (board[0][0] == board[1][1]) && (board[0][0] == board[2][2])){
        return 1
    }
    if ((board[0][2] != ' ') && (board[0][2] == board[1][1]) && (board[0][2] == board[2][0])){
        return 1
    }
    return 0

}