Skip to content

Added my solution#79

Open
banalui wants to merge 4 commits intovikingeducation:masterfrom
banalui:master
Open

Added my solution#79
banalui wants to merge 4 commits intovikingeducation:masterfrom
banalui:master

Conversation

@banalui
Copy link
Copy Markdown

@banalui banalui commented Dec 14, 2016

No description provided.

puts "Do you want to play against computer?(y/n):"
user_input = gets.chomp
good_input_received = check_input user_input
until good_input_received
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

until can be easily replaced by loop and the repeated user_input = gets.chomp line can be avoided.

Comment thread connect_four/lib/human.rb
end

def is_legal_column?(input)
((input.to_i > 0) && (input.to_i < 8)) ? true : false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be replaced by (1..7).include?(input)

Comment thread connect_four/lib/human.rb
end

def is_integer?(input)
(input.to_i.to_s == input) ? true : false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ternary operator can be avoided here and in is_legal_column?.
(input.to_i.to_s == input) ? true : false is equivalent to input.to_i.to_s == input

Comment thread connect_four/lib/board.rb
@number_of_pos_filled = 0
@number_of_rows = num_rows
@number_of_cols = num_cols
@board_array = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@board_array initialization should be refactored. See docs on ruby Array initialization.

Comment thread connect_four/lib/board.rb

def add_piece(color, col)
if board_array_column[col][0] == NO_COLOR
index_to_add = board_array_column[col].each_index.select{|i| board_array_column[col][i] == NO_COLOR}.max
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this long line is repeated in add_piece and remove_piece. it can be pulled out to a method.

Comment thread connect_four/lib/board.rb
possible_win_pos.times do |index|
start_pos = index
end_pos = index + 3
if line[start_pos..end_pos].all? {|item| item == color}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if block can be replaced by return true if line[start_pos..end_pos].all? {|item| item == color}

Comment thread connect_four/lib/board.rb
end

def check_line(line, color)
return_val = false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused variable

Comment thread connect_four/lib/board.rb

def check_winning_pos_horiz(color)
@board_array.each do |row|
if check_line(row, color)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equivalent to return true if check_line(row, color)

Comment thread connect_four/lib/board.rb

def check_winning_pos_vertic(color)
board_array_column.each do |col|
if check_line(col, color)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants