Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3210c0e
primary requirements
pottery123 Mar 8, 2016
ff6d6bf
added first test. got spec to work. Added self.score to Scoring class
Lisa-Sano Mar 8, 2016
c7006d8
test to check that we can create array of letters from a given word
Lisa-Sano Mar 8, 2016
208511d
added 7 word length 50 point bonus
pottery123 Mar 8, 2016
6d635d9
added self.highest_score_from method to Scoring class. Added test to…
Lisa-Sano Mar 8, 2016
ee27614
updated self.highest_score_from to include situations where multiple …
Lisa-Sano Mar 8, 2016
ac171d6
added test cases. modified the self.highest_score_from method to fix …
Lisa-Sano Mar 8, 2016
6e9a07b
added methods and specs for player class
pottery123 Mar 8, 2016
5dbf07b
added comments to the scoring class.
Lisa-Sano Mar 9, 2016
0e09d55
added play(word) method, and total_score method
pottery123 Mar 9, 2016
e868f28
added return false if player has already won and the won? method
pottery123 Mar 9, 2016
736a874
added highest_scoring_word and highest_word_score methods and new spe…
Lisa-Sano Mar 9, 2016
bdbe49a
added comments
Lisa-Sano Mar 9, 2016
14f62e1
added TileBag class and spec file. added methods draw_tiles and tiles…
Lisa-Sano Mar 10, 2016
4660937
added tile hash and fill tile bag method
pottery123 Mar 10, 2016
013439a
fixed our problem with the spec
pottery123 Mar 10, 2016
d055b62
Made better test for scoring. Fixed win condition for word.length == …
Lisa-Sano Mar 10, 2016
6f91aa4
added test to check that removing tiles changes the length of the rem…
Lisa-Sano Mar 10, 2016
e8e1ea7
added draw_tile method in splyer class and the tile collection instan…
pottery123 Mar 10, 2016
f0cb0d3
Created a Board class that makes a 15x15 board matrix. can_be_played …
Lisa-Sano Mar 11, 2016
69bc6d8
did 10 tests for the board class
pottery123 Mar 11, 2016
6f95389
changed from attr_accessor to attr_reader for @board
Lisa-Sano Mar 11, 2016
973db9f
added comments to board.rb
Lisa-Sano Mar 11, 2016
01cef02
added classes to scrabble module, changed words in player-spec
Lisa-Sano Mar 11, 2016
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
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['specs/*-spec.rb']
end

task default: :test
61 changes: 61 additions & 0 deletions lib/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Board
attr_reader :board

def initialize
@board = make_board
end

def make_board
a = []
15.times do # fills array with 15 new arrays
a << Array.new(15) # each array has 15 elements, creating a 15 x 15 grid
end

return a
end

def can_be_played(row, col, direction, word)
can_play = true
letter_array = word.split(//)

letter_array.each do |letter|
# if there's something in the specific index on the board that does not match the current letter
if @board[row][col] != nil && @board[row][col] != letter
can_play = false
end

# increment either the row or column (depending on which direction specified by player)
row, col = increment_row_or_col(row, col, direction)
end

can_play
end

def play_word(row, col, direction, word)
# do not play the word if a conflicting word is already on the board
return "can't play there" if can_be_played(row, col, direction, word) == false

letter_array = word.split(//)

i = 0
while i < letter_array.length
@board[row][col] = letter_array[i]
i += 1

row, col = increment_row_or_col(row, col, direction)
end
end

def increment_row_or_col(row, col, direction)
if direction.downcase.include? 'right'
col += 1
else
row += 1
end

return [row, col]
end



end
44 changes: 44 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative './tilebag'
require_relative './scoring'

class Player
attr_reader :name, :plays ,:total_score, :tiles

def initialize(name)
@name = name
@plays = []
@total_score = 0
@already_won = false
@tiles = []
end

def play(word)
return false if @already_won == true # can't play more words if already won
@plays << word # adds word to list of words played
score = Scoring.score(word)
@total_score += score
won? # check to see if the game is won before giving back score
score
end

def won?
return false unless total_score > 100
puts "You win!"
@already_won = true # changes to true when score > 100
end

def highest_scoring_word
Scoring.highest_score_from(@plays)
end

def highest_word_score
highest_word = Scoring.highest_score_from(@plays)
Scoring.score(highest_word)
end

def draw_tiles(tile_bag)
@tiles.push *tile_bag.draw_tiles(7 - @tiles.length)
@tiles

end
end
60 changes: 60 additions & 0 deletions lib/scoring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Scoring

LETTER_SCORES = {
1 => ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
2 => ['D', 'G'],
3 => ['B', 'C', 'M', 'P'],
4 => ['F', 'H', 'V', 'W', 'Y'],
5 => ['K'],
8 => ['J', 'X'],
10 => ['Q', 'Z']
}

def self.score(word)
word_score = 0
word = word.upcase
letter_array = word.split(//)

letter_array.each do |letter|
LETTER_SCORES.each do |key, value|
if value.include? letter
word_score += key
end
end
end

word_score += 50 if letter_array.length == 7

word_score
end

def self.highest_score_from(array_of_words)
word_hash = {}
# create a hash where the key is the word and the value is its score
array_of_words.each do |word|
word_hash[word] = score(word)
end

word_list = []
# find the highest score value
max_value = word_hash.max_by { |key, value| value }

# put all the words with the highest score into an array
word_hash.each do |key, value|
if value == max_value[1]
word_list << key
end
end

# if the word used all 7 letters, it wins
seven_long = word_list.find {|word| word.length == 7}

return seven_long unless seven_long.nil?

# if no words used 7 letters, the shortest one wins
# or if multiple are shortest, the first one in the array wins
word_list.min_by {|words| words.length}

end

end
56 changes: 56 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class TileBag
TILE_HASH = {
"A" => 9,
"B" => 2,
"C" => 2,
"D" => 4,
"E" => 12,
"F" => 2,
"G" => 2,
"H" => 2,
"I" => 9,
"J" => 1,
"K" => 1,
"L" => 4,
"M" => 2,
"N" => 6,
"O" => 8,
"P" => 2,
"Q" => 1,
"R" => 6,
"S" => 4,
"T" => 6,
"U" => 4,
"V" => 2,
"W" => 2,
"X" => 1,
"Y" => 2,
"Z" => 1

}

def initialize
@tile_bag = fill_tile_bag
end

def fill_tile_bag
tile_array = []

TILE_HASH.each do |letter, number|
a = letter * number
a = a.split(//)
tile_array.push *a
end
tile_array
end

def draw_tiles(num)
@tile_bag.shuffle!
@tile_bag.shift(num)
end

def tiles_remaining
@tile_bag.length
end

end
14 changes: 14 additions & 0 deletions scrabble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Scrabble
include Scoring

include Player

include TileBag

include Board
end

require_relative './lib/scoring'
require_relative './lib/player'
require_relative './lib/tilebag'
require_relative './lib/board'
63 changes: 63 additions & 0 deletions specs/board-spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative '../lib/board'
require_relative './spec_helper'

describe Board do
before do
@board = Board.new
end

it "is an object we have access to" do
@board.wont_be_nil
end


it "should be an instance of Array" do
@board.board.must_be_instance_of Array
end

it "should return an array with a length of 15" do
@board.board.length.must_equal 15
end

describe "letter in indexes" do
before do
@board.play_word(0,0,'down','cat')
end

it "index [0][0] should be c" do
@board.board[0][0].must_equal "c"
end

it "index [1][0] should be a" do
@board.board[1][0].must_equal "a"

end

it "index [2][0] should be t" do
@board.board[2][0].must_equal "t"
end

end

describe "overlapping words on board" do
before do
@board.play_word(0,0,'down','cat')
@board.play_word(1,0,"right","apple")
end

it "index[1][1] should be p" do
@board.board[1][1].must_equal "p"
end

it "should return can't play there if word overlaps" do
@board.play_word(1,3,"down","dog").must_equal "can't play there"
end

it "index [1][3] should still be l" do
@board.play_word(1,3,"down","dog")
@board.board[1][3].must_equal "l"

end
end

end
Loading