Created
January 14, 2014 20:59
-
-
Save akirillov/8425582 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Figure(val x: Int, val y: Int, val figureType: Int) | |
class ChessBoard(val m: Int, val n: Int){ | |
val board = Array.ofDim[Int](m,n) | |
def putFigure(f: Figure){ | |
f.figureType match { | |
case -2 => putKing(f.x,f.y) | |
case -4 => putQueen(f.x,f.y) | |
case -8 => putBishop(f.x,f.y) | |
case -16 => putRook(f.x,f.y) | |
case -82 => putKnight(f.x,f.y) | |
} | |
} | |
def putKing(x: Int, y: Int){ //finished | |
board(x)(y) = -2 | |
if(isOnBoard(x-1, y-1)){ | |
board(x-1)(y-1) = 1 | |
board(x-1)(y) = 1 | |
board(x)(y-1) = 1 | |
} else { | |
if(isOnBoard(x-1, y)) board(x-1)(y) = 1 | |
if(isOnBoard(x, y-1)) board(x)(y-1) = 1 | |
} | |
if(isOnBoard(x+1, y+1)){ | |
board(x+1)(y+1) = 1 | |
board(x+1)(y) = 1 | |
board(x)(y+1) = 1 | |
} else { | |
if(isOnBoard(x+1, y)) board(x+1)(y) = 1 | |
if(isOnBoard(x, y+1)) board(x)(y+1) = 1 | |
} | |
if(isOnBoard(x-1, y+1)) board(x-1)(y+1) = 1 | |
if(isOnBoard(x+1, y-1)) board(x+1)(y-1) = 1 | |
} | |
def putQueen(x: Int, y: Int){ | |
board(x)(y) = -4 | |
fillDiagonals(x,y) | |
fillHorizontalVertical(x,y) | |
} | |
def putBishop(x: Int, y: Int){ | |
board(x)(y) = -8 | |
fillDiagonals(x,y) | |
} | |
def putRook(x: Int, y: Int){ | |
board(x)(y) = -16 | |
fillHorizontalVertical(x,y) | |
} | |
def putKnight(x: Int, y: Int){ | |
board(x)(y) = -32 | |
if(isOnBoard(x+2, y-1)) board(x+2)(y-1) = 1 | |
if(isOnBoard(x-2, y-1)) board(x-2)(y-1) = 1 | |
if(isOnBoard(x+2, y+1)) board(x+2)(y+1) = 1 | |
if(isOnBoard(x-2, y+1)) board(x-2)(y+1) = 1 | |
if(isOnBoard(x+1, y-2)) board(x+1)(y-2) = 1 | |
if(isOnBoard(x-1, y-2)) board(x-1)(y-2) = 1 | |
if(isOnBoard(x+1, y+2)) board(x+1)(y+2) = 1 | |
if(isOnBoard(x-1, y+2)) board(x-1)(y+2) = 1 | |
} | |
def fillDiagonals(x: Int, y: Int){ // -> DONE | |
for{ | |
i <- 0 to Math.min(m-x,n-y)-1 | |
if board(x+i)(y+i) == 0 //TODO: remove in future, debug only | |
} board(x+i)(y+i) = 1 | |
for{ | |
i <- Math.min(x,y) to 0 by -1 | |
if board(x-i)(y-i) == 0 //TODO: remove in future, debug only | |
} board(x-i)(y-i) = 1 | |
for{ | |
i <- 0 to Math.min(x,n-y-1) | |
if board(x-i)(y+i) == 0 //TODO: remove in future, debug only | |
} board(x-i)(y+i) = 1 | |
for{ | |
i <- Math.min(m-x-1,y) to 0 by -1 | |
if board(x+i)(y-i) == 0 //TODO: remove in future, debug only | |
} board(x+i)(y-i) = 1 | |
} | |
def fillHorizontalVertical(x: Int, y: Int){ //DONE | |
for{ | |
i <- 0 to m-1 | |
if board(i)(y) == 0 | |
} board(i)(y) = 1 | |
for{ | |
i <- 0 to n-1 | |
if board(x)(i) == 0 | |
} board(x)(i) = 1 | |
} | |
def isOnBoard(x: Int, y: Int): Boolean = (x >= 0) && (x < m) && (y >= 0) && (y < n) | |
def printBoard(){ | |
for(raw <- board){ | |
for(column <-raw){ | |
print(column match { | |
case -2 => "K " | |
case -4 => "Q " | |
case -8 => "B " | |
case -16 => "R " | |
case -32 => "G " | |
case x: Int => x+" " | |
}) | |
} | |
println | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment