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

코틀린 프로그래밍 기본 1

임시 이미지 황영덕
http://www.boostcourse.org/mo132/forum/49869
좋아요 647 수강생 4341
var boardPic = arrayOf(arrayOf(0,0,0),
arrayOf(0,0,0), arrayOf(0,0,0))
fun main(){
var turnCount:Int=0
while(true)
{
makeBoard(turnCount)
inputArr(turnCount)
if(checkWin())break
turnCount++
}
makeBoard(turnCount)
println("경기가 끝났습니다 Player ${turnCount%2+1}의 승리입니다")
}
fun checkWin():Boolean {
for (i in 0..2) if((boardPic[i][0]!=0)
&&(boardPic[i][0]==boardPic[i][1])
&&(boardPic[i][2]==boardPic[i][1])) return true
else if((boardPic[0][i]!=0)
&&(boardPic[0][i]==boardPic[1][i])
&&(boardPic[2][i]==boardPic[1][i])) return true
if((boardPic[1][1]!=0)
&&(boardPic[1][1]==boardPic[2][2])
&&(boardPic[0][0]==boardPic[2][2])) return true
if((boardPic[1][1]!=0)
&&(boardPic[0][2]==boardPic[2][0])
&&(boardPic[0][2]==boardPic[1][1])) return true
return false
}

fun inputArr(count:Int):Unit{
print("Player ${count%2+1} 입력(줄,칸): ")
readLine()?.split(",")
?.also {boardPic[it[0].toInt()][it[1].toInt()]=count%2+1}
}
fun findOX(row:Int,col:Int){
if(boardPic[row][col]==0) print(" ")
else {if(boardPic[row][col]==1) print("O") else print("X")}
}
fun makeBoard(count:Int):Unit{
for (i in 0..5){
for (j in 0..5){
if (i==0&&j%2==1){print("${j/2}")}
else if (i==0&&j%2==0) {print(" ")}
else if (j==0&&i%2==1){print("${i/2}")}
else if (j==0&&i%2==0) {print(" ")}
else if((i%2==1)&&(j==2||j==4)){print("|")}
else if((i==2||i==4)&&(j!=0))
{if(j%2==1)print("-") else print("+")}
else {findOX(i/2,j/2)}
}
println()
}
}