/
main.go
100 lines (96 loc) · 1.63 KB
/
main.go
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
// Copyright (c) 2015 David R. Jenni. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"math"
"math/rand"
"time"
)
func main() {
const (
height = 20
width = 14
)
w, err := newWin(width, height)
if err != nil {
log.Fatal(err)
}
rand.Seed(time.Now().UnixNano())
var score, level, lines int
f := newField(width-1, height-1)
d := nop
for !f.full() {
b := randBlock()
b.x += width/2 - 2
b.y++
for b.active {
timeout := time.Duration(500 * math.Pow(0.75, float64(level-1)))
for {
f.reset()
switch d {
case up:
tmp := b
tmp.rotate()
if f.possible(tmp, tmp.x, tmp.y) {
b.rotate()
}
case down:
timeout = time.Duration(40)
case left:
if f.possible(b, b.x-1, b.y) {
b.x--
}
case right:
if f.possible(b, b.x+1, b.y) {
b.x++
}
case space:
for f.possible(b, b.x, b.y+1) {
b.y++
}
}
f.put(b)
w.draw(f)
select {
case d = <-w.input:
if d == quit {
goto Exit
}
case <-time.After(timeout * time.Millisecond):
goto Break
}
}
Break:
d = nop
if f.possible(b, b.x, b.y+1) {
b.y++
} else {
b.active = false
f.put(b)
}
}
n, baseLine := f.delFullLines()
lines += n
level = 1 + lines/10
switch n {
case 1:
score += 4 * level
case 2:
score += 10 * level
case 3:
score += 30 * level
case 4:
score += 120 * level
}
if baseLine {
score += 1000 * level
}
w.draw(f)
}
Exit:
w.close()
fmt.Printf("Score: %d\n", score)
}