chess: refactoring getters

Getters are not needed according to the official nim documentation.
https://nim-lang.org/docs/manual.html#procedures-properties
Getters were replaced accordingly
master
TiynGER 4 years ago
parent 75e804ef6f
commit 81d5d57e75

@ -20,8 +20,8 @@ type
board*: Board board*: Board
moved: Moved moved: Moved
toMove*: Color toMove*: Color
previousBoard*: seq[Board] previousBoard: seq[Board]
previousCastleRights*: seq[CastleRights] previousCastleRights: seq[CastleRights]
fiftyMoveCounter: int fiftyMoveCounter: int
## Move as object ## Move as object
Move* = object Move* = object
@ -121,19 +121,14 @@ const InsufficientMaterial = @[
(0, 2, 0, 0, 0) (0, 2, 0, 0, 0)
] ]
proc getField*(board: Board, field: int): int =
return board[field]
proc setField(board: var Board, field: int, val: int): bool {.discardable.} = proc setField(board: var Board, field: int, val: int): bool {.discardable.} =
if (val in PieceChar): try:
try: if (val in PieceChar):
board[field] = val board[field] = val
return true return true
except Exception: return false
return false except Exception:
return false
proc getField*(moved: Moved, field: int): bool =
return moved[field]
proc setField(moved: var Moved, field: int, val: bool): bool {.discardable.} = proc setField(moved: var Moved, field: int, val: bool): bool {.discardable.} =
try: try:
@ -155,7 +150,7 @@ proc checkInsufficientMaterial(board: Board): bool =
var br = 0 var br = 0
var bq = 0 var bq = 0
for field in board.low..board.high: for field in board.low..board.high:
case board.getField(field): case board[field]:
of WPawn: of WPawn:
wp = wp + 1 wp = wp + 1
of BPawn: of BPawn:
@ -265,7 +260,7 @@ proc echoBoard*(game: Game, color: Color) =
var line_str = "" var line_str = ""
if (color == Color.Black): if (color == Color.Black):
for i in countup(0, len(game.board)-1): for i in countup(0, len(game.board)-1):
if (game.board.getField(i) == 999): if (game.board[i] == 999):
continue continue
line_str &= PieceChar[game.board[i]] & " " line_str &= PieceChar[game.board[i]] & " "
if ((i+2) %% 10 == 0): if ((i+2) %% 10 == 0):
@ -274,7 +269,7 @@ proc echoBoard*(game: Game, color: Color) =
echo "h g f e d c b a" echo "h g f e d c b a"
else: else:
for i in countdown(len(game.board)-1, 0): for i in countdown(len(game.board)-1, 0):
if (game.board.getField(i) == 999): if (game.board[i] == 999):
continue continue
line_str &= PieceChar[game.board[i]] & " " line_str &= PieceChar[game.board[i]] & " "
if ((i-1) %% 10 == 0): if ((i-1) %% 10 == 0):
@ -306,14 +301,10 @@ proc indToField*(ind: int): string =
proc genCastleRights(moved: Moved): CastleRights = proc genCastleRights(moved: Moved): CastleRights =
## Generate rights to castle from given `moved` ## Generate rights to castle from given `moved`
let wk = not moved.getField(fieldToInd("e1")) and not moved.getField( let wk = not moved[fieldToInd("e1")] and not moved[fieldToInd("h1")]
fieldToInd("h1")) let wq = not moved[fieldToInd("e1")] and not moved[fieldToInd("a1")]
let wq = not moved.getField(fieldToInd("e1")) and not moved.getField( let bk = not moved[fieldToInd("e8")] and not moved[fieldToInd("h8")]
fieldToInd("a1")) let bq = not moved[fieldToInd("e8")] and not moved[fieldToInd("a8")]
let bk = not moved.getField(fieldToInd("e8")) and not moved.getField(
fieldToInd("h8"))
let bq = not moved.getField(fieldToInd("e8")) and not moved.getField(
fieldToInd("a8"))
return (wk, wq, bk, bq) return (wk, wq, bk, bq)
proc notationToMove*(notation: string, color: Color): Move = proc notationToMove*(notation: string, color: Color): Move =
@ -350,14 +341,14 @@ proc genBishopDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for move in Bishop_Moves: for move in Bishop_Moves:
dest = field+move dest = field+move
target = game.board.getField(dest) target = game.board[dest]
while (target != 999 and (ord(color) * target <= 0) or target == while (target != 999 and (ord(color) * target <= 0) or target ==
EnPassantID or target == -EnPassantID): EnPassantID or target == -EnPassantID):
res.add(dest) res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > -EnPassantID): if (ord(color) * target < 0 and ord(color) * target > -EnPassantID):
break break
dest = dest+move dest = dest+move
target = game.board.getField(dest) target = game.board[dest]
return res return res
except IndexDefect: except IndexDefect:
return @[] return @[]
@ -371,14 +362,14 @@ proc genRookDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for move in Rook_Moves: for move in Rook_Moves:
dest = field+move dest = field+move
target = game.board.getField(dest) target = game.board[dest]
while (target != 999 and (ord(color) * target <= 0) or target == while (target != 999 and (ord(color) * target <= 0) or target ==
EnPassantID or target == -EnPassantID): EnPassantID or target == -EnPassantID):
res.add(dest) res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > -EnPassantID): if (ord(color) * target < 0 and ord(color) * target > -EnPassantID):
break break
dest = dest+move dest = dest+move
target = game.board.getField(dest) target = game.board[dest]
return res return res
except IndexDefect: except IndexDefect:
return @[] return @[]
@ -392,14 +383,14 @@ proc genQueenDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for move in Queen_Moves: for move in Queen_Moves:
dest = field+move dest = field+move
target = game.board.getField(dest) target = game.board[dest]
while (target != 999 and (ord(color) * target <= 0) or target == while (target != 999 and (ord(color) * target <= 0) or target ==
EnPassantID or target == -EnPassantID): EnPassantID or target == -EnPassantID):
res.add(dest) res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > -EnPassantID): if (ord(color) * target < 0 and ord(color) * target > -EnPassantID):
break break
dest = dest+move dest = dest+move
target = game.board.getField(dest) target = game.board[dest]
return res return res
except IndexDefect: except IndexDefect:
return @[] return @[]
@ -415,9 +406,9 @@ proc genKingCastleDest(game: Game, field: int, color: Color): seq[int] =
var half_target: int var half_target: int
for castle in King_Moves_White_Castle: for castle in King_Moves_White_Castle:
dest = field + castle dest = field + castle
target = game.board.getField(dest) target = game.board[dest]
half_dest = field + (int)castle/2 half_dest = field + (int)castle/2
half_target = game.board.getField(half_dest) half_target = game.board[half_dest]
if (target == 999 or (target != 0)): if (target == 999 or (target != 0)):
continue continue
if (half_target == 999 or (half_target != 0)): if (half_target == 999 or (half_target != 0)):
@ -437,7 +428,7 @@ proc genKingDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for move in King_Moves: for move in King_Moves:
dest = field + move dest = field + move
target = game.board.getField(dest) target = game.board[dest]
if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)): if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)):
continue continue
res.add(dest) res.add(dest)
@ -455,7 +446,7 @@ proc genKnightDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for move in Knight_Moves: for move in Knight_Moves:
dest = field + move dest = field + move
target = game.board.getField(dest) target = game.board[dest]
if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)): if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)):
continue continue
res.add(dest) res.add(dest)
@ -472,7 +463,7 @@ proc genPawnAttackDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for attacks in Pawn_Moves_White_Attack: for attacks in Pawn_Moves_White_Attack:
dest = field + (attacks * ord(color)) dest = field + (attacks * ord(color))
target = game.board.getField(dest) target = game.board[dest]
if (target == 999 or ord(color) * target >= 0): if (target == 999 or ord(color) * target >= 0):
continue continue
res.add(dest) res.add(dest)
@ -489,9 +480,9 @@ proc genPawnDoubleDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for doubles in Pawn_Moves_White_Double: for doubles in Pawn_Moves_White_Double:
dest = field + doubles * ord(color) dest = field + doubles * ord(color)
target = game.board.getField(dest) target = game.board[dest]
if (game.moved.getField(field) or (target != 0) or ( if (game.moved[field] or (target != 0) or (
game.board.getField(dest+(S*ord(color))) != 0)): game.board[dest+(S*ord(color))] != 0)):
continue continue
res.add(dest) res.add(dest)
return res return res
@ -507,7 +498,7 @@ proc genPawnDests(game: Game, field: int, color: Color): seq[int] =
var target: int var target: int
for move in Pawn_Moves_White: for move in Pawn_Moves_White:
dest = field + move * ord(color) dest = field + move * ord(color)
target = game.board.getField(dest) target = game.board[dest]
if (target != 0 and target != ord(color) * EnPassantID): if (target != 0 and target != ord(color) * EnPassantID):
continue continue
res.add(dest) res.add(dest)
@ -521,7 +512,7 @@ proc pieceOn(game: Game, color: Color, sequence: seq[int],
pieceID: int): bool = pieceID: int): bool =
## Check if a piece with `pieceID` of a given `color` is in a field described in a `sequence` in a `game`. ## Check if a piece with `pieceID` of a given `color` is in a field described in a `sequence` in a `game`.
for check in sequence: for check in sequence:
if game.board.getField(check) == ord(color) * -1 * pieceID: if game.board[check] == ord(color) * -1 * pieceID:
return true return true
return false return false
@ -546,7 +537,7 @@ proc isInCheck*(game: Game, color: Color): bool =
## Check if the King of a given `color` is in check in a `game`. ## Check if the King of a given `color` is in check in a `game`.
var king_pos: int var king_pos: int
for i in countup(0, game.board.high): for i in countup(0, game.board.high):
if game.board.getField(i) == ord(color) * KingID: if game.board[i] == ord(color) * KingID:
king_pos = i king_pos = i
return game.isAttacked(king_pos, color) return game.isAttacked(king_pos, color)
@ -555,7 +546,7 @@ proc uncheckedMove(game: var Game, start: int, dest: int): bool {.discardable.}
## Doesnt check boundaries, checks, movement. ## Doesnt check boundaries, checks, movement.
## returns true if the piece moved, else false ## returns true if the piece moved, else false
try: try:
let piece = game.board.getField(start) let piece = game.board[start]
if game.board.setField(start, 0): if game.board.setField(start, 0):
if game.board.setField(dest, piece): if game.board.setField(dest, piece):
game.moved.setField(start, true) game.moved.setField(start, true)
@ -576,12 +567,12 @@ proc moveLeadsToCheck(game: Game, start: int, dest: int,
proc removeEnPassant(board: var Board, color: Color): void = proc removeEnPassant(board: var Board, color: Color): void =
## Removes every en passant of given `color` from the `game`. ## Removes every en passant of given `color` from the `game`.
for field in board.low..board.high: for field in board.low..board.high:
if board.getField(field) == ord(color) * EnPassantID: if board[field] == ord(color) * EnPassantID:
board.setField(field, 0) board.setField(field, 0)
proc genLegalKnightMoves(game: Game, field: int, color: Color): seq[Move] = proc genLegalKnightMoves(game: Game, field: int, color: Color): seq[Move] =
## Generates all legal knight moves starting from `field` in a `game` for a `color`. ## Generates all legal knight moves starting from `field` in a `game` for a `color`.
if game.board.getField(field) != KnightID * ord(color): if game.board[field] != KnightID * ord(color):
return @[] return @[]
var res = newSeq[Move]() var res = newSeq[Move]()
var moves = game.genKnightDests(field, color) var moves = game.genKnightDests(field, color)
@ -592,7 +583,7 @@ proc genLegalKnightMoves(game: Game, field: int, color: Color): seq[Move] =
proc genLegalBishopMoves(game: Game, field: int, color: Color): seq[Move] = proc genLegalBishopMoves(game: Game, field: int, color: Color): seq[Move] =
## Generates all legal bishop moves starting from `field` in a `game` for a `color`. ## Generates all legal bishop moves starting from `field` in a `game` for a `color`.
if game.board.getField(field) != BishopID * ord(color): if game.board[field] != BishopID * ord(color):
return @[] return @[]
var res = newSeq[Move]() var res = newSeq[Move]()
var moves = game.genBishopDests(field, color) var moves = game.genBishopDests(field, color)
@ -603,7 +594,7 @@ proc genLegalBishopMoves(game: Game, field: int, color: Color): seq[Move] =
proc genLegalRookMoves(game: Game, field: int, color: Color): seq[Move] = proc genLegalRookMoves(game: Game, field: int, color: Color): seq[Move] =
## Generates all legal rook moves starting from `field` in a `game` for a `color`. ## Generates all legal rook moves starting from `field` in a `game` for a `color`.
if game.board.getField(field) != RookID * ord(color): if game.board[field] != RookID * ord(color):
return @[] return @[]
var res = newSeq[Move]() var res = newSeq[Move]()
var moves = game.genRookDests(field, color) var moves = game.genRookDests(field, color)
@ -614,7 +605,7 @@ proc genLegalRookMoves(game: Game, field: int, color: Color): seq[Move] =
proc genLegalQueenMoves(game: Game, field: int, color: Color): seq[Move] = proc genLegalQueenMoves(game: Game, field: int, color: Color): seq[Move] =
## Generates all legal queen moves starting from `field` in a `game` for a `color`. ## Generates all legal queen moves starting from `field` in a `game` for a `color`.
if game.board.getField(field) != QueenID * ord(color): if game.board[field] != QueenID * ord(color):
return @[] return @[]
var res = newSeq[Move]() var res = newSeq[Move]()
var moves = game.genQueenDests(field, color) var moves = game.genQueenDests(field, color)
@ -625,7 +616,7 @@ proc genLegalQueenMoves(game: Game, field: int, color: Color): seq[Move] =
proc genLegalKingMoves(game: Game, field: int, color: Color): seq[Move] = proc genLegalKingMoves(game: Game, field: int, color: Color): seq[Move] =
## Generates all legal king moves starting from `field` in a `game` for a `color`. ## Generates all legal king moves starting from `field` in a `game` for a `color`.
if game.board.getField(field) != KingID * ord(color): if game.board[field] != KingID * ord(color):
return @[] return @[]
var res = newSeq[Move]() var res = newSeq[Move]()
var moves = game.genKingDests(field, color) var moves = game.genKingDests(field, color)
@ -650,7 +641,7 @@ proc genPawnPromotion(move: Move, color: Color): seq[Move] =
proc genLegalPawnMoves(game: Game, field: int, color: Color): seq[Move] = proc genLegalPawnMoves(game: Game, field: int, color: Color): seq[Move] =
## Generates all legal pawn moves starting from `field` in a `game` for a `color`. ## Generates all legal pawn moves starting from `field` in a `game` for a `color`.
if game.board.getField(field) != PawnID * ord(color): if game.board[field] != PawnID * ord(color):
return @[] return @[]
var res = newSeq[Move]() var res = newSeq[Move]()
var moves = game.genPawnDests(field, color) var moves = game.genPawnDests(field, color)
@ -666,7 +657,7 @@ proc genLegalPawnMoves(game: Game, field: int, color: Color): seq[Move] =
proc genLegalMoves*(game: Game, field: int, color: Color): seq[Move] = proc genLegalMoves*(game: Game, field: int, color: Color): seq[Move] =
## Generates all legal moves starting from `field` in a `game` for a `color`. ## Generates all legal moves starting from `field` in a `game` for a `color`.
var legal_moves = newSeq[Move]() var legal_moves = newSeq[Move]()
var target = ord(color) * game.board.getField(field) var target = ord(color) * game.board[field]
if 0 < target and target < EnPassantID: if 0 < target and target < EnPassantID:
legal_moves = case target: legal_moves = case target:
of PawnID: of PawnID:
@ -711,7 +702,7 @@ proc castling(game: var Game, kstart: int, dest_kingside: bool,
rstart = kstart + (W+W+W+W) rstart = kstart + (W+W+W+W)
rdest = rstart + (E+E+E) rdest = rstart + (E+E+E)
kdest = kstart + (W+W) kdest = kstart + (W+W)
if not game.moved.getField(kstart) and not game.moved.getField(rstart): if not game.moved[kstart] and not game.moved[rstart]:
var check = false var check = false
if (dest_kingside): if (dest_kingside):
check = check or game.isAttacked(kstart, color) check = check or game.isAttacked(kstart, color)
@ -745,7 +736,7 @@ proc checkedMove*(game: var Game, move: Move): bool {.discardable.} =
if game.toMove != color: if game.toMove != color:
return false return false
var sequence = newSeq[Move]() var sequence = newSeq[Move]()
let piece = game.board.getField(start) let piece = game.board[start]
var createEnPassant = false var createEnPassant = false
var capturedEnPassant = false var capturedEnPassant = false
var fiftyMoveRuleReset = false var fiftyMoveRuleReset = false
@ -753,9 +744,9 @@ proc checkedMove*(game: var Game, move: Move): bool {.discardable.} =
move = getMove(start, dest, color) move = getMove(start, dest, color)
if (piece == PawnID * ord(color)): if (piece == PawnID * ord(color)):
createEnPassant = dest in game.genPawnDoubleDests(start, color) createEnPassant = dest in game.genPawnDoubleDests(start, color)
capturedEnPassant = (game.board.getField(dest) == -1 * ord(color) * EnPassantID) capturedEnPassant = (game.board[dest] == -1 * ord(color) * EnPassantID)
fiftyMoveRuleReset = true fiftyMoveRuleReset = true
if (game.board.getField(move.dest) != 0): if (game.board[move.dest] != 0):
fiftyMoveRuleReset = true fiftyMoveRuleReset = true
sequence.add(game.genLegalMoves(start, color)) sequence.add(game.genLegalMoves(start, color))
if (move in sequence): if (move in sequence):
@ -772,7 +763,7 @@ proc checkedMove*(game: var Game, move: Move): bool {.discardable.} =
if capturedEnPassant: if capturedEnPassant:
game.board.setField(dest-(N*ord(color)), 0) game.board.setField(dest-(N*ord(color)), 0)
if ((90 < dest and dest < 99) or (20 < dest and dest < 29)) and if ((90 < dest and dest < 99) or (20 < dest and dest < 29)) and
game.board.getField(dest) == PawnID * ord(color): game.board[dest] == PawnID * ord(color):
game.board.setField(dest, prom) game.board.setField(dest, prom)
var prevBoard = game.previousBoard var prevBoard = game.previousBoard
var prevCastle = game.previousCastleRights var prevCastle = game.previousCastleRights

@ -930,7 +930,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] &
"6", Color.White)) "6", Color.White))
self.check(test) self.check(test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "5"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "5")])
self.check(test) self.check(test)
str.reverse() str.reverse()
for ind, file in str: for ind, file in str:
@ -948,7 +948,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] &
"6", Color.White)) "6", Color.White))
self.check(test) self.check(test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "5"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "5")])
self.check(test) self.check(test)
method testCheckedMovePawnEnPassantTrueBlack() = method testCheckedMovePawnEnPassantTrueBlack() =
@ -980,7 +980,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] &
"3", Color.Black)) "3", Color.Black))
self.check(test) self.check(test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "4"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "4")])
self.check(test) self.check(test)
str.reverse() str.reverse()
for ind, file in str: for ind, file in str:
@ -998,7 +998,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] &
"3", Color.Black)) "3", Color.Black))
self.check(test) self.check(test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "4"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "4")])
self.check(test) self.check(test)
method testCheckedMovePawnEnPassantFalseWhite() = method testCheckedMovePawnEnPassantFalseWhite() =
@ -1030,7 +1030,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] &
"6", Color.White)) "6", Color.White))
self.check(not test) self.check(not test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "5"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "5")])
self.check(not test) self.check(not test)
str.reverse() str.reverse()
for ind, file in str: for ind, file in str:
@ -1048,7 +1048,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "5" & $str[ind+1] &
"6", Color.White)) "6", Color.White))
self.check(not test) self.check(not test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "5"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "5")])
self.check(not test) self.check(not test)
method testCheckedMovePawnEnPassantFalseBlack() = method testCheckedMovePawnEnPassantFalseBlack() =
@ -1080,7 +1080,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] &
"3", Color.Black)) "3", Color.Black))
self.check(not test) self.check(not test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "4"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "4")])
self.check(not test) self.check(not test)
str.reverse() str.reverse()
for ind, file in str: for ind, file in str:
@ -1098,7 +1098,7 @@ testSuite GameTest of TestSuite:
test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] & test = self.game.checkedMove(notationToMove($file & "4" & $str[ind+1] &
"3", Color.Black)) "3", Color.Black))
self.check(not test) self.check(not test)
test = (0 == self.game.board.getField(fieldToInd($str[ind+1] & "4"))) test = (0 == self.game.board[fieldToInd($str[ind+1] & "4")])
self.check(not test) self.check(not test)
## Tests for King moves ## Tests for King moves

Loading…
Cancel
Save