Code Monkey home page Code Monkey logo

chessforandroid's Introduction

chessforandroid's People

Contributors

nikitasutulov avatar

Stargazers

 avatar

Watchers

 avatar

chessforandroid's Issues

Code review

  1. It's a bad practice to hardcode numbers even if your game is limited by standard board:
    for (i in 0..7) {
    for (j in 0..7) {
  2. Looks terrible:
    arrayOf<Piece?>(Rook(WHITE), Knight(WHITE), Bishop(WHITE), Queen(WHITE), King(WHITE), Bishop(WHITE), Knight(WHITE), Rook(WHITE)),
    arrayOf<Piece?>(Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE), Pawn(WHITE)),
    arrayOf<Piece?>(null, null, null, null, null, null, null, null),
    arrayOf<Piece?>(null, null, null, null, null, null, null, null),
    arrayOf<Piece?>(null, null, null, null, null, null, null, null),
    arrayOf<Piece?>(null, null, null, null, null, null, null, null),
    arrayOf<Piece?>(Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK), Pawn(BLACK)),
    arrayOf<Piece?>(Rook(BLACK), Knight(BLACK), Bishop(BLACK), Queen(BLACK), King(BLACK), Bishop(BLACK), Knight(BLACK), Rook(BLACK)),
  3. Long and complex, need to decompose:
    private fun doMove(cell: Cell) {
    if (!isMoveStarted && cell.piece != null && cell.piece?.color == currentTeam) {
    selectedCell = cell
    possibleMoves = selectedCell!!.getPossibleMoves()
    if (selectedCell!!.piece is King && !selectedCell!!.piece!!.getIsMoved()) {
    checkForCastling(selectedCell!!, possibleMoves)
    Log.d("Castling", "checking")
    }
    isMoveStarted = true
    moveTimer.start()
    } else if (isMoveStarted && cell.piece != null && cell.piece?.color == currentTeam) {
    selectedCell = cell
    possibleMoves = selectedCell!!.getPossibleMoves()
    if (selectedCell!!.piece is King && !selectedCell!!.piece!!.getIsMoved()) {
    checkForCastling(selectedCell!!, possibleMoves)
    }
    } else if (isMoveStarted && cell.piece?.color != currentTeam) {
    if (possibleMoves.any { pair -> pair.first == cell.getX() && pair.second == cell.getY() }) {
    activity.requireViewById<TextView>(R.id.last_move_tv).apply {
    text = "${getChessCoords(selectedCell!!.getX()!!, selectedCell!!.getY()!!)} ${getChessCoords(cell.getX()!!, cell.getY()!!)}"
    }
    movePiece(selectedCell!!, cell)
    selectedCell = null
    isMoveStarted = false
    switchCurrentTeam()
    resetMoveTimer()
    moveTimer.start()
    }
    }
    }
    as well as
    private fun checkForCastling(king: Cell, possibleMoves: MutableList<Pair<Int, Int>>) {
    var teamCells: MutableList<Cell>
    var enemyCells: MutableList<Cell>
    if (currentTeam == WHITE) {
    teamCells = whiteCells
    enemyCells = blackCells
    } else {
    teamCells = blackCells
    enemyCells = whiteCells
    }
    val rooks = teamCells.filter { cell -> cell.piece is Rook && !cell.piece!!.getIsMoved() }
    if (rooks.isEmpty()) {
    return
    }
    var startY: Int
    var endY: Int
    var yToMoveKingTo: Int
    val kingX = king.getX()!!
    for (rook: Cell in rooks) {
    var isRookAbleForCastling = true
    if (rook.getY()!! < king.getY()!!) {
    startY = rook.getY()!!
    endY = king.getY()!!
    yToMoveKingTo = king.getY()!! - 2
    } else {
    startY = king.getY()!!
    endY = rook.getY()!!
    yToMoveKingTo = king.getY()!! + 2
    }
    for (y in startY..endY) {
    if (y == rook.getY()!! || y == king.getY()!!) {
    continue
    }
    Log.d("Castling", "checking cell ${getChessCoords(kingX, y)}")
    if (
    cells[kingX][y]!!.piece == null && cells[kingX][y]!!.isUnderAttack(enemyCells) ||
    cells[kingX][y]!!.piece != null
    ) {
    isRookAbleForCastling = false
    break
    }
    }
    if (!isRookAbleForCastling) {
    continue
    }
    Log.d("Castling", "adding a move")
    possibleMoves.add(Pair(kingX, yToMoveKingTo))
    }
    }
  4. Terrible hardcoded if-based logic: https://github.com/NikitaSutulov/ChessForAndroid/blob/ab2d37b0675b31f6cd2fd26ad40745855a371088/app/src/main/java/com/nikitasutulov/chessforandroid/Cell.kt you need to invent data structures to hold all rules and iterate them

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.