diff --git a/countgame.rb b/countgame.rb new file mode 100644 index 0000000..e767a65 --- /dev/null +++ b/countgame.rb @@ -0,0 +1,41 @@ +#Counting game, 10 players count to 100 which player will be the one to count 100. +#Special rules if the number is divisible by 7 the direction switches from 1,2,3 to 3,2,1 ect.. +#If the number is divisble by 11 skip a player for the next number + +def count_game(final_number, players) + count = 1 + direction = 1 + player_turn = 1 + skip = false + + while count <= final_number + + puts "Player #{player_turn} says #{count}" + + player_turn += (1*direction) + count += 1 + + if skip == true + player_turn += (1*direction) + skip = false + end + + direction *= -1 if count % 7 == 0 + + skip = true if count % 11 == 0 + + if player_turn < 1 + player_turn += players + elsif player_turn > players + player_turn -= players + end + + end +end + +puts "Enter amount of players:" +all_players = gets.chomp +puts "Enter the final number to count to:" +last_num = gets.chomp + +count_game(last_num.to_i, all_players.to_i) \ No newline at end of file diff --git a/problems1-6.rb b/problems1-6.rb new file mode 100644 index 0000000..7cbc110 --- /dev/null +++ b/problems1-6.rb @@ -0,0 +1,90 @@ + +puts "Power:" +def power(base, exponent) + counter = 1 + total = 1 + while counter <= exponent + total = total * base + counter +=1 + end + puts total +end + +power(3,4) +#----------------------------------------- +#----------------------------------------- +puts "" +puts "Factorial:" +def factorial(num) + total = 1 + (1..num).each do |n| + total = total * n + end + puts total +end + +puts factorial(5) +#----------------------------------------- +#----------------------------------------- +puts "" +puts "Uniques:" +def uniques(array) + unique_array = [] + array.each do |item| + if unique_array.include?(item) == false + unique_array.push(item) + end + end + puts unique_array +end + +uniques([1,5,"frog",2,1,3,"frog"]) +#----------------------------------------- +#----------------------------------------- +puts "" +puts "Combinations:" +def combinations(array1, array2) + new_array = [] + array1.each do |first| + array2.each do |second| + new_array << first + second + end + end + puts new_array +end +combinations(["on", "in"],["to", "rope"]) +#----------------------------------------- +#----------------------------------------- +puts "" +puts "Primes:" +def is_prime?(number) + start = 2 + while start < number + if (number % start == 0) then + return false + else + start += 1 + end + end + return true +end +puts is_prime?(7) +puts is_prime?(14) +#----------------------------------------- +#----------------------------------------- +puts "" +puts "Rectangle Overlap:" + +def overlap(rect1, rect2) + if (((rect1[0][0] <= rect2[0][0]) && (rect2[0][0] < rect1[1][0]) && + (rect1[0][1] <= rect2[0][1]) && (rect2[0][1] < rect1[1][1])) || + ((rect2[0][0] <= rect1[0][0]) && (rect1[0][0] < rect2[1][0]) && + (rect2[0][1] <= rect1[0][1]) && (rect1[0][1] < rect2[1][1]))) then + puts true + else + puts false + end +end + +overlap( [ [0,0],[3,3] ], [ [1,1],[4,5] ] ) +overlap( [ [0,0],[1,4] ], [ [1,1],[3,2] ] ) \ No newline at end of file