Class: Binary_Puzzle_Solver::Board_View
- Inherits:
-
Board
- Object
- Board
- Binary_Puzzle_Solver::Board_View
show all
- Defined in:
- lib/binary_puzzle_solver/base.rb
Instance Attribute Summary
Attributes inherited from Board
#iters_quota, #num_iters_done
Instance Method Summary
collapse
Methods inherited from Board
#add_move, #add_to_iters_quota, #as_string, #dim_range, #flush_moves, #get_cell_state, #get_complete_map, #get_moves, #get_new_move, #get_view, #list_views, #max_idx, #num_moves_done, #opposite_value, #rotate_dir, #try_to_solve_using, #validate
Constructor Details
#initialize(board, rotation) ⇒ Board_View
Returns a new instance of Board_View.
405
406
407
408
409
410
411
412
413
414
415
|
# File 'lib/binary_puzzle_solver/base.rb', line 405
def initialize (board, rotation)
@board = board
@rotation = rotation
if rotation
@dims_map = {:x => :y, :y => :x}
else
@dims_map = {:x => :x, :y => :y}
end
return
end
|
Instance Method Details
#_append_move(params) ⇒ Object
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
# File 'lib/binary_puzzle_solver/base.rb', line 472
def _append_move(params)
coord = params[:coord]
dir = params[:dir]
@board.add_move(
Move.new(
:coord => _calc_mapped_coord(coord),
:val => params[:val],
:reason => params[:reason],
:dir => _calc_mapped_dir(dir)
)
)
return
end
|
#_calc_mapped_coord(coord) ⇒ Object
429
430
431
|
# File 'lib/binary_puzzle_solver/base.rb', line 429
def _calc_mapped_coord(coord)
return _calc_mapped_item(coord, :rotate_coord)
end
|
#_calc_mapped_dir(dir) ⇒ Object
433
434
435
|
# File 'lib/binary_puzzle_solver/base.rb', line 433
def _calc_mapped_dir(dir)
return _calc_mapped_item(dir, :rotate_dir)
end
|
#_calc_mapped_item(item, rotation_method) ⇒ Object
421
422
423
424
425
426
427
|
# File 'lib/binary_puzzle_solver/base.rb', line 421
def _calc_mapped_item(item, rotation_method)
if @rotation then
return method(rotation_method).call(item)
else
return item
end
end
|
#_get_cell(coord) ⇒ Object
437
438
439
|
# File 'lib/binary_puzzle_solver/base.rb', line 437
def _get_cell(coord)
return @board._get_cell(_calc_mapped_coord(coord))
end
|
#check_and_handle_cells_of_one_value_in_row_were_all_found(params) ⇒ Object
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
# File 'lib/binary_puzzle_solver/base.rb', line 588
def check_and_handle_cells_of_one_value_in_row_were_all_found(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
full_val = row.get_summary().find_full_value()
if (full_val)
opposite_val = opposite_value(full_val)
row.iter_of_handles().each do |cell_h|
x = cell_h.x
cell = cell_h.get_cell
cell_state = cell.state
if cell_state == Cell::UNKNOWN
perform_and_append_move(
:coord => row.get_coord(x),
:val => opposite_val,
:reason => "Filling unknowns in row with an exceeded value with the other value",
:dir => col_dim()
)
end
end
end
return
end
|
#check_and_handle_known_unknown_sameknown_in_row(params) ⇒ Object
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
# File 'lib/binary_puzzle_solver/base.rb', line 556
def check_and_handle_known_unknown_sameknown_in_row(params)
row_idx = params[:idx]
prev_cell_states = []
max_in_a_row = 2
row = get_row_handle(row_idx)
(1 .. (row.max_idx - 1)).each do |x|
get_coord = lambda { |offset| row.get_coord(x+offset); }
get_state = lambda { |offset| row.get_state(x+offset); }
if (get_state.call(-1) != Cell::UNKNOWN and
get_state.call(0) == Cell::UNKNOWN and
get_state.call(1) == get_state.call(-1))
then
perform_and_append_move(
:coord => get_coord.call(0),
:val => opposite_value(get_state.call(-1)),
:reason => "In between two identical cells",
:dir => col_dim()
)
end
end
return
end
|
#check_and_handle_sequences_in_row(params) ⇒ Object
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
# File 'lib/binary_puzzle_solver/base.rb', line 500
def check_and_handle_sequences_in_row(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
prev_cell_states = []
max_in_a_row = 2
handle_prev_cell_states = lambda { |x|
if prev_cell_states.length > max_in_a_row then
raise GameIntegrityException, "Too many #{prev_cell_states[0]} in a row"
elsif (prev_cell_states.length == max_in_a_row)
coords = Array.new
start_x = x - max_in_a_row - 1;
if (start_x >= 0)
coords << row.get_coord(start_x)
end
if (x <= row.max_idx())
coords << row.get_coord(x)
end
coords.each do |c|
if (get_cell_state(c) == Cell::UNKNOWN)
perform_and_append_move(
:coord => c,
:val => opposite_value(prev_cell_states[0]),
:reason => "Vicinity to two in a row",
:dir => col_dim()
)
end
end
end
return
}
row.iter_of_handles().each do |h|
x = h.x
cell_state = h.get_cell.state
if cell_state == Cell::UNKNOWN
handle_prev_cell_states.call(x)
prev_cell_states = []
elsif ((prev_cell_states.length == 0) or
(prev_cell_states[-1] != cell_state)) then
handle_prev_cell_states.call(x)
prev_cell_states = [cell_state]
else
prev_cell_states << cell_state
end
end
handle_prev_cell_states.call(row.max_idx + 1)
return
end
|
#check_exceeded_numbers_while_accounting_for_two_unknown_gaps(params) ⇒ Object
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
# File 'lib/binary_puzzle_solver/base.rb', line 618
def check_exceeded_numbers_while_accounting_for_two_unknown_gaps(params)
row_idx = params[:idx]
row = get_row_handle(row_idx)
gaps = {}
next_gap = []
add_gap = lambda {
l = next_gap.length
if (l > 0)
if not gaps[l]
gaps[l] = []
end
gaps[l] << next_gap
next_gap = []
end
}
row.iter_of_handles().each do |cell_h|
if (cell_h.get_state == Cell::UNKNOWN)
next_gap << cell_h.x
else
add_gap.call()
end
end
add_gap.call()
if (gaps.has_key?(2)) then
implicit_counts = {Cell::ZERO => 0, Cell::ONE => 0,}
gaps[2].each do |gap|
x_s = []
if (gap[0] > 0)
x_s << gap[0]-1
end
if (gap[-1] < row.max_idx)
x_s << gap[-1]+1
end
bordering_values = {Cell::ZERO => 0, Cell::ONE => 0,}
x_s.each do |x|
bordering_values[row.get_state(x)] += 1
end
for v in [Cell::ZERO, Cell::ONE] do
if bordering_values[opposite_value(v)] > 0
implicit_counts[v] += 1
end
end
end
summ = row.get_summary()
v = [Cell::ZERO, Cell::ONE].find {
|v| summ.get_count(v) + implicit_counts[v] \
== summ.half_limit()
}
if v then
gap_keys = gaps.keys.select { |x| x != 2 }
opposite_val = opposite_value(v)
gap_keys.each do |k|
gaps[k].each do |gap|
gap.each do |x|
perform_and_append_move(
:coord => row.get_coord(x),
:val => opposite_val,
:reason => \
"Analysis of gaps and their neighboring values",
:dir => row.col_dim()
)
end
end
end
end
end
end
|
#check_try_placing_last_of_certain_digit_in_row(params) ⇒ Object
734
735
736
737
738
739
740
741
|
# File 'lib/binary_puzzle_solver/base.rb', line 734
def check_try_placing_last_of_certain_digit_in_row(params)
row_idx = params[:idx]
return _generic_check_try_placing_last_digit(
:idx => row_idx,
:callback_method => :_do_values_have_a_three_in_a_row,
)
end
|
#check_try_placing_last_of_certain_digit_in_row_to_avoid_dups(params) ⇒ Object
725
726
727
728
729
730
731
732
|
# File 'lib/binary_puzzle_solver/base.rb', line 725
def check_try_placing_last_of_certain_digit_in_row_to_avoid_dups(params)
row_idx = params[:idx]
return _generic_check_try_placing_last_digit(
:idx => row_idx,
:callback_method => :_are_values_duplicates,
)
end
|
#col_dim ⇒ Object
456
457
458
|
# File 'lib/binary_puzzle_solver/base.rb', line 456
def col_dim()
return :x
end
|
#get_complete_row_map ⇒ Object
464
465
466
|
# File 'lib/binary_puzzle_solver/base.rb', line 464
def get_complete_row_map()
return @board.get_complete_map(mapped_row_dim())
end
|
#get_row_handle(idx) ⇒ Object
468
469
470
|
# File 'lib/binary_puzzle_solver/base.rb', line 468
def get_row_handle(idx)
return RowHandle.new(self, idx)
end
|
#get_row_summary(params) ⇒ Object
445
446
447
448
449
450
|
# File 'lib/binary_puzzle_solver/base.rb', line 445
def get_row_summary(params)
return @board.get_row_summary(
:idx => params[:idx],
:dim => @dims_map[params[:dim]]
)
end
|
#limit(dim) ⇒ Object
441
442
443
|
# File 'lib/binary_puzzle_solver/base.rb', line 441
def limit(dim)
return @board.limit(@dims_map[dim])
end
|
#mapped_row_dim ⇒ Object
460
461
462
|
# File 'lib/binary_puzzle_solver/base.rb', line 460
def mapped_row_dim()
return _calc_mapped_dir(row_dim())
end
|
495
496
497
498
|
# File 'lib/binary_puzzle_solver/base.rb', line 495
def perform_and_append_move(params)
set_cell_state( params[:coord], params[:val] )
_append_move(params)
end
|
#rotate_coord(coord) ⇒ Object
417
418
419
|
# File 'lib/binary_puzzle_solver/base.rb', line 417
def rotate_coord(coord)
return coord.rotate
end
|
#row_dim ⇒ Object
452
453
454
|
# File 'lib/binary_puzzle_solver/base.rb', line 452
def row_dim()
return :y
end
|
#set_cell_state(coord, state) ⇒ Object
489
490
491
492
493
|
# File 'lib/binary_puzzle_solver/base.rb', line 489
def set_cell_state(coord, state)
@board.set_cell_state(
_calc_mapped_coord(coord), state
)
end
|
#validate_rows ⇒ Object
791
792
793
794
795
796
797
798
799
800
801
|
# File 'lib/binary_puzzle_solver/base.rb', line 791
def validate_rows()
is_final = true
dim_range(row_dim()).each do |row_idx|
row = get_row_handle(row_idx)
ret = row.validate(:foo => false)
is_final &&= ret[:is_final]
end
return is_final
end
|