commit a35c009f9170479a12d587fd8cde49c49c320a8f Author: Michael Date: Tue Feb 15 14:02:43 2022 +0000 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ae5ffb --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# basic-computer-games + +Rewrite of basic computer games in golang using [basic-computer-games](https://github.com/coding-horror/basic-computer-games) as a reference with the basic source code from [here](http://www.vintage-basic.net/games.html). diff --git a/train/go.mod b/train/go.mod new file mode 100755 index 0000000..42b7f9b --- /dev/null +++ b/train/go.mod @@ -0,0 +1,3 @@ +module train + +go 1.17 diff --git a/train/main.go b/train/main.go new file mode 100755 index 0000000..11d34b3 --- /dev/null +++ b/train/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "bufio" + "fmt" + "math" + "math/rand" + "os" + "strconv" + "time" +) + +func main() { + for { + rand.Seed(time.Now().UnixNano()) + fmt.Println("Train") + fmt.Println("Creative Computing Morristown, New Jersey") + fmt.Printf("Time - Speed Distance Exercise:") + var car int = rand.Intn(65-40) + 40 + var distance int = rand.Intn(20-5) + 20 + var train int = rand.Intn(39-20) + 39 + fmt.Printf(" A Car traveling %d MPH can make a certain trip in %d hours less than a Train traveling at %d MPH.\n", car, distance, train) + for { + scanner := bufio.NewScanner(os.Stdin) + fmt.Printf("\nHow long does the trip take by car ") + scanner.Scan() + answer, err := strconv.ParseFloat(scanner.Text(), 64) + if err != nil { + fmt.Println("Please enter a number") + continue + } + var car_time int = distance * train / (car - train) + var percent int = int(math.Abs((float64(car_time)-answer)*100/answer) + .5) + if percent > 5 { + fmt.Printf("Sorry, you were off by %d percent\n", percent) + fmt.Printf("Correct answer is %d hours\n", car_time) + } else { + fmt.Printf("Good! Answer within %d percent\n", percent) + fmt.Printf("Another Problem? (yes or no) ") + scanner.Scan() + if scanner.Text() == "yes" { + break + } + return + } + continue + } + } +}