main_testing4/shogun.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 15 Sep 2024 12:57:59 +0100
changeset 493 244df77507c2
parent 476 7550c816187a
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
     1
// Resit about the Shogun Board Game
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
     2
//====================================
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
     3
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
     4
// Task 1 - 6 see below
476
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
object M4 {   
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
type Pos = (Int, Int)    // a position on a chessboard 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// Colours: Red or White
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
abstract class Colour
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
case object Red extends Colour
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
case object Wht extends Colour
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
// Pieces: Either Pawns or Kings
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
//===============================
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
abstract class Piece {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
  def pos : Pos       
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
  def col : Colour    
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
  def en : Int      // energy for Pawns 1 - 4, for Kings 1 - 2
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class Pawn(en: Int, col: Colour, pos: Pos) extends Piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case class King(en: Int, col: Colour, pos: Pos) extends Piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
    25
// how to extract components from pieces
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
//val p = Pawn(4, Wht, (3,2))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
//assert(p.pos == (3,2))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
//assert(p.col == Wht)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
//assert(p.en == 4)  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
// checks if a piece is a king
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
def is_king(pc: Piece) : Boolean = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  case King(_, _, _) => true
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  case _ => false
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
// incrementing and decrementing the position of a piece
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
def incx(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x+1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  case King(en, c, (x,y)) => King(en, c, (x+1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
def incy(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x,y+1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
  case King(en, c, (x,y)) => King(en, c, (x,y+1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
def decx(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x-1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  case King(en, c, (x,y)) => King(en, c, (x-1,y))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
def decy(pc: Piece) : Piece = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  case Pawn(en, c, (x,y)) => Pawn(en, c, (x,y-1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  case King(en, c, (x,y)) => King(en, c, (x,y-1))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
//pretty printing colours and pieces
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
def pp_color(c: Colour) : String = c match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
  case Red => "R"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  case Wht => "W"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
def pp(pc: Piece) : String = pc match {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  case Pawn(n, c, _) => s"P${pp_color(c)}$n"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case King(n, c, _) => s"K${pp_color(c)}$n"
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
}
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
// Boards are sets of pieces
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
//===========================
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
case class Board(pces: Set[Piece]) {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  def +(pc: Piece) : Board = Board(pces + pc)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
  def -(pc: Piece) : Board = Board(pces - pc)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
// checking whether a position is occupied in a board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
def occupied(p: Pos, b: Board) : Option[Piece] =  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  b.pces.find(p == _.pos)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
def occupied_by(p: Pos, b: Board) : Option[Colour] =
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  occupied(p, b).map(_.col)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
def is_occupied(p: Pos, b: Board) : Boolean =
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  occupied(p, b).isDefined
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
// is a position inside a board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
def inside(p: Pos, b: Board): Boolean = 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
  1 <= p._1 && 1 <= p._2 && p._1 <= 8 && p._2 <= 8 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// pretty printing a board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
def print_board(b: Board): Unit = {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  println()
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
  for (i <- 8 to 1 by -1) {
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
    94
    println("----" * 8)
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
    for (j <- 1 to 8) {
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
      val opc = occupied((j,i), b)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
      if (opc.isDefined) print(s"|${pp(opc.get)}") 
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
      else print("|   ")
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("|")
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
  } 
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   102
  println("----" * 8)
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
// example board: initial board
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
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
   107
                  		 Pawn(4,Wht,(1,1)), Pawn(4,Red,(1,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
                  		 Pawn(3,Wht,(2,1)), Pawn(2,Red,(2,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
                  		 Pawn(2,Wht,(3,1)), Pawn(3,Red,(3,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
                  		 Pawn(1,Wht,(5,1)), Pawn(1,Red,(4,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
                  		 Pawn(4,Wht,(6,1)), Pawn(3,Red,(6,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
                  		 Pawn(3,Wht,(7,1)), Pawn(1,Red,(7,8)),
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
                  		 Pawn(2,Wht,(8,1)), Pawn(3,Red,(8,8))))
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
//print_board(b_init)
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
// |PR4|PR2|PR3|PR1|KR1|PR3|PR1|PR3|
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
// |   |   |   |   |   |   |   |   |
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
// |PW4|PW3|PW2|KW2|PW1|PW4|PW3|PW2|
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
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
// Moves
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
//=======
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
abstract class Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
case object U extends Move    // up
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
case object D extends Move    // down
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
case object R extends Move    // right
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
case object L extends Move    // left
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   141
case object RU extends Move   // first right, then possibly up
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   142
case object LU extends Move   // first left, then possibly up
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   143
case object RD extends Move   // ...
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
case object LD extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
case object UR extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
case object UL extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
case object DR extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
case object DL extends Move
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   150
//======================
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   151
// ADD YOUR CODE BELOW
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   152
//======================
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   153
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   154
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   155
// Task 1: 
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
def eval(pc: Piece, m: Move, en: Int, b: Board) : Set[Piece] = {
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   157
  val pos = pc.pos
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   158
  if (!inside(pos, b)) then Set()
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   159
  else if (en == 0) then 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   160
    if is_occupied(pos, b) then   
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   161
      val occupant = occupied_by(pos, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   162
      if occupant.get != pc.col then
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   163
        Set(pc) 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   164
      else Set()
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   165
    else Set(pc)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   166
  else if (is_occupied(pos,b)) then Set()
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   167
  else {m match {
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   168
    case U => eval(incy(pc), U, en-1, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   169
    case D => eval(decy(pc), D, en-1, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   170
    case R => eval(incx(pc), R, en-1, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   171
    case L => eval(decx(pc), L, en-1, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   172
    case RU => eval(incx(pc), RU, en-1, b) ++ eval(pc, U, en, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   173
    case LU => eval(decx(pc), LU, en-1, b) ++ eval(pc, U, en, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   174
    case RD => eval(incx(pc), RD, en-1, b) ++ eval(pc, D, en, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   175
    case LD => eval(decx(pc), LD, en-1, b) ++ eval(pc, D, en, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   176
    case UR => eval(incy(pc), UR, en-1, b) ++ eval(pc, R, en, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   177
    case UL => eval(incy(pc), UL, en-1, b) ++ eval(pc, L, en, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   178
    case DR => eval(decy(pc), DR, en-1, b) ++ eval(pc, R, en, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   179
    case DL => eval(decy(pc), DL, en-1, b) ++ eval(pc, L, en, b) 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   180
  }
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   181
  }
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   182
}
476
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
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   185
// Task 2: 
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
def all_moves(pc: Piece, b: Board) : Set[Piece] = {
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   187
  val temporary_board = b - pc //not sure if this is
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   188
  //what was meant by changes to the board (Perth discussion on keats)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   189
  val all_possible_moves = Set(U, D, R, L, RU, LU, RD, LD, UR, UL, DR, DL)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   190
  all_possible_moves.flatMap(move => eval(pc, move, pc.en, temporary_board))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   191
} 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   192
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   193
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   194
// Task 3: 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   195
def attacked(c: Colour, b: Board) : Set[Piece] = {
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   196
  val my_pieces = b.pces.filter(_.col == c)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   197
  val opponent_pieces = b.pces.filter(_.col != c)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   198
  val attacked_pieces = opponent_pieces.filter(pc => my_pieces.flatMap(p => all_moves(p, b)).exists(_.pos == pc.pos))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   199
  attacked_pieces
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   200
}
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   201
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   202
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   203
// Task 4: 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   204
def attackedN(pc: Piece, b: Board) : Int = {
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   205
  val opponent_pieces = b.pces.filter(_.col != pc.col)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   206
  val attacking_pieces_num = opponent_pieces.count(p => all_moves(p, b).exists(_.pos == pc.pos))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   207
  attacking_pieces_num
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   208
}
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   209
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   210
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   211
// Task 5: 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   212
def protectedN(pc: Piece, b: Board) : Int = {
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   213
  val temporary_board = b - pc 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   214
  val my_pieces = b.pces.filter(_.col == pc.col)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   215
  val protecting_pieces_num = my_pieces.count(p => all_moves(p, temporary_board).exists(_.pos == pc.pos))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   216
  protecting_pieces_num
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   217
}
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   218
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   219
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   220
// Task 6: 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   221
def legal_moves(pc: Piece, b: Board) : Set[Piece] = {
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   222
  if is_king(pc) then 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   223
    val all_king_moves = all_moves(pc, b)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   224
    val valid_king_moves = all_king_moves.filter{m =>
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   225
      val temporary_board = Board(b.pces.filter(_.pos != m.pos))//remove potentially captured pieces
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   226
      val updated_temp_board = temporary_board + m
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   227
      val opponent_pieces = updated_temp_board.pces.filter(_.col != pc.col)
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   228
      val opponent_moves = opponent_pieces.flatMap(p => all_moves(p, updated_temp_board))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   229
      !(opponent_moves.exists(_.pos == m.pos))}
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   230
    valid_king_moves
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   231
  else {all_moves(pc, b)}
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
/*
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   235
// more test cases
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   236
//=================
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
val pw1 = Pawn(4, Wht, (4,6))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
val pw2 = Pawn(4, Wht, (2,4))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
val pw3 = Pawn(3, Red, (6,8))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
val pw4 = Pawn(2, Red, (2,8))
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
val bt = b_init + pw1 + pw2
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
print_board(bt)
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
println(s"Capture Red: ${attacked(Wht, bt)}")
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   245
  // => Set(Pawn(2,Red,(2,8)), Pawn(3,Red,(6,8)))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   246
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
println(s"Capture Wht: ${attacked(Red, bt)}")
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   248
  // => Set(Pawn(4,Wht,(4,6)))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   249
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
println(s"ProtectedN:  ${protectedN(pw3, bt)}")
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   251
  // => 2
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   252
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
println(s"AttackedN:   ${attackedN(pw4, bt)}")
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   254
  // => 2
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   255
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
println(s"all moves:   ${all_moves(pw2, bt)}")
493
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   257
  // => Set(Pawn(4,Wht,(4,2)), Pawn(4,Wht,(1,7)), Pawn(4,Wht,(5,3)), Pawn(4,Wht,(5,5)), 
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   258
  //        Pawn(4,Wht,(2,8)), Pawn(4,Wht,(3,7)), Pawn(4,Wht,(6,4)))
244df77507c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 476
diff changeset
   259
*/
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
}
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263