ããããããã¨ãã«ã¯ã©ã¹ãªãã¦è¦ãã®ã
CodeIQã¨ããããã°ã©ãã³ã°èª²é¡ã«ææ¦ãããµã¤ãã«ãRubyã§ã¸ã£ã³ã±ã³ã¯ã©ã¹ãä½ãã¨ããåé¡ããã£ãã®ã§ãã£ã¦ã¿ãããã§ã«åé¡ã¯èªããªããªã£ã¦ãããã©ããããªæãã®Jankenã¯ã©ã¹ãä½ãã¨ããã
$ irb > require './janken.rb' > left = Janken.new > right = Janken.new > left.versus(right) å·¦ã®äººãåã¾ãããå³ããã§ããå·¦ãã°ã¼ã > left.versus(right) å³ã®äººãåã¡ã¾ãããå³ããã§ããå·¦ããã¼ã : :
ç¹°ãè¿ãirbã§å®è¡ã§ããããã«ãã¨ããã
åé¡ãè¦ãç¬éãããã¯åé¡èªä½ãããããã®ã§ã¯ãªããã¨æã£ããã©ããã£ã¦ã¿ãã
# -*- coding: utf-8 -*- class Janken attr_reader :hand NAME = { goo: "ã°ã¼", choki: "ãã§ã", pah: "ãã¼" } def initialize generate_new_hand end def versus(other) generate_new_hand other.generate_new_hand case match(other) when :win puts "å·¦ã®äººãåã¡ã¾ãããå³ã#{other}ãå·¦ã#{self}ã" when :lose puts "å³ã®äººãåã¡ã¾ãããå³ã#{other}ãå·¦ã#{self}ã" when :even puts "ãããã§ããã#{self}" end end protected def generate_new_hand @hand = [:goo, :choki, :pah].shuffle.pop end def match(other) match = [@hand, other.hand] case when @hand == other.hand; :even when match == [:goo, :choki] || match == [:choki, :pah] || match == [:pah, :goo]; :win else :lose end end def to_s NAME[@hand] end end
åæã¡ãã»ã¼ã¸ã®åºåãJankenã¯ã©ã¹ã§ããã®ã¯ããã¾ãã¡ãJankenã¯ã©ã¹ã¯åã¡è² ãã®å¤å®ãã¸ãã¯ãæã¤ã¹ãã§ã¯ãã£ã¦ãããã®çµæã«åºã¥ãã¬ãã¼ãæ©è½ã¾ã§å«ãã®ã¯æ±ãè¾¼ã¿ããã ã¨æãããã1ã¤ãJankenã¤ã³ã¹ã¿ã³ã¹èªèº«ã¯èªåãå³ãªã®ãå·¦ãªã®ãåãããªãã¯ããªã®ã§ãå³ãå·¦ã¨ãã£ãç¾å®ä¸çã¨ã®çµã³ã¤ãã¯å¼ã³åºãå´ã§ããã¹ããã¨ããå³ãã¨ããå·¦ãã¨ãã£ãæååãJankenã¯ã©ã¹ã«å«ã¾ããã®ã¯ããããã
ã¨ãããã¨ãèããã¨ã以ä¸ã®ã»ããè¯ãã
# -*- coding: utf-8 -*- class Janken attr_reader :hand NAME = { goo: "ã°ã¼", choki: "ãã§ã", pah: "ãã¼" } def initialize generate_new_hand end def versus(other) generate_new_hand other.generate_new_hand match(other) end protected def generate_new_hand @hand = [:goo, :choki, :pah].shuffle.pop end def match(other) match = [@hand, other.hand] case when @hand == other.hand; :even when match == [:goo, :choki] || match == [:choki, :pah] || match == [:pah, :goo]; :win else :lose end end def to_s NAME[@hand] end end if __FILE__ == $0 left = Janken.new right = Janken.new 10.times { case left.versus(right) when :win puts "å·¦ã®äººãåã¡ã¾ãããå³ã#{right}ãå·¦ã#{left}ã" when :lose puts "å³ã®äººãåã¡ã¾ãããå³ã#{right}ãå·¦ã#{left}ã" when :even puts "ãããã§ããã#{left}" end } end
ããã«çåãã
Jankenã¯ã©ã¹ãversusãå¼ã³åºããã³ã«å é¨ã®ç¶æ ãå¤ãã¦ããã¨ããã®ã¯éåæãè¦ãããJankenã¯ã©ã¹ã¯æã®ç¶æ ãæã£ã¦ããã¹ãã§ã¯ãã£ã¦ããåãã¤ã³ã¹ã¿ã³ã¹ãã©ãã©ãç¶æ ãå¤ãã¦ããã®ãè¯ãAPIã¨ã¯æããªãããã人ãã°ã¼ãåºãã次ã«ãã§ããåºããæããã®2ã¤ãåããªãã¸ã§ã¯ãã§ã¯ããºã¤ã
ãããè¨åã«ããJankenã¯ã©ã¹ã¨ããã®ã¯ãPlayerã¯ã©ã¹ã§ããã¹ãã§ãJanken#versusã¨ããã®ã¯ãPlayer#jankenã¨ã§ãããã»ããã¸ã£ã³ã±ã³ã®èªç¶ãªè¡¨ç¾ãã ããã2ã¤ã®ã¯ã©ã¹ã«åãã¦ã
# -*- coding: utf-8 -*- class Player attr_accessor :hand def initialize @hand = Janken.new end def janken(other) case @hand.match(other.hand) when :win puts "ç§ã®åã¡ã§ããç§ã#{@hand}ãç¸æã#{other.hand}ã" when :lose puts "ç§ã®è² ãã§ããç§ã#{@hand}ãç¸æã#{other.hand}ã" when :even puts "ãããã§ããã#{@hand}" end end end class Janken attr_reader :hand NAME = { goo: "ã°ã¼", choki: "ãã§ã", pah: "ãã¼" } def initialize @hand = [:goo, :choki, :pah].shuffle.pop end def match(other) match = [@hand, other.hand] case when @hand == other.hand; :even when match == [:goo, :choki] || match == [:choki, :pah] || match == [:pah, :goo]; :win else :lose end end def to_s NAME[@hand] end end if __FILE__ == $0 me = Player.new opponent = Player.new 5.times { me.janken opponent me.hand = Janken.new opponent.hand = Janken.new } end
ã¨ã§ãããã»ããããã
ãã ã2æç®ä»¥éã§å¤é¨ããJanken.newãPlayerã¤ã³ã¹ã¿ã³ã¹ã«çªã£è¾¼ãã®ã¯ã¡ãã£ã¨ãã³ãPlayerã¯ã©ã¹ã常ã«Janken.newãããã»ããè¯ããåºãæãèããã®ã¯Playerã ããèãã¦ã¿ãã¨ã¸ã£ã³ã±ã³ã¨ããã®ã¯ä¸é£ã®æã®ã·ã¼ã±ã³ã¹ãªã®ã§ã以ä¸ã®ããã«Enumeratorã使ã£ãã»ãããããç¾å®ã®ã¸ã£ã³ã±ã³ã®è¡¨ç¾ã¨ãã¦é©åãããããªãã
# -*- coding: utf-8 -*- class Player def hand_seq @hand_seq ||= Enumerator.new do |y| loop { y << Janken.new } end end def janken(other) hand = hand_seq.peek other_hand = other.hand_seq.peek case hand.match(other_hand) when :win puts "ç§ã®åã¡ã§ããç§ã#{hand}ãç¸æã#{other_hand}ã" when :lose puts "ç§ã®è² ãã§ããç§ã#{hand}ãç¸æã#{other_hand}ã" when :even puts "ãããã§ããã#{hand}" end end end class Janken attr_reader :hand NAME = { goo: "ã°ã¼", choki: "ãã§ã", pah: "ãã¼" } def initialize @hand = [:goo, :choki, :pah].shuffle.first end def match(other) match = [@hand, other.hand] case when @hand == other.hand; :even when match == [:goo, :choki] || match == [:choki, :pah] || match == [:pah, :goo]; :win else :lose end end def to_s NAME[@hand] end end if __FILE__ == $0 me = Player.new opponent = Player.new 5.times { me.janken opponent me.hand_seq.next opponent.hand_seq.next } end
Enumeratorã使ãã®ã¯ããã¨ãã¦ããå¤é¨ã«ãã®ã¾ã¾è¦ããã®ã¯ããºã¤ãpeekã£ã¦ä½ã ãã¨ããæããããããã¨ãã¯å¿ è¦ãªã¡ã½ããã«ã ãé©å½ãªååãä»ãã¦ããªã²ã¼ãã
# -*- coding: utf-8 -*- require 'forwardable' class Player extend Forwardable def initialize @hand_seq = Enumerator.new do |y| loop { y << Janken.new } end end def_delegator :@hand_seq, :peek, :hand def_delegator :@hand_seq, :next, :next! def janken(other) case hand.match(other.hand) when :win puts "ç§ã®åã¡ã§ããç§ã#{hand}ãç¸æã#{other.hand}ã" when :lose puts "ç§ã®è² ãã§ããç§ã#{hand}ãç¸æã#{other.hand}ã" when :even puts "ãããã§ããã#{hand}" end end end class Janken attr_reader :hand NAME = { goo: "ã°ã¼", choki: "ãã§ã", pah: "ãã¼" } def initialize @hand = [:goo, :choki, :pah].shuffle.first end def match(other) match = [@hand, other.hand] case when @hand == other.hand; :even when match == [:goo, :choki] || match == [:choki, :pah] || match == [:pah, :goo]; :win else :lose end end def to_s NAME[@hand] end end if __FILE__ == $0 me = Player.new opponent = Player.new 5.times { me.janken opponent me.next! opponent.next! } end
ããã¾ã§æ¥ãã¨æ» è¶è¦è¶ã¨ããæ°ãããªãããªãã
ãããã¸ã£ã³ã±ã³ããã°ã©ã ãã¨ãã§å¤§ãããã°ã¼ãã§ããã¼ã [0, 1, 2] ã§è¡¨ç¾ãã¦ããããæ¯è¼ããifæãããã£ã¨ä¸¦ã¹ãã°ãã話ã§ãã¯ã©ã¹ãªãã¦1ã¤ãä½ããã¨ãªãã¦ãªãã¨ããæ°ãããããããå¿ è¦ãªã®ã¯ããã·ã¥ãã¼ãã«1ã¤ã¨ã2ã¤ã®ã¸ã£ã³ã±ã³ãåãåã£ã¦è©ä¾¡ããé¢æ°ã®1ã¤ã ããããã®ããã YouTubeã§è¦ãPythonãªäººã®ãã¼ã¯ã§ããã¼ã¿æ§é ã§æ¸ããã®ã«ã¤ãã¤ãååãä»ãããã¯ã©ã¹ã«ããããããªãã¨ãã主張ã«ãããã¯ãããããªã¨æã£ãããã¦ãããPythonã¨Rubyã§æåã®éãããããããããããæ £ãã¦ã¿ããã¯ã©ã¹ã¯ä¾¿å©ã§ãå¥ã«ã³ã¼ãè¡æ°ãå¢ããããã¨ãèªã¿ã¥ãããªãã¨ããããã§ããªããå®è¡å¹çãå¤ãã®å ´åãå ¨ãã©ãã§ããã話ã§ãããã大äºãªã®ã¯äººéãèªèããã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ã©ããªã£ã¦ããã®ãã¨ããã¨ãããåé¡ã®æãæ¹ãã¢ã¸ã¥ã¼ã«ã«åãããã¨ã§èªã¿æã«ç¤ºããé¢é£ããã³ã¼ããã¾ã¨ããå ´ã¨ãã¦ã¯ã©ã¹ã¨ãã容ãç©ã使ãã®ã¯ããããè¯ããã¨ã«æããã®ã ããªã