ruby 数当てゲーム

数当てゲーム

class NumberGuessingGame
attr_accessor :player_name, :target_number, :guess, :attempts, :difficulty, :games_played, :total_attempts

def initialize
@player_name = “”
@target_number = 0
@guess = nil
@attempts = 0
@difficulty = “easy”
@games_played = 0
@total_attempts = 0
end

def start
welcome_message
get_player_name
loop do
set_difficulty
setup_game
play_round
display_result
update_statistics
break unless play_again?
end
display_statistics
farewell_message
end

def welcome_message
puts “数当てゲームへようこそ!”
end

def get_player_name
print “あなたの名前を入力してください: “
@player_name = gets.chomp
end

def set_difficulty
puts “難易度を選択してください: easy, medium, hard”
@difficulty = gets.chomp.downcase
end

def setup_game
@target_number = case @difficulty
when “medium”
rand(1..500)
when “hard”
rand(1..1000)
else
rand(1..100)
end
@attempts = 0
end

def play_round
until @guess == @target_number
print “数を予想してください: “
@guess = gets.to_i
@attempts += 1
give_hint
end
end

def give_hint
if @guess < @target_number puts “もっと大きい数です。” elsif @guess > @target_number
puts “もっと小さい数です。”
else
puts “正解です!”
end
end

def display_result
puts “#{@player_name}さん、おめでとうございます!”
puts “#{@attempts}回目で正解です!”
end

def play_again?
print “もう一度プレイしますか?(yes/no): “
answer = gets.chomp.downcase
answer == ‘yes’
end

def update_statistics
@games_played += 1
@total_attempts += @attempts
end

def display_statistics
puts “統計情報:”
puts “プレイしたゲーム数: #{@games_played}”
puts “総試行回数: #{@total_attempts}”
puts “平均試行回数: #{@total_attempts / @games_played}” if @games_played > 0
end

def farewell_message
puts “ゲームをプレイしていただきありがとうございました!またお会いしましょう!”
end
end

ゲーム開始

game = NumberGuessingGame.new
game.start
機能概要
難易度選択: プレイヤーはeasy, medium, hardのいずれかの難易度を選択でき、難易度によって予想する数の範囲が変わります。

easy: 1-100
medium: 1-500
hard: 1-1000
プレイ統計の表示: プレイしたゲーム数、総試行回数、平均試行回数を表示します。

再プレイ機能: プレイヤーはゲーム終了後、再度プレイするかどうかを選択できます。

実行方法
上記のコードをファイル(例: number_guessing_game_extended.rb)として保存します。
ターミナル(コマンドプロンプト)を開き、そのファイルがあるディレクトリに移動します。
以下のコマンドを実行してゲームを開始します。
bash
ruby number_guessing_game_extended.rb
この拡張版では、より多くの機能を楽しむことができ、プレイヤーは複数回のゲームプレイを通じて自分のパフォーマンスを追跡することができます。

ruby メソッドをオーバーライド

class Score
  def initialize(subject, score)
    @subject = subject
    @score = score
  end

  def get_info
    "#{@subject}/#{@score} -> #{get_result}"
  end

  private

  def get_result
    @score >= 80 ? "Pass" : "Fail"
  end
end

class MathScore < Score
  def initialize(score)
    super("Math", score)
  end

  private 

  # ovierride
  def get_result
  @score >= 50 ? "Pass" : "Fail"
  end
end

class EnglishScore < Score
  def initialize(score)
    super("English", score)
  end
end

class User
  def initialize(name, score)
    @name = name
    @score = score
  end

  def get_info
    "Name: #{@name}, Score: #{@score.get_info}"
  end
end

user1 = User.new("Taro", MathScore.new(70))
user2 = User.new("Jiro", EnglishScore.new(90))
puts user1.get_info
puts user2.get_info

ruby 子クラス

class Score
  def initialize(subject, score)
    @subject = subject
    @score = score
  end

  def get_info
    "#{@subject}/#{@score} -> #{get_result}"
  end

  private

  def get_result
    @score >=80 ? "Pass" : "Fail"
  end
end

class MathScore < Score
  def initialize(score)
  super("Math", score)
  end
end

class EnglishScore < Score
 def initialize(score)
  super("Math", score)
  end
end

class User
  def initialize(name, score)
    @name = name
    @score = score
  end

  def get_info
    "Name: #{@name}, Score: #{@score.get_info}"
  end
end

user1 = User.new("Taro", MathScore.new(70))
user2 = User.new("Jiro", EnglishScore.new(90))
puts user1.get_info
puts user2.get_info

ruby メソッドを実装

class Score
  def initialize(subject, score)
    @subject = subject
    @score = score
  end

  def get_info
    "#{@subject}/#{@score}"
  end
end

class User
  def initialize(name, score)
    @name = name
    @score = score
  end

  def get_info
    "Name: #{@name}, Score: #{@score.get_info}"
  end
end

user1 = User.new("Taro", Score.new("Math", 70))
user2 = User.new("Jiro", Score.new("English", 90))
puts user1.get_info
puts user2.get_info

ruby rssserver

require ‘cgi’
require ‘rss’
require ‘net/http’

def parse(page_source)
dates = page_source.scan(%r!(\d+)年 ?(\d+)月 ?(\d+)日
!).map { |year, month, day| [year.to_i, month.to_i, day.to_i] }
url_titles = page_source.scan(%r!^(.+?)
!).map { |url, title| [CGI.unescapeHTML(url), CGI.unescapeHTML(title)] }
url_titles.zip(dates).map { |(url, title), date| [url, title, Time.local(*date)] }
end

def format_text(title, url, url_title_time_ary)
text = “Title: #{title}\nURL: #{url}\n\n”
url_title_time_ary.each do |aurl, atitle, atime|
text << “* (#{atime})#{atitle}\n”
text << ” #{aurl}\n”
end
text
end

def format_rss(title, url, url_title_time_ary)
RSS::Maker.make(“2.0”) do |maker|
maker.channel.updated = Time.now.to_s
maker.channel.link = url
maker.channel.title = title
maker.channel.description = title
url_title_time_ary.each do |aurl, atitle, atime|
maker.items.new_item do |item|
item.link = aurl
item.title = atitle
item.updated = atime
item.description = atitle
end
end
end
end

uri = URI(“http://crawler.sbcr.jp/samplepage.html”)
response = Net::HTTP.get_response(uri)
parsed = parse(response.body.force_encoding(“UTF-8”))

formatter = case ARGV.first
when “rss-output”
:format_rss
else
:format_text
end

puts send(formatter, “WWW.SBCR.JP トピックス”, “http://crawler.sbcr.jp/samplepage.html”, parsed)

Ruby RSS

require 'rss'
require 'open-uri'

# RSSフィードのURL
rss_url = 'http://2ch-ranking.net/rss/livemarket1.rdf'

# RSSフィードを取得してパース
rss_content = URI.open(rss_url).read
rss = RSS::Parser.parse(rss_content)

# フィードのタイトルと記事を出力
puts "フィードのタイトル: #{rss.channel.title}"
puts "-----"

rss.items.each do |item|
  puts "タイトル: #{item.title}"
  puts "リンク: #{item.link}"
  puts "概要: #{item.description}"
  puts "-----"
end

ruby クラスメソッド

class User
  @@count = 0

  def initialize(name, score)
    @name = name
    @score = score
    @@count += 1
  end

  def get_info
    "Name: #{@name}, Score: #{@score}"
  end

  def self.get_info
    "Count: #{@@count}"
  end
end

# count = 0
user1 = User.new("Taro", 70)
# count += 1
user2 = User.new("Jiro", 90)
# count += 1
# puts count
puts User.get_info

ruby attr_accessorruby

class User
  # attr_accessor :name
  # attr_writer :name
  attr_reader :name

  def initialize(name, score)
    @name = name
    @score = score
  end

  # # getter
  # def name
  #   @name
  # end

  # # setter
  # def name=(new_name)
  #   @name = new_name
  # end

  def get_info
    "Name: #{@name}, Score: #{@score}"
  end
end

user1 = User.new("Taro", 70)
user2 = User.new("Jiro", 90)
user1.name = "TARO"
puts user1.name