Learn about objects

This commit is contained in:
mollusk 2019-12-17 09:37:42 -07:00
parent e752b2ac26
commit 7abf04edd7

View File

@ -1,6 +1,6 @@
# This is just an example to get you started. A typical binary package # This is just an example to get you started. A typical binary package
# uses this file as the main entry point of the application. # uses this file as the main entry point of the application.
import system, os, random, strutils,typetraits import system, os, random, strutils, typetraits, strformat
when isMainModule: when isMainModule:
#[ #[
@ -19,39 +19,67 @@ when isMainModule:
for line in lines: for line in lines:
f.writeLine(line.toUpper) f.writeLine(line.toUpper)
]# ]#
#[
type
Person = object
name: string
age: int
cases: int
let people = [
Person(name: "Allison", age: 25, cases: 80),
Person(name: "Mike", age: 40, cases: 100)
]
for person in people:
echo(fmt"{person.name} is {person.age} years old and threw {person.cases} cases")
]#
proc testOop() =
type
Dog = object
proc bark(self: Dog) =
echo "Woof"
proc drink(self: Dog) =
echo "Gulp"
let dog = Dog()
dog.bark()
dog.drink()
proc geussNum() =
randomize()
var rng = rand(101)
var tries = 5
var guessed = 0
echo "I'm thinking of a number between 1 & 100"
echo "You have ", tries, " tries"
while guessed < tries:
stdout.write("Guess the number: ")
var guess = readLine(stdin).parseInt()
if guess > rng:
echo "Too high"
guessed += 1
echo "Guesses Made: ", guessed
elif guess < rng:
echo "Too low"
guessed += 1
echo "Guesses Made: ", guessed
elif guess == rng:
echo "You have guessed the number: ", rng, "Good job!"
quit(0)
echo "You did not guess the number, it was : ", rng
proc geussNum() =
randomize()
var rng = rand(101)
var tries = 5
var guessed = 0
echo "I'm thinking of a number between 1 & 100"
echo "You have ", tries, " tries"
while guessed < tries: proc help(): string =
stdout.write("Guess the number: ") result = """
var guess = readLine(stdin).parseInt()
if guess > rng:
echo "Too high"
guessed += 1
echo "Guesses Made: ", guessed
elif guess < rng:
echo "Too low"
guessed += 1
echo "Guesses Made: ", guessed
elif guess == rng:
echo "You have guessed the number: ", rng, "Good job!"
quit(0)
echo "You did not guess the number, it was : ", rng
proc help(): string =
result = """
scatter [-a, -b, -k, -r] scatter [-a, -b, -k, -r]
@ -60,25 +88,27 @@ scatter [-a, -b, -k, -r]
-k prints 'kill' -k prints 'kill'
-r Guess the number (1..100) -r Guess the number (1..100)
""" """
return result return result
var argv = commandLineParams() var argv = commandLineParams()
for args in argv: for args in argv:
case args: case args:
of "": of "":
echo help() echo help()
of "-a": of "-a":
echo "yay" echo "yay"
of "-b": of "-b":
echo "Oh yeah!" echo "Oh yeah!"
of "-k": of "-k":
echo "kill" echo "kill"
of "-r": of "-r":
geussNum() geussNum()
of "-h", "--help": of "-t":
echo help() testOop()
else: of "-h", "--help":
echo help() echo help()
else:
echo help()