Confusing control flow (Ruby) -


i'm trying create game in ruby involving selecting numbers in certian order , getting right sequence in limited number of tries, blah, blah, blah. issue no matter numbers guess, doesn't add @correct_r variable, nor display if it's in correct position.

i'm certian i'm missing if/else statement in there somewhere, not sure where. appreciated.

code

#by touka, ©2015 actions = (0..9).to_a @sequence = [] 4.times |key|     key = actions.sample     @sequence << key end @fails_left = 10 @correct_a = 0 @correct_r = 0 @a = " " @b = " " @c = " " @d = " " def sel     system "cls"     puts """     __________________________________    /   ___     ___     ___     ___    \\    |  |   |   |   |   |   |   |   |   |    |  | #{@a} |   | #{@b} |   | #{@c} |   | #{@d} |   |    |  |___|   |___|   |___|   |___|   |    \\__________________________________/  tries left:                      #{@fails_left} correct numbers (in wrong spot): #{@correct_r}  enter guess (x x x x):"""     guess = gets.chomp     guess = guess.split(" ")     case guess[0]     when @sequence[0]         @correct_a += 1         @a = "#"     when @sequence[1]         @correct_r += 1     when @sequence[2]         @correct_r += 1     when @sequence[3]         @correct_r += 1     end     case guess[1]     when @sequence[0]         @correct_r += 1     when @sequence[1]         @correct_a += 1         @b = "#"     when @sequence[2]         @correct_r += 1     when @sequence[3]         @correct_r += 1     end     case guess[2]     when @sequence[0]         @correct_r += 1     when @sequence[1]         @correct_r += 1     when @sequence[2]         @correct_a += 1         @c = "#"     when @sequence[3]         @correct_r += 1     end     case guess[3]     when @sequence[0]         @correct_r += 1     when @sequence[1]         @correct_r += 1     when @sequence[2]         @correct_r += 1     when @sequence[3]         @correct_a += 1         @d = "#"     end     ct = @correct_r + @correct_a     if ct == 0         @fails_left -= 1     else         hi = " "     end     sel end sel 

your problem lies in different types of sequence , guess variables. sequence array of integer , guess array of strings. need convert them same type.

guess = guess.map(&:to_i) 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -