<commit_before>module Upwords
  class UI

    def initialize(game, row_height = 1, col_width = 4)
      # Game and drawing variables
      @game = game
      @rows = game.board.num_rows
      @cols = game.board.num_columns
      @row_height = row_height
      @col_width = col_width
      @rack_visible = false

      # Configure Curses and initialize screen
      Curses.noecho
      Curses.curs_set(2)  # Blinking cursor
      Curses.init_screen
      Curses.start_color

      # Initialize colors
      Curses.init_pair(RED, Curses::COLOR_RED, Curses::COLOR_BLACK) # Red on black background
      Curses.init_pair(YELLOW, Curses::COLOR_YELLOW, Curses::COLOR_BLACK) # Yellow on black background

      # Initialize main window and game loop
      begin
        @win = Curses.stdscr
        @win.keypad=(true)
        @win.setpos(*letter_pos(*@game.cursor.posn))
        draw_update_loop
      ensure
        Curses.close_screen
      end      
    end

    # ==================================
    # Main methods: draw and input loops
    # ==================================

    def draw_update_loop
      draw_grid
      draw_controls

      # TODO: make this the main game loop
      while @game.running? do
        @rack_visible = false
        draw_player_info
        draw_message "#{@game.current_player.name}'s turn"

        # TODO: make CPU move subroutine it's own method
        if @game.current_player.cpu?
          draw_message "#{@game.current_player.name} is thinking..."
          @game.cpu_move
        else
          # TODO: make human player subroutine it's own method
          # Read key inputs then update cursor and window
          while read_key do
            @win.setpos(*letter_pos(*@game.cursor.posn))
            draw_letters
            draw_stack_heights
            draw_player_info # TODO: remove duplicate method?
          end
        end

        draw_letters  # Draw letters again to remove any highlights          
        draw_stack_heights
        draw_player_info

        # Game over subroutine
        if @game.game_over?
          draw_confirm("The game is over. Press any key to continue to see who won...")
          
          top_score = @game.get_top_score
          winners = @game.get_winners
          
          if winners.size == 1 
            draw_confirm "And the winner is... #{winners.first} with #{top_score} points!"
          else
            draw_confirm "We have a tie! #{winners.join(', ')} all win with #{top_score} points!"
          end<commit_msg>Update based on cursor method name change<commit_after>module Upwords
  class UI

    def initialize(game, row_height = 1, col_width = 4)
      # Game and drawing variables
      @game = game
      @rows = game.board.num_rows
      @cols = game.board.num_columns
      @row_height = row_height
      @col_width = col_width
      @rack_visible = false

      # Configure Curses and initialize screen
      Curses.noecho
      Curses.curs_set(2)  # Blinking cursor
      Curses.init_screen
      Curses.start_color

      # Initialize colors
      Curses.init_pair(RED, Curses::COLOR_RED, Curses::COLOR_BLACK) # Red on black background
      Curses.init_pair(YELLOW, Curses::COLOR_YELLOW, Curses::COLOR_BLACK) # Yellow on black background

      # Initialize main window and game loop
      begin
        @win = Curses.stdscr
        @win.keypad=(true)
        @win.setpos(*letter_pos(*@game.cursor.pos))
        draw_update_loop
      ensure
        Curses.close_screen
      end      
    end

    # ==================================
    # Main methods: draw and input loops
    # ==================================

    def draw_update_loop
      draw_grid
      draw_controls

      # TODO: make this the main game loop
      while @game.running? do
        @rack_visible = false
        draw_player_info
        draw_message "#{@game.current_player.name}'s turn"

        # TODO: make CPU move subroutine it's own method
        if @game.current_player.cpu?
          draw_message "#{@game.current_player.name} is thinking..."
          @game.cpu_move
        else
          # TODO: make human player subroutine it's own method
          # Read key inputs then update cursor and window
          while read_key do
            @win.setpos(*letter_pos(*@game.cursor.pos))
            draw_letters
            draw_stack_heights
            draw_player_info # TODO: remove duplicate method?
          end
        end

        draw_letters  # Draw letters again to remove any highlights          
        draw_stack_heights
        draw_player_info

        # Game over subroutine
        if @game.game_over?
          draw_confirm("The game is over. Press any key to continue to see who won...")
          
          top_score = @game.get_top_score
          winners = @game.get_winners
          
          if winners.size == 1 
            draw_confirm "And the winner is... #{winners.first} with #{top_score} points!"
          else
            draw_confirm "We have a tie! #{winners.join(', ')} all win with #{top_score} points!"
          end
{'fraction_non_alphanumeric': 0.09980276134122287, 'fraction_numerical': 0.0015779092702169625, 'mean_word_length': 2.097739767868051, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256478', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1426, 'n_words': 399}