Class: ConsoleShogi::Terminal::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/console_shogi/terminal/operator.rb

Defined Under Namespace

Modules: EscapeSequence

Constant Summary collapse

TEBAN =
{
  sente: '先手',
  gote: '後手'
}

Class Method Summary collapse

Class Method Details

.active_piece(board:, cursor:) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/console_shogi/terminal/operator.rb', line 111

def active_piece(board:, cursor:)
  piece = board.fetch_piece(x: cursor.grid_position.x, y: cursor.grid_position.y)

  return if piece.nil?

  print_image(image: piece.active_image, height: image_height)

  back_to_cursor
end

.clear_scrrenObject



35
36
37
38
39
# File 'lib/console_shogi/terminal/operator.rb', line 35

def clear_scrren
  print EscapeSequence::SCREEN_CLEAR

  print EscapeSequence::MOVE_START_POINT
end

.cursorObject



27
28
29
# File 'lib/console_shogi/terminal/operator.rb', line 27

def cursor
  @cursor ||= Cursor.new
end

.deactive_piece(board:, previous_cursor:) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/console_shogi/terminal/operator.rb', line 101

def deactive_piece(board:, previous_cursor:)
  print "\e[#{previous_cursor.terminal_position.y};#{previous_cursor.terminal_position.x}H"

  piece = board.fetch_piece(x: previous_cursor.grid_position.x, y: previous_cursor.grid_position.y)

  print_image(image: piece.image, height: image_height) unless piece.nil?

  back_to_cursor
end

.focus_piece(board:, cursor:) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/console_shogi/terminal/operator.rb', line 91

def focus_piece(board:, cursor:)
  piece = board.fetch_piece(x: cursor.grid_position.x, y: cursor.grid_position.y)

  return if piece.nil?

  print_image(image: piece.focused_image, height: image_height)

  back_to_cursor
end

.image_heightObject



31
32
33
# File 'lib/console_shogi/terminal/operator.rb', line 31

def image_height
  @image_height ||= calculate_fit_image_height
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/console_shogi/terminal/operator.rb', line 41

def print_board(board:, sente_komadai:, gote_komadai:)
  print EscapeSequence::MOVE_START_POINT

  board.matrix.row_vectors.each_with_index do |vector, i|
    vector.each_with_index do |piece, j|
      print_image(image: piece.image, height: image_height)
    end

    print "#{EscapeSequence::RESET}\n"
  end

  # NOTE 駒台を表示
  print_komadai(komadai: sente_komadai, start_position: DisplayArea::Komadai::Sente.start_position)
  print_komadai(komadai: gote_komadai, start_position: DisplayArea::Komadai::Gote.start_position)

  back_to_cursor
end

TODO 駒台の表示方法も考える



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/console_shogi/terminal/operator.rb', line 60

def print_diff_board(previous_board:, board:, sente_komadai:, gote_komadai:)
  print EscapeSequence::MOVE_START_POINT

  board.matrix.row_vectors.each_with_index do |vector, i|
    vector.each_with_index do |piece, j|
      if previous_board.matrix[i, j].nil? || previous_board.matrix[i, j].same?(piece)
        print EscapeSequence::MOVE_RIGHT_2
      else
        print_image(image: piece.image, height: image_height)
      end
    end

    print "#{EscapeSequence::RESET}\n"
  end

  # NOTE 駒台を表示
  print_komadai(komadai: sente_komadai, start_position: DisplayArea::Komadai::Sente.start_position)
  print_komadai(komadai: gote_komadai, start_position: DisplayArea::Komadai::Gote.start_position)

  back_to_cursor
end

TODO データの指定の仕方は後で整理する



151
152
153
154
155
156
157
158
159
160
# File 'lib/console_shogi/terminal/operator.rb', line 151

def print_history_button
  print EscapeSequence::MOVE_HISTORY_AREA
  print EscapeSequence::SCREEN_CLEAR_NOW_LINE

  print_image(image: File.read("images/history_button/left.png"), height: image_height)
  print_image(image: File.read("images/history_button/right.png"), height: image_height)
  print_image(image: File.read("images/history_button/play.png"), height: image_height)

  back_to_cursor
end


82
83
84
85
86
87
88
89
# File 'lib/console_shogi/terminal/operator.rb', line 82

def print_start_history
  print EscapeSequence::MOVE_INFOMATION_AREA
  print EscapeSequence::SCREEN_CLEAR_AFTER_CURSOR

  print '履歴モード'

  back_to_cursor
end

TODO 見た目は後で直す



141
142
143
144
145
146
147
148
# File 'lib/console_shogi/terminal/operator.rb', line 141

def print_teban(teban)
  print EscapeSequence::MOVE_INFOMATION_AREA
  print EscapeSequence::SCREEN_CLEAR_AFTER_CURSOR

  print "手番 : #{TEBAN[teban.to_sym]}"

  back_to_cursor
end


132
133
134
135
136
137
138
# File 'lib/console_shogi/terminal/operator.rb', line 132

def print_winner(player)
  print "\e[4;7H"

  print_image(image: player.win_image, height: image_height * 3)

  print EscapeSequence::MOVE_INFOMATION_AREA
end

.select_promotionObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/console_shogi/terminal/operator.rb', line 121

def select_promotion
  print EscapeSequence::MOVE_INFOMATION_AREA

  prompt = TTY::Prompt.new

  prompt.select('成りますか?', show_help: :never) {|menu|
    menu.choice '成る', true
    menu.choice '成らない', false
  }
end