-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.fivesixteensvgt.cs
126 lines (116 loc) · 5.94 KB
/
ai.fivesixteensvgt.cs
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
// I have written the same program in C# 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:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebPage
{
class Program
{
// A function to generate a random word of length n
static string RandomWord(int n)
{
string word = ""; // Initialize the word
string alphabet = "abcdefghijklmnopqrstuvwxyz"; // Define the alphabet
Random random = new Random(); // Create a random object
for (int i = 0; i < n; i++)
{
int r = random.Next(26); // Generate a random index
word += alphabet[r]; // Append a random letter
}
return word; // Return the word
}
// A function to generate a random list of words of size m
static string[] RandomList(int m)
{
string[] list = new string[m]; // Initialize the list
Random random = new Random(); // Create a random object
for (int i = 0; i < m; i++)
{
int n = random.Next(1, 11); // 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
static void CreateButton(string label)
{
Console.WriteLine("<button onclick=\"select(this)\">{0}</button>", label); // Print the HTML code for the button
}
// A function to create a text element with a content
static void CreateText(string content)
{
Console.WriteLine("<text x=\"50%\" y=\"50%\" text-anchor=\"middle\" alignment-baseline=\"middle\" font-size=\"32\">{0}</text>", content); // Print the SVG code for the text
}
// A function to join three words with spaces
static string JoinWords(string word1, string word2, string word3)
{
return word1 + " " + word2 + " " + word3; // Return the joined words
}
// A function to shuffle an array of strings
static void Shuffle(string[] array)
{
Random random = new Random(); // Create a random object
for (int i = 0; i < array.Length; i++)
{
int j = random.Next(array.Length); // Generate a random index
string temp = array[i]; // Swap the elements at i and j
array[i] = array[j];
array[j] = temp;
}
}
// A global variable to store the user's choices
static string[] choices = new string[5];
static int choiceCount = 0;
// A function to handle the user's selection of a button
static void Select(string button)
{
if (choiceCount < 5) // If the user has not selected 5 buttons yet
{
choices[choiceCount] = button; // Add the button's label to the choices array
choiceCount++; // Increment the choice count
button.disabled = true; // Disable the button
if (choiceCount == 5) // If the user has selected 5 buttons
{
DisplayChoices(); // Display the choices as text
}
}
}
// A function to display the user's choices as text
static void DisplayChoices()
{
Console.WriteLine("<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 100 100\">"); // Print the SVG tag
for (int i = 0; i < 5; i++) // For each choice
{
CreateText(choices[i]); // Create a text element with the choice
Console.WriteLine("<animateTransform attributeName=\"transform\" type=\"translate\" from=\"0 0\" to=\"0 {0}\" dur=\"1s\" fill=\"freeze\"/>", (i - 2) * 20); // Animate the text to move vertically
}
Console.WriteLine("</svg>"); // Print the closing SVG tag
}
static void Main(string[] args)
{
string[] list = RandomList(16); // Generate a random list of 16 words
Console.WriteLine("<!DOCTYPE html>"); // Print the HTML declaration
Console.WriteLine("<html>"); // Print the HTML tag
Console.WriteLine("<head>"); // Print the head tag
Console.WriteLine("<script>"); // Print the script tag
Console.WriteLine("{0}", Select); // Print the C# code for the select function as JavaScript
Console.WriteLine("</script>"); // Print the closing script tag
Console.WriteLine("</head>"); // Print the closing head tag
Console.WriteLine("<body>"); // Print the body tag
Shuffle(list); // Shuffle the list of words
for (int i = 0; i < 4; i++) // For each row of buttons
{
for (int j = 0; j < 4; j++) // For each column of buttons
{
int k = i * 4 + j; // Get the index of the word
string 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
}
Console.WriteLine("<br>"); // Print a line break
}
Console.WriteLine("</body>"); // Print the closing body tag
Console.WriteLine("</html>"); // Print the closing HTML tag
}
}
}