25.4. Rodzaje ruchów

W pliku Move.scala zdefiniowana jest abstrakcyjna klasa Move, reprezentująca pojedynczy ruch partii szachowej, oraz jej cztery podklasy:

Plik Move.scala:
package chess

abstract sealed class Move {
  val from: Field
  val to: Field
}
case class RegularMove(from: Field, to: Field) extends Move
case class PromotionMove(from: Field, to: Field, figure: Figure) extends Move
case class EnPassantMove(from: Field, to: Field, captured: Field) extends Move
case class CastlingMove(from: Field, to: Field, rookFrom: Field, rookTo: Field) extends Move

Każda z podklas ma pola from oraz to, reprezentujące pole, z którego rusza poruszana figura i pole docelowe. Klasa PromotionMove ma dodatkowo pole figure, reprezentujące figurę, na którą promowany jest pion. Klasa EnPassantMove ma pole captured, reprezentujące zbijaną figurę. Klasa CastlingMove ma dodatkowo pola rookFrom i rookTo, reprezentujące startowe i docelowe pola dla poruszanej podczas roszady wieży.

Język programowania Scala Wydanie 2. Copyright © Grzegorz Balcerek 2016

Licencja Creative Commons

Ten utwór jest dostępny na licencji Creative Commons Uznanie autorstwa-Na tych samych warunkach 4.0 Międzynarodowe.

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.