Created
December 17, 2017 01:28
-
-
Save madipally/2eab84abd777e4d22646db65201569e8 to your computer and use it in GitHub Desktop.
capturing the opponent while moving to target
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
class MoveToTarget | |
COLS = ('a'..'h').to_a | |
ROWS = (1..8).to_a | |
def initialize | |
@knight_moves_arr = [[-1,-2],[1,-2],[-2,-1],[2,-1],[-1,2],[1,2],[-2,1],[2,1]] | |
@queen_moves_arr = [[1,1],[-1,-1],[1,-1],[-1,1],[0,1],[0,-1],[1,0],[-1,0]] | |
@rook_moves_arr = [[0,1],[0,-1],[1,0],[-1,0]] | |
@moves =[] | |
end | |
def move_to_target(piece,pos,target,cnt) | |
@moves,num_steps,stop = piece_move(piece,pos,target,cnt) | |
if stop != 1 | |
if stop == "opponent" | |
return @moves,@num_steps,stop | |
end | |
@moves =[]<< "#{target[0].downcase}#{pos[1].to_i}" | |
@moves << "#{target[0].downcase}#{target[1].to_i}" | |
@num_steps = @moves.count | |
end | |
moving_steps = trace_opponent(pos,target) | |
return moving_steps,@moves | |
end | |
def trace_opponent(pos,target) | |
pos_row = pos[1] | |
pos_col = pos[0] | |
initial_target = @moves[0] | |
initial_target_row = initial_target[1] | |
initial_target_col = initial_target[0] | |
target_row = target[1] | |
target_col = target[0] | |
pos_col > initial_target_col ? count = -1 : count = 1 | |
moving_steps = [] | |
loop do | |
break if pos_col == initial_target_col | |
pos_col = move_col(pos_col,count) | |
moving_steps << "#{pos_col}#{pos_row}" | |
end | |
for move in (initial_target_row..target_row).to_a | |
moving_steps << "#{initial_target_col}#{move}" | |
end | |
return moving_steps.uniq | |
end | |
def piece_move(piece,pos,target,target_count) | |
col = pos[0].downcase | |
row = pos[1].to_i | |
raise "Invalid row #{row} at #{pos}" if validate_pos?(row,'a') == false | |
raise "Invalid column #{col} at #{pos}" if validate_pos?(1,col) == false | |
case piece | |
when 'knight' | |
step_arr = @knight_moves_arr | |
when 'queen' | |
step_arr = @queen_moves_arr | |
when 'rook' | |
step_arr = @rook_moves_arr | |
end | |
step_arr.each do |elem| | |
@moves.clear | |
@moves,@num_steps,stop = move_piece(piece,row,col,elem,count=0,target,target_count) | |
return @moves,@num_steps,stop if stop == 1 || stop == "opponent" | |
end | |
end | |
def move_col(pos,count) | |
col = (pos.ord + count).chr | |
return col if validate_pos?(1,col) | |
end | |
def validate_pos?(row,col) | |
res = ROWS.include?(row) ? row : false | |
return res if res == false | |
res =COLS.include?(col) ? col : false | |
return res if res == false | |
return true | |
end | |
def move_piece(piece,row,col,elem,count,target,target_count) | |
row_to_move = row + (elem[0]) | |
col_to_move = move_col(col,elem[1]) | |
if validate_pos?(row_to_move,'a') | |
if col_to_move | |
puts "#{col_to_move}#{row_to_move}" | |
#puts target_count | |
#if @opponent_piecs_pos.include?("#{col_to_move}#{row_to_move}") | |
#@moves << "#{col_to_move}#{row_to_move}" | |
#return @moves,target_count,stop="opponent" | |
#end | |
if target == "#{col_to_move}#{row_to_move}" | |
@moves << "#{col_to_move}#{row_to_move}" | |
return @moves,target_count,stop=1 | |
end | |
return @moves<<"#{col_to_move}#{row_to_move}" if piece == 'knight' | |
move_piece(piece,row_to_move,col_to_move,elem,count+=1,target,target_count=1) | |
else | |
@moves<<"#{col}#{row}" if count!=0 | |
return @moves,target_count | |
end | |
else | |
@moves<<"#{col}#{row}" if count!=0 | |
return @moves,target_count | |
end | |
end | |
end | |
time = Benchmark.measure do | |
piece = "queen" | |
pos = "d3" | |
target="a6" | |
#MoveToTarget.new.move_to_target('knight','b1','g8',1) | |
steps,original_moves = MoveToTarget.new.move_to_target(piece,pos,target,1) | |
#steps,count = MoveToTarget.new.move_to_target('rook','a8','f6',1) | |
opponent_piecs_pos = ['c2','a4','f5','c4','d5','g6'] | |
opp_array = opponent_piecs_pos.inject([]) do |opp_array,elem| | |
opp_array << elem if steps.include?(elem) | |
opp_array | |
end | |
opp_array.sort! | |
if opp_array.size>0 | |
puts "Captured the opponent at #{opp_array[0]} while moving the #{piece} from #{pos} to #{target} " | |
else | |
puts "moved the #{piece} from #{pos} to #{target} in #{steps} steps" | |
end | |
#puts elem | |
end | |
puts time | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment