3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RubyとPHPで簡単なアプリケーションを作ってみる。

Last updated at Posted at 2019-11-20

##概要
巷では
RubyとPHPどちらを勉強した方がいいですか?
といった質問や

RubyとPHP徹底比較!
といった記事が散見されます。

初学者の方でも書けるじゃんけんアプリをRubyとPHPで作成しました。

##Ruby

janken.rb
class Play
  def player
    puts '数字を入力してください'
    puts '0:グー'
    puts '1:チョキ'
    puts '2:パー'
    player_hand = gets.to_i
  end

  def enemy
    janken = [0,1,2]
    enemy_hand = janken.sample
  end

  def pon player_hand,enemy_hand
    puts "あなたの手#{player_hand}"
    puts "相手の手#{enemy_hand}"
    
      if player_hand === enemy_hand
        puts "アイコです、もう1回"
        return true
      elsif enemy_hand - player_hand === 1 || enemy_hand - player_hand === -2
        puts "あなたの勝ち"
      else
        puts "あなたの負け"
      end
  end
end

$play = Play.new
$p = $play.player
$e = $play.enemy
$j = $play.pon($p, $e)

if $j == true
  $p = $play.player
  $e = $play.enemy
  $j = $play.pon($p, $e)
end

##PHP

janken.php
<?php
class Play
{
    public function player(){
        echo "数字を入力してください";
        echo "0:グー";
        echo "1:チョキ";
        echo "2:パー";
        $player_hand = (int)fgets(STDIN);
        return $player_hand;     
    }
    public function enemy(){
        $janken = array(0,1,2);
        $enemy_hand = array_rand($janken);
        return $enemy_hand;
    }
    public function janken($player_hand, $enemy_hand){
        print_r('あなたの手'. $player_hand);
        echo PHP_EOL;
        print_r('相手の手'.$enemy_hand);
        echo PHP_EOL;
            if ($player_hand === $enemy_hand){
                echo "アイコです";
                echo PHP_EOL;
               return true;
            }elseif($enemy_hand - $player_hand === 1 || $enemy_hand - $player_hand === -2){
                echo  "あなたの勝ち";
            }elseif($player_hand - $enemy_hand === 1){
                echo  "あなたの負け";
            }
    }
}

$play = new Play();
$p = $play->player();
$e = $play->enemy();
$j = $play->janken($p, $e);

if ($j == true) {
    $p = $play->player();
    $e = $play->enemy();
    $j = $play->janken($p, $e);
}
?>

参考にさせて頂いた比較記事。

【超初心者向け】大まかにRubyとPHPを比較してみる
https://eng-entrance.com/ruby-php

気になる!PHPエンジニアの年収と案件。Rubyとの違いも含めて説明
https://blog.codecamp.jp/php-ruby-income

【PHP vs Ruby】Webサービス開発はどっちがオススメ?
https://mynavi-agent.jp/it/geekroid/2017/08/php-vs-rubyweb.html

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?