wordle_game 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 411405a2cded8c2c067e8108c54428c9221fee0e00b8ed9a89b29ead3e055d4e
4
+ data.tar.gz: 164c1383ba7658f780a8cb4ed62b8e382c93eaef0844cf35732db31d06ed93fd
5
+ SHA512:
6
+ metadata.gz: 395f2be102a208ea191da883d69c57387d9617d3bafc1963a2eef79bc7843f83f484f95d7d4a10171c5a3de239b286a25e12843974a5c1b59932b42c47c75a7e
7
+ data.tar.gz: e589a24f8516219af88d30ca4d1a88e7d86ae60f95ed1c65a994e0a4e0fd6f821ac1187cd7dd73fab4edb5570c88b1ab88e03ce7b6e08e45e02bcde42aaee2c9
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Ruble
2
+
3
+ Ruble - это библиотека на языке Ruby для создания и игры в игру, похожую на Wordle, с настраиваемым числом букв в слове. Игра случайным образом выбирает слово из заранее определенного списка, и игроки могут делать попытки, чтобы угадать слово. Библиотека предоставляет обратную связь по каждому предположению, отмечая буквы как зеленые (правильная позиция), желтые (неправильная позиция) или серые (отсутствуют в слове).
4
+
5
+ ## Особенности
6
+
7
+ - Настраиваемая длина слова
8
+ - Настраиваемое максимальное количество попыток
9
+ - Обратная связь по каждой попытке с цветовой разметкой
10
+ - Простой и легкий в использовании API
11
+
12
+ ## Установка
13
+
14
+ Добавьте эту строку в ваш Gemfile:
15
+
16
+ ```ruby
17
+ gem 'wordle_game'
18
+
19
+
20
+
21
+ А затем выполните:
22
+
23
+ bundle install
24
+
25
+
26
+
27
+ Или установите библиотеку самостоятельно с помощью команды:
28
+
29
+ gem install wordle_game
30
+
31
+
32
+
33
+ Вот пример того, как использовать библиотеку WordleGame:
34
+
35
+ require 'wordle_game'
36
+
37
+ # Инициализация игры с длиной слова 5 букв и максимальным количеством попыток 6
38
+ game = WordleGame::Game.new(5, 6)
39
+
40
+ # Делайте попытки
41
+ result = game.attempt('apple')
42
+ puts result # => { status: :correct, result: [:green, :green, :green, :green, :green], attempts_left: 5 }
43
+
44
+ result = game.attempt('banjo')
45
+ puts result # => { status: :incorrect, result: [:grey, :grey, :grey, :grey, :grey], attempts_left: 4 }
46
+
47
+
48
+ Для настройки среды разработки выполните:
49
+
50
+ bin/setup
51
+
52
+
53
+ Для запуска тестов используйте:
54
+
55
+ bundle exec rspec
56
+
57
+ Вклад
58
+ Сообщения об ошибках и запросы на внесение изменений приветствуются на GitHub по адресу https://github.com/lok70/Ruble. Этот проект предназначен для того, чтобы быть безопасным и гостеприимным пространством для сотрудничества, и от участников ожидается соблюдение кодекса поведения.
59
+
60
+
61
+ Лицензия
62
+ Библиотека доступна как open-source под условиями MIT License.
63
+
64
+ Кодекс поведения
65
+ Все, кто взаимодействует с проектом WordleGame в репозиториях кода, трекерах ошибок, чатах и почтовых списках, обязаны следовать кодексу поведения.
66
+
@@ -0,0 +1,51 @@
1
+ module WordleGame
2
+ class Game
3
+ attr_reader :word_length, :max_attempts, :attempts, :target_word
4
+
5
+ WORD_LIST = %w[apple banjo cider delta eagle other words]
6
+
7
+ def initialize(word_length = 5, max_attempts = 6)
8
+ @word_length = word_length
9
+ @max_attempts = max_attempts
10
+ @attempts = []
11
+ @target_word = WORD_LIST.select { |word| word.length == word_length }.sample
12
+ end
13
+
14
+ def attempt(guess)
15
+ return { status: :game_over, message: "No more attempts allowed" } if game_over?
16
+
17
+ if guess.length != word_length
18
+ return { status: :invalid, message: "Guess length must be #{word_length}" }
19
+ end
20
+
21
+ result = check_guess(guess)
22
+ attempts << { guess: guess, result: result }
23
+
24
+ if guess == target_word
25
+ { status: :correct, result: result, attempts_left: max_attempts - attempts.size }
26
+ elsif attempts.size >= max_attempts
27
+ { status: :game_over, result: result, attempts_left: 0 }
28
+ else
29
+ { status: :incorrect, result: result, attempts_left: max_attempts - attempts.size }
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def game_over?
36
+ attempts.size >= max_attempts || attempts.any? { |a| a[:guess] == target_word }
37
+ end
38
+
39
+ def check_guess(guess)
40
+ guess.chars.map.with_index do |char, index|
41
+ if char == target_word[index]
42
+ :green
43
+ elsif target_word.include?(char)
44
+ :yellow
45
+ else
46
+ :grey
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1 @@
1
+ require "wordle_game/game"
@@ -0,0 +1,15 @@
1
+ require "rspec"
2
+ require "wordle_game"
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |expectations|
6
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
7
+ expectations.syntax = :expect
8
+ end
9
+
10
+ config.mock_with :rspec do |mocks|
11
+ mocks.verify_partial_doubles = true
12
+ end
13
+
14
+ config.shared_context_metadata_behavior = :apply_to_host_groups
15
+ end
@@ -0,0 +1,84 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe WordleGame::Game do
4
+ let(:game) { WordleGame::Game.new(5, 6) }
5
+
6
+ describe '#initialize' do
7
+ it 'sets the word length' do
8
+ expect(game.word_length).to eq(5)
9
+ end
10
+
11
+ it 'sets the max attempts' do
12
+ expect(game.max_attempts).to eq(6)
13
+ end
14
+
15
+ it 'chooses a target word from the word list' do
16
+ expect(WordleGame::Game::WORD_LIST).to include(game.target_word)
17
+ end
18
+ end
19
+
20
+ describe '#attempt' do
21
+ context 'when the guess length is incorrect' do
22
+ it 'returns an invalid status' do
23
+ result = game.attempt('shor')
24
+ expect(result[:status]).to eq(:invalid)
25
+ expect(result[:message]).to eq("Guess length must be 5")
26
+ end
27
+ end
28
+
29
+ context 'when the guess is correct' do
30
+ it 'returns a correct status and green result' do
31
+ game.instance_variable_set(:@target_word, 'apple')
32
+ result = game.attempt('apple')
33
+ expect(result[:status]).to eq(:correct)
34
+ expect(result[:result]).to eq([:green, :green, :green, :green, :green])
35
+ end
36
+ end
37
+
38
+ context 'when the guess is incorrect' do
39
+ it 'returns an incorrect status and the correct result array' do
40
+ game.instance_variable_set(:@target_word, 'apple')
41
+ result = game.attempt('apply')
42
+ expect(result[:status]).to eq(:incorrect)
43
+ expect(result[:result]).to eq([:green, :green, :green, :green, :grey])
44
+ expect(result[:attempts_left]).to eq(5)
45
+ end
46
+
47
+ it 'handles partially correct guesses' do
48
+ game.instance_variable_set(:@target_word, 'apple')
49
+ result = game.attempt('pearl')
50
+ expect(result[:status]).to eq(:incorrect)
51
+ expect(result[:result]).to eq([:yellow, :yellow, :yellow, :grey, :yellow])
52
+ expect(result[:attempts_left]).to eq(5)
53
+ end
54
+ end
55
+
56
+ context 'when the game is over' do
57
+ it 'returns game over status if no attempts are left' do
58
+ game.instance_variable_set(:@attempts, Array.new(6) { { guess: 'wrong', result: [:grey, :grey, :grey, :grey, :grey] } })
59
+ result = game.attempt('apple')
60
+ expect(result[:status]).to eq(:game_over)
61
+ expect(result[:message]).to eq("No more attempts allowed")
62
+ end
63
+
64
+ it 'returns game over status if the correct word was guessed' do
65
+ game.instance_variable_set(:@target_word, 'apple')
66
+ game.instance_variable_set(:@attempts, [{ guess: 'apple', result: [:green, :green, :green, :green, :green] }])
67
+ result = game.attempt('apple')
68
+ expect(result[:status]).to eq(:game_over)
69
+ expect(result[:message]).to eq("No more attempts allowed")
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '#check_guess' do
75
+ it 'returns the correct result array' do
76
+ game.instance_variable_set(:@target_word, 'apple')
77
+ result = game.send(:check_guess, 'apply')
78
+ expect(result).to eq([:green, :green, :green, :green, :grey])
79
+
80
+ result = game.send(:check_guess, 'pearl')
81
+ expect(result).to eq([:yellow, :yellow, :yellow, :grey, :yellow])
82
+ end
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wordle_game
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Egor Pavlov Nikita Kolomytsev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This library allows you to play a Wordle-like game with customizable
28
+ word lengths.
29
+ email:
30
+ - lokwer321@gmail.com nkolomytsev10@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - README.md
36
+ - lib/wordle_game.rb
37
+ - lib/wordle_game/game.rb
38
+ - spec/spec_helper.rb
39
+ - spec/wordle_game_spec.rb
40
+ homepage: https://github.com/lok70/Ruble
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.5.9
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: A Ruby library for playing a Wordle-like game.
63
+ test_files: []