Advertisement
Nemx

2048_r2_ruby

Jan 3rd, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.87 KB | None | 0 0
  1. require 'io/console'
  2. class Base_game
  3.     def initialize
  4.         @table = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  5.         @score = 0
  6.         2.times{create_random}
  7.         game
  8.     end
  9.  
  10.     def create_random
  11.         iszero = true
  12.         if @table.flatten.count(0) == 0
  13.             return 0
  14.         end
  15.         while(iszero)
  16.             int1 = rand(0..3)
  17.             int2 = rand(0..3)
  18.             float1 = rand(0.0..100.0).round(2)
  19.             if @table[int1][int2] == 0
  20.                 if float1 <= 10.0
  21.                     @table[int1][int2] = 4
  22.                 else
  23.                     @table[int1][int2] = 2
  24.                 end
  25.                 iszero = false
  26.             end
  27.         end
  28.     end
  29.     def compare
  30.         currindex = 3
  31.         3.times do
  32.             currlvl = 1
  33.             currindex.times do
  34.                 if @line[currindex-currlvl] != 0 && @line[currindex] !=  @line[currindex-currlvl]
  35.                     break
  36.                 elsif @line[currindex] == @line[currindex-currlvl]
  37.                     temp1 = @line[currindex]
  38.                     @line[currindex] = temp1*2
  39.                     @line[currindex-currlvl] = 0
  40.                     @score += temp1*2
  41.                     break
  42.                 end
  43.                 currlvl += 1
  44.             end
  45.             currindex -= 1
  46.         end
  47.     end
  48.     def sort
  49.         3.times do
  50.             number = 0
  51.             3.times do
  52.                 if @line[number+1]==0
  53.                     @line[number+1]=@line[number]
  54.                     @line[number]=0
  55.                 end
  56.                 number += 1
  57.             end
  58.         end
  59.     end
  60.  
  61.     def game
  62.         while(true)
  63.             @old_table = @table.map{|i|i.clone}
  64.             system('cls')
  65.             ui_index = 0
  66.             puts "* * * * * * * * * * * * * * * * *"
  67.             4.times do
  68.                 puts "* *   *   *   *\n*  #{@table[ui_index][0] if @table[ui_index][0] != 0}    *   #{@table[ui_index][1] if @table[ui_index][1] != 0}  *   #{@table[ui_index][2] if @table[ui_index][2] != 0}  *   #{@table[ui_index][3] if @table[ui_index][3] != 0}  *\n*    *   *   *   *\n* * * * * * * * * * * * * * * * *"
  69.                 ui_index += 1
  70.             end
  71.             puts "@score: #{@score}"
  72.             STDIN.raw!
  73.             key = STDIN.getc
  74.             case key
  75.                 when 'c'
  76.                     exit
  77.                 when 'd'
  78.                     currline = 0
  79.                     4.times do
  80.                         @line = @table[currline].clone
  81.                         compare
  82.                         sort
  83.                         @table[currline] = @line
  84.                         currline += 1
  85.                     end
  86.                 when 'a'
  87.                     currline = 0
  88.                     4.times do
  89.                         @line = @table[currline].clone.reverse
  90.                         compare
  91.                         sort
  92.                         @table[currline] = @line.reverse
  93.                         currline += 1
  94.                     end
  95.                 when 'w'
  96.                     currline = 0
  97.                     4.times do
  98.                         @line = [@table[0][currline],@table[1][currline],@table[2][currline],@table[3][currline]].reverse
  99.                         compare
  100.                         sort
  101.                         @line.reverse!
  102.                         @table[0][currline] = @line[0]
  103.                         @table[1][currline] = @line[1]
  104.                         @table[2][currline] = @line[2]
  105.                         @table[3][currline] = @line[3]
  106.                         currline += 1
  107.                     end
  108.                 when 's'
  109.                     currline = 0
  110.                     4.times do
  111.                         @line = [@table[0][currline],@table[1][currline],@table[2][currline],@table[3][currline]]
  112.                         compare
  113.                         sort
  114.                         @table[0][currline] = @line[0]
  115.                         @table[1][currline] = @line[1]
  116.                         @table[2][currline] = @line[2]
  117.                         @table[3][currline] = @line[3]
  118.                         currline += 1
  119.                     end
  120.             end
  121.             create_random if @old_table != @table
  122.         end
  123.     end
  124. end
  125. Base_game.new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement