-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.fivesixteensvgt.go
111 lines (99 loc) · 5.14 KB
/
ai.fivesixteensvgt.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
101
102
103
104
105
106
107
108
109
110
111
// I have written the same program in Go that creates a web page that has a background scripted element that generates and stores as a vector a random list of words, store the words in an array, and output the selections as 16 separate buttons on the webpage, each button containing at least 3 of those many words to the user. The program will make the users choose up to 5 of a total of 16 (up to 5 out of 16 choices) and put their choices in a second table array, and using vectors display them as text, generated as svg drawn as close to how the letters appear on an interactive web page. Here is the code:
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
// A function to generate a random word of length n
func randomWord(n int) string {
word := "" // Initialize the word
alphabet := "abcdefghijklmnopqrstuvwxyz" // Define the alphabet
for i := 0; i < n; i++ {
r := rand.Intn(26) // Generate a random index
word += string(alphabet[r]) // Append a random letter
}
return word // Return the word
}
// A function to generate a random list of words of size m
func randomList(m int) []string {
list := make([]string, m) // Initialize the list
for i := 0; i < m; i++ {
n := rand.Intn(10) + 1 // Generate a random word length between 1 and 10
list[i] = randomWord(n) // Generate a random word and add it to the list
}
return list // Return the list
}
// A function to create a button with a label
func createButton(label string) {
fmt.Printf("<button onclick=\"select(this)\">%s</button>\n", label) // Print the HTML code for the button
}
// A function to create a text element with a content
func createText(content string) {
fmt.Printf("<text x=\"50%%\" y=\"50%%\" text-anchor=\"middle\" alignment-baseline=\"middle\" font-size=\"32\">%s</text>\n", content) // Print the SVG code for the text
}
// A function to join three words with spaces
func joinWords(word1, word2, word3 string) string {
return word1 + " " + word2 + " " + word3 // Return the joined words
}
// A global variable to store the user's choices
var choices []string
var choiceCount int
// A global variable to store the timer value
var timer int = 5
// A function to handle the user's selection of a button
func select(button string) {
if choiceCount < 5 { // If the user has not selected 5 buttons yet
choices = append(choices, button) // Add the button's label to the choices slice
choiceCount++ // Increment the choice count
button.disabled = true // Disable the button
if choiceCount == 5 { // If the user has selected 5 buttons
startTimer() // Start the timer and display it
}
}
}
// A function to start the timer and display it
func startTimer() {
fmt.Println("<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 100 100\">") // Print the SVG tag
createText(fmt.Sprint(timer)) // Create a text element with the timer value
fmt.Println("<animate id=\"timer\" attributeName=\"content\" values=\"5;4;3;2;1\" keyTimes=\"0;0.2;0.4;0.6;0.8;1\" dur=\"5s\" fill=\"freeze\"/>") // Animate the text to change the timer value
fmt.Println("<set attributeName=\"display\" to=\"none\" begin=\"timer.end+1s\"/>") // Hide the text after the timer ends and waits for 1 second
fmt.Println("</svg>") // Print the closing SVG tag
fmt.Println("<script>") // Print the script tag
fmt.Println("setTimeout(function() { displayChoices(); }, 6000);") // Set a timeout to display the choices after 6 seconds
fmt.Println("</script>") // Print the closing script tag
}
// A function to display the user's choices as text
func displayChoices() {
fmt.Println("<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 100 100\">") // Print the SVG tag
for i := 0; i < 5; i++ { // For each choice
createText(choices[i]) // Create a text element with the choice
fmt.Printf("<animateTransform attributeName=\"transform\" type=\"translate\" from=\"0 0\" to=\"0 %d\" dur=\"1s\" fill=\"freeze\"/>\n", (i-2)*20) // Animate the text to move vertically
}
fmt.Println("</svg>") // Print the closing SVG tag
}
func main() {
rand.Seed(time.Now().UnixNano()) // Initialize the random seed
list := randomList(16) // Generate a random list of 16 words
fmt.Println("<!DOCTYPE html>") // Print the HTML declaration
fmt.Println("<html>") // Print the HTML tag
fmt.Println("<head>") // Print the head tag
fmt.Println("<script>") // Print the script tag
fmt.Println(select) // Print the Go code for the select function as JavaScript
fmt.Println(displayChoices) // Print the Go code for the displayChoices function as JavaScript
fmt.Println("</script>") // Print the closing script tag
fmt.Println("</head>") // Print the closing head tag
fmt.Println("<body>") // Print the body tag
rand.Shuffle(len(list), func(i, j int) { list[i], list[j] = list[j], list[i] }) // Shuffle the list of words
for i := 0; i < 4; i++ { // For each row of buttons
for j := 0; j < 4; j++ { // For each column of buttons
k := i*4 + j // Get the index of the word
label := joinWords(list[k], list[(k+1)%16], list[(k+2)%16]) // Join three words as the label
createButton(label) // Create a button with the label
}
fmt.Println("<br>") // Print a line break
}
fmt.Println("</body>") // Print the closing body tag
fmt.Println("</html>") // Print the closing HTML tag
}