Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

J Pillay March 23 Cohort #393

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

gem "rspec", "~> 3.12"

# gem "rails"
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.5.0)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)

PLATFORMS
x64-mingw-ucrt

DEPENDENCIES
rspec (~> 3.12)

BUNDLED WITH
2.4.9
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# J Pillay Implementation and Design Phylosophy

Each of the objects has a separate function and the models reflect
real world (RW) objects. The data in score card and frame reflect the data
that make up their RW counterparts - any extra data can be extrapolated from
base data eg, messages and bonuses which also need to be reflexive. No base data
recorded is ever mutated or deleted as it may be called upon later and also means
that the score card has persistence and can be used for historical comparison. Integrety
of data is essential to the system.

With this design the system is very malleable. Print Card (when implemented) can be changed
with different formats without the worry of affecting the underlying program. The same going for
Score Game, which can be changed to accommodate extra players. The program could be changed
to randomise games and throws - thus it may on day even help us answer the age old question...
who is more lucky? Player 1, or 2? A question which will have to wait, for now...

Bowling Challenge in Ruby
=================

Expand Down
7 changes: 7 additions & 0 deletions lib/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative './controllers'

card = ScoreCard.new

scorer = Scorer.new

scorer.score_game(card)
54 changes: 54 additions & 0 deletions lib/controllers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require_relative './models'

class Printer
attr_accessor :score_card
def initialize
end

def print
return "Frame 5 - You got a STRIKE!" #This was a dummy print to see if I could test for puts output in RSpec.
end
end

class Scorer
def initialize
end

def score_game(score_card)

# populate the score card with frame objects to be written to
for i in (1..10)
frame = Frame.new(i)
score_card.frames.push(frame)
end

# for each frame, populate each roll with a score.
for i in (0..8)
for n in (0..1)
puts "Please enter the score of your roll from 0-10"
score = gets.chomp.to_i
if score == 10
score_card.frames[i].score.push(10)
break
else
score_card.frames[i].score.push(score)
end
end
end
for n in (0..1)
puts "This is the last frame"
puts "Please enter the score of your roll from 0-10"
score = gets.chomp.to_i
score_card.frames[9].score.push(score)
if n == 1 && score_card.frames[9].score.sum != 10
score_card.frames[9].score.push(0)
else
puts "This is the last frame"
puts "Please enter the score of your roll from 0-10"
score = gets.chomp.to_i
score_card.frames[9].score.push(score)
end
end
puts "Your final score is #{score_card.current_score}"
end
end
66 changes: 66 additions & 0 deletions lib/models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class Frame
attr_accessor :frame_num, :score, :str_rep, :message
def initialize(frame_num, score=[])
@frame_num = frame_num
@score = score
@str_rep = []
@message = ""
end

def add_roll(roll_score)
@score.push(roll_score)
end

def str_rep(score)
if @score[0] = 10
@str_rep = ["", "X"]
elsif @score.sum == 10
@str_rep = [@score[0].to_s, "/"]
elsif
for i in @score
@str_rep.push(i.to_s)
end
end
end
end

class ScoreCard
attr_accessor :frames, :frame_bonuses
def initialize
@frames = []
@frame_bonuses = []
end

def add_frame(frame)
@frames.push(frame)
end

def current_score
frames_total = 0
@frames.each { |frame| frames_total += frame.score.sum }
self.gen_bonuses
return frames_total + @frame_bonuses.sum
end

def gen_bonuses
@frame_bonuses = []
for i in 0...9
if @frames[i].score[0] == 10
if @frames[i+1] == nil and @frames[i].frame_num != 9
return nil
elsif @frames[i].frame_num == 9
@frame_bonuses.push(@frames[i+1].score[0..1].sum)
return nil
elsif @frames[i+1].score[0] == 10 && @frames[i+2] != nil
@frame_bonuses.push(10+@frames[i+2].score[0])
else
@frame_bonuses.push(@frames[i+1].score.sum)
end
elsif @frames[i].score.sum == 10
frame_bonuses.push(@frames[i+1].score[0])
else
frame_bonuses.push(0)
end
end
end
end
Loading