Class: Yahtzee::Game

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

Overview

CLI interface

Instance Method Summary collapse

Instance Method Details

#determine_preserve(s) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/yahtzee.rb', line 330

def determine_preserve(s)
  ret = [1,1,1,1,1]
  s.split("").each do |c|
    case c
    when 'a'
      ret[0] = 0
    when 'b'
      ret[1] = 0
    when 'c'
      ret[2] = 0
    when 'd'
      ret[3] = 0
    when 'e'
      ret[4] = 0
    end
  end
  return ret
end

#format(score) ⇒ Object



307
308
309
310
# File 'lib/yahtzee.rb', line 307

def format(score)
  return "  " if score == -1
  return "%2d" %[score]
end

#parse(c) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/yahtzee.rb', line 349

def parse c
  case c
  when 'a'
    cat = :aces
  when 'b'
    cat = :twos
  when 'c'
    cat = :threes
  when 'd'
    cat = :fours
  when 'e'
    cat = :fives
  when 'f'
    cat = :sixes
  when 'g'
    cat = :three_of_a_kind
  when 'h'
    cat = :four_of_a_kind
  when 'i'
    cat = :full_house
  when 'j'
    cat = :small_straight
  when 'k'
    cat = :large_straight
  when 'l'
    cat = :yahtzee
  when 'm'
    cat = :chance
  else
    return false
  end
  return cat
end


312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/yahtzee.rb', line 312

def print(score, dice, m1, m2, m3)
  puts   " ______________________________________ " 
  printf "|a Aces    [%s] |g 3 of a kind    [%s] | Dice:\n", format(score[:aces]), format(score[:three_of_a_kind])
  printf "|b Twos    [%s] |h 4 of a kind    [%s] |   a %d\n", format(score[:twos]), format(score[:four_of_a_kind]), dice[0]
  printf "|c Threes  [%s] |i Full house     [%s] |   b %d\n", format(score[:threes]), format(score[:full_house]), dice[1]
  printf "|d Fours   [%s] |j Small straight [%s] |   c %d\n", format(score[:fours]), format(score[:small_straight]), dice[2]
  printf "|e Fives   [%s] |k Large straight [%s] |   d %d\n", format(score[:fives]), format(score[:large_straight]), dice[3]
  printf "|f Sixes   [%s] |l Yahtzee        [%s] |   e %d\n", format(score[:sixes]), format(score[:yahtzee]), dice[4]
  printf "|               |  Yahtzee bonus   %3d |\n", score[:yahtzee_bonus]
  printf "|Subtotal:   %2d |m Chance         [%s] | %s\n", score[:upper_section_subtotal], format(score[:chance]), m1
  printf "|Bonus:      %2d |                      | %s\n", score[:upper_section_bonus], m2
  printf "|Total:     %3d |Total:            %3d | %s\n", score[:upper_section_total], score[:lower_section_total], m3
  printf "|______ Grand Total:%4d ______________|   > ", score[:grand_total]
  input = STDIN.gets.chomp
  return input
end

#startObject



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/yahtzee.rb', line 383

def start
  game = Yahtzee::Controller.new

  13.times do |i|
    print game.get_data, [0,0,0,0,0], "", "", "Press enter to roll"
    game.roll
    selection = print game.get_data, game.get_dice, "", "Select which dice you would like to roll again.", "Type the letters of the dice followed by enter: "
    game.reroll determine_preserve(selection)
    selection = print game.get_data, game.get_dice, "", "Select which dice you would like to roll again.", "Type the letters of the dice followed by enter: "
    game.reroll determine_preserve(selection)
    
    m1 = ""
    success = -1
    while success == -1
      selection = print game.get_data, game.get_dice, m1, "Select the category for this roll. Type the", "letter of the category followed by enter: "
      m1 = "Selection invalid"
      success = game.score(parse(selection.split("")[0]))
    end
  end
  print game.get_data, [0,0,0,0,0], "", "Game complete", ""
end