Пример #1
0
func (b *TBoard) CalcMaterial() {
	b.Material[INDEX_OF_WHITE] = 0
	b.Material[INDEX_OF_BLACK] = 0
	for sq := 0; sq < square.BOARD_SIZE; sq++ {
		p := b.PieceAtSq(square.TSquare(sq))
		c := piece.ColorOf(p)
		t := piece.TypeOf(p)
		b.Material[IndexOfColor(c)] += PIECE_VALUES[t]
	}
}
Пример #2
0
func (pos *TPosition) SetFromFen(fen string) bool {
	var l = len(fen)

	if l <= 0 {
		return false
	}

	var ok = true

	var ptr = 0

	var feni = 0

	for ; (feni < l) && ok; feni++ {
		c := fen[feni]

		if (c >= '1') && (c <= '8') {
			for fill := 0; fill < int(c-'0'); fill++ {
				if ptr < square.BOARD_SIZE {
					pos[ptr] = piece.NO_PIECE
					ptr++
				}
			}
		} else if ptr < square.BOARD_SIZE {
			p := piece.FromFenChar(c)
			if p != piece.NO_PIECE {
				pos[ptr] = byte(p)
				if piece.TypeOf(p) == piece.KING {
					pos[KINGSPOS_INDEX+IndexOfColor(piece.ColorOf(p))] = byte(ptr)
				}
				ptr++
			}
		}

		ok = (ptr < square.BOARD_SIZE)
	}

	if (ptr < square.BOARD_SIZE) || (feni >= (l - 1)) {
		return false
	}

	pos[TURN_INDEX] = piece.WHITE

	feni++

	if fen[feni] == 'b' {
		pos[TURN_INDEX] = piece.BLACK
	}

	pos[DEPTH_INDEX] = 0

	return true
}
Пример #3
0
func (b *TBoard) UnMakeMove(m TMove) {

	b.SetSq(m.From, m.Piece)

	if m.Piece == (piece.WHITE | piece.KING) {
		b.Pos[KINGSPOS_INDEX+1] = byte(m.From)
	}

	if m.Piece == (piece.BLACK | piece.KING) {
		b.Pos[KINGSPOS_INDEX] = byte(m.From)
	}

	b.SetSq(m.To, m.CapPiece)

	b.Material[IndexOfColor(b.GetColorOfTurn())] += PIECE_VALUES[piece.TypeOf(m.CapPiece)]

	b.SetDepth(b.GetDepth() - 1)

	b.SetTurn(InvTurnOf(b.GetTurn()))

}
Пример #4
0
func (b *TBoard) NextLegalMove() bool {
	if b.BlackKingOnBaseRank() {
		return false
	}
	wb := b.WhiteKingOnBaseRank()
	if wb && b.IsWhiteTurn() {
		return false
	}
	if b.HasBestMove {
		if !b.BestMoveDone {
			b.CurrentMove = b.BestMove
			b.BestMoveDone = true
			return true
		}
	}
	for b.NextPseudoLegalMove() {
		b.MakeMove(b.CurrentMove)
		incheck := b.IsInCheck()
		oppincheck := b.IsOppInCheck()
		blackmated := (wb && (!b.BlackKingOnBaseRank()))
		b.UnMakeMove(b.CurrentMove)
		ok := (!incheck) && (!oppincheck) && (!blackmated)
		if !ok {
			//fmt.Printf("move thrown out for check %s\n",b.CurrentMove.ToAlgeb())
		}
		if ok {
			if b.BestMoveDone || (b.CurrentMove != b.BestMove) {
				// move is ok
				if piece.TypeOf(b.CurrentMove.Piece) == piece.KING {
					if square.RankOf(b.CurrentMove.To) > square.RankOf(b.CurrentMove.From) {
						b.CurrentMove.IsForwardKingMove = true
					}
				}
				return true
			}
		}
	}
	return false
}