-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.rb
70 lines (58 loc) · 2.02 KB
/
game.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
require_relative 'player'
require_relative 'game_board'
require_relative 'token'
require_relative 'die'
class Game
include Die
attr_accessor :player_list
attr_accessor :current_player
attr_reader :game_board
def initialize(num_players)
@game_board = GameBoard.new(10)
@player_list = Array.new
num_players.times do
@player_list << Player.new
end
puts "#{num_players} are playing the game, Starting with player 1"
@current_player = @player_list.first
end
def start_game
while(true) do
@game_board.print_board
puts "Press Enter to roll dice and move your token"
STDIN.gets.chomp
roll_die_and_move_token
if player_won?
break
end
print_break
move_to_next_player
end
puts "Player won"
end
private
def roll_die_and_move_token
die_roll_result = @current_player.roll_die
current_token_location = @current_player.get_token_location
p "Players previous location is #{current_token_location}"
final_location = die_roll_result + current_token_location
final_location = @game_board.location_is_a_snake(final_location) || @game_board.location_is_a_ladder(final_location) || final_location
unless @game_board.location_is_valid(final_location)
final_location = current_token_location
end
@current_player.move_token_to_location final_location
puts "Player token has moved to this location #{final_location}"
end
def player_won?
@game_board.location_is_last(@current_player.get_token_location)
end
def move_to_next_player
player_number = (@player_list.index(@current_player) + 1) % @player_list.count
puts "Now Player #{player_number + 1}"
@current_player = @player_list[player_number]
end
def print_break
puts
puts "******************************************"
end
end