Class: ConsoleShogi::Game
- Inherits:
-
Object
- Object
- ConsoleShogi::Game
- Defined in:
- lib/console_shogi/game.rb
Instance Method Summary collapse
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #start ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/console_shogi/game.rb', line 7 def initialize @sente_player = Player.new(teban: Teban::SENTE) @gote_player = Player.new(teban: Teban::GOTE) @board = NewBoardBuilder.build @selected_cursor = nil @teban_player = @sente_player @game_histories = [{board: board.deep_copy, sente_komadai: sente_player.komadai.deep_copy, gote_komadai: gote_player.komadai.deep_copy}] Terminal::Operator.clear_scrren Terminal::Operator.print_board(board: board, sente_komadai: sente_player.komadai, gote_komadai: gote_player.komadai) Terminal::Operator. Terminal::Operator.print_teban(teban_player.teban) end |
Instance Method Details
#start ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/console_shogi/game.rb', line 23 def start while key = STDIN.getch cursor = Terminal::Operator.cursor # NOTE Ctrl-C を押したら終了 if key == "\C-c" exit # NOTE 矢印キーを押したらカーソルを移動 elsif key == "\e" && STDIN.getch == "[" key = STDIN.getch @last_cursor_on_grid = cursor.dup unless cursor.grid_position.location.outside? cursor.move(key) # TODO 選択したピースは色を変えないようにしている。状態の持ち方を見直したい deactive_piece(last_cursor_on_grid) if selected_cursor&.grid_position != last_cursor_on_grid.grid_position focus_piece(cursor) if selected_cursor&.grid_position != cursor.grid_position # NOTE Enter を押したら駒を移動 elsif key == "\r" if cursor.grid_position.location.history? GameHistory.new(game_histories: game_histories).start next end if selected_cursor.nil? # TODO PieceMover の can_move? と分散してしまっている気もする next if cursor.grid_position.location.outside? active_piece(cursor) @selected_cursor = cursor.dup else next unless cursor.grid_position.location.board? piece_mover = PieceMover.build(board: board, player: teban_player, from_cursor: selected_cursor, to_cursor: cursor) piece_mover.move! if piece_mover.moved_piece? Terminal::Operator.print_diff_board(previous_board: game_histories.last[:board], board: board, sente_komadai: sente_player.komadai, gote_komadai: gote_player.komadai) @game_histories << {board: board.deep_copy, sente_komadai: sente_player.komadai.deep_copy, gote_komadai: gote_player.komadai.deep_copy} if teban_player.win? Terminal::Operator.print_winner(teban_player) exit else change_teban! Terminal::Operator.print_teban(teban_player.teban) end else deactive_piece(selected_cursor) end @selected_cursor = nil end end end end |