Class: Cardgame::Gameplay

Inherits:
Object
  • Object
show all
Defined in:
lib/warcards/gameplay.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Gameplay

Returns a new instance of Gameplay.



9
10
11
12
13
14
15
# File 'lib/warcards/gameplay.rb', line 9

def initialize(args)
  @deck         = args[:deck]
  @player       = args[:player]
  @ai           = args[:ai]
  @player_cards = Array.new
  @ai_cards     = Array.new
end

Instance Method Details

#aiObject



85
86
87
# File 'lib/warcards/gameplay.rb', line 85

def ai
  @ai
end

#ai_cardsObject



89
90
91
# File 'lib/warcards/gameplay.rb', line 89

def ai_cards
  @ai_cards
end

#contestObject



54
55
56
57
58
59
60
61
62
# File 'lib/warcards/gameplay.rb', line 54

def contest
  if @ai_cards.last.value > @player_cards.last.value
    winner = @ai
  elsif @ai_cards.last.value < @player_cards.last.value
    winner = @player
  end

  { :winner => winner, :player_cards => @player_cards, :ai_cards => @ai_cards }
end

#dealObject



21
22
23
24
25
# File 'lib/warcards/gameplay.rb', line 21

def deal
  while @deck.length > 0
    deck.length.even? ? @player.stack << @deck.pop : @ai.stack << @deck.pop
  end
end

#deckObject



93
94
95
# File 'lib/warcards/gameplay.rb', line 93

def deck
  @deck
end

#discard(result) ⇒ Object



78
79
80
81
82
83
# File 'lib/warcards/gameplay.rb', line 78

def discard(result)
  while result[:ai_cards].size > 0
    result[:winner].discard << result[:ai_cards].pop
    result[:winner].discard << result[:player_cards].pop
  end
end

#game_over?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/warcards/gameplay.rb', line 27

def game_over?
  end_game     = Hash.new
  participants = [@player, @ai]
  participants.each_with_index do |participant, index|
    if (participant.stack.length + participant.discard.length) < 1
      participants.delete_at index
      end_game[:winner] = participants.first.name
      end_game[:over?]  = TRUE
    else
      end_game[:over?] = FALSE
    end
  end
  end_game
end

#playerObject



97
98
99
# File 'lib/warcards/gameplay.rb', line 97

def player
  @player
end

#player_cardsObject



101
102
103
# File 'lib/warcards/gameplay.rb', line 101

def player_cards
  @player_cards
end

#rearm?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/warcards/gameplay.rb', line 42

def rearm?
  [@ai, @player].each do |participant|
    if participant.stack.length < 1
      participant.discard.each do |card|
        participant.stack << card
      end
      participant.stack.flatten!
      participant.discard.clear
    end
  end
end

#show_cardsObject



73
74
75
76
# File 'lib/warcards/gameplay.rb', line 73

def show_cards
  player_cards << @player.stack.pop
  ai_cards << @ai.stack.pop
end

#shuffleObject



17
18
19
# File 'lib/warcards/gameplay.rb', line 17

def shuffle
  @deck.shuffle!
end

#war?(output = STDOUT) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/warcards/gameplay.rb', line 64

def war?(output = STDOUT)
  while @ai_cards.last.value == @player_cards.last.value
    rearm?
    #TODO make the following line a flag or something so the view code can decide to show it or not.
    output.puts "WAR!!!"
    show_cards
  end
end