Ejercicio 5a
This commit is contained in:
parent
450a3b34d0
commit
96bde3c362
|
@ -1,52 +1,150 @@
|
||||||
test1 = {
|
import sys
|
||||||
"1h": "bking",
|
|
||||||
"6c": "wqueen",
|
tests = [
|
||||||
"2g": "bbishop",
|
# Valid
|
||||||
"5h": "bqueen",
|
{
|
||||||
"3e": "wking",
|
"1h": "bking",
|
||||||
}
|
"6c": "wqueen",
|
||||||
test2 = {
|
"2g": "bbishop",
|
||||||
"9h": "bking",
|
"5h": "bqueen",
|
||||||
}
|
"3e": "wking",
|
||||||
test3 = {
|
},
|
||||||
"1r": "bking",
|
# Invalid: Illegal total amount of pieces
|
||||||
}
|
{
|
||||||
test4 = {
|
"1a": "wrook",
|
||||||
"8h": "cking",
|
"1b": "wknight",
|
||||||
}
|
"1c": "wbishop",
|
||||||
test5 = {
|
"1d": "wqueen",
|
||||||
"8h": "wring",
|
"1e": "wking",
|
||||||
}
|
"1f": "wbishop",
|
||||||
test6 = {
|
"1g": "wknight",
|
||||||
"8h": "wking",
|
"1h": "wrook",
|
||||||
"7h": "wking",
|
"2a": "wpawn",
|
||||||
}
|
"2b": "wpawn",
|
||||||
test7 = None
|
"2c": "wpawn",
|
||||||
|
"2d": "wpawn",
|
||||||
|
"2e": "wpawn",
|
||||||
|
"2f": "wpawn",
|
||||||
|
"2g": "wpawn",
|
||||||
|
"2h": "wpawn",
|
||||||
|
"3a": "wrook",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal amount of pieces
|
||||||
|
{
|
||||||
|
"8h": "wking",
|
||||||
|
"7h": "wking",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal position length
|
||||||
|
{
|
||||||
|
"9": "bking",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal position length
|
||||||
|
{
|
||||||
|
"9hh": "bking",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal tile number
|
||||||
|
{
|
||||||
|
"9h": "bking",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal tile letter
|
||||||
|
{
|
||||||
|
"1r": "bking",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal player color
|
||||||
|
{
|
||||||
|
"8h": "cking",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal piece name
|
||||||
|
{
|
||||||
|
"8h": "wring",
|
||||||
|
},
|
||||||
|
# Invalid: Illegal piece data type
|
||||||
|
{
|
||||||
|
"8h": None,
|
||||||
|
},
|
||||||
|
# Invalid: Illegal board data type
|
||||||
|
None,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def invalidAmount(name, present, expected):
|
||||||
|
err = f"AmountError: total {name}"
|
||||||
|
return f"{err}; total == {present}, expected: <= {expected}"
|
||||||
|
|
||||||
|
|
||||||
|
def invalidPos(name, pos, present, expected):
|
||||||
|
err = f"PositionError: {name} '{pos}'"
|
||||||
|
return f"{err}; length == {present}, expected: {expected}"
|
||||||
|
|
||||||
|
|
||||||
|
def invalidPiece(name, color_piece, present, expected):
|
||||||
|
err = f"PieceError: {name} '{color_piece}'"
|
||||||
|
return f"{err}; {name} == {present}, expected: {expected}"
|
||||||
|
|
||||||
|
|
||||||
|
def invalidType(name, present, expected):
|
||||||
|
err = f"TypeError: {name} type"
|
||||||
|
return False, f"{err}; type == {present}, expected: {expected}"
|
||||||
|
|
||||||
|
|
||||||
|
def isValidPos(pos):
|
||||||
|
if len(pos) != 2:
|
||||||
|
return invalidPos("length", pos, len(pos), 2)
|
||||||
|
number, letter = pos[0], pos[1]
|
||||||
|
if number not in "12345678":
|
||||||
|
return invalidPos("number", pos, number, "1-8")
|
||||||
|
if letter not in "abcdefgh":
|
||||||
|
return invalidPos("letter", pos, letter, "a-h")
|
||||||
|
|
||||||
|
|
||||||
|
def isValidPiece(color_piece):
|
||||||
|
pieces = "pawn knight bishop rook queen king".split()
|
||||||
|
color, piece = color_piece[0], color_piece[1:]
|
||||||
|
if color not in "bw":
|
||||||
|
return invalidPiece("color", color_piece, color, "w | b")
|
||||||
|
if piece not in pieces:
|
||||||
|
return invalidPiece("piece", color_piece, piece, " | ".join(pieces))
|
||||||
|
|
||||||
|
|
||||||
|
def isValidAmount(player):
|
||||||
|
totals = {
|
||||||
|
"all": 16,
|
||||||
|
"pawn": 8,
|
||||||
|
"king": 1,
|
||||||
|
}
|
||||||
|
total = 0
|
||||||
|
for key, val in totals.items():
|
||||||
|
if key in player.keys() and player[key] > val:
|
||||||
|
return invalidAmount(f"'{key}'", player[key], val)
|
||||||
|
for val in player.values():
|
||||||
|
total += val
|
||||||
|
if total > totals["all"]:
|
||||||
|
return invalidAmount("pieces", total, totals["all"])
|
||||||
|
|
||||||
|
|
||||||
def isValidChessBoard(board):
|
def isValidChessBoard(board):
|
||||||
pieces = "pawn knight bishop rook queen king".split()
|
players, valid, msg = {}, True, "Valid"
|
||||||
players = {"w": {}, "b": {}}
|
|
||||||
totals = {"pa": 8, "kn": 2, "bi": 2, "ro": 2, "qu": 1, "ki": 1}
|
|
||||||
if not isinstance(board, dict):
|
if not isinstance(board, dict):
|
||||||
return False
|
return invalidType("board", type(board), type({}))
|
||||||
for k, v in board.items():
|
for pos, piece in board.items():
|
||||||
if len(k) > 2 or k[0] not in "12345678" or k[1] not in "abcdefgh":
|
if not isinstance(piece, str):
|
||||||
return False
|
return invalidType("piece", type(piece), type(""))
|
||||||
if not isinstance(v, str) or v[0] not in "bw" or v[1:] not in pieces:
|
players.setdefault(piece[0], {})
|
||||||
return False
|
player = players[piece[0]]
|
||||||
players[v[0]].setdefault(v[1:3], 0)
|
player.setdefault(piece[1:], 0)
|
||||||
players[v[0]][v[1:3]] += 1
|
player[piece[1:]] += 1
|
||||||
# TODO
|
checks = [isValidPos(pos), isValidPiece(piece), isValidAmount(player)]
|
||||||
for board in players.values():
|
for check in checks:
|
||||||
if board != totals:
|
if check is not None:
|
||||||
return False
|
valid, msg = False, check
|
||||||
return True
|
break
|
||||||
|
return valid, msg
|
||||||
|
|
||||||
|
|
||||||
for test in [test1, test2, test3, test4, test5, test6, test7]:
|
valid = True
|
||||||
print(f"Testing board '{test}': ", end="")
|
for test in tests:
|
||||||
if isValidChessBoard(test):
|
print(f"Testing board '{test}': ")
|
||||||
print("Valid")
|
valid, msg = isValidChessBoard(test)
|
||||||
else:
|
print("=>", msg)
|
||||||
print("Invalid")
|
if not valid:
|
||||||
|
sys.exit(1)
|
||||||
|
|
Loading…
Reference in New Issue