Skip to content

Commit

Permalink
some style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
magicgoose committed Jun 22, 2013
1 parent b05de2f commit 840323f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 53 deletions.
17 changes: 10 additions & 7 deletions src/magicgoose/gomoku/ai/GomokuBoard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,18 @@ class GomokuBoard private (
if (li(current_player, 4) >= 1)
WIN1
else if (li(-current_player, 4, OPEN) >= 1
// || li(-current_player, 4) >= 1
// &&
// li(-current_player, 3, OPEN) +
// li(-current_player, 3, BROKEN) >= 1
|| li(-current_player, 4) >= 1
&&
li(-current_player, 3, OPEN) +
li(-current_player, 3, BROKEN) >= 1
||
li(current_player, 3) == 0 &&
li(-current_player, 3, OPEN)+li(-current_player, 3, BROKEN) > 1
)
LOSS1
// else if (li(current_player, 3, OPEN) +
// li(current_player, 3, BROKEN) >= 1)
// WIN2
else if (li(current_player, 3, OPEN) +
li(current_player, 3, BROKEN) >= 1)
WIN2
else if (li(-current_player, 4) + li(-current_player, 3, OPEN) + li(-current_player, 3, BROKEN) >= 2)
LOSS2
else 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ package magicgoose.gomoku.ai

import scala.annotation.tailrec
import java.util.Arrays
/**
* General interface for an implementation of AI-powered move searching
*/
//trait MoveSearcher {
// /**
// * returns all canditates for next move
// */
// def findPossibleMoves(): Indexed[Int]
//
// /**
// * returns best move
// */
// def findMove(): Int
//}

class MoveSearcher(val board: GomokuBoard) {
@volatile var max_depth = 5
Expand Down Expand Up @@ -110,10 +96,12 @@ class MoveSearcher(val board: GomokuBoard) {
res
}

import LineInfo._

/**
* returns best move
*/
def search_move(depth: Int, alpha: Int = -Int.MaxValue, b: Int = Int.MaxValue): Int = {
def search_move(depth: Int, alpha: Int = LOSS1, b: Int = WIN1): Int = {
val moves = findPossibleMoves()
if (moves.length == 1) return moves.head
if (moves.length == 0) throw new Error("This should not happen! No available moves.")
Expand All @@ -128,7 +116,7 @@ class MoveSearcher(val board: GomokuBoard) {
bestscore = recursedscore
bestmove = moves(i)
}
if (bestscore >= LineInfo.WIN1) {
if (bestscore >= LineInfo.WIN) {
bestmove
} else loop(i + 1)
} else bestmove
Expand Down Expand Up @@ -160,14 +148,14 @@ class MoveSearcher(val board: GomokuBoard) {
he.score
} else {
val heur = board.heur_score()
if (math.abs(heur) >= LineInfo.WIN1 || depth <= 0)
if (math.abs(heur) >= LineInfo.WIN || depth <= 0)
heur
else {
val next_depth =
if (threats4(player) > threats4_before) depth
else depth - 1

val moves = findPossibleMoves().sortByInplace(test_move_heur) //.trim(4)
val moves = findPossibleMoves().sortByInplace(test_move_heur(_)) //.trim(4)
if (moves.length == 0) {
board.update_!(coord)(0)
return Int.MaxValue
Expand Down Expand Up @@ -195,4 +183,4 @@ class MoveSearcher(val board: GomokuBoard) {
board.update_!(coord)(0)
result
}
}
}
1 change: 0 additions & 1 deletion src/magicgoose/gomoku/ai/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package magicgoose.gomoku

import java.util.Arrays
import magicgoose.sorting.Ord
//import magicgoose.sorting.DualPivotQuicksortGenSingleton
package object ai {
import scala.reflect.ClassTag

Expand Down
37 changes: 11 additions & 26 deletions src/magicgoose/sorting/QuickSort2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,16 @@ import scala.{ specialized => spec }
import scala.util.Random

object QuickSort2 {

// def main(args: Array[String]): Unit = {
// while(true) {
// val a1 = readLine().split(" ").map(_.toInt)
// val a2 = readLine().split(" ").map(_.toInt)
// val maxlen = a1.length max a2.length
// import NaturalOrd._
// QuickSort2.sortInPlace(a1, a2, 0, maxlen - 1)
// println(a1.mkString(" "))
// println(a2.mkString(" "))
//
// }
//
// }


import SimpleSort2.Suck0.swap
import Suck1._
//Dummy objects are for workaround scala specialization bug
import SimpleSort2.Dummy0.swap
import Dummy1._
/**
* sort 2 arrays based on values in second array
*/
def sortInPlace[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]) = {
quicksort(x, y, left, right)
}
private object Suck0 {
private object Dummy0 {
@inline def partition[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int, pivotIndex: Int)(implicit ar: Ord[B]) = {
val pivotValue = y(pivotIndex)
swap(x, y, pivotIndex, right) // Move pivot to end
Expand All @@ -45,8 +30,8 @@ object QuickSort2 {
storeIndex
}
}
import Suck0._
private object Suck1 {
import Dummy0._
private object Dummy1 {
@inline def quicksort[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]): Unit = {
if (left < right) {
if (right - left < 5) SimpleSort2.sortInPlace(x, y, left, right)
Expand All @@ -62,7 +47,7 @@ object QuickSort2 {
}

object SimpleSort2 {
object Suck0 {
object Dummy0 {
@inline def swap[@spec A, @spec B](x: Array[A], y: Array[B], i1: Int, i2: Int) = {
val tmp = x(i1)
x(i1) = x(i2)
Expand All @@ -72,8 +57,8 @@ object SimpleSort2 {
y(i2) = tmp2
}
}
import Suck0._
private object Suck1 {
import Dummy0._
private object Dummy1 {
def sortInPlace[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]) = {
var i1 = left
while (i1 <= right) {
Expand All @@ -90,5 +75,5 @@ object SimpleSort2 {
}
// sort 2 arrays based on values in second array
def sortInPlace[@spec A, @spec B](x: Array[A], y: Array[B], left: Int, right: Int)(implicit ar: Ord[B]) =
Suck1.sortInPlace(x, y, left, right)
}
Dummy1.sortInPlace(x, y, left, right)
}

0 comments on commit 840323f

Please sign in to comment.