main_testing4/shogun.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 08 Dec 2023 00:54:36 +0000
changeset 481 e03a0100ec46
parent 476 7550c816187a
child 493 244df77507c2
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Main Part 4 about the Shogun Board Game
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//=========================================
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
object M4 {   
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
type Pos = (Int, Int)    // a position on a chessboard 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// Colours: Red or White
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
abstract class Colour
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case object Red extends Colour
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
case object Wht extends Colour
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
// Pieces: Either Pawns or Kings
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
//===============================
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
abstract class Piece {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  def pos : Pos       
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  def col : Colour    
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
  def en : Int      // energy for Pawns 1 - 4, for Kings 1 - 2
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case class Pawn(en: Int, col: Colour, pos: Pos) extends Piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class King(en: Int, col: Colour, pos: Pos) extends Piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
//val p = Pawn(4, Wht, (3,2))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
//assert(p.pos == (3,2))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
//assert(p.col == Wht)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
//assert(p.en == 4)  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
// checks if a piece is a king
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
def is_king(pc: Piece) : Boolean = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  case King(_, _, _) => true
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  case _ => false
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
// incrementing and decrementing the position of a piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
def incx(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x+1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  case King(en, c, (x,y)) => King(en, c, (x+1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
def incy(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x,y+1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  case King(en, c, (x,y)) => King(en, c, (x,y+1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
def decx(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x-1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  case King(en, c, (x,y)) => King(en, c, (x-1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
def decy(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x,y-1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
  case King(en, c, (x,y)) => King(en, c, (x,y-1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
//pretty printing colours and pieces
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
def pp_color(c: Colour) : String = c match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case Red => "R"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case Wht => "W"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
def pp(pc: Piece) : String = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
  case Pawn(n, c, _) => s"P${pp_color(c)}$n"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  case King(n, c, _) => s"K${pp_color(c)}$n"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
// Boards are sets of pieces
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
//===========================
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
case class Board(pces: Set[Piece]) {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  def +(pc: Piece) : Board = Board(pces + pc)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  def -(pc: Piece) : Board = Board(pces - pc)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
// checking whether a position is occupied in a board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
def occupied(p: Pos, b: Board) : Option[Piece] =  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
  b.pces.find(p == _.pos)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
def occupied_by(p: Pos, b: Board) : Option[Colour] =
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  occupied(p, b).map(_.col)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
def is_occupied(p: Pos, b: Board) : Boolean =
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  occupied(p, b).isDefined
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
// is a position inside a board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
def inside(p: Pos, b: Board): Boolean = 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  1 <= p._1 && 1 <= p._2 && p._1 <= 8 && p._2 <= 8 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
// pretty printing a board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
def print_board(b: Board): Unit = {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
  println()
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
  for (i <- 8 to 1 by -1) {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
    println("+" ++ "-" * 31 ++ "+")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
    for (j <- 1 to 8) {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
      val opc = occupied((j,i), b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
      if (opc.isDefined) print(s"|${pp(opc.get)}") 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
      else print("|   ")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
    }
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
    println("|")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  } 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  println("+" ++ "-" * 31 ++ "+")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
// example board: initial board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
val b_init = Board(Set(King(2,Wht,(4,1)), King(1,Red,(5,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
                  		 Pawn(4,Wht,(1,1)), Pawn(4,Red,(1,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
                  		 Pawn(3,Wht,(2,1)), Pawn(2,Red,(2,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
                  		 Pawn(2,Wht,(3,1)), Pawn(3,Red,(3,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
                  		 Pawn(1,Wht,(5,1)), Pawn(1,Red,(4,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
                  		 Pawn(4,Wht,(6,1)), Pawn(3,Red,(6,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
                  		 Pawn(3,Wht,(7,1)), Pawn(1,Red,(7,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
                  		 Pawn(2,Wht,(8,1)), Pawn(3,Red,(8,8))))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
//print_board(b_init)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
// |PR4|PR2|PR3|PR1|KR1|PR3|PR1|PR3|
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
// |   |   |   |   |   |   |   |   |
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
// |   |   |   |   |   |   |   |   |
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
// |   |   |   |   |   |   |   |   |
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
// |   |   |   |   |   |   |   |   |
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
// |   |   |   |   |   |   |   |   |
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
// |   |   |   |   |   |   |   |   |
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
// |PW4|PW3|PW2|KW2|PW1|PW4|PW3|PW2|
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
// --------------------------------
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
// Moves
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
//=======
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
abstract class Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
case object U extends Move    // up
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
case object D extends Move    // down
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
case object R extends Move    // right
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
case object L extends Move    // left
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
case object RU extends Move   // ...
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
case object LU extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
case object RD extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
case object LD extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
case object UR extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
case object UL extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
case object DR extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
case object DL extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
// Task 1: calculates all next possible positions according to a move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
def eval(pc: Piece, m: Move, en: Int, b: Board) : Set[Piece] = {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
  val p = pc.pos
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  val c = pc.col
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  if (!inside(p, b)) Set() 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
  else if (en == 0 && !is_occupied(p, b)) Set(pc)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
  else if (en == 0 && is_occupied(p, b) && c != occupied_by(p, b).get) Set(pc)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  else if (is_occupied(p, b)) Set()  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  else m match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
    case  U => eval(incy(pc), U, en - 1, b) 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
    case  D => eval(decy(pc), D, en - 1, b) 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
    case  R => eval(incx(pc), R, en - 1, b) 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
    case  L => eval(decx(pc), L, en - 1, b) 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
    case  RU => eval(incx(pc), RU, en - 1, b) ++ eval(pc, U, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
    case  LU => eval(decx(pc), LU, en - 1, b) ++ eval(pc, U, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
    case  RD => eval(incx(pc), RD, en - 1, b) ++ eval(pc, D, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
    case  LD => eval(decx(pc), LD, en - 1, b) ++ eval(pc, D, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
    case  UR => eval(incy(pc), UR, en - 1, b) ++ eval(pc, R, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
    case  UL => eval(incy(pc), UL, en - 1, b) ++ eval(pc, L, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
    case  DR => eval(decy(pc), DR, en - 1, b) ++ eval(pc, R, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
    case  DL => eval(decy(pc), DL, en - 1, b) ++ eval(pc, L, en, b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
}}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
/*
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
// test cases
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
val pw_a = Pawn(4, Wht, (4,4))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
println(eval(pw_a, U,  4, b_init))  // Set(Pawn(4,Wht,(4,8)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
println(eval(pw_a, U,  3, b_init))  // Set(Pawn(4,Wht,(4,7)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
println(eval(pw_a, RU, 4, b_init))  // Set(Pawn(4,Wht,(6,6)), Pawn(4,Wht,(4,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
                                    //     Pawn(4,Wht,(5,7)), Pawn(4,Wht,(7,5)), 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
                                    //     Pawn(4,Wht,(8,4)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
val pw_b = Pawn(4, Red, (4,4))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
println(eval(pw_b, RU, 4, b_init))  // Set(Pawn(4,Red,(8,4)), Pawn(4,Red,(7,5)), 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
                                           Pawn(4,Red,(6,6)), Pawn(4,Red,(5,7)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
*/
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
// Task 2: calculates all possible moves for a piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
def all_moves(pc: Piece, b: Board) : Set[Piece] = {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
  Set(U,D,L,R,RU,LU,RD,LD,UR,UL,DR,DL).flatMap(eval(pc, _, pc.en, b - pc))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
/*
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
// test cases
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
val pw_c = Pawn(2, Wht, (4,4))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
val pw_d = Pawn(3, Red, (4,4))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
println(all_moves(pw_c, b_init))  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
  // Set(Pawn(2,Wht,(3,5)), Pawn(2,Wht,(2,4)), Pawn(2,Wht,(3,3)), Pawn(2,Wht,(5,5)), 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  //     Pawn(2,Wht,(6,4)), Pawn(2,Wht,(4,6)), Pawn(2,Wht,(4,2)), Pawn(2,Wht,(5,3)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
println(all_moves(pw_d, b_init)) 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
  // Set(Pawn(3,Red,(4,7)), Pawn(3,Red,(5,2)), Pawn(3,Red,(3,2)), Pawn(3,Red,(1,4)), 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
  //     Pawn(3,Red,(6,3)), Pawn(3,Red,(3,6)), Pawn(3,Red,(2,5)), Pawn(3,Red,(2,3)), 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
  //     Pawn(3,Red,(4,1)), Pawn(3,Red,(5,6)), Pawn(3,Red,(7,4)), Pawn(3,Red,(6,5)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
*/
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
// Task 3: calculates all pieces that are attacked by colour
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
def attacked(c: Colour, b: Board) : Set[Piece] = {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
  val (me, opponent) = b.pces.partition(_.col == c)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
  val all = me.flatMap(all_moves(_, b))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
  opponent.filter(pc => is_occupied(pc.pos, Board(all)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
// test cases
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
val b_checkmate = Board(Set(King(2, Red, (4,2)), King(2, Wht, (7,1)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
                            Pawn(3, Red, (6,1)), Pawn(2, Wht, (8,4)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
                            Pawn(4, Red, (4,4)), Pawn(2, Wht, (4,1)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
                            Pawn(4, Red, (5,3)), Pawn(3, Wht, (8,7)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
                            Pawn(3, Red, (6,5))))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
print_board(b_checkmate)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
println(attacked(Red, b_checkmate)) // Set(Pawn(2,Wht,(8,4)), King(2,Wht,(7,1)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
println(attacked(Wht, b_checkmate)) // Set(Pawn(3,Red,(6,1)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
println(attacked(Wht, b_init)) // Set()
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
println(attacked(Red, b_init)) // Set()
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
// Task 4: calculates the number of pieces that attack a piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
def attackedN(pc: Piece, b: Board) : Int = {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
  val (me, opponent) = b.pces.partition(_.col == pc.col)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
  val all = opponent.toList.flatMap(all_moves(_, b))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
  all.count(_.pos == pc.pos)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
// test cases
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
println(attackedN(Pawn(2, Wht, (8,4)), b_checkmate)) // 3
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
println(attackedN(King(2, Wht, (7,1)), b_checkmate)) // 1
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
println(attackedN(Pawn(3, Red, (6,1)), b_checkmate)) // 1
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
// Task 5: calculates the number of pieces that protect a piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
def protectedN(pc: Piece, b: Board) : Int = {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
  val (me, opponent) = b.pces.partition(_.col == pc.col)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
  val all = (me - pc).toList.flatMap(all_moves(_, (b - pc)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
  all.count(_.pos == pc.pos)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
println(protectedN(Pawn(2, Wht, (8,4)), b_checkmate)) // 1
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
println(protectedN(Pawn(4, Red, (5,3)), b_checkmate)) // 3
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
//
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
val pw1 = Pawn(4, Wht, (4,6))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
val pw2 = Pawn(4, Wht, (2,4))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
val pw3 = Pawn(3, Red, (6,8))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
val pw4 = Pawn(2, Red, (2,8))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
val bt = b_init + pw1 + pw2
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
print_board(bt)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
println(s"Capture Red: ${attacked(Wht, bt)}")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
  // Set(Pawn(2,Red,(2,8)), Pawn(3,Red,(6,8)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
println(s"Capture Wht: ${attacked(Red, bt)}")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
  // Set(Pawn(4,Wht,(4,6)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
println(s"ProtectedN:  ${protectedN(pw3, bt)}")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
  // 2
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
println(s"AttackedN:   ${attackedN(pw4, bt)}")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
  // 2
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
println(s"all moves:   ${all_moves(pw2, bt)}")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
  // Set(Pawn(4,Wht,(4,2)), Pawn(4,Wht,(1,7)), Pawn(4,Wht,(5,3)), Pawn(4,Wht,(5,5)), 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
  //     Pawn(4,Wht,(2,8)), Pawn(4,Wht,(3,7)), Pawn(4,Wht,(6,4)))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271