-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rb
138 lines (117 loc) · 2.4 KB
/
mod.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require 'open-uri'
def make_piece(piece_code)
case piece_code
when 'i'
return [[1],[1],[1],[1]]
when 'j'
return [[0,1],[0,1],[1,1]]
when 'l'
return [[1,0],[1,0],[1,1]]
when 'o'
return [[1,1],[1,1]]
when 's'
return [[0,1,1],[1,1,0]]
when 't'
return [[1,1,1],[0,1,0]]
when 'z'
return [[1,1,0],[0,1,1]]
end
end
def rotate_degree(piece,degree)
while degree > 0
piece = rotate piece
degree -= 90
end
return piece
end
def rotate(piece)
return piece.transpose.collect {|row| row.reverse}
end
def place(piece, piece_code, pos, board)
endgame unless check piece, pos, 0, board
top = 0
20.times do |top|
break if !check piece, pos, top, board
end
board = insert piece, piece_code, pos, top, board
return board
end
def insert(piece, piece_code, pos, top, board)
piece.each_with_index do |y,i|
y.each_with_index do |x,j|
board[i + top][j + pos] = piece_code if x == 1
end
end
return board
end
def check(piece, pos, top, board)
height = piece.length
return false if top + height >= 20
good = true
piece.each_with_index do |y,i|
y.each_with_index do |x,j|
good = false if x == 1 and board[i + top + 1][j + pos] != '.'
end
end
return good
end
def init_board
board = []
20.times do |i|
board[i] = [];
10.times do |j|
board[i][j] = '.'
end
end
return board
end
def print_board(board)
board.each do |row|
row.each do |cell|
print cell
end
print "\n"
end
end
def collapse_board(board)
board.reverse.each_with_index do |row,i|
row = row.to_s
if row =~ /^[^.]+$/
$score += 10
board.delete_at(19-i)
board.insert 0, %w(. . . . . . . . . .)
board = collapse_board board
break
end
end
return board
end
def endgame
#puts "Final Score: #{$score}"
#puts "Total Moves: #{$moves}"
print $score
exit
end
def go
bot_url = ARGV[0]
board = init_board
pieces = %w(i j l o s t z)
$score = 0
$moves = 0
while true
piece_code = pieces[rand pieces.length]
sep = bot_url =~ /\?/ ? '&' : '?'
url = "#{bot_url}#{sep}piece=#{piece_code}&board=#{board.collect{|row| row.to_s}.join '%20'}"
response = open(url).read
response = response.split('&').collect{|pair| pair.split('=')[1].to_i}
position = response[0]
rotation = response[1]
piece = make_piece piece_code
piece = rotate_degree piece, rotation
$moves += 1
board = place piece, piece_code, position, board
#print_board board
board = collapse_board board
end
end
go