first commit

This commit is contained in:
Michael 2022-02-15 14:02:43 +00:00
commit a35c009f91
4 changed files with 56 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.exe

3
README.md Normal file
View File

@ -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).

3
train/go.mod Executable file
View File

@ -0,0 +1,3 @@
module train
go 1.17

49
train/main.go Executable file
View File

@ -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
}
}
}