I have mapped user input with 2d ascii letters and now I am trying to print out ascii letters below horizontally, so they could be printed side by side not top down as they are right now. Letter height is 8 lines and width varies. I run the code go run . "hello" Could really appriciate some help. I have tried to loop over the Table, but no success and haveb't been able to figure out yet how to print letters side by side.
_
| |
| |__
| _ \
| | | |
|_| |_|
___
/ _ \
| __/
\___|
_
| |
| |
| |
| |
|_|
_
| |
| |
| |
| |
|_|
___
/ _ \
| (_) |
\___/
I use struct for mapping and storying data on that format below:
{h 104 [[ ] [_] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{h 104 [[|] [ ] [|] [ ] [ ] [ ] [ ] [ ]] []}
{h 104 [[|] [ ] [|] [_] [_] [ ] [ ] [ ]] []}
{h 104 [[|] [ ] [ ] [_] [ ] [\] [ ] [ ]] []}
{h 104 [[|] [ ] [|] [ ] [|] [ ] [|] [ ]] []}
{h 104 [[|] [_] [|] [ ] [|] [_] [|] [ ]] []}
{h 104 [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{h 104 [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{e 101 [[ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{e 101 [[ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{e 101 [[ ] [ ] [_] [_] [_] [ ] [ ]] []}
{e 101 [[ ] [/] [ ] [_] [ ] [\] [ ]] []}
{e 101 [[|] [ ] [ ] [_] [_] [/] [ ]] []}
{e 101 [[ ] [\] [_] [_] [_] [|] [ ]] []}
{e 101 [[ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{e 101 [[ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{l 108 [[ ] [_] [ ] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [_] [|] [ ]] []}
{l 108 [[ ] [ ] [ ] [ ]] []}
{l 108 [[ ] [ ] [ ] [ ]] []}
{l 108 [[ ] [_] [ ] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [ ] [|] [ ]] []}
{l 108 [[|] [_] [|] [ ]] []}
{l 108 [[ ] [ ] [ ] [ ]] []}
{l 108 [[ ] [ ] [ ] [ ]] []}
{o 111 [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{o 111 [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{o 111 [[ ] [ ] [_] [_] [_] [ ] [ ] [ ]] []}
{o 111 [[ ] [/] [ ] [_] [ ] [\] [ ] [ ]] []}
{o 111 [[|] [ ] [(] [_] [)] [ ] [|] [ ]] []}
{o 111 [[ ] [\] [_] [_] [_] [/] [ ] [ ]] []}
{o 111 [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
{o 111 [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]] []}
My code ascii.go:
import (
"fmt"
"os"
"strings"
)
type letter struct {
Letter string
Ascii int
Table [][]string
Color []string
}
func mapFont(font string) ([]letter, error) {
readFile, err := os.ReadFile(font)
if err != nil {
return nil, fmt.Errorf("could not read the content in the file: %v", err)
}
slice := strings.Split(string(readFile), "\n")
letters := make([]letter, 0)
index := 31
for _, row := range slice {
if row == "" {
index++
continue
}
grid := make([][]string, 0)
for _, char := range row {
grid = append(grid, []string{string(char)})
}
letters = append(letters, letter{
Letter: string(index),
Ascii: index,
Table: grid,
})
}
return letters, nil
}
func mapInput(input string, letters []letter) ([]letter, error) {
lines := strings.Split(input, "\\n")
output := make([]letter, 0)
for _, line := range lines {
characters := []rune(line)
if line != "" {
for _, ch := range characters {
if ch >= 32 && ch <= 126 {
for _, v := range letters {
if rune(v.Ascii) == ch {
output = append(output, v)
}
}
} else {
return nil, fmt.Errorf("input includes non ascii character(s), please use ascii character(s)")
}
}
} else {
output = append(output, letter{})
}
}
return output, nil
}
func printLettersSideBySide(mapping []letter) {
output := ""
for i := 0; i < len(mapping); i++ {
for j := 0; j < len(mapping[i].Table); j++ {
output = output + strings.Join(mapping[i].Table[j], "")
}
output = output + "\n"
}
fmt.Println(output)
}
main.go
package main
import (
"fmt"
)
func main() {
args := os.Args
banners := "banner/standard.txt"
str := args[1]
letter, _ := mapFont(banners)
mapping, _ := mapInput(str, letter)
printLettersSideBySide(mapping)
for _, item := range mapping {
fmt.Println(item)
}
}
Leaving answer to others who may need it:
Was able to solve it by making data structure for Table in type letter simpler. Replaced [][]string with []string in and made changes in mapFont function where I used that struct. Then I added new function for printing letters horizontally.
ascii.go
import (
"fmt"
"os"
"strings"
)
type letter struct {
Letter string
Ascii int
Table []string
}
func mapFont(font string) ([]letter, error) {
readFile, err := os.ReadFile(font)
if err != nil {
return nil, fmt.Errorf("could not read the content in the file: %v", err)
}
slice := strings.Split(string(readFile), "\n")
letters := make([]letter, 0)
index := 31
for _, row := range slice {
if row == "" {
index++
continue
}
grid := make([]string, 0)
for _, char := range row {
grid = append(grid, string(char))
}
letters = append(letters, letter{
Letter: string(index),
Ascii: index,
Table: grid,
})
}
return letters, nil
}
func mapInput(input string, letters []letter) ([]letter, error) {
lines := strings.Split(input, "\\n")
output := make([]letter, 0)
for _, line := range lines {
characters := []rune(line)
if line != "" {
for _, ch := range characters {
if ch >= 32 && ch <= 126 {
for _, v := range letters {
if rune(v.Ascii) == ch {
output = append(output, v)
}
}
} else {
return nil, fmt.Errorf("input includes non ascii character(s), please use ascii character(s)")
}
}
} else {
output = append(output, letter{})
}
}
return output, nil
}
func printInput(str string, letter []letter) []string {
var pixelRows []string
for row := 0; row < 8; row++ {
pixelRow := ""
for letterIndex := 0; letterIndex < len(str); letterIndex++ {
character, _ := mapInput(string(str[letterIndex]), letter)
for column := 0; column < len(character[row].Table); column++
pixelRow = pixelRow + character[row].Table[column]
}
}
pixelRows = append(pixelRows, pixelRow)
}
return pixelRows
}
main.go
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args
banners := "banner/standard.txt"
str := args[1]
letter, _ := mapFont(banners)
sentence, _ := mapInput(str, letter)
output := printInput(str, sentence)
for _, v := range output {
fmt.Println(v)
}
}
Output:
❯ go run . "hello"
_ _ _
| | | | | |
| |__ ___ | | | | ___
| _ \ / _ \ | | | | / _ \
| | | | | __/ | | | | | (_) |
|_| |_| \___| |_| |_| \___/
$ sudo service --status-all | grep -q oijawpawuouzmaomwaoiwadlknasdlknascsc &>/dev/null
[ ? ] hwclock.sh
[ ? ] kmod
[ ? ] plymouth
[ ? ] plymouth-log
$ sudo service --status-all
[ + ] apache-htcacheclean
[ + ] apache2
[ - ] apparmor
[ + ] awslogs
[ - ] console-setup.sh
[ - ] cron
[ - ] dbus
[ ? ] hwclock.sh
[ + ] irqbalance
[ - ] keyboard-setup.sh
[ ? ] kmod
[ + ] php7.2-fpm
[ ? ] plymouth
[ ? ] plymouth-log
[ - ] procps
[ - ] rsync
[ - ] rsyslog
[ + ] udev
[ - ] ufw
[ - ] unattended-upgrades
[ - ] uuidd
[ - ] x11-common
Wondering why the output from grep with the quiet flag is appearing, sending outputs to dev null is also failing.
I'm also confused as to why the gibberish is matching against anything.
The following code is displaying a Progress Bar on an HTML page. It runs fine but takes up far to many lines of code.
QUESTION:
- How would I go about transforming this into a for or while loop, replicating its functionality?
if [ $per_usage -ge 1 ] && [ $per_usage -le 10 ]
then
indg="|"
indb=""
indr=""
elif [ $per_usage -gt 10 ] && [ $per_usage -le 20 ]
then
indg="||"
indb=""
indr=""
elif [ $per_usage -gt 20 ] && [ $per_usage -le 30 ]
then
indg="|||"
indb=""
indr=""
elif [ $per_usage -gt 30 ] && [ $per_usage -le 40 ]
then
indg="||||"
indb=""
indr=""
elif [ $per_usage -gt 40 ] && [ $per_usage -le 50 ]
then
indg="|||||"
indb=""
indr=""
elif [ $per_usage -gt 50 ] && [ $per_usage -le 60 ]
then
indg="|||||"
indb="|"
indr=""
elif [ $per_usage -gt 60 ] && [ $per_usage -le 70 ]
then
indg="|||||"
indb="||"
indr=""
elif [ $per_usage -gt 70 ] && [ $per_usage -le 80 ]
then
indg="|||||"
indb="|||"
indr=""
elif [ $per_usage -gt 80 ] && [ $per_usage -le 90 ]
then
indg="|||||"
indb="||"
indr="||"
elif [ $per_usage -gt 90 ]
then
indg=""
indb=""
indr="||||||||||"
else
indg=""
indb=""
indr=""
fi
For example my output is like if per_usage value is 41
41 % |||||
Thank you in advance.
This sort of thing can be looped pretty easily:
#!/bin/bash
get_string () {
per_usage="$1"
if [ "$per_usage" -le 100 ] && [ "$per_usage" -ge 0 ]; then
echo -en "${per_usage}%\t"
bars=$(($per_usage / 10 + 1))
printf "%0.s|" $(seq 1 $bars)
echo
fi
}
i=0
while [ "$i" -le 100 ]; do
string=$(get_string "$i")
echo "$string"
let i++
done
In this example, the get_string function can be used to generate a string based on an input number. For instance, get_string 41 will print 41% |||||. In the little while loop below, the string for 0 - 100 is stored in the $string variable and printed.
bars stores the number of bars to print. Just one bar per 10%. Then printf and seq are used to print a | bars number of times.
Hopefully with this, you'll be on the right track to cleaning up your code.
I have been working with Debian for a couple of months for school and because it seems really handy to work with. but now I want to make a game for in the terminal. A Higher or Lower card game (also for school but I wanted to make it before I was given this task). You have to pick a Random card and then it asked if you want to bet High or Low. When you choose you bet, it wil created another random card and it wil then check if the new card is higer or lower. I still got a few problems that I can't find myself and hopefully you guys can lead me to the right direction.
The error is my if loops that it cannot find a command, but even after redoing the if loops, it is still the same error. I don't really see what my fault is. In the terminal, it says that my errors ar at line 58,49,97 an 107 and I don't really see my fault or what is wrong.
(if you don't understand a sentence, it is written in dutch)
"Here is the compleet code for the card game:
#!/bin/bash
score=0
uitkomst=juist
waardegok=0
echo "Hallo, welkom bij het spel {Hoger of Lager}"
echo "Het doel van het spel is om juist te kunnen gokken..."
echo "...Met kaarten."
echo "Je krijgt een kaart en jij moet gokken of de volgende kaart"
echo "hoger of lager in waarde is."
echo "Als je juist hebt geraden wordt uw juiste gok opgeslagen."
echo "veel succes!!"
echo "Ik ben niet verantwoordelijk voor eventuele verslavingen."
until [ $uitkomst != juist ]
do
Number=$(($RANDOM % 52))
KAARTEN=(Harten1 Harten2 Harten3 Harten4 Harten5 Harten6 Harten7 Harten8
Harten9 Harten10 Harten11 Harten12 Harten13 Klaver1 Klaver2 Klaver3
Klaver4 Klaver5 Klaver6 Klaver7 Klaver8 Klaver9 Klaver10 Klaver11 Klaver12
Klaver13 Schop1 Schop2 Schop3 Schop4 Schop5 Schop6 Schop7 Schop8 Schop9
Schop10 Schop11 Schop12 Schop13 Ruit1 Ruit2 Ruit3 Ruit4 Ruit5 Ruit6 Ruit7
Ruit8 Ruit9 Ruit10 Ruit11 Ruit12 Ruit13 )
Gokkaart=${KAARTEN[$Number]}
echo " "
echo $Gokkaart
waardekaart=$((Number+1))
echo -n "hoger of lager? type [H of L] > "
read gok
echo "gok: $gok"
if [ "$gok" = "H" ];
then
echo "Uw gok is hoger"
waardegok=54
elif [ "$gok" = "L" ];
then
echo "Uw gok is lager"
waardegok=-1
fi
Number2=$(($RANDOM % 52))
KAARTEN2=(Harten1 Harten2 Harten3 Harten4 Harten5 Harten6 Harten7 Harten8
Harten9 Harten10 Harten11 Harten12 Harten13 Klaver1 Klaver2 Klaver3
Klaver4 Klaver5 Klaver6 Klaver7 Klaver8 Klaver9 Klaver10 Klaver11 Klaver12
Klaver13 Schop1 Schop2 Schop3 Schop4 Schop5 Schop6 Schop7 Schop8 Schop9
Schop10 Schop11 Schop12 Schop13 Ruit1 Ruit2 Ruit3 Ruit4 Ruit5 Ruit6 Ruit7
Ruit8 Ruit9 Ruit10 Ruit11 Ruit12 Ruit13 )
Gokkaart2=${KAARTEN2[$Number2]}
waardekaart2=$((Number2+1))
if [$Number2 -ge 0 ] && [ $Number2 -le 12 ];
then
echo "De volgende kaart is een harten, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
elif [ $Number2 -ge 13 ] && [ $Number2 -le 25 ];
then
echo "De volgende kaart is een klaver, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
elif [ $Number2 -ge 26 ] && [ $Number2 -le 38 ];
then
echo "De volgende kaart is een Schoppen, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
elif [ $Number2 -ge 39 ] && [ $Number2 -le 52 ];
then
echo "De volgende kaart is een Ruiten, namelijk $Gokkaart2"
if [ $waardegok -gt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
elif [ $waardegok -lt $waardekaart2 ];
then
echo "goed gegokt"
score=$((score+1))
else
echo "fout, goed geprobeerd"
uitkomst=false
fi
fi
done
echo " uw score is $score"
Here is the example:
$ credits="29" ; echo "C=$credits" ; initial="1064" ; echo "C=$credits" ; limit=`expr $initial / 10` ; echo "C=$credits" ; if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$credits" ; then echo "C=$credtis" ; fi ; echo "C=$credits" ; echo "I=$initial"
C=29
C=29
C=29
C=29
C=
C=29
I=1064
$ c="29" ; echo "C=$c" ; initial="1064" ; echo "C=$c" ; limit=`expr $initial / 10` ; echo "C=$c" ; if [ "$c" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$c" ; then echo "C=$c" ; fi ; echo "C=$c" ; echo "I=$initial"
C=29
C=29
C=29
C=29
C=29
C=29
I=1064
When var name is "c", everything is ok, but when it's "credits", I can't use var content in the if condition.
Don't understand why...
Your problem is an incorrectly formatted if expression in both lines of code. Specifically, the first line includes an incorrect echo "C=$credits" ; between the end of the if and the then:
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$credits" ; then
should be:
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; then
echo "C=$credtis"
fi
The same error is repeated here:
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; echo "C=$credits" ; then
should be
if [ "$credits" -le "$limit" ] && [ ! "$initial" -eq "50" ] ; then
echo "C=$credtis"
fi