Class: NeuroGammon::Board
- Inherits:
-
Object
- Object
- NeuroGammon::Board
- Defined in:
- lib/neuro_gammon/board.rb
Constant Summary collapse
- BLACK =
-1- WHITE =
1
Instance Method Summary collapse
- #bar_count(colour) ⇒ Object
- #bearing_off?(colour) ⇒ Boolean
-
#initialize ⇒ Board
constructor
A new instance of Board.
- #move(move, colour) ⇒ Object
- #move!(move, colour, dice = nil) ⇒ Object
- #piece_count(colour) ⇒ Object
- #state ⇒ Object
- #valid_moves(colour, dice) ⇒ Object
- #validate_move(move, colour) ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize ⇒ Board
Returns a new instance of Board.
22 23 24 25 |
# File 'lib/neuro_gammon/board.rb', line 22 def initialize @board_state=[-2,0,0,0,0,5,0,3,0,0,0,-5,5,0,0,0,-3,0,-5,0,0,0,0,2] =[0,0] end |
Instance Method Details
#bar_count(colour) ⇒ Object
31 32 33 |
# File 'lib/neuro_gammon/board.rb', line 31 def colour return colour==Board::BLACK ? [0] : [1] end |
#bearing_off?(colour) ⇒ Boolean
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/neuro_gammon/board.rb', line 110 def bearing_off? colour return false if (colour)!=0 range=Board::BLACK==colour ? (18..23) : (0..5) (0..23).each do |i| if not range.include?(i) return false if board_state[i]*colour>0 end end return true end |
#move(move, colour) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/neuro_gammon/board.rb', line 75 def move move,colour old_state=Marshal.load(Marshal.dump(board_state)) =Marshal.load(Marshal.dump()) move!(move,colour) result=state @board_state=old_state = return result end |
#move!(move, colour, dice = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/neuro_gammon/board.rb', line 85 def move! move,colour,dice=nil validate_move move,colour start=move[0] fin=move[1] if start==-1 Board::BLACK==colour ? [0]-=1 : [1]-=1 elsif fin==-1 @board_state[start]-=colour else @board_state[start]-=colour end if fin!=-1 @board_state[fin]+=colour if (board_state[fin]==0) @board_state[fin]+=colour Board::BLACK==colour ? [1]+=1 : [0]+=1 end end if !dice.nil? update_dice(dice, move) end end |
#piece_count(colour) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/neuro_gammon/board.rb', line 35 def piece_count colour result=0 board_state.each do |x| result+=x.abs if (x/colour > 0) end result+=(colour) return result; end |
#state ⇒ Object
27 28 29 |
# File 'lib/neuro_gammon/board.rb', line 27 def state [@board_state,] end |
#valid_moves(colour, dice) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/neuro_gammon/board.rb', line 121 def valid_moves colour,dice result=[] if ((colour)>0) dice.to_a.uniq.each do |d| dest=colour==Board::BLACK ? d-1 : 24-d if board_state[dest]*colour >= -1 and board_state[dest]*colour < 5 result << [-1,dest] end end else dice.to_a.uniq.each do |d| occupied_spaces(colour).each do |x| dest=x+d*-colour if ((0..23).include?(dest)) if board_state[dest]*colour >= -1 and board_state[dest]*colour < 5 result << [x,dest] end end end if (bearing_off?(colour)) bearing_off_range=colour==Board::BLACK ? (18..23).to_a : (0..5).to_a.reverse bearing_off_range.each do |x| dest=x+d*-colour if board_state[x]*colour>0 if (!bearing_off_range.include?(dest)) if board_state[x]*colour>0 result << [x,-1] break end else break end end end end end end return result end |
#validate_move(move, colour) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/neuro_gammon/board.rb', line 44 def validate_move move,colour valid_range=colour==WHITE ? (18..23) : (0..5) start=move[0] fin=move[1] if (move[0]==-1) raise Execption.new("No piece on the bar") if ((colour)==0) raise Exception.new("Invalid destination") if !valid_range.include?(fin) raise Exception.new("Wrong colour at destination") if board_state[fin]*colour < -1 elsif (move[1]==-1) validate_bearing_off_move(move,colour) else raise Exception.new("Unable to move while there is a piece on the bar") if (colour)>0 raise Exception.new("Wrong colour at destination") if board_state[fin]*colour < -1 raise Exception.new("Move must specify start and end") if move.size!=2 raise Exception.new("Invalid start position") if not (0..23).to_a.include?(start) raise Exception.new("Invalid destination position") if not (0..23).to_a.include?(fin) if (colour==Board::WHITE) raise Exception.new("Wrong direction for WHITE") if start<=fin else raise Exception.new("Wrong direction for BLACK") if start>=fin end raise Exception.new("Wrong colour at starting point") if board_state[start]/colour < 0 raise Exception.new("No piece at starting point") if board_state[start]==0 raise Exception.new("Destination is already full") if board_state[fin].abs >= 5 end end |
#winner ⇒ Object
161 162 163 164 165 |
# File 'lib/neuro_gammon/board.rb', line 161 def winner return Board::WHITE if piece_count(Board::WHITE)==0 return Board::BLACK if piece_count(Board::BLACK)==0 return nil end |