강의/부캠 안드로이드 학습정리

day 7 클래스 상속

return this.file == other.file && this.rank == other.rank

체스 만드는건데 이거 가지고 클래스 상속을 코틀린에서 연습해봤다.

어제 다른사람이 한거 따라해서 try catch로 예외처리도 해봤음.

enum으로 상수 처리를 할 수 있다는데 이건 몰라서 안했다. 내일 잘 봐야지.

어제꺼 정규표현식

정규표현식 기초 https://poqw.tistory.com/26

 

"코딩용으로 monospaced font 고정폭 글꼴을 한 번 찾아보세요. 공개된게 많이 있습니다."

 


import java.util.regex.Pattern

if(Pattern.matches("[A-H][0-9]->[A-Z][1-8]", input)){

enum string으로 사용하는 방법 등

'\u265F' 나 "\u265F" 형식으로 유니코드 가져올 수 있는듯.

 

class Knight(color: String, type: String, shape: String, position: Position) : Piece() {
    //    ♔♕♖♗♘♙♚♛♜♝♞
    init {
        this.color = color
        this.type = type
        this.shape = shape
        this.position = position
    }

대신

class Knight(val color: String, val type: String, val shape: String, val position: Position) : Piece() {
    //    ♔♕♖♗♘♙♚♛♜♝♞

이런식으로 init 할 수 있는 듯.

 

//체스말 종류에 따른 처리
        val piece: ChessPiece = when(type){

            "Pawn" -> {
                val cntPawn = if(cntMap[key] == null) 0 else cntMap[key]
                if (cntPawn!! >= maxPawn) {
                    throw Exception("$team $type 체스말 입력 초과!")
                }
                cntMap[key] = cntPawn+1

                Pawn(Position(strPos), black)
            }

            "Bishop" -> {
                val cntBishop = if(cntMap[key] == null) 0 else cntMap[key]
                if (cntBishop!! >= maxBishop) {
                    throw Exception("$team $type 체스말 입력 초과!")
                }
                cntMap[key] = cntBishop+1

                Bishop(Position(strPos), black)
            }

when으로 변수 다르게 받기

\t으로 간격 일정하게 추가가능

 

abstract class ChessPiece(var position: Position, val black: Boolean) {

class Knight(pos: Position, bl: Boolean) : ChessPiece(pos, bl) {
enum class File{
    A, B, C, D, E, F, G, H
}

enum class Rank{
    R1, R2, R3, R4, R5, R6, R7, R8
}

class Position(pos: String) {
    var file = File.valueOf(pos.substring(0,1))
    var rank = Rank.valueOf("R${pos.substring(1)}")

.valueOf 로 enum을 바꿀 수 있는듯.

 

return this.file == other.file && this.rank == other.rank

 

클래스가 너무 많아지면 패키지 폴더 만들고 분류해서 import, package로 사용하면 된다.

// 상위폴더 .kt에서
package chess_piece
import Position

// 하위폴더 .kt에서
import chess_piece.*

 

open class 대신 abstract class

throw를 하면 그 함수를 try로 실행한 곳으로 넘어가서 catch

내부에서만 쓰이는 함수면 private로 encapsulation을 지키자.