-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.sh
executable file
Β·130 lines (122 loc) Β· 2.53 KB
/
matrix.sh
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
#!/bin/bash
tcols=$(tput cols)
trows=$(tput lines)
chars="01"
space=" "
declare -A matrix
function createMatrix {
num_rows="$trows"
num_cols="$tcols"
for (( i=0; i<=num_rows; i++)) do
for (( j=0; j<num_cols;j++ )) do
matrix[$i,$j]=0
done
done
}
function setMatrix {
matrix[$1,$2]=1
}
function unsetMatrix {
matrix[$1,$2]=0
}
declare -a lines
declare -a linesLength
declare -a linesTail
declare -a linesHead
x=0
y=0
function printBody {
tl=$1
hd=$2
col=$3
for (( i=tl; i<=hd ; i++ ));do
setMatrix $i $col
done
}
function startLine {
let n=$(( (RANDOM % 100) + 1 ))
if [[ $n -eq 1 ]];then
echo 0
else
echo 1
fi
}
function implementMatrix {
for (( lineNumber=0;lineNumber<tcols ;lineNumber++ ));do
if [[ ${linesHead[$lineNumber]} -gt ${linesLength[$lineNumber]} ]];then
let linesTail[$lineNumber]+=1
fi
if [[ ${linesTail[$lineNumber]} -eq $trows ]];then
reInitLine $lineNumber
fi
if [[ $(startLine) -eq 0 || ${linesHead[$lineNumber]} -gt 0 ]];then
let linesHead[$lineNumber]+=1
fi
printBody ${linesTail[$lineNumber]} ${linesHead[$lineNumber]} $lineNumber
done
}
function reInitLine {
MAXLINELENGTH=$(( tcols / 3))
lineNumber=$1
length=$(( $RANDOM % $MAXLINELENGTH ))
linesLength[$lineNumber]=$length
linesHead[$lineNumber]=0
linesTail[$lineNumber]=0
}
function initFirstLines {
MAXLINELENGTH=$(( tcols / 2 ))
for ((i=0;i<tcols;i++));do
length=$(( $RANDOM % $MAXLINELENGTH ))
linesLength[$i]=$length
linesHead[$i]=0
linesTail[$i]=0
done
}
function initBackgroundColor {
printf '\e[38;5;070m Foreground color: red\n'
printf '\e[48;5;0m Background color: black\n'
}
function main {
initFirstLines
initBackgroundColor
for(( ; ; ));do
createMatrix
implementMatrix
draw
done
}
declare -a grid
function loadFinalGrid {
gridToPrint="$1"
tput clear
printf "%s" "$gridToPrint"
}
function draw {
grid=
row=
for (( current_row=0;current_row<=trows;current_row++ ));do
row=$(getColumnPaint $current_row)
grid=$grid"$row"
done
loadFinalGrid "$grid"
}
function getColumnPaint {
row=$1
numberOfChars=${#chars}
line=""
char=""
GREEN='\033[0;32m'
#for loop handle column panting
for (( currColumn=0;currColumn<tcols;currColumn++ ));do
if [[ ${matrix[$row,$currColumn]} -eq 1 ]];then
charIndex=$(( RANDOM % $numberOfChars ))
charIndex=$charIndex
char=${chars:$charIndex:1}
else
char=" "
fi
line="${line}${char}"
done
echo -n "$line"
}
main