Skip to content

Commit

Permalink
feat: create diagonal rules + create bishop
Browse files Browse the repository at this point in the history
  • Loading branch information
mirko-felice committed Feb 20, 2023
1 parent db6013f commit 44a78ce
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chess/src/main/resources/theories/south_east.pl
@@ -0,0 +1,5 @@
south_east(Xs, Ys, X, Y) :- south_east(Xs, Ys, 1, X, Y) .

south_east(Xs, Ys, N, X, Y) :- X is Xs + N, Y is Ys - N .

south_east(Xs, Ys, N, X, Y) :- N1 is N + 1, south_east(Xs, Ys, N1, X, Y) .
5 changes: 5 additions & 0 deletions chess/src/main/resources/theories/south_west.pl
@@ -0,0 +1,5 @@
south_west(Xs, Ys, X, Y) :- south_west(Xs, Ys, 1, X, Y) .

south_west(Xs, Ys, N, X, Y) :- X is Xs - N, Y is Ys - N .

south_west(Xs, Ys, N, X, Y) :- N1 is N + 1, south_west(Xs, Ys, N1, X, Y) .
28 changes: 28 additions & 0 deletions chess/src/main/scala/io/github/chess/model/Bishop.scala
@@ -0,0 +1,28 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model

import io.github.chess.model.rules.{ChessRule, DiagonalRule}

/** Represents the particular piece of the bishop. */
trait Bishop extends Piece

/** Factory for [[Bishop]] instances. */
object Bishop:

/**
* Creates a new bishop.
* @return a new [[Bishop]]
*/
def apply(): Bishop = SimpleBishop()

private case class SimpleBishop() extends Bishop:

override val rule: ChessRule = DiagonalRule()

// TODO da rimuovere
override def findMoves(position: Position): Set[Position] = ???
5 changes: 5 additions & 0 deletions chess/src/main/scala/io/github/chess/model/Pawn.scala
Expand Up @@ -5,6 +5,7 @@
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model
import io.github.chess.model.rules.ChessRule

/** Represents the particular piece of the pawn. */
trait Pawn extends Piece
Expand All @@ -19,6 +20,10 @@ object Pawn:
def apply(): Pawn = SimplePawn()

private case class SimplePawn() extends Pawn:

override val rule: ChessRule = ???

// TODO da rimuovere
override def findMoves(position: Position): Set[Position] =
List
.iterate(0, 8)(_ + 1)
Expand Down
6 changes: 6 additions & 0 deletions chess/src/main/scala/io/github/chess/model/Piece.scala
Expand Up @@ -6,9 +6,15 @@
*/
package io.github.chess.model

import io.github.chess.model.rules.ChessRule

/** Represents every piece of the [[ChessBoard]]. */
trait Piece:

/** Returns the [[ChessRule]] able to give all the possible moves from a starting position. */
def rule: ChessRule

// TODO da rimuovere
/**
* Returns all the possibile positions that the piece can go to, considering only its own rules.
* @param position source [[Position]]
Expand Down
@@ -0,0 +1,32 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.rules

import io.github.chess.model.{Move, Position}

/** Represents the chess rule that can find all the moves in diagonal directions, analyzing the status. */
class DiagonalRule extends ChessRule:

private val northWestRule = NWPrologRule()
private val northEastRule = NEPrologRule()
private val southWestRule = SouthWestRule()
private val southEastRule = SouthEastRule()

override def findMoves(position: Position): Set[Move] =
reduceInsideBoard(northWestRule.findPositions(position), position) ++
reduceInsideBoard(northEastRule.findPositions(position), position) ++
reduceInsideBoard(southWestRule.findPositions(position), position) ++
reduceInsideBoard(southEastRule.findPositions(position), position)

private def reduceInsideBoard(
infiniteDirectionPositions: LazyList[(Int, Int)],
position: Position
): Set[Move] =
infiniteDirectionPositions
.takeWhile((x, y) => x >= 0 && x <= 7 && y >= 0 && y <= 7)
.map(Move(position, _))
.toSet
@@ -0,0 +1,12 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.rules

import io.github.chess.model.PrologEngine

/** Represents the prolog rule to find all the coordinates in the south east direction from a starting position. */
class SouthEastRule extends PrologRule("south_east")
@@ -0,0 +1,12 @@
/*
* MIT License
* Copyright (c) 2023 Cesario Jahrim Gabriele & Derevyanchenko Maxim & Felice Mirko & Kentpayeva Madina
*
* Full license description available at: https://github.com/jahrim/PPS-22-chess/blob/master/LICENSE
*/
package io.github.chess.model.rules

import io.github.chess.model.PrologEngine

/** Represents the prolog rule to find all the coordinates in the south west direction from a starting position. */
class SouthWestRule extends PrologRule("south_west")

0 comments on commit 44a78ce

Please sign in to comment.