Created
September 12, 2016 09:40
-
-
Save sameh-sharaf/1d64263174fca25a6191b45545adcef2 to your computer and use it in GitHub Desktop.
This is a game where 4 players have 6 dices each. They throw the dices each time. Dices with 6 will be removed. Dices with 1 will be added to player's dices in the right.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# | |
# This is a game where 4 players have 6 dices each. They throw the dices each time. | |
# Dices with 6 will be removed. Dices with 1 will be added to player's dices in the right. | |
# | |
## | |
class DiceGame | |
private | |
# Number of players. | |
@players = 4 | |
# Number of dice for each player. | |
@dice = 6 | |
# Dice grid to be initialized when calling constructor. | |
@dice_grid = Array.new(@players) { Array.new(@dice) } | |
public | |
## | |
# DiceGame constructor. | |
# | |
# @params | |
# players: Number of players. | |
# dice: Number of dice for each player. | |
# | |
def initialize(players, dice) | |
@players = players | |
@dice = dice | |
@dice_grid = Array.new(@players) { Array.new(@dice) } | |
end | |
## | |
# Rolls the dice for each player in each round. | |
# | |
def roll_dice | |
@dice_grid.each do |row| | |
(0...row.length).each do |i| | |
row[i] = rand(6) + 1 | |
end | |
end | |
end | |
## | |
# Removes all dice with number '6' on top. | |
# | |
def remove_6s | |
@dice_grid.each do |row| | |
row.delete(6) | |
end | |
end | |
## | |
# Moves dice with number '1' on top to right player's dice. | |
# | |
def move_1s | |
count_1s = [] | |
@dice_grid.each do |row| | |
count = 0 | |
row.each {|x| (x == 1) ? count += 1 : 0} | |
count_1s.push(count) | |
row.delete(1) | |
end | |
(1...@dice_grid.length).each do |i| | |
(1..count_1s[i-1]).each do | |
@dice_grid[i].push(1) | |
end | |
end | |
(1..count_1s[count_1s.length - 1]).each do | |
@dice_grid[0].push(1) | |
end | |
end | |
## | |
# Checks if there is a winner to end the game. | |
# | |
def check_winners | |
@dice_grid.each do |row| | |
if (row.length == 0) | |
return true | |
end | |
end | |
return false | |
end | |
## | |
# Prints dice grid to console. | |
# | |
def print_grid | |
(0...@dice_grid.length).each do |i| | |
puts "Player #{i+1}: #{@dice_grid[i]}" | |
end | |
end | |
## | |
# Prints dice game winner(s). | |
# | |
def print_winners | |
(0...@dice_grid.length).each do |i| | |
if (@dice_grid[i].length == 0) | |
puts "Player #{i+1}" | |
end | |
end | |
end | |
## | |
# Here is where all the action begins. | |
# | |
def begin_game | |
round = 0 | |
while (!check_winners) | |
round += 1 | |
puts "Round #{round}:" | |
puts "-" * 7 | |
roll_dice | |
puts "After dice rolled:" | |
puts "-" * 15 | |
print_grid | |
puts | |
remove_6s | |
puts "Removed dice of 6:" | |
puts "-" * 15 | |
print_grid | |
puts | |
move_1s | |
puts "Moved dice of 1:" | |
puts "-" * 15 | |
print_grid | |
puts puts | |
end | |
puts "Winner(s):" | |
print_winners | |
end | |
end | |
### Create the game ### | |
player_count = 4 | |
dice_count = 6 | |
DiceGame.new(player_count, dice_count).begin_game |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment