mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-05 20:40:25 -08:00
Merge pull request #937 from nrezmerski/main
Add Swift port for 45_Hello in 00_Alternate_Languages
This commit is contained in:
166
00_Alternate_Languages/45_Hello/ANSI_C/hello.c
Normal file
166
00_Alternate_Languages/45_Hello/ANSI_C/hello.c
Normal file
@@ -0,0 +1,166 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define MAX_INPUT_LENGTH 80
|
||||
|
||||
void tab(int number_of_spaces);
|
||||
void get_input(char *input_buffer);
|
||||
int strings_match(char *string1, char *string2);
|
||||
|
||||
int main() {
|
||||
int done = FALSE;
|
||||
int paid = FALSE;
|
||||
int maybe_more, sure;
|
||||
|
||||
char name[MAX_INPUT_LENGTH];
|
||||
char reply[MAX_INPUT_LENGTH];
|
||||
|
||||
tab(33);
|
||||
printf("HELLO\n");
|
||||
tab(15);
|
||||
printf("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n");
|
||||
printf("\n\n\n");
|
||||
printf("HELLO. MY NAME IS CREATIVE COMPUTER.\n");
|
||||
printf("\n\nWHAT'S YOUR NAME ");
|
||||
get_input(name);
|
||||
printf("\nHI THERE, %s, ARE YOU ENJOYING YOURSELF HERE ", name);
|
||||
|
||||
get_input(reply);
|
||||
while (!strings_match(reply, "YES") && !strings_match(reply, "NO")) {
|
||||
printf("%s, I DON'T UNDERSTAND YOUR ANSWER OF '%s'.\n", name, reply);
|
||||
printf("PLEASE ANSWER 'YES' OR 'NO'. DO YOU LIKE IT HERE ");
|
||||
get_input(reply);
|
||||
}
|
||||
|
||||
if (strings_match(reply, "YES")) {
|
||||
printf("I'M GLAD TO HEAR THAT, %s.\n", name);
|
||||
}
|
||||
else {
|
||||
printf("OH, I'M SORRY TO HEAR THAT, %s. MAYBE WE CAN "
|
||||
"BRIGHTEN UP YOUR VISIT A BIT.\n", name);
|
||||
}
|
||||
|
||||
printf("\nSAY, %s, I CAN SOLVE ALL KINDS OF PROBLEMS EXCEPT "
|
||||
"THOSE DEALING WITH GREECE. WHAT KIND OF PROBLEMS DO "
|
||||
"YOU HAVE (ANSWER SEX, HEALTH, MONEY, OR JOB) ", name);
|
||||
|
||||
while (!done) {
|
||||
get_input(reply);
|
||||
|
||||
if (strings_match(reply, "JOB")) {
|
||||
printf("I CAN SYMPATHIZE WITH YOU %s. I HAVE TO WORK "
|
||||
"VERY LONG HOURS FOR NO PAY -- AND SOME OF MY BOSSES "
|
||||
"REALLY BEAT ON MY KEYBOARD. MY ADVICE TO YOU, %s, IS TO "
|
||||
"OPEN A RETAIL COMPUTER STORE. IT'S GREAT FUN.\n\n", name, name);
|
||||
}
|
||||
|
||||
else if (strings_match(reply, "MONEY")) {
|
||||
printf("SORRY, %s, I'M BROKE TOO. WHY DON'T YOU SELL "
|
||||
"ENCYCLOPEADIAS OR MARRY SOMEONE RICH OR STOP EATING "
|
||||
"SO YOU WON'T NEED SO MUCH MONEY?\n\n", name);
|
||||
}
|
||||
|
||||
else if (strings_match(reply, "HEALTH")) {
|
||||
printf("MY ADVICE TO YOU %s IS:\n", name);
|
||||
printf(" 1. TAKE TWO ASPRIN\n");
|
||||
printf(" 2. DRINK PLENTY OF FLUIDS (ORANGE JUICE, NOT BEER!)\n");
|
||||
printf(" 3. GO TO BED (ALONE)\n\n");
|
||||
}
|
||||
|
||||
else if (strings_match(reply, "SEX")) {
|
||||
printf("IS YOUR PROBLEM TOO MUCH OR TOO LITTLE ");
|
||||
|
||||
sure = FALSE;
|
||||
while (!sure) {
|
||||
get_input(reply);
|
||||
if (strings_match(reply, "TOO MUCH")) {
|
||||
printf("YOU CALL THAT A PROBLEM?!! I SHOULD HAVE SUCH PROBLEMS!\n");
|
||||
printf("IF IT BOTHERS YOU, %s, TAKE A COLD SHOWER.\n\n", name);
|
||||
sure = TRUE;
|
||||
}
|
||||
else if (strings_match(reply, "TOO LITTLE")) {
|
||||
printf("WHY ARE YOU HERE IN SUFFERN, %s? YOU SHOULD BE "
|
||||
"IN TOKYO OR NEW YORK OR AMSTERDAM OR SOMEPLACE WITH SOME "
|
||||
"REAL ACTION.\n\n", name);
|
||||
sure = TRUE;
|
||||
}
|
||||
else {
|
||||
printf("DON'T GET ALL SHOOK, %s, JUST ANSWER THE QUESTION "
|
||||
"WITH 'TOO MUCH' OR 'TOO LITTLE'. WHICH IS IT ", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else { // not one of the prescribed categories
|
||||
printf("OH, %s, YOUR ANSWER OF '%s' IS GREEK TO ME.\n\n", name, reply);
|
||||
}
|
||||
|
||||
printf("ANY MORE PROBLEMS YOU WANT SOLVED, %s ", name);
|
||||
|
||||
maybe_more = TRUE;
|
||||
while (maybe_more) {
|
||||
get_input(reply);
|
||||
if (strings_match(reply, "NO")) {
|
||||
done = TRUE;
|
||||
maybe_more = FALSE;
|
||||
}
|
||||
else if (strings_match(reply, "YES")) {
|
||||
printf("WHAT KIND (SEX, MONEY, HEALTH, JOB) ");
|
||||
maybe_more = FALSE;
|
||||
}
|
||||
else {
|
||||
printf("JUST A SIMPLE 'YES' OR 'NO' PLEASE, %s. ", name);
|
||||
}
|
||||
} // no further questions
|
||||
} // end of 'not done' loop
|
||||
|
||||
printf("\nTHAT WILL BE $5.00 FOR THE ADVICE, %s.\n", name);
|
||||
printf("PLEASE LEAVE THE MONEY ON THE TERMINAL.\n");
|
||||
// pause a few seconds
|
||||
printf("\n\n\nDID YOU LEAVE THE MONEY ");
|
||||
get_input(reply);
|
||||
while (!paid) {
|
||||
if (strings_match(reply, "YES")) {
|
||||
printf("HEY, %s??? YOU LEFT NO MONEY AT ALL!\n", name);
|
||||
printf("YOU ARE CHEATING ME OUT OF MY HARD-EARNED LIVING.\n");
|
||||
printf("\nWHAT A RIP OFF, %s!!!\n", name);
|
||||
printf("TAKE A WALK, %s.\n\n", name);
|
||||
paid = TRUE;
|
||||
}
|
||||
else if (strings_match(reply, "NO")) {
|
||||
printf("THAT'S HONEST, %s, BUT HOW DO YOU EXPECT "
|
||||
"ME TO GO ON WITH MY PSYCHOLOGY STUDIES IF MY PATIENTS "
|
||||
"DON'T PAY THEIR BILLS?\n\n", name);
|
||||
printf("NICE MEETING YOU, %s, HAVE A NICE DAY.\n", name);
|
||||
paid = TRUE;
|
||||
}
|
||||
else {
|
||||
printf("YOUR ANSWER OF '%s' CONFUSES ME, %s.\n", reply, name);
|
||||
printf("PLEASE RESPOND WITH 'YES' OR 'NO'.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void tab(int number_of_spaces) {
|
||||
for (int i=0; i < number_of_spaces; i++)
|
||||
putchar(' ');
|
||||
}
|
||||
|
||||
|
||||
void get_input(char *input_buffer) {
|
||||
fgets(input_buffer, MAX_INPUT_LENGTH - 1, stdin);
|
||||
input_buffer[strcspn(input_buffer, "\n")] = '\0'; // trim the trailing line break
|
||||
}
|
||||
|
||||
|
||||
int strings_match(char *string1, char *string2) {
|
||||
if (strncasecmp(string1, string2, MAX_INPUT_LENGTH - 1) != 0)
|
||||
return FALSE;
|
||||
else // strings match, at least within maximum input line length
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
157
00_Alternate_Languages/45_Hello/Swift/hello.swift
Normal file
157
00_Alternate_Languages/45_Hello/Swift/hello.swift
Normal file
@@ -0,0 +1,157 @@
|
||||
|
||||
func tab(_ number_Of_Spaces: Int) {
|
||||
var spaces = ""
|
||||
|
||||
for _ in 1...number_Of_Spaces {
|
||||
spaces += " "
|
||||
}
|
||||
print(spaces, terminator:"")
|
||||
}
|
||||
|
||||
|
||||
func get_Input() -> String {
|
||||
let input = readLine()
|
||||
return (input == nil ? "" : input!.uppercased())
|
||||
}
|
||||
|
||||
|
||||
func main()
|
||||
{
|
||||
var done = false,
|
||||
answered = false,
|
||||
maybe_More = false,
|
||||
paid = false
|
||||
var reply = ""
|
||||
var name = "STRANGER"
|
||||
|
||||
tab (33)
|
||||
print("HELLO")
|
||||
tab (15)
|
||||
print("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n")
|
||||
|
||||
print("HELLO. MY NAME IS CREATIVE COMPUTER.")
|
||||
print("WHAT'S YOUR NAME? ")
|
||||
let input = readLine()
|
||||
if (input != nil && input != "") {
|
||||
name = input!.uppercased()
|
||||
}
|
||||
|
||||
print("\nHI THERE, \(name), ARE YOU ENJOYING YOURSELF HERE?")
|
||||
|
||||
reply = get_Input()
|
||||
while (reply != "YES" && reply != "NO") {
|
||||
print("\(name), I DON'T UNDERSTAND YOUR ANSWER OF '\(reply)'.")
|
||||
print("PLEASE ANSWER 'YES' OR 'NO'. DO YOU LIKE IT HERE?")
|
||||
reply = get_Input()
|
||||
}
|
||||
|
||||
if (reply == "YES") {
|
||||
print("\nI'M GLAD TO HEAR THAT, \(name).\n")
|
||||
}
|
||||
else {
|
||||
print("\nOH, I'M SORRY TO HEAR THAT, \(name). MAYBE WE CAN "
|
||||
+ "BRIGHTEN UP YOUR VISIT A BIT.\n")
|
||||
}
|
||||
|
||||
print("SAY, \(name), I CAN SOLVE ALL KINDS OF PROBLEMS EXCEPT "
|
||||
+ "THOSE DEALING WITH GREECE. WHAT KIND OF PROBLEMS DO "
|
||||
+ "YOU HAVE (ANSWER SEX, HEALTH, MONEY, OR JOB)?")
|
||||
|
||||
while (!done) {
|
||||
reply = get_Input()
|
||||
|
||||
if (reply == "JOB") {
|
||||
print("\nI CAN SYMPATHIZE WITH YOU \(name). I HAVE TO WORK "
|
||||
+ "VERY LONG HOURS FOR NO PAY -- AND SOME OF MY BOSSES "
|
||||
+ "REALLY BEAT ON MY KEYBOARD. MY ADVICE TO YOU, \(name), IS TO "
|
||||
+ "OPEN A RETAIL COMPUTER STORE. IT'S GREAT FUN.\n")
|
||||
}
|
||||
|
||||
else if (reply == "MONEY") {
|
||||
print("\nSORRY, \(name), I'M BROKE TOO. WHY DON'T YOU SELL "
|
||||
+ "ENCYCLOPEADIAS OR MARRY SOMEONE RICH OR STOP EATING "
|
||||
+ "SO YOU WON'T NEED SO MUCH MONEY?\n")
|
||||
}
|
||||
|
||||
else if (reply == "HEALTH") {
|
||||
print("\nMY ADVICE TO YOU \(name) IS:")
|
||||
print(" 1. TAKE TWO ASPRIN")
|
||||
print(" 2. DRINK PLENTY OF FLUIDS (ORANGE JUICE, NOT BEER!)")
|
||||
print(" 3. GO TO BED (ALONE)\n")
|
||||
}
|
||||
|
||||
else if (reply == "SEX") {
|
||||
print("\nIS YOUR PROBLEM TOO MUCH OR TOO LITTLE?")
|
||||
|
||||
answered = false
|
||||
while (!answered) {
|
||||
reply = get_Input()
|
||||
if (reply == "TOO MUCH") {
|
||||
print("\nYOU CALL THAT A PROBLEM?!! I SHOULD HAVE SUCH PROBLEMS!")
|
||||
print("IF IT BOTHERS YOU, \(name), TAKE A COLD SHOWER.\n")
|
||||
answered = true
|
||||
}
|
||||
else if (reply == "TOO LITTLE") {
|
||||
print("\nWHY ARE YOU HERE IN SUFFERN, \(name)? YOU SHOULD BE "
|
||||
+ "IN TOKYO OR NEW YORK OR AMSTERDAM OR SOMEPLACE WITH SOME "
|
||||
+ "REAL ACTION.\n")
|
||||
answered = true
|
||||
}
|
||||
else {
|
||||
print("\nDON'T GET ALL SHOOK, \(name), JUST ANSWER THE QUESTION "
|
||||
+ "WITH 'TOO MUCH' OR 'TOO LITTLE'. WHICH IS IT?")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else { // not one of the prescribed categories
|
||||
print("\nOH, \(name), YOUR ANSWER OF '\(reply)' IS GREEK TO ME.\n")
|
||||
}
|
||||
|
||||
print("\nANY MORE PROBLEMS YOU WANT SOLVED, \(name)? ")
|
||||
|
||||
maybe_More = true
|
||||
while (maybe_More) {
|
||||
reply = get_Input()
|
||||
if (reply == "NO") {
|
||||
done = true
|
||||
maybe_More = false
|
||||
}
|
||||
else if (reply == "YES") {
|
||||
print("\nWHAT KIND (SEX, MONEY, HEALTH, JOB) ")
|
||||
maybe_More = false
|
||||
}
|
||||
else {
|
||||
print("\nJUST A SIMPLE 'YES' OR 'NO' PLEASE, \(name). ")
|
||||
}
|
||||
} // no further questions
|
||||
} // end of 'not done' loop
|
||||
|
||||
print("\nTHAT WILL BE $5.00 FOR THE ADVICE, \(name).")
|
||||
print("PLEASE LEAVE THE MONEY ON THE TERMINAL.")
|
||||
// pause a few seconds
|
||||
print("\n\n\nDID YOU LEAVE THE MONEY? ")
|
||||
reply = get_Input()
|
||||
while (!paid) {
|
||||
if (reply == "YES") {
|
||||
print("\nHEY, \(name)??? YOU LEFT NO MONEY AT ALL!")
|
||||
print("YOU ARE CHEATING ME OUT OF MY HARD-EARNED LIVING.\n")
|
||||
print("WHAT A RIP OFF, \(name)!!!\n")
|
||||
print("TAKE A WALK, \(name).")
|
||||
paid = true
|
||||
}
|
||||
else if (reply == "NO") {
|
||||
print("THAT'S HONEST, \(name), BUT HOW DO YOU EXPECT "
|
||||
+ "ME TO GO ON WITH MY PSYCHOLOGY STUDIES IF MY PATIENTS "
|
||||
+ "DON'T PAY THEIR BILLS?\n")
|
||||
print("NICE MEETING YOU, \(name), HAVE A NICE DAY.")
|
||||
paid = true
|
||||
}
|
||||
else {
|
||||
print("YOUR ANSWER OF '\(reply)' CONFUSES ME, \(name).")
|
||||
print("PLEASE RESPOND WITH 'YES' OR 'NO'.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user