-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Board that holds white and black BoardOfColor
- Loading branch information
Showing
3 changed files
with
224 additions
and
57 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,30 @@ | ||
import model.Board | ||
import model.Coords | ||
import model.PieceType | ||
import model.Color | ||
|
||
@main def hello: Unit = | ||
val board = | ||
Board.fromFENBoard("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR") | ||
// val board = Board.fromFENBoard("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR") | ||
val board = Board.initial | ||
println(board) | ||
|
||
val c1 = Coords.fromString("a2").get | ||
val c2 = Coords.fromString("a1").get | ||
val c3 = Coords.fromString("a8").get | ||
val c4 = Coords.fromString("a7").get | ||
val c5 = Coords.fromString("d4").get | ||
|
||
println(board.isEmpty(c1)) | ||
println(board.isEmpty(c2)) | ||
println(board.isEmpty(c3)) | ||
println(board.isEmpty(c4)) | ||
println(board.isEmpty(c5)) | ||
println(board.getPieceAndColorAt(c1)) | ||
println(board.getPieceAndColorAt(c2)) | ||
println(board.getPieceAndColorAt(c3)) | ||
println(board.getPieceAndColorAt(c4)) | ||
println(board.getPieceAndColorAt(c5)) | ||
|
||
println( | ||
board.setPiece(PieceType.Queen, Color.White, Coords.fromString("d7").get) | ||
) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package model | ||
|
||
enum Color(white: Boolean) { | ||
case White extends Color(true) | ||
case Black extends Color(false) | ||
|
||
def isWhite: Boolean = this.white | ||
|
||
/** Get opposite color */ | ||
def opposite: Color = this match { | ||
case White => Black | ||
case Black => White | ||
} | ||
|
||
/** Convert to string representation */ | ||
override def toString: String = this match { | ||
case White => "white" | ||
case Black => "black" | ||
} | ||
|
||
/** Get starting rank for pawns */ | ||
def pawnRank: Int = this match { | ||
case White => 1 // second rank (indexed from 0) | ||
case Black => 6 // seventh rank | ||
} | ||
|
||
/** Get promotion rank for pawns */ | ||
def promotionRank: Int = this match { | ||
case White => 7 // eighth rank | ||
case Black => 0 // first rank | ||
} | ||
|
||
/** Get direction of forward movement */ | ||
def direction: Int = this match { | ||
case White => 1 // moving up the board | ||
case Black => -1 // moving down the board | ||
} | ||
|
||
/** Check if a rank is on home half of board */ | ||
def isHomeHalf(rank: Int): Boolean = this match { | ||
case White => rank < 4 | ||
case Black => rank >= 4 | ||
} | ||
} | ||
|
||
object Color { | ||
|
||
/** Create Color from character representation */ | ||
def fromChar(c: Char): Option[Color] = c.toLower match { | ||
case 'w' => Some(White) | ||
case 'b' => Some(Black) | ||
case _ => None | ||
} | ||
|
||
def promotionRank(color: Color): Int = color match { | ||
case White => 7 | ||
case Black => 0 | ||
} | ||
|
||
/** Get both colors */ | ||
def all: Seq[Color] = Color.values.toSeq | ||
} |