-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.scala
164 lines (132 loc) · 4.92 KB
/
game.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import scala.actors._
import scala.actors.Actor._
import scala.collection.mutable.ListBuffer
/**
* Logic class which coordinates the different parts of the game. It offers also methods callable by the View.
*
*/
class Game private(val white_must_eat:Boolean){
var pView = new ChessboardView
def view = pView
def view_= (view:ChessboardView) {pView = view}
var pChessboard = new Chessboard
view.updateChessboard(chessboard)
def chessboard = pChessboard
def chessboard_= (chessboard:Chessboard) {pChessboard = chessboard}
val intelligence = new Intelligence
var multiple_moves : Array[Array[Move]] = null
var finished = true
view.setOperationForChessboard(
(x,y,i,j) => updateChessboard(x,y,i,j)
)
/**
* set Operation to be called by View to make Computer play
*/
view.setReplyAction({
_ => {
replayActions
}
})
view.setNewGameAction({_ => Game.newGame })
def replayActions() {
val heur = if(view.getHeuristic == "killer heuristic") true else false
val s = intelligence.minMax(view.getDepth,chessboard.grid,heur,view.getEval)
if(s._2 != null) {
println("\nvalore minmax per 'b' = "+s._1+" mossa : ")
s._2.foreach(m => m.printMove)
Chessboard.executeMoves(chessboard.grid,s._2,"b")
view.updateChessboard(chessboard)
chessboard.printBoard
} else {
println("\nNessuna mossa possibile!")
view.showPopUpMessage("Partita finita, il bianco vince!")
}
// If white doesn't have other possible moves, communicates that black wins!
if(intelligence.getPossibleMovesFor(chessboard.grid,"w","b").length == 0)
view.showPopUpMessage("Partita finita, il nero vince!")
}
/**
* Method callable by the view after human interaction (white player move!!)
*/
def updateChessboard(x:Int,y:Int,to_x:Int,to_y:Int): Boolean = {
val move = new Move(x,y,to_x,to_y,"move")
val isValid = Chessboard.isMoveValid(chessboard.grid,move,"w","b")
// Force user to choose a "capture" move
if(isValid){
if(white_must_eat && move.move_type != "capture"){
val moves = intelligence.getPossibleMovesFor(chessboard.grid,"w","b")
// If the first element of the first move array is a captur move...
if(moves.length > 0 && moves(0) != null && moves(0).length > 0 && moves(0)(0).move_type == "capture"){
println("LA MOSSA NON E' VALIDA PERCHE' IL GIOCATORE DEVE MANGIARE!!")
view.showPopUpMessage("Mossa non valida, il bianco deve mangiare!")
return false;
}
}
// Force user to choose longest "capture" move
if(white_must_eat && move.move_type == "capture"){
val moves = intelligence.getPossibleMovesFor(chessboard.grid,"w","b")
if(moves.length > 0){
// curr contains all (multiple) moves which first move is 'move'.
val curr = moves.filter(mv => mv.length > 0 && mv(0) == move)
if(curr.length > 0){
var max = moves(0).length
moves.foreach(m => if(m.length > max) max = m.length)
// If the length of curr moves is less then the maximum, communicate error to user and stop
if(curr(0).length < max){
println("LA MOSSA NON E' VALIDA PERCHE' IL GIOCATORE DEVE SCEGLIERE LA MOSSA DI CATTURA PIU LUNGA")
view.showPopUpMessage("Mossa non valida, il bianco deve scegliere la mossa di cattura più lunga!")
return false
}
// update multiple moves to see if white user has multiple moves to do
if (multiple_moves == null) multiple_moves = curr.filter(m => m.length == max)
multiple_moves = multiple_moves.map(move_array =>
if(move_array!=null && move_array(0) == move) move_array.drop(1) else null
)
// Check if has finished
finished = true
multiple_moves.foreach(m => if(m!=null && m.length > 0) finished = false)
println("finished? "+finished)
}else{
println("LA MOSSA NON E' VALIDA PERCHE' IL GIOCATORE DEVE SCEGLIERE LA MOSSA DI CATTURA PIU LUNGA")
view.showPopUpMessage("Mossa non valida, il bianco deve scegliere la mossa di cattura più lunga!")
return false;
}
}
}
Chessboard.executeMoves(chessboard.grid,Array(move),"w")
view.updateChessboard(chessboard)
if (finished) {
view.setStatus("b","Black moves")
multiple_moves = null
// delegate to an actor opponent's reply
view.showLoadingPopUp
actor {
reactWithin(50){
case TIMEOUT => replayActions;view.hideLoadingPopUp;view.setStatus("w","White moves")
}
}
}else{
multiple_moves.foreach(a => {if(a!=null) a.foreach(m => m.printMove);println})
}
}
isValid
}
}
/**
* Companion Object used to implement Singleton design pattern
*/
object Game{
var white_must_eat = true;
var instance : Game = null
def getInstance() = {
if(instance == null) instance = new Game(white_must_eat)
instance
}
def newGame() {instance.chessboard = new Chessboard;instance.view.updateChessboard(instance.chessboard) }
}
object Main extends App{
override def main(a:Array[String]){
//Game.white_must_eat = false
Game.getInstance
}
}