-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
85 lines (74 loc) · 1.6 KB
/
line.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
package main
import (
"fmt"
)
var brick string
var lineSep string
var linePad string
// Basic line that board consists of
type line struct {
items []int
}
// Generates new line and
// returns a pointer to it
func NewLine(size int) *line {
return &line{make([]int, size)}
}
/*---------------------------------------------------*/
/*-----------| HERE GOES THE ENGINE PART |-----------*/
/*---------------------------------------------------*/
// Moves elements to the Left
func (l *line) move() {
cursor := 0
nl := NewLine(len(l.items))
for _, v := range l.items {
if v != 0 {
if nl.items[cursor] == 0 {
nl.items[cursor] = v
} else if v == nl.items[cursor] {
nl.items[cursor] *= 2
cursor += 1
} else {
cursor += 1
nl.items[cursor] = v
}
}
}
*l = *nl
}
//Reverses line items without changing the original line
func (l *line) reversed() line {
c := len(l.items)
nl := NewLine(c)
for i := 0; i < c/2; i++ {
nl.items[i], nl.items[c-i-1] = l.items[c-i-1], l.items[i]
}
return *nl
}
// Moves elements to the Right
func (l *line) reverseMove() {
nl := l.reversed()
nl.move()
*l = nl.reversed()
}
/*---------------------------------------------------*/
/*-----------| HERE ENDS THE ENGINE PART |-----------*/
/*---------------------------------------------------*/
// Returns line score
func (l *line) score() (s int) {
for _, v := range l.items {
s += v
}
return
}
// Draws line in CLI
func (l *line) draw() {
fmt.Println(linePad)
fmt.Print(brick)
for _, v := range l.items {
fmt.Print(center(v, 6), brick)
}
fmt.Println()
fmt.Println(linePad)
fmt.Println(lineSep)
}