Skip to content

Instantly share code, notes, and snippets.

@bartekupartek
Created April 24, 2015 12:05
Show Gist options
  • Save bartekupartek/9a900fac91a1e21dd9dd to your computer and use it in GitHub Desktop.
Save bartekupartek/9a900fac91a1e21dd9dd to your computer and use it in GitHub Desktop.
a = [323,434,543]
(1..5).collect { |v| {name: "pole #{v}"} }
a.tap {|ary| ary << 333}
a.include? 333
a.inject{|sum,x| sum + x }
a.reduce(:+)
headers = ["one","two","three"]
rows = ["1","2","3"]
sheet_arr = headers.zip(rows)
sheet_arr.transpose
sheet_hsh = Hash[headers.zip(rows)]
a << 333
a.uniq!
a.sort!
a.shuffle!
a.sample
a.reverse
class Tracker
attr_accessor :is_running
def initialize
@time = Time.now
@is_running = true
end
def to_s
@time.strftime("%m-%e-%y %H:%M")
end
end
trackers = []
5.times do |i|
trackers << Tracker.new
end
# trackers.map do |t|
# puts t
# end
# trackers.map { |t| puts t }
trackers.last(2).map{|t| t.is_running = false}
trackers.reject { |t| t.is_running }
trackers.select { |t| t.is_running }
# merge
# h = [{"id"=>3,"val"=>"vw","model"=>"golf"},{"id"=>4,"val"=>"vw","model"=>"polo"},{"id"=>5,"val"=>"vw","model"=>"garbus"}]
# h.group_by { |i| i["vw"] }.map do |brand, group|
# {
# "id" => group["id"],
# brand["val"] => {
# "model" => group["model"]
# }
# }
# end
class Board
def initialize
@cells = []
@cells[0] = {name: "pole 1"}
@cells[1] = {name: "pole 2"}
@cells[2] = {name: "pole 3"}
@cells[3] = {name: "pole 4"}
@cells[4] = {name: "pole 5"}
@cells[5] = {name: "won!"}
end
def cell_name(pos)
@cells[pos][:name]
end
end
class Player
attr_accessor :pos, :name, :won
def initialize(game, name)
@pos, @name = 0, name
@board = game.board
@won = false
end
def turn
rolled = roll_diece
@pos = rolled - 1
cell_name = @board.cell_name(@pos)
@won = true if cell_name == "won!"
puts "player #{@name} rolled a #{rolled} landed on #{cell_name}"
end
def roll_diece
(rand 1..6)
end
end
class Game
attr_accessor :board, :players
def initialize
@turn = 0
@round = 0
@board = Board.new
@players = [Player.new(self, "GRZESIU"), Player.new(self, "MICHAL"), Player.new(self, "BARTEK")]
end
def play!
until game_over?
@turn += 1
if @turn >= @players.size
@turn = 0
@round += 1
puts "round #{@round}"
end
@players[@turn].turn
end
puts "game over"
end
def game_over?
@players.select { |plr| plr.won }.any?
end
end
game = Game.new
game.play!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment