Skip to content

Commit 840323f

Browse files
author
magicgoose
committed
some style fixes
1 parent b05de2f commit 840323f

File tree

4 files changed

+28
-53
lines changed

4 files changed

+28
-53
lines changed

src/magicgoose/gomoku/ai/GomokuBoard.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,18 @@ class GomokuBoard private (
176176
if (li(current_player, 4) >= 1)
177177
WIN1
178178
else if (li(-current_player, 4, OPEN) >= 1
179-
// || li(-current_player, 4) >= 1
180-
// &&
181-
// li(-current_player, 3, OPEN) +
182-
// li(-current_player, 3, BROKEN) >= 1
179+
|| li(-current_player, 4) >= 1
180+
&&
181+
li(-current_player, 3, OPEN) +
182+
li(-current_player, 3, BROKEN) >= 1
183+
||
184+
li(current_player, 3) == 0 &&
185+
li(-current_player, 3, OPEN)+li(-current_player, 3, BROKEN) > 1
183186
)
184187
LOSS1
185-
// else if (li(current_player, 3, OPEN) +
186-
// li(current_player, 3, BROKEN) >= 1)
187-
// WIN2
188+
else if (li(current_player, 3, OPEN) +
189+
li(current_player, 3, BROKEN) >= 1)
190+
WIN2
188191
else if (li(-current_player, 4) + li(-current_player, 3, OPEN) + li(-current_player, 3, BROKEN) >= 2)
189192
LOSS2
190193
else 0

src/magicgoose/gomoku/ai/Brain.scala renamed to src/magicgoose/gomoku/ai/MoveSearcher.scala

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@ package magicgoose.gomoku.ai
22

33
import scala.annotation.tailrec
44
import java.util.Arrays
5-
/**
6-
* General interface for an implementation of AI-powered move searching
7-
*/
8-
//trait MoveSearcher {
9-
// /**
10-
// * returns all canditates for next move
11-
// */
12-
// def findPossibleMoves(): Indexed[Int]
13-
//
14-
// /**
15-
// * returns best move
16-
// */
17-
// def findMove(): Int
18-
//}
195

206
class MoveSearcher(val board: GomokuBoard) {
217
@volatile var max_depth = 5
@@ -110,10 +96,12 @@ class MoveSearcher(val board: GomokuBoard) {
11096
res
11197
}
11298

99+
import LineInfo._
100+
113101
/**
114102
* returns best move
115103
*/
116-
def search_move(depth: Int, alpha: Int = -Int.MaxValue, b: Int = Int.MaxValue): Int = {
104+
def search_move(depth: Int, alpha: Int = LOSS1, b: Int = WIN1): Int = {
117105
val moves = findPossibleMoves()
118106
if (moves.length == 1) return moves.head
119107
if (moves.length == 0) throw new Error("This should not happen! No available moves.")
@@ -128,7 +116,7 @@ class MoveSearcher(val board: GomokuBoard) {
128116
bestscore = recursedscore
129117
bestmove = moves(i)
130118
}
131-
if (bestscore >= LineInfo.WIN1) {
119+
if (bestscore >= LineInfo.WIN) {
132120
bestmove
133121
} else loop(i + 1)
134122
} else bestmove
@@ -160,14 +148,14 @@ class MoveSearcher(val board: GomokuBoard) {
160148
he.score
161149
} else {
162150
val heur = board.heur_score()
163-
if (math.abs(heur) >= LineInfo.WIN1 || depth <= 0)
151+
if (math.abs(heur) >= LineInfo.WIN || depth <= 0)
164152
heur
165153
else {
166154
val next_depth =
167155
if (threats4(player) > threats4_before) depth
168156
else depth - 1
169157

170-
val moves = findPossibleMoves().sortByInplace(test_move_heur) //.trim(4)
158+
val moves = findPossibleMoves().sortByInplace(test_move_heur(_)) //.trim(4)
171159
if (moves.length == 0) {
172160
board.update_!(coord)(0)
173161
return Int.MaxValue
@@ -195,4 +183,4 @@ class MoveSearcher(val board: GomokuBoard) {
195183
board.update_!(coord)(0)
196184
result
197185
}
198-
}
186+
}

src/magicgoose/gomoku/ai/package.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package magicgoose.gomoku
22

33
import java.util.Arrays
44
import magicgoose.sorting.Ord
5-
//import magicgoose.sorting.DualPivotQuicksortGenSingleton
65
package object ai {
76
import scala.reflect.ClassTag
87

src/magicgoose/sorting/QuickSort2.scala

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,16 @@ import scala.{ specialized => spec }
44
import scala.util.Random
55

66
object QuickSort2 {
7-
8-
// def main(args: Array[String]): Unit = {
9-
// while(true) {
10-
// val a1 = readLine().split(" ").map(_.toInt)
11-
// val a2 = readLine().split(" ").map(_.toInt)
12-
// val maxlen = a1.length max a2.length
13-
// import NaturalOrd._
14-
// QuickSort2.sortInPlace(a1, a2, 0, maxlen - 1)
15-
// println(a1.mkString(" "))
16-
// println(a2.mkString(" "))
17-
//
18-
// }
19-
//
20-
// }
21-
22-
23-
import SimpleSort2.Suck0.swap
24-
import Suck1._
7+
//Dummy objects are for workaround scala specialization bug
8+
import SimpleSort2.Dummy0.swap
9+
import Dummy1._
2510
/**
2611
* sort 2 arrays based on values in second array
2712
*/
2813
def sortInPlace[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]) = {
2914
quicksort(x, y, left, right)
3015
}
31-
private object Suck0 {
16+
private object Dummy0 {
3217
@inline def partition[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int, pivotIndex: Int)(implicit ar: Ord[B]) = {
3318
val pivotValue = y(pivotIndex)
3419
swap(x, y, pivotIndex, right) // Move pivot to end
@@ -45,8 +30,8 @@ object QuickSort2 {
4530
storeIndex
4631
}
4732
}
48-
import Suck0._
49-
private object Suck1 {
33+
import Dummy0._
34+
private object Dummy1 {
5035
@inline def quicksort[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]): Unit = {
5136
if (left < right) {
5237
if (right - left < 5) SimpleSort2.sortInPlace(x, y, left, right)
@@ -62,7 +47,7 @@ object QuickSort2 {
6247
}
6348

6449
object SimpleSort2 {
65-
object Suck0 {
50+
object Dummy0 {
6651
@inline def swap[@spec A, @spec B](x: Array[A], y: Array[B], i1: Int, i2: Int) = {
6752
val tmp = x(i1)
6853
x(i1) = x(i2)
@@ -72,8 +57,8 @@ object SimpleSort2 {
7257
y(i2) = tmp2
7358
}
7459
}
75-
import Suck0._
76-
private object Suck1 {
60+
import Dummy0._
61+
private object Dummy1 {
7762
def sortInPlace[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]) = {
7863
var i1 = left
7964
while (i1 <= right) {
@@ -90,5 +75,5 @@ object SimpleSort2 {
9075
}
9176
// sort 2 arrays based on values in second array
9277
def sortInPlace[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]) =
93-
Suck1.sortInPlace(x, y, left, right)
94-
}
78+
Dummy1.sortInPlace(x, y, left, right)
79+
}

0 commit comments

Comments
 (0)