Basics of Vala
Descripción del contenido de la página
Conversión de antiguos programas de BASIC a Vala para aprender los rudimentos de este lenguaje.
Etiquetas:
3D Plot
/*
3D Plot
Original version in BASIC:
Creative Computing (Morristown, New Jersey, USA), ca. 1980.
This version in Vala:
Copyright (c) 2023, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written in 2023-08-30/31.
Last modified 20231103T2132+0100.
*/
using GLib; // Math needed
// Clear the screen, reset the attributes and move the cursor to the home position.
void clear() {
print("\033[2J\033[0m\033[H");
}
// Display the credits and wait until the user presses Enter.
void print_credits() {
print("3D Plot\n\n");
print("Original version in BASIC:\n");
print(" Creative computing (Morristown, New Jersey, USA), ca. 1980.\n\n");
print("This version in Vala:\n");
print(" Copyright (c) 2023, Marcos Cruz (programandala.net)\n");
print(" SPDX-License-Identifier: Fair\n\n");
print("Press Enter to start the program.\n");
stdin.read_line();
}
double a(double z) {
return 30 * Math.exp(-z * z / 100);
}
void draw() {
const int width = 56;
const char space = ' ';
const char dot = '*';
char line[width];
int l = 0;
int z = 0;
int y1 = 0;
for (double x = -30.0; x <= 30.0; x += 1.5) {
for (int pos = 0; pos < width; pos++) {
line[pos] = space;
}
l = 0;
y1 = 5 * (int)(Math.sqrt(900 - x * x) / 5);
for (int y = y1; y >= -y1; y += -5) {
z = (int)(25 + a(Math.sqrt(x * x + (y * y))) - 0.7 * y);
if (z > l) {
l = z;
line[z] = dot;
}
} // y loop
for (int pos = 0; pos < width; pos++) {
print(line[pos].to_string());
}
print("\n");
} // x loop
}
void main() {
clear();
print_credits();
clear();
draw();
}
Bunny
/*
Bunny
Original version in BASIC:
Creative Computing (Morristown, New Jersey, USA), 1978.
This version in Vala:
Copyright (c) 2023, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written in 2023-08-31.
Last modified 20231103T2132+0100.
*/
// Erase the screen, reset the attributes and move the cursor to the home position.
void clear_screen() {
print("\x1B[2J\x1B[0m\x1B[H");
}
// Print the credits and wait for a keypress.
void print_credits() {
print("Bunny\n\n");
print("Original version in BASIC:\n");
print(" Creative Computing (Morristown, New Jersey, USA), 1978.\n\n");
print("This version in Vala:\n");
print(" Copyright (c) 2023, Marcos Cruz (programandala.net)\n");
print(" SPDX-License-Identifier: Fair\n\n");
print("Press Enter to start the program.\n");
stdin.read_line();
}
const int WIDTH = 53;
unichar line[WIDTH]; // buffer
int col; // `line` index
// Clear the line buffer with spaces.
void clear_line() {
for (int c = 0; c < WIDTH; c++) {
line[c] = ' ';
}
col = 0;
}
// Print the line buffer.
void print_line() {
for (int c = 0; c < WIDTH; c++) {
print(line[c].to_string());
}
print("\n");
}
const string LETTER = "BUNNY"; // letters pattern
const int EOL = -1; // end of line identifier
const int[] data = {
1, 2, EOL, 0, 2, 45, 50, EOL, 0, 5, 43, 52, EOL, 0, 7, 41, 52, EOL,
1, 9, 37, 50, EOL, 2, 11, 36, 50, EOL, 3, 13, 34, 49, EOL, 4, 14,
32, 48, EOL, 5, 15, 31, 47, EOL, 6, 16, 30, 45, EOL, 7, 17, 29, 44,
EOL, 8, 19, 28, 43, EOL, 9, 20, 27, 41, EOL, 10, 21, 26, 40, EOL,
11, 22, 25, 38, EOL, 12, 22, 24, 36, EOL, 13, 34, EOL, 14, 33, EOL,
15, 31, EOL, 17, 29, EOL, 18, 27, EOL, 19, 26, EOL, 16, 28, EOL,
13, 30, EOL, 11, 31, EOL, 10, 32, EOL, 8, 33, EOL, 7, 34, EOL, 6,
13, 16, 34, EOL, 5, 12, 16, 35, EOL, 4, 12, 16, 35, EOL, 3, 12, 15,
35, EOL, 2, 35, EOL, 1, 35, EOL, 2, 34, EOL, 3, 34, EOL, 4, 33,
EOL, 6, 33, EOL, 10, 32, 34, 34, EOL, 14, 17, 19, 25, 28, 31, 35,
35, EOL, 15, 19, 23, 30, 36, 36, EOL, 14, 18, 21, 21, 24, 30, 37, 37,
EOL, 13, 18, 23, 29, 33, 38, EOL, 12, 29, 31, 33, EOL, 11, 13, 17,
17, 19, 19, 22, 22, 24, 31, EOL, 10, 11, 17, 18, 22, 22, 24, 24, 29,
29, EOL, 22, 23, 26, 29, EOL, 27, 29, EOL, 28, 29, EOL };
const uint DATA_LEN = data.length;
// Draw the graphic out of `data` and `LETTER`.
void draw() {
uint letters = LETTER.length;
int to_col; // last col to print to
uint d = 0; // data pointer
clear_line();
while (d < DATA_LEN) {
col = data[d];
d += 1;
if (col == EOL) {
print_line();
clear_line();
} else {
to_col = data[d];
d += 1;
for (int c = col; c <= to_col; c++) {
line[c] = LETTER[c % letters];
}
}
}
}
void main() {
clear_screen();
print_credits();
clear_screen();
draw();
}
Diamond
/*
Diamond
Original version in BASIC:
Example included in Vintage BASIC 1.0.3.
http://www.vintage-basic.net
This version in Vala:
Copyright (c) 2023, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written in 2023-08-30/31.
Last modified 20231103T2130+0100.
*/
void main() {
const int lines = 17;
int i = 1;
int j;
while (i <= lines / 2 + 1) {
j = 1;
while (j <= (lines + 1) / 2 - i + 1) {
print(" ");
j += 1;
}
j = 1;
while (j <= i * 2 - 1) {
print("*");
j += 1;
}
print("\n");
i += 1;
}
i = 1;
while (i <= lines / 2) {
j = 1;
while (j <= i + 1) {
print(" ");
j += 1;
}
j = 1;
while (j <= ((lines + 1) / 2 - i) * 2 - 1) {
print("*");
j += 1;
}
print("\n");
i += 1;
}
}
Sine Wave
/*
Sine Wave
Original version in BASIC:
Creative Computing (Morristown, New Jersey, USA), ca. 1980.
This version in Vala:
Copyright (c) 2023, Marcos Cruz (programandala.net)
SPDX-License-Identifier: Fair
Written in 2023-08, 2023-09.
Last modified 20231103T2132+0100.
*/
using GLib; // Math needed
// Erase the screen, reset the attributes and move the cursor to the home position.
void clear() {
print("\033[2J\033[0m\033[H");
}
void print_credits() {
print("Sine Wave\n\n");
print("Original version in BASIC:\n");
print(" Creative computing (Morristown, New Jersey, USA), ca. 1980.\n\n");
print("This version in Vala:\n");
print(" Copyright (c) 2023, Marcos Cruz (programandala.net)\n");
print(" SPDX-License-Identifier: Fair\n\n");
print("Press Enter to start the program.\n");
stdin.read_line();
}
string word[2]; // user words
void get_words() {
string order[2] = {"first", "second"};
for (int i = 0; i < word.length; i++) {
print(@"Enter the $(order[i]) word: ");
word[i] = stdin.read_line();
}
}
string repeat_char(char c, int times) {
string s = "";
for (int i = 0; i < times; i++) {
s += c.to_string();
}
return s;
}
void draw() {
bool even = false;
double angle = 0.0;
for (angle = 0.0; angle <= 40.0; angle += 0.25) {
print(repeat_char(' ', (int) (26 + 25 * Math.sin(angle))));
stdout.printf("%s\n", word[(int) even]);
even = !even;
}
}
void main() {
clear();
print_credits();
clear();
get_words();
clear();
draw();
}
