Class: Cardgame::Game

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

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



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

def initialize
  @deck = Deck.new
  @player = Player.new
  @ai = Ai.new
  @gameplay = gameplay(:deck => @deck, :player => @player, :ai => @ai)
  @gameplay.shuffle
  @gameplay.deal
  @output = Struct.new(:winner, :player_feedback, :ai_feedback, :posed)
  @slow = TRUE
end

Instance Method Details

#aiObject



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

def ai
  @ai
end

#ai_holdingsObject



112
113
114
115
116
# File 'lib/warcards.rb', line 112

def ai_holdings
  # When this is run there is one card from each participant in play.
  # That is what the +1 compensates for and why the test looks wack.
  (@gameplay.ai.stack.length + @gameplay.ai.discard.length + 1)
end

#build_graph(player_holdings, ai_holdings) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/warcards.rb', line 98

def build_graph(player_holdings, ai_holdings)
  graph = ""
  player_holdings.times { graph << "p" }
  graph << "|"
  ai_holdings.times { graph << "a" }
  graph
end

#challenge_ai(result, output = STDOUT, rnd_src = rand) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/warcards.rb', line 126

def challenge_ai(result, output = STDOUT, rnd_src = rand)
  if test_ai(rnd_src)
    output.puts "Ai was correct. Ai wins the round."
  else
    output.puts "Ai was wrong. #{@gameplay.player.name} became the winner!"
    result[:winner] = @gameplay.player
    if @slow
      sleep 0.75
    end
  end
end

#challenge_participants(result, question = @questions.sample, input = STDIN, output = STDOUT, rnd_src = rand) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/warcards.rb', line 118

def challenge_participants(result, question = @questions.sample, input = STDIN, output = STDOUT, rnd_src = rand)
  if result[:winner].class == Player
    challenge_player(result, question, input, output)
  else
    challenge_ai(result, output, rnd_src)
  end
end

#challenge_player(result, question = @questions.sample, input = STDIN, output = STDOUT) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/warcards.rb', line 138

def challenge_player(result, question = @questions.sample, input = STDIN, output = STDOUT)
  #question = @questions.sample
  if test_player(question, input, output)
    output.puts "Correct! Yay!"
  else
    output.puts %Q{Oooh. I'm sorry. The correct answer was "#{question.answer}". #{@gameplay.ai.name} became the winner.}
    result[:winner] = @gameplay.ai
  end
  #continue?(input, output)
end

#deckObject



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

def deck
  @deck
end

#gameplay(args) ⇒ Object



29
30
31
32
33
34
# File 'lib/warcards.rb', line 29

def gameplay(args)
  deck = args[:deck]
  player = args[:player]
  ai = args[:ai]
  Gameplay.new(:deck => deck, :player => player, :ai => ai)
end

#get_filename(input = STDIN, output = STDOUT) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/warcards.rb', line 66

def get_filename(input = STDIN, output = STDOUT)
  output.puts(Dir.glob "**/*.txt")
  sample_questions = "spec/test_question_file.txt"
  output.puts "What question file?\n(Just hit enter to use the sample questions.)\nfilename: "
  filename = input.gets.chomp
  if File.file?(File.expand_path(filename))
    File.expand_path(filename)
  else
    sample_questions
  end
end

#output_cli(result, output = STDOUT) ⇒ Object

def continue?(input = STDIN, output = STDOUT, really_end = :yes)

output.puts("go again?\n")
next_round = input.gets
if next_round.downcase.chomp.include? "n"
  output.puts "You ended the game"
  if really_end == :yes
    exit
  end
end

end



89
90
91
92
93
94
95
96
# File 'lib/warcards.rb', line 89

def output_cli(result, output = STDOUT)
  graph = build_graph(player_holdings, ai_holdings)
  output.puts "\n#{result[:winner].name} has the high card."
  output.puts "Player has #{player_holdings} cards.\tAI has #{ai_holdings} cards."
  output.puts graph
  #challenge_participants(result)
  # TODO get challenge_participants out of here. No longer belongs. Causing hard tests.
end

#playerObject



25
26
27
# File 'lib/warcards.rb', line 25

def player
  @player
end

#player_holdingsObject



106
107
108
109
110
# File 'lib/warcards.rb', line 106

def player_holdings
  # When this is run there is one card from each participant in play.
  # That is what the +1 compensates for and why the test looks wack.
  (@gameplay.player.stack.length + @gameplay.player.discard.length + 1)
end

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/warcards.rb', line 36

def run
  filename = get_filename
  until File.file? filename
    get_filename
  end
  @questions = Querinator::Game.new.get_questions(filename)
  loop do
    begin
      end_game = @gameplay.game_over?

      if end_game[:over?]
        puts "Game over #{end_game[:winner]} won!"
        exit
      else
        @gameplay.rearm?
        @gameplay.show_cards
        @gameplay.war?
        result = @gameplay.contest
        output_cli(result)
        challenge_participants(result)
        #continue?
        @gameplay.discard(result)
      end
    rescue SignalException => e
      puts "\n\nThanks for playing!"
      exit(status=1)
    end
  end
end

#test_ai(rand_src = rand, difficulty = 0.4) ⇒ Object



155
156
157
# File 'lib/warcards.rb', line 155

def test_ai(rand_src = rand, difficulty = 0.4)
  @ai.difficulty_check?(rand_src, difficulty)
end

#test_player(question, input = STDIN, output = STDOUT) ⇒ Object



149
150
151
152
153
# File 'lib/warcards.rb', line 149

def test_player(question, input = STDIN, output = STDOUT)
  output.puts question.pose
  answer = input.gets
  question.is_correct?(answer.chomp)
end