Advertisement
Nemx

2048

Jan 3rd, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.88 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.                     break
  41.                 end
  42.                 currlvl += 1
  43.             end
  44.             currindex -= 1
  45.         end
  46.     end
  47.     def sort
  48.         3.times do
  49.             number = 0
  50.             3.times do
  51.                 if @line[number+1]==0
  52.                     @line[number+1]=@line[number]
  53.                     @line[number]=0
  54.                 end
  55.                 number += 1
  56.             end
  57.         end
  58.     end
  59.  
  60.     def game
  61.         while(true)
  62.             @old_table = @table.clone
  63.             system('cls')
  64.             ui_index = 0
  65.             puts "* * * * * * * * * * * * * * * * *"
  66.             4.times do
  67.                 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* * * * * * * * * * * * * * * * *"
  68.                 ui_index += 1
  69.             end
  70.             puts "@score: #{@score}"
  71.             STDIN.raw!
  72.             key = STDIN.getc
  73.             case key
  74.                 when 'c'
  75.                     exit
  76.                 when 'd'
  77.                     currline = 0
  78.                     4.times do
  79.                         @line = @table[currline].clone
  80.                         compare
  81.                         sort
  82.                         @table[currline] = @line
  83.                         currline += 1
  84.                     end
  85.                 when 'a'
  86.                     currline = 0
  87.                     4.times do
  88.                         @line = @table[currline].clone.reverse
  89.                         compare
  90.                         sort
  91.                         @table[currline] = @line.reverse
  92.                         currline += 1
  93.                     end
  94.                 when 'w'
  95.                     currline = 0
  96.                     4.times do
  97.                         @line = [@table[0][currline],@table[1][currline],@table[2][currline],@table[3][currline]].reverse
  98.                         compare
  99.                         sort
  100.                         @line.reverse!
  101.                         @table[0][currline] = @line[0]
  102.                         @table[1][currline] = @line[1]
  103.                         @table[2][currline] = @line[2]
  104.                         @table[3][currline] = @line[3]
  105.                         currline += 1
  106.                     end
  107.                 when 's'
  108.                     currline = 0
  109.                     4.times do
  110.                         @line = [@table[0][currline],@table[1][currline],@table[2][currline],@table[3][currline]]
  111.                         compare
  112.                         sort
  113.                         @table[0][currline] = @line[0]
  114.                         @table[1][currline] = @line[1]
  115.                         @table[2][currline] = @line[2]
  116.                         @table[3][currline] = @line[3]
  117.                         currline += 1
  118.                     end
  119.             end
  120.             create_random #if @old_table != @table
  121.         end
  122.     end
  123. end
  124. Base_game.new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement