Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'io/console'
- class Base_game
- def initialize
- @table = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
- @score = 0
- 2.times{create_random}
- game
- end
- def create_random
- iszero = true
- if @table.flatten.count(0) == 0
- return 0
- end
- while(iszero)
- int1 = rand(0..3)
- int2 = rand(0..3)
- float1 = rand(0.0..100.0).round(2)
- if @table[int1][int2] == 0
- if float1 <= 10.0
- @table[int1][int2] = 4
- else
- @table[int1][int2] = 2
- end
- iszero = false
- end
- end
- end
- def compare
- currindex = 3
- 3.times do
- currlvl = 1
- currindex.times do
- if @line[currindex-currlvl] != 0 && @line[currindex] != @line[currindex-currlvl]
- break
- elsif @line[currindex] == @line[currindex-currlvl]
- temp1 = @line[currindex]
- @line[currindex] = temp1*2
- @line[currindex-currlvl] = 0
- break
- end
- currlvl += 1
- end
- currindex -= 1
- end
- end
- def sort
- 3.times do
- number = 0
- 3.times do
- if @line[number+1]==0
- @line[number+1]=@line[number]
- @line[number]=0
- end
- number += 1
- end
- end
- end
- def game
- while(true)
- @old_table = @table.clone
- system('cls')
- ui_index = 0
- puts "* * * * * * * * * * * * * * * * *"
- 4.times do
- 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* * * * * * * * * * * * * * * * *"
- ui_index += 1
- end
- puts "@score: #{@score}"
- STDIN.raw!
- key = STDIN.getc
- case key
- when 'c'
- exit
- when 'd'
- currline = 0
- 4.times do
- @line = @table[currline].clone
- compare
- sort
- @table[currline] = @line
- currline += 1
- end
- when 'a'
- currline = 0
- 4.times do
- @line = @table[currline].clone.reverse
- compare
- sort
- @table[currline] = @line.reverse
- currline += 1
- end
- when 'w'
- currline = 0
- 4.times do
- @line = [@table[0][currline],@table[1][currline],@table[2][currline],@table[3][currline]].reverse
- compare
- sort
- @line.reverse!
- @table[0][currline] = @line[0]
- @table[1][currline] = @line[1]
- @table[2][currline] = @line[2]
- @table[3][currline] = @line[3]
- currline += 1
- end
- when 's'
- currline = 0
- 4.times do
- @line = [@table[0][currline],@table[1][currline],@table[2][currline],@table[3][currline]]
- compare
- sort
- @table[0][currline] = @line[0]
- @table[1][currline] = @line[1]
- @table[2][currline] = @line[2]
- @table[3][currline] = @line[3]
- currline += 1
- end
- end
- create_random #if @old_table != @table
- end
- end
- end
- Base_game.new
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement