tiy-houston-q3-rails / day-7

0 stars 0 forks source link

2048.rb #5

Open dpalley opened 10 years ago

dpalley commented 10 years ago

require 'io/console'

implement the add random 2 or 4

input character issue

clear screen

clean up diagnostic output

make the move more elegant

@grid = [

[ 2, 4, 8, 512],

[64, 8, 4, 128],

[32, 2, 1024, 8],

[0, 8, 0, 4]

]

@grid = [

[0, 0, 0, 0],

[0, 0, 0, 0],

[0, 0, 0, 0],

[0, 0, 0, 0]

]

@grid = [ [0, 0, 2, 2], [8, 4, 4, 0], [0, 16, 0, 32], [0, 2, 0, 2] ]

@grid = [

[2, 4, 2, 4],

[8, 2, 8, 2],

[2, 4, 2, 4],

[8, 2, 8, 2]

]

def move_left

add adjacent values that are equal

for row_index in 0..3 do for col_index in 0..2 do if @grid[row_index][col_index] == @grid[row_index][col_index+1] @grid[row_index][col_index]*=2 @grid[row_index][col_index+1]=0 end end #column end #row

3.times do #band-aid solution for row_index in 0..3 do for col_index in 0..2 do if @grid[row_index][col_index] == 0 @grid[row_index][col_index] = @grid[row_index][col_index+1] @grid[row_index][col_index+1]=0 end end #column end #row end #3.times end

def move_right

add adjacent values that are equal

for row_index in 0..3 do for col_index in (3).downto(1) do if @grid[row_index][col_index] == @grid[row_index][col_index-1] @grid[row_index][col_index]*=2 @grid[row_index][col_index-1]=0 end end #column end #row

3.times do #band-aid solution for row_index in 0..3 do for col_index in (3).downto(1) do if @grid[row_index][col_index] == 0 @grid[row_index][col_index] = @grid[row_index][col_index-1] @grid[row_index][col_index-1]=0 end end #column end #row end #3.times end

def move_up

add adjacent values that are equal

for col_index in 0..3 do for row_index in 0..2 do if @grid[row_index][col_index] == @grid[row_index+1][col_index] @grid[row_index][col_index]*=2 @grid[row_index+1][col_index]=0 end end #row end #col

3.times do #band-aid solution for col_index in 0..3 do for row_index in 0..2 do if @grid[row_index][col_index] == 0 @grid[row_index][col_index] = @grid[row_index+1][col_index] @grid[row_index+1][col_index]=0 end end #row end #col end #3.times end

def move_down

add adjacent values that are equal

for col_index in 0..3 do for row_index in (3).downto(1) do if @grid[row_index-1][col_index] == @grid[row_index][col_index] @grid[row_index][col_index]*=2 @grid[row_index-1][col_index]=0 end end #row end #col

3.times do #band-aid solution for col_index in 0..3 do for row_index in (3).downto(1) do if @grid[row_index][col_index] == 0 @grid[row_index][col_index] = @grid[row_index-1][col_index] @grid[row_index-1][col_index]=0 end end #row end #col end #3.times end

def set_value_for_cell(cell, value) row = cell.first column = cell.last row = @grid[row] row[column] = value end

def add_random_to_grid value_to_add = [2,4].sample chosen_cell = open_cells.sample if chosen_cell set_value_for_cell(chosen_cell, value_to_add) end end

def value_at(cell) x = cell.first y = cell.last row = @grid[x] if row value = row[y] else 0 end end

def open_cells cells = [] @grid.each_with_index do |rows, row_index| rows.each_with_index do |columns, column_index| if value_at([row_index, column_index]) == 0 cells << [row_index, column_index] end end end cells end

def matrix_closed? @grid.flatten.all? do |number| number > 0 end end

def no_adjacent_pair_exists? no_adjacent_pair = true for row_index in 0..3 do for col_index in 0..2 do if @grid[row_index][col_index] == @grid[row_index][col_index+1] no_adjacent_pair = false end end #column end #row

for col_index in 0..3 do for row_index in 0..2 do if @grid[row_index][col_index] == @grid[row_index+1][col_index] no_adjacent_pair = false end end #column end #row if no_adjacent_pair puts "no adjacent pair found in method" end return no_adjacent_pair end

def run_game

add_random_to_grid

add_random_to_grid

available_move = true while available_move do

system "clear"

add_random_to_grid
print_grid
matrix_full = false
matrix_full = matrix_closed?
no_pairs = true
no_pairs = no_adjacent_pair_exists?
if matrix_full and no_pairs
  puts "Matrix is full"
  puts "Loser"
  available_move = false
else

  puts "A move is available"
  puts "Enter direction (u/d/l/r) (q to quit)"
  # move = STDIN.getc
  move = nil
  move = gets.chomp.strip

  case move
    when "l"
     move_left
    when "r"
      move_right
    when "u"
      move_up
    when "d"
      move_down
    when "q"
      available_move = false
  end #case
end #if-else game_is_over

end #while available move end

def print_grid score = 0 @grid.each do |row| row.each do |i| value_to_show = i.to_s value_to_show = "" if i == 0 printf "%6s |", value_to_show score+=i end print "\n" end puts "Score is " + score.to_s end

run_game