<commit_before>require 'single_game'
require 'logic'
require 'ui'
require 'io/console'

module MasterMind
  module Tobi
    class MultiPlayer < SinglePlayer
      attr_reader :hide_guess
      
      def initialize(sequence, game_logic, hide_guess)
        super(sequence, game_logic)
        @hide_guess = hide_guess  
      end
      
      def start_game
        number = num_of_players
        end_guess = 12*number + 1
        
        puts UI::GENERATE_MESSAGE % [game_logic.sequence_type, game_logic.length, UI::COLOR_STRINGS[game_logic.level]]
        history_hash = {}
        guesses_hash = {}
        (1..number).each {|x| 
          history_hash[x] = []
          guesses_hash[x] = 0 
        }
        
        multi_start_game(number, history_hash, guesses_hash)    
      end  
      
      def multi_start_game(number, history_hash, guesses_hash)
        total_guesses = 0                                       # total guesses of all players
        catch :player_wins do
          while total_guesses < (UI::GUESS_MAX * number)        # until all players have exhausted their guesses
            for i in (1..number)                                # loop through to allow each player have a guess
              total_guesses += 1
              last_guess = guesses_hash[i]                      # to store player's last guess
              print "************"
              print UI::PLAYER_MESSAGE % i
              while guesses_hash[i] == last_guess               # to counter situations where user enters invalid input, guess would still be same
                guesses_hash[i] = get_guess(history_hash, guesses_hash, i)
                throw(:player_wins) if guesses_hash[i] == end_guess # if there is a win
              end
            end
          end
        end
        puts UI::SORRY_MULTI_MESSAGE % sequence.join.upcase if total_guesses == UI::GUESS_MAX * number # guesses exhausted with no winner
      end
      
      def get_guess(history_hash, guesses_hash, index)
        if hide_guess
          input = STDIN.noecho(&:gets).chomp
        else
          input = gets.chomp
        end
        
        guesses_hash[index] = length_option_check(input, guesses_hash, history_hash, index)
      end
      
      def length_option_check
        confirm_guess = false
        confirm_guess = invalid_length?(input)                            # invalid length for entry
        confirm_guess = treat_option?(input, history_hash[index])             # entry is a game option
        
        if !confirm_guess
          guesses_hash[index] = treat_guess(input, guesses_hash[index], history_hash[index])  # player enters a guess
          
        guess_hash[index]
      end      
      
      def wrong_guess(sequence, guesses, input, history)<commit_msg>refactor multi_game<commit_after>require 'single_game'
require 'logic'
require 'ui'
require 'io/console'

module MasterMind
  module Tobi
    class MultiPlayer < SinglePlayer
      attr_reader :hide_guess
      
      def initialize(sequence, game_logic, hide_guess)
        super(sequence, game_logic)
        @hide_guess = hide_guess  
      end
      
      def start_game
        number = num_of_players
        end_guess = 12*number + 1
        
        puts UI::GENERATE_MESSAGE % [game_logic.sequence_type, game_logic.length, UI::COLOR_STRINGS[game_logic.level]]
        history_hash = {}
        guesses_hash = {}
        (1..number).each {|x| 
          history_hash[x] = []
          guesses_hash[x] = 0 
        }
        
        multi_start_game(number, history_hash, guesses_hash)    
      end  
      
      def multi_start_game(number, history_hash, guesses_hash)
        total_guesses = 0                                       # total guesses of all players
        catch :player_wins do
          while total_guesses < (UI::GUESS_MAX * number)        # until all players have exhausted their guesses
            for i in (1..number)                                # loop through to allow each player have a guess
              total_guesses += 1
              last_guess = guesses_hash[i]                      # to store player's last guess
              print "************"
              print UI::PLAYER_MESSAGE % i
              while guesses_hash[i] == last_guess               # to counter situations where user enters invalid input, guess would still be same
                guesses_hash[i] = get_guess(history_hash, guesses_hash, i)
                throw(:player_wins) if guesses_hash[i] == end_guess # if there is a win
              end
            end
          end
        end
        puts UI::SORRY_MULTI_MESSAGE % sequence.join.upcase if total_guesses == UI::GUESS_MAX * number # guesses exhausted with no winner
      end
      
      def get_guess(history_hash, guesses_hash, index)
        if hide_guess
          input = STDIN.noecho(&:gets).chomp
        else
          input = gets.chomp
        end
        
        guesses_hash[index] = length_option_check(input, guesses_hash, history_hash, index)
      end
      
      def length_option_check(input, guesses_hash, history_hash, index)
        confirm_guess = false
        confirm_guess = invalid_length?(input)                            # invalid length for entry
        confirm_guess = treat_option?(input, history_hash[index])             # entry is a game option
        
        if !confirm_guess
          guesses_hash[index] = treat_guess(input, guesses_hash[index], history_hash[index])  # player enters a guess
          
        guess_hash[index]
      end      
      
      def wrong_guess(sequence, guesses, input, history)
{'fraction_non_alphanumeric': 0.07140313390313391, 'fraction_numerical': 0.002849002849002849, 'mean_word_length': 1.8440506329113924, 'pattern_counts': {'":': 0, '<': 7, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3847405', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1571, 'n_words': 401}