forked from vikingeducation/prep_ruby_challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountingGame.rb
81 lines (60 loc) · 1.58 KB
/
CountingGame.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 10 friends in a circle count from 1-100
# Catch 1: when number is divisible by 7, switch directions
# Catch 2: when number is divisble by 11, skip next person
def countingGame(totalPlayers, targetNumber)
player = 1
normalRotation = true
# Find next player based on direction, current player, total # of players, and current number
def rotatePlayer(normalRotation, currentPlayer, totalPlayers, numberNeeded)
#CHECK DIRECTION
if normalRotation == true
# DIVISIBLE BY 11? SKIP
if numberNeeded % 11 == 0
if currentPlayer == totalPlayers - 1
nextPlayer = 1
elsif currentPlayer == totalPlayers
nextPlayer = 2
else
nextPlayer = currentPlayer + 2
end
# NOT DIVISBLE BY 11
else
if currentPlayer == totalPlayers
nextPlayer = 1
else
nextPlayer = currentPlayer + 1
end
end
# CHANGE DIRECTION
else
#DIVISIBLE BY 11? SKIP
if numberNeeded % 11 == 0
if currentPlayer == 1
nextPlayer = totalPlayers - 1
elsif currentPlayer == 2
nextPlayer = totalPlayers
else
nextPlayer = currentPlayer - 2
end
#NOT DIVISBLE BY 11
else
if currentPlayer == 1
nextPlayer = totalPlayers
else
nextPlayer = currentPlayer - 1
end
end
end
return nextPlayer
end
(1..targetNumber).each do |number|
puts "Player #{player} said the number #{number}"
#CHECK FOR CHANGE IN DIRECTION
if number % 7 == 0
normalRotation = !normalRotation
end
player = rotatePlayer(normalRotation, player, totalPlayers, number)
end
end
# TEST:
countingGame(10, 100)