Exemple #1
0
// simple heuristic for board evaluation
func scoreBoard(b game.Board, c game.Color) (score int) {

	var myPieceCount, theirPieceCount int

	for x := 0; x < b.Width(); x++ {
		for y := 0; y < b.Height(); y++ {
			sq, _ := b.Get(game.Position{x, y})
			if sq.PieceColor == c {
				myPieceCount += sq.PieceCount
				if sq.PieceCount == 1 || sq.PieceCount > 4 {
					score--
				}
			} else {
				theirPieceCount += sq.PieceCount
				if sq.PieceCount == 1 || sq.PieceCount > 4 {
					score++
				}
			}
		}
	}

	return score + 5*int(float64(myPieceCount-theirPieceCount)*float64(2*b.Width())/float64(myPieceCount)+0.5)
}
Exemple #2
0
func (p cliPlayer) Move(b game.Board) game.Move {
	buf := new(bytes.Buffer)

	buf.WriteString(" " + strings.Repeat("-", 5*b.Width()-1) + "\n")

	for y := 0; y < b.Height(); y++ {
		buf.WriteByte('|')
		for x := 0; x < b.Width(); x++ {
			sq, _ := b.Get(game.Position{x, y})
			if sq.PieceCount == 0 {
				buf.WriteString("    ")
			} else {
				var c string
				if sq.PieceColor == game.Black {
					c = "b"
				} else {
					c = "w"
				}

				num := strconv.Itoa(sq.PieceCount)
				if len(num) == 1 {
					buf.WriteString(" " + num + c + " ")
				} else {
					buf.WriteString(num + c + " ")
				}
			}

			buf.WriteByte('|')
		}

		buf.WriteString(" " + strconv.Itoa(y) + "\n")
		buf.WriteString(" " + strings.Repeat("-", 5*b.Width()-1) + "\n")
	}

	buf.WriteString(" ")
	for x := 0; x < b.Width(); x++ {
		fmt.Fprintf(buf, " %2d  ", x)
	}
	buf.WriteString("\n\n")

	clear := exec.Command("clear")
	clear.Stdout = os.Stdout
	clear.Run()

	os.Stdout.Write(buf.Bytes())

	for {
		var (
			move game.Move

			n   int
			err error
		)

		for n < 4 || err != nil {
			fmt.Fprintf(os.Stdout, "Enter move for %s (fromx,fromy tox,toy count): ", p.Name())
			scanner := bufio.NewScanner(os.Stdin)
			scanner.Scan()
			n, err = fmt.Sscanf(scanner.Text(), "%d,%d %d,%d %d",
				&move.From.X,
				&move.From.Y,
				&move.To.X,
				&move.To.Y,
				&move.PieceCount,
			)
		}

		available := b.AvailableMoves(p.Color())
		for _, m := range available {
			if move == m {
				return move
			}
		}

		fmt.Fprintf(os.Stdout, "Move %+v is not legal!\n", move)
	}
}