Learn about objects
This commit is contained in:
parent
e752b2ac26
commit
7abf04edd7
116
src/scatter.nim
116
src/scatter.nim
@ -1,6 +1,6 @@
|
||||
# This is just an example to get you started. A typical binary package
|
||||
# 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:
|
||||
|
||||
#[
|
||||
@ -19,39 +19,67 @@ when isMainModule:
|
||||
for line in lines:
|
||||
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)
|
||||
]
|
||||
|
||||
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"
|
||||
for person in people:
|
||||
echo(fmt"{person.name} is {person.age} years old and threw {person.cases} cases")
|
||||
]#
|
||||
proc testOop() =
|
||||
type
|
||||
Dog = object
|
||||
|
||||
while guessed < tries:
|
||||
stdout.write("Guess the number: ")
|
||||
var guess = readLine(stdin).parseInt()
|
||||
proc bark(self: Dog) =
|
||||
echo "Woof"
|
||||
|
||||
if guess > rng:
|
||||
echo "Too high"
|
||||
guessed += 1
|
||||
echo "Guesses Made: ", guessed
|
||||
proc drink(self: Dog) =
|
||||
echo "Gulp"
|
||||
|
||||
elif guess < rng:
|
||||
echo "Too low"
|
||||
guessed += 1
|
||||
echo "Guesses Made: ", guessed
|
||||
let dog = Dog()
|
||||
|
||||
elif guess == rng:
|
||||
echo "You have guessed the number: ", rng, "Good job!"
|
||||
quit(0)
|
||||
echo "You did not guess the number, it was : ", rng
|
||||
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 help(): string =
|
||||
result = """
|
||||
proc help(): string =
|
||||
result = """
|
||||
|
||||
scatter [-a, -b, -k, -r]
|
||||
|
||||
@ -60,25 +88,27 @@ scatter [-a, -b, -k, -r]
|
||||
-k prints 'kill'
|
||||
-r Guess the number (1..100)
|
||||
"""
|
||||
return result
|
||||
return result
|
||||
|
||||
|
||||
|
||||
var argv = commandLineParams()
|
||||
var argv = commandLineParams()
|
||||
|
||||
for args in argv:
|
||||
case args:
|
||||
of "":
|
||||
echo help()
|
||||
of "-a":
|
||||
echo "yay"
|
||||
of "-b":
|
||||
echo "Oh yeah!"
|
||||
of "-k":
|
||||
echo "kill"
|
||||
of "-r":
|
||||
geussNum()
|
||||
of "-h", "--help":
|
||||
echo help()
|
||||
else:
|
||||
echo help()
|
||||
for args in argv:
|
||||
case args:
|
||||
of "":
|
||||
echo help()
|
||||
of "-a":
|
||||
echo "yay"
|
||||
of "-b":
|
||||
echo "Oh yeah!"
|
||||
of "-k":
|
||||
echo "kill"
|
||||
of "-r":
|
||||
geussNum()
|
||||
of "-t":
|
||||
testOop()
|
||||
of "-h", "--help":
|
||||
echo help()
|
||||
else:
|
||||
echo help()
|
||||
|
Loading…
x
Reference in New Issue
Block a user