From 81e3c6b310adfeac145a5e35ec18a226139769eb Mon Sep 17 00:00:00 2001 From: Alex Clink Date: Sat, 2 Oct 2021 20:24:06 -0400 Subject: [PATCH] Refactor term_game --- src/lx_chess.cr | 1 - src/lx_chess/board.cr | 4 +++ src/lx_chess/term_game.cr | 62 ++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/lx_chess.cr b/src/lx_chess.cr index 0be5cd9..63f4386 100644 --- a/src/lx_chess.cr +++ b/src/lx_chess.cr @@ -83,5 +83,4 @@ if theme = options["theme"]? end end -term_game.clear_screen term_game.run! diff --git a/src/lx_chess/board.cr b/src/lx_chess/board.cr index b0c1bb4..81f4193 100644 --- a/src/lx_chess/board.cr +++ b/src/lx_chess/board.cr @@ -75,6 +75,10 @@ module LxChess index_of(x, y) end + def indexes_of(cords : Array(String)) + cords.map { |c| index_of(c) } + end + # Convert an *index* into a human coordinate (ex: `a1`) def cord(index : Int) y, x = index.divmod(@width) diff --git a/src/lx_chess/term_game.cr b/src/lx_chess/term_game.cr index a6e1fc2..beeaf0e 100644 --- a/src/lx_chess/term_game.cr +++ b/src/lx_chess/term_game.cr @@ -9,6 +9,15 @@ require "./change" module LxChess # Represents a chess game played through the terminal class TermGame + HELP_TEXT = <<-HELP + Commands: + flip - Flip the board + moves [SQUARE] - Show the moves (optionally for a given square) + [SAN] - Standard algebraic notation (make a move) + [FROM] [TO] - specify move by coordinates + HELP + LOG_LENGTH = 20 + property gb : TermBoard getter log = [] of String property pgn : PGN = PGN.new @@ -31,6 +40,8 @@ module LxChess end def run! + clear_screen + show_help loop do draw break if @game.checkmate? @@ -67,14 +78,7 @@ module LxChess case input when /help/i - msg = <<-HELP - Commands: - flip - Flip the board - moves [SQUARE] - Show the moves (optionally for a given square) - [SAN] - Standard algebraic notation (make a move) - [FROM] [TO] - specify move by coordinates - HELP - msg.lines.reverse.each { |l| @log.unshift(l) } + show_help when /flip/i @gb.flip! when /score/i @@ -134,16 +138,9 @@ module LxChess if matches = input.downcase.match(/\s*([a-z]\d)\s*([a-z]\d)\s*(?:=\s*)?([RNBQ])?/i) from = matches[1] to = matches[2] - promo = if matches[3]? - matches[3][0] - end + promo = matches[3]? ? matches[3][0] : nil if from && to - @gb.clear - san = @game.move_to_san(from, to, promo) - @changes << @game.make_move(from, to, promo) - @pgn.history << san - @gb.highlight([@game.board.index_of(from), @game.board.index_of(to)]) - @log.unshift "#{san.to_s}: #{from} => #{to}" + make_move(from, to, promo) end end end @@ -153,12 +150,7 @@ module LxChess notation = Notation.new(input) from, to = @game.parse_san(notation) if from && to - @gb.clear - san = @game.move_to_san(from, to, notation.promotion) - @changes << @game.make_move(from, to, notation.promotion) - @pgn.history << san - @gb.highlight([from.to_i16, to.to_i16]) - @log.unshift "#{san.to_s}: #{@game.board.cord(from)} => #{@game.board.cord(to)}" + make_move(from, to, notation.promotion) end end end @@ -170,7 +162,7 @@ module LxChess @log.unshift msg end ensure - until log.size < 8 + until log.size < LOG_LENGTH log.pop end end @@ -188,6 +180,28 @@ module LxChess @term.clear_scroll end + def show_help + HELP_TEXT.lines.reverse.each { |l| @log.unshift(l) } + end + + # ===================== + # = Game Manipulation = + # ===================== + + def make_move(from : String, to : String, promotion : Char? = nil) + from, to = @game.board.indexes_of([from, to]) + make_move(from, to, promotion) + end + + def make_move(from : Int16, to : Int16, promotion : Char? = nil) + @gb.clear + san = @game.move_to_san(from, to, promotion) + @changes << @game.make_move(from, to, promotion) + @pgn.history << san + @gb.highlight([from, to]) + @log.unshift "#{san.to_s}: #{@game.board.cord(from)} => #{@game.board.cord(to)}" + end + # =================== # = Drawing methods = # ===================