Class: Cliptic::Main::Player::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/cliptic/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game:) ⇒ Controller

Returns a new instance of Controller.



751
752
753
# File 'lib/cliptic/main.rb', line 751

def initialize(game:)
  @game = game
end

Instance Attribute Details

#gameObject (readonly)

Returns the value of attribute game.



750
751
752
# File 'lib/cliptic/main.rb', line 750

def game
  @game
end

Instance Method Details

#arrow(char) ⇒ Object



788
789
790
791
792
793
794
795
796
# File 'lib/cliptic/main.rb', line 788

def arrow(char)
  mv = case char
  when Curses::KEY_UP    then {y:-1}
  when Curses::KEY_DOWN  then {y:1}
  when Curses::KEY_LEFT  then {x:-1}
  when Curses::KEY_RIGHT then {x:1}
  end
  ->{game.board.move(**mv)}
end

#await_deleteObject



844
845
846
847
848
# File 'lib/cliptic/main.rb', line 844

def await_delete
  case game.user_input
  when ?w then game.board.clear_clue
  end
end

#await_int(n:) ⇒ Object



827
828
829
830
831
832
833
834
835
# File 'lib/cliptic/main.rb', line 827

def await_int(n:)
  char = game.user_input
  case char
  when ?g then ->{game.board.goto_clue(n:n)}
  when ?G then ->{game.board.goto_cell(n:n)}
  when ?0..?9 then await_int(n:(10*n)+char.to_i)
  else normal(char:char, n:n)
  end
end

#await_replaceObject



836
837
838
839
840
841
842
843
# File 'lib/cliptic/main.rb', line 836

def await_replace
  char = game.user_input 
  case char
  when ?A..?z 
    game.board.insert_char(
      char:char, advance:false)
  end
end

#controls(n = 1) ⇒ Object



797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/cliptic/main.rb', line 797

def controls(n=1)
  {
    G:{
      3  => ->{game.exit},
      5  => ->{game.reset_menu.choose_opt},
      7  => ->{game.board.puzzle.check_all},
      9  => ->{game.board.swap_direction},
      12 => ->{game.redraw},
      16 => ->{game.pause},
      18 => ->{game.reveal},
      19 => ->{game.save}
    },
    N:{
      ?j => ->{game.board.move(y:n)},
      ?k => ->{game.board.move(y:n*-1)},
      ?h => ->{game.board.move(x:n*-1)},
      ?l => ->{game.board.move(x:n)},
      ?i => ->{game.mode = :I},
      ?I => ->{game.board.to_start; game.mode=:I},
      ?w => ->{game.board.next_clue(n:n)},
      ?a => ->{game.board.advance_cursor(n:1); game.mode=:I},
      ?b => ->{game.board.prev_clue(n:n)},
      ?e => ->{game.board.to_end},
      ?r => ->{await_replace},
      ?c => ->{await_delete; game.mode=:I},
      ?d => ->{await_delete},
      ?x => ->{game.board.delete_char(advance:false)}
    }
  }
end

#insert(char:) ⇒ Object



773
774
775
776
777
778
779
780
781
# File 'lib/cliptic/main.rb', line 773

def insert(char:)
  case char
  when 27  then ->{game.mode = :N}
  when 127 then ->{game.board.delete_char}
  when ?A..?z 
    ->{game.board.insert_char(char:char);
       game.unsaved = true }
  end
end

#is_arrow_key?(char) ⇒ Boolean

Returns:

  • (Boolean)


785
786
787
# File 'lib/cliptic/main.rb', line 785

def is_arrow_key?(char)
  (258..261).cover?(char)
end

#is_ctrl_key?(char) ⇒ Boolean

Returns:

  • (Boolean)


782
783
784
# File 'lib/cliptic/main.rb', line 782

def is_ctrl_key?(char)
  (1..26).cover?(char)
end

#normal(char:, n: 1) ⇒ Object



766
767
768
769
770
771
772
# File 'lib/cliptic/main.rb', line 766

def normal(char:, n:1)
  if (?0..?9).cover?(char)
    await_int(n:char.to_i)
  else
    controls(n)[:N][char]
  end
end

#route(char:) ⇒ Object



754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/cliptic/main.rb', line 754

def route(char:)
  if is_ctrl_key?(char)
    controls[:G][char.to_i]
  elsif is_arrow_key?(char)
    arrow(char)
  else
    case game.mode
    when :N then normal(char:char)
    when :I then insert(char:char)
    end
  end
end