Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. # U2.W6: Create a Bingo Scorer (SOLO CHALLENGE)
  2.  
  3. # I spent 3 hours on this challenge.
  4.  
  5. # Pseudocode
  6. #initialize the class with a Bingo Board
  7. #method to check for winner, print "BINGO" if the board meets the conditions of one type of winning board
  8. #horizontal board method
  9. #
  10. #if one subarray of the board has 'x' in each slot, BINGO
  11. #vertical method
  12. #if each sub array has one 'x' in the same index position, BINGO
  13. #right-to-left method
  14. #check first sub array for an 'x' in first index position
  15. #second subarray for an 'x' in second index position
  16. #third subarray for 'x' in third index position
  17. #fourth subarray for 'x' in fourth index position and fifth subarray, 'x' in fifth and final index position
  18. #left-to-right
  19. #reverse logic of the right-to-left method
  20.  
  21.  
  22.  
  23. # sample boards
  24.  
  25. # horizontal = [[47, 44, 71, 8, 88],
  26. # ['x', 'x', 'x', 'x', 'x'],
  27. # [83, 85, 97, 89, 57],
  28. # [25, 31, 96, 68, 51],
  29. # [75, 70, 54, 80, 83]]
  30.  
  31. # vertical = [[47, 44, 71, 'x', 88],
  32. # [22, 69, 75, 'x', 73],
  33. # [83, 85, 97, 'x', 57],
  34. # [25, 31, 96, 'x', 51],
  35. # [75, 70, 54, 'x', 83]]
  36.  
  37.  
  38. # right_to_left = [['x', 44, 71, 8, 88],
  39. # [22, 'x', 75, 65, 73],
  40. # [83, 85, 'x', 89, 57],
  41. # [25, 31, 96, 'x', 51],
  42. # [75, 70, 54, 80, 'x']]
  43.  
  44.  
  45. # left_to_right = [[47, 44, 71, 8, 'x'],
  46. # [22, 69, 75, 'x', 73],
  47. # [83, 85, 'x', 89, 57],
  48. # [25, 'x', 96, 68, 51],
  49. # ['x', 70, 54, 80, 83]]
  50.  
  51.  
  52.  
  53.  
  54. # Initial Solution
  55. class BingoScorer
  56. attr_reader :board
  57.  
  58. def initialize(board)
  59. @board = board
  60. @bingo = false
  61. end
  62.  
  63. def print_winning_board
  64. horizontal_win
  65. vertical_win
  66. right_to_left
  67. left_to_right
  68. if @bingo == true
  69. puts "CHAMPION!"
  70. end
  71. end
  72.  
  73. #print "BINGO" for a nested array with 'x' in each slot
  74. #there must be a better way to do line77
  75. def horizontal_win
  76. board.each do |blob|
  77. if blob == ['x', 'x', 'x', 'x', 'x']
  78. @bingo = true
  79. end#if
  80. end#board.each
  81. end#horizontal_win
  82.  
  83. #print "WINNER" for a board with 5 vertical x's in same index slot of nested array
  84. def vertical_win #will print all 5 x's
  85. board.each do |row|
  86. row.each do |element, index|
  87. if element == 'x'
  88. @bingo = true
  89. end#if
  90. end #row.each
  91. end #board.each
  92. end #vertical_win
  93.  
  94. #print "winner!" for diagonal right to left THIS WORKS
  95. def right_to_left
  96. r_index = 0
  97. c_index = 0
  98. board.each_with_index do |row, r_index|
  99. row.each_with_index do |number, c_index|
  100. if number == 'x'
  101. r_index += 1
  102. c_index += 1
  103. end #if
  104. end #row.each
  105. end #board.each
  106. @bingo = true
  107.  
  108. end #right_to_left
  109.  
  110. #print "winner!" for diagonal right to left THIS WORKS
  111. def left_to_right
  112. r_index = 0
  113. c_index = 0
  114. board.each_with_index do |row, r_index|
  115. row.each_with_index do |number, c_index|
  116. if number == 'x'
  117. r_index += 1
  118. c_index -= 1
  119. end
  120. end
  121. end
  122. @bingo = true
  123. end#left_to_right
  124.  
  125. end #class
  126.  
  127.  
  128. # Refactored Solution
  129.  
  130.  
  131. #print WINNER if each element of a subarray includes 'x' IT WORKS
  132. # def horizontal_win
  133. # @horizontal.each do |subarray|
  134. # if subarray.include?('x')
  135. # print "WINNER"
  136. # end
  137. # end
  138. # end
  139.  
  140.  
  141. # DRIVER TESTS GO BELOW THIS LINE
  142. # implement tests for each of the methods here:
  143. #HORIZONTAL
  144. play = BingoScorer.new([[47, 44, 71, 8, 88],
  145. ['x', 'x', 'x', 'x', 'x'],
  146. [83, 85, 97, 89, 57],
  147. [25, 31, 96, 68, 51],
  148. [75, 70, 54, 80, 83]])
  149.  
  150. p play.print_winning_board
  151.  
  152. #VERTICAL
  153. try_again = BingoScorer.new([
  154. [47, 44, 71, 'x', 88],
  155. [22, 69, 75, 'x', 73],
  156. [83, 85, 97, 'x', 57],
  157. [25, 31, 96, 'x', 51],
  158. [75, 70, 54, 'x', 83]])
  159.  
  160. p try_again.print_winning_board
  161.  
  162. #right-to-left
  163. diag = BingoScorer.new([['x', 44, 71, 8, 88],
  164. [22, 'x', 75, 65, 73],
  165. [83, 85, 'x', 89, 57],
  166. [25, 31, 96, 'x', 51],
  167. [75, 70, 54, 80, 'x']])
  168.  
  169. p diag.print_winning_board
  170.  
  171. play_again = BingoScorer.new(
  172. [[47, 44, 71, 8, 'x'],
  173. [22, 69, 75, 'x', 73],
  174. [83, 85, 'x', 89, 57],
  175. [25, 'x', 96, 68, 51],
  176. ['x', 70, 54, 80, 83]])
  177.  
  178. p play_again.print_winning_board
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement