11
10

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 5 years have passed since last update.

パラダイム別 じゃんけんの書き方

Last updated at Posted at 2013-03-26

データ手続き型

procedural.coffee
beats = (you, opponent)->
    if you is 'rock'
        if opponent is 'scissors'
            return true
        else
            return false 
    else if you is 'paper'
        if opponent is 'rock'
            return true
        else
            return false
    else if you is 'scissors'
        if opponent is 'paper'
            return true
        else
            return false
    else
        throw new Error("your hand is unexpected: #{you}")
    
you = 'rock'
opponent = 'paper'

console.log beats(you, opponent)
console.log beats(opponent, you)

オブジェクト指向

oo.coffee
class Rock
    beats: (hand)->
        return hand instanceof Scissors

class Paper
    beats: (hand)->
        return hand instanceof Rock

class Scissors
    beats: (hand)->
        return hand instanceof Paper


you = new Rock
opponent = new Paper

console.log you.beats(opponent)
console.log opponent.beats(you)

論理型

janken.pro
beats(rock, scissors).
beats(paper, rock).
beats(scissors, paper).

関数型

だれか...

11
10
3

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
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?