Class: RubyShogi::Mgen

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_shogi/mgen.rb

Direct Known Subclasses

MgenBlack, MgenWhite

Constant Summary collapse

ROOK_MOVES =

both

[[1, 0], [0, 1], [-1, 0], [0, -1]].freeze
BISHOP_MOVES =
[[1, 1], [-1, 1], [1, -1], [-1, -1]].freeze
KING_MOVES =
(ROOK_MOVES + BISHOP_MOVES).freeze
ROOK_MOVES
BISHOP_MOVES
WHITE_GOLD_GENERAL_MOVES =

white

[[1, 0], [-1, 0], [0, -1], [0, 1], [1, 1], [-1, 1]].freeze
WHITE_SILVER_GENERAL_MOVES =
[[-1, -1], [-1, 1], [0, 1], [1, 1], [1, -1]].freeze
WHITE_KNIGHT_MOVES =
[[-1, 2], [1, 2]].freeze
WHITE_LANCE_MOVES =
[[0, 1]].freeze
BLACK_GOLD_GENERAL_MOVES =

black

[[1, 0], [-1, 0], [0, 1], [0, -1], [1, -1], [-1, -1]].freeze
BLACK_SILVER_GENERAL_MOVES =
[[-1, 1], [-1, -1], [0, -1], [1, -1], [1, 1]].freeze
BLACK_KNIGHT_MOVES =
[[-1, -2], [1, -2]].freeze
BLACK_LANCE_MOVES =
[[0, -1]].freeze
PROMO_NO =

promotions

0
PROMO_STAY =
1
PROMO_YES =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Mgen

Returns a new instance of Mgen.



37
38
39
40
41
42
# File 'lib/ruby_shogi/mgen.rb', line 37

def initialize(board)
	@board, @moves = board, []
	@x_gen, @y_gen, @from_gen = 0, 0, 0 # move generation
	@x_checks, @y_checks = 0, 0 # checks
	@pseudo_moves = false # 3x speed up
end

Instance Attribute Details

#only_capturesObject

Returns the value of attribute only_captures.



35
36
37
# File 'lib/ruby_shogi/mgen.rb', line 35

def only_captures
  @only_captures
end

#pseudo_movesObject

Returns the value of attribute pseudo_moves.



35
36
37
# File 'lib/ruby_shogi/mgen.rb', line 35

def pseudo_moves
  @pseudo_moves
end

Instance Method Details

#checks_b?Boolean

(here)

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ruby_shogi/mgen.rb', line 144

def checks_b?#(here)
	here = @board.wking
	81.times do |i|
		@x_checks, @y_checks = i % 9, i / 9 
		case @board.brd[i]
		when -1 then return true if pawn_checks_b?(here)
		when -2 then return true if jump_checks_to?(BLACK_GOLD_GENERAL_MOVES, here)
		when -3 then return true if slider_checks_to?(BLACK_LANCE_MOVES, here)
		when -4 then return true if jump_checks_to?(BLACK_GOLD_GENERAL_MOVES, here)
		when -5 then return true if jump_checks_to?(BLACK_KNIGHT_MOVES, here)
		when -6 then return true if jump_checks_to?(BLACK_GOLD_GENERAL_MOVES, here)
		when -7 then return true if jump_checks_to?(BLACK_SILVER_GENERAL_MOVES, here)
		when -8 then return true if jump_checks_to?(BLACK_GOLD_GENERAL_MOVES, here)
		when -9 then return true if jump_checks_to?(BLACK_GOLD_GENERAL_MOVES, here)
		when -10 then return true if slider_checks_to?(BISHOP_MOVES, here)
		when -11 then return true if slider_checks_to?(BISHOP_MOVES, here) || jump_checks_to?(PROMOTED_BISHOP_MOVES, here)
		when -12 then return true if slider_checks_to?(ROOK_MOVES, here)
		when -13 then return true if slider_checks_to?(ROOK_MOVES, here) || jump_checks_to?(PROMOTED_ROOK_MOVES, here)
		when -14 then return true if jump_checks_to?(KING_MOVES, here)
		end
	end
	false
end

#checks_w?Boolean

(here)

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ruby_shogi/mgen.rb', line 120

def checks_w?#(here)
	here = @board.bking
	81.times do |i|
		@x_checks, @y_checks = i % 9, i / 9 
		case @board.brd[i]
		when 1 then return true if pawn_checks_w?(here)
		when 2 then return true if jump_checks_to?(WHITE_GOLD_GENERAL_MOVES, here)
		when 3 then return true if slider_checks_to?(WHITE_LANCE_MOVES, here)
		when 4 then return true if jump_checks_to?(WHITE_GOLD_GENERAL_MOVES, here)
		when 5 then return true if jump_checks_to?(WHITE_KNIGHT_MOVES, here)
		when 6 then return true if jump_checks_to?(WHITE_GOLD_GENERAL_MOVES, here)
		when 7 then return true if jump_checks_to?(WHITE_SILVER_GENERAL_MOVES, here)
		when 8 then return true if jump_checks_to?(WHITE_GOLD_GENERAL_MOVES, here)
		when 9 then return true if jump_checks_to?(WHITE_GOLD_GENERAL_MOVES, here)
		when 10 then return true if slider_checks_to?(BISHOP_MOVES, here)
		when 11 then return true if slider_checks_to?(BISHOP_MOVES, here) || jump_checks_to?(PROMOTED_BISHOP_MOVES, here)
		when 12 then return true if slider_checks_to?(ROOK_MOVES, here)
		when 13 then return true if slider_checks_to?(ROOK_MOVES, here) || jump_checks_to?(PROMOTED_ROOK_MOVES, here)
		when 14 then return true if jump_checks_to?(KING_MOVES, here)
		end
	end
	false
end

#eaten_piece(eat) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_shogi/mgen.rb', line 74

def eaten_piece(eat) 
	case eat
	when 2 then 1
	when 4 then 3
	when 6 then 5
	when 8 then 7
	when 11 then 10
	when 13 then 12
	else eat
	end
end

#good_coord?(i) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ruby_shogi/mgen.rb', line 70

def good_coord?(i)
	i >= 0 && i <= 80
end

#is_on_board?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ruby_shogi/mgen.rb', line 52

def is_on_board?(x, y)	
	x >= 0 && x <= 8 && y >= 0 && y <= 8
end

#jump_checks_to?(jumps, here) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/ruby_shogi/mgen.rb', line 112

def jump_checks_to?(jumps, here)
	jumps.each do |jmp|
		px, py = @x_checks + jmp[0], @y_checks + jmp[1]
		return true if px + py * 9 == here
	end
	false
end

#pawn_checks_b?(here) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ruby_shogi/mgen.rb', line 94

def pawn_checks_b?(here)
	@x_checks + 9 * (@y_checks - 1) == here
end

#pawn_checks_w?(here) ⇒ Boolean

Checks

Returns:

  • (Boolean)


90
91
92
# File 'lib/ruby_shogi/mgen.rb', line 90

def pawn_checks_w?(here)
	@x_checks + 9 * (@y_checks + 1) == here
end

Utils



48
49
50
# File 'lib/ruby_shogi/mgen.rb', line 48

def print_move_list
	@moves.each_with_index { |board, i| puts "#{i}: #{board.move_str}" }
end

#remove_from_array(array, x) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_shogi/mgen.rb', line 56

def remove_from_array(array, x)
	found, a = false, []
	array.each do |q|
		if q == x
			a.push(q) if found
			found = true 
		else
			a.push(q)
		end
	end
	fail if !found
	a
end

#slider_checks_to?(slider, here) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_shogi/mgen.rb', line 98

def slider_checks_to?(slider, here)
	slider.each do |jmp|
		px, py = @x_checks, @y_checks
		loop do
			px, py = px + jmp[0], py + jmp[1]
			break if !is_on_board?(px, py)
			to = px + py * 9
			return true if to == here
			break if !@board.empty?(to)
		end
	end
	false
end