我目前正在为学校任务制作一个国际象棋游戏,我的棋子已经定义好了。
data Piece = Piece { piecetype :: PieceType, color :: PieceColor }
data PieceType = Pawn | Knight | Bishop | Rook | Queen | King
data PieceColor = Black | White deriving Eq
现在我需要将棋子打印成单个字符(国王= k,皇后= q,骑士= n等)。对于黑色棋子,其值为相应白色棋子的大写字母(国王= K,皇后= Q,骑士= N等)。
因此,我创建了三个show的实例。
instance Show PieceColor where
show Black = "B"
show White = "W"
instance Show PieceType where
show Pawn = "P"
show Knight = "N"
show Bishop = "B"
show Rook = "R"
show Queen = "Q"
show King = "K"
第三个问题是什么?
instance Show Piece where
show (piecetype, color) = show piecetype
if show color == "W"
then show piecetype
else toUpper (show piecetype)
我收到了以下错误信息(我尝试了很多,但根据这个链接,看起来我已经非常接近 类似的东西).
Couldn't match expected type `Piece' with actual type `(t0, t1)'
我感激任何帮助。 顺祝商祺,Me
show (Piece piecetype color)
而不是show (piecetype,color)
。 - Satvik