celtic / chess

chess AI

This URL has Read+Write access

chess / first_engine.rb
100644 32 lines (23 sloc) 0.738 kb
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
# My First Engine
 
module Chess::Engine; end
 
module Chess::Engine::FirstEngine
  UNIT_SCORES = {
    Chess::Piece::PAWN => 1,
    Chess::Piece::BISHOP => 3,
    Chess::Piece::KNIGHT => 3,
    Chess::Piece::ROOK => 5,
    Chess::Piece::QUEEN => 8,
    Chess::Piece::KING => 0
  }
 
  MOVES_PER_UNIT = 5
 
  def position colour
    my_pieces = pieces_owned_by colour
    score = 0
 
    # add the score of each unit we actually own here
    score += MOVES_PER_UNIT * my_pieces.map {|p| UNIT_SCORES[p.type]}.inject {|x,y| x+y}
 
    # now add points for each square our units can move to
    score += my_pieces.map {|p| p.possible_targets.length}.inject {|x,y| x+y}
 
    # TODO: add points for pieces defending each other?
 
    score
  end
end