examples: update vcasino.v (#5880)

pull/5941/head
Iman 2020-07-22 22:15:55 +04:30 committed by GitHub
parent 938e71b468
commit d820a97c83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 107 additions and 112 deletions

View File

@ -1,151 +1,146 @@
import rand import rand
import time
import os import os
const ( const (
help_text = ' Usage:\t./VCasino\n help_text = ' Usage:\t./VCasino\n
Description:\n VCasino is a little game only made to learn V.\n' Description:\n VCasino is a little game only made to learn V.\n'
g_desc = ' The object of Roulette is to pick the number where the spinning ball will land on the wheel. g_desc = " The object of Roulette is to pick the number where the spinning ball will land on the wheel.
If your number is the good one, you\'ll get your bet x3. If your number is the good one, you'll get your bet x3.
If your number is the same color as the ball one, you\'ll get your bet /2. If your number is the same color as the ball one, you'll get your bet /2.
Otherwise, you will lose your bet.\n' Otherwise, you will lose your bet.\n"
odd = 'red' odd = 'red'
even = 'black' even = 'black'
) )
struct Options { struct Options {
long_opt string long_opt string
short_opt string short_opt string
} }
fn display_help() { fn display_help() {
println(help_text + g_desc) println(help_text + g_desc)
} }
fn option_parser() bool { fn option_parser() bool {
help := Options{'--help', '-h'} help := Options{'--help', '-h'}
for i in 0..os.args.len { for i in 0 .. os.args.len {
if os.args[i]== help.long_opt || os.args[i]== help.short_opt { if os.args[i] == help.long_opt || os.args[i] == help.short_opt {
display_help() display_help()
return true return true
} }
} }
return false return false
} }
fn str_is_nbr(s string) bool { fn str_is_nbr(s string) bool {
for i in 0..s.len { for i in 0 .. s.len {
if !s[i].is_digit() { if !s[i].is_digit() {
return false return false
} }
} }
return true return true
} }
fn get_bet_nbr() int { fn get_bet_nbr() int {
mut bet_nbr := -1 mut bet_nbr := -1
for bet_nbr < 0 || bet_nbr > 49 { for bet_nbr < 0 || bet_nbr > 49 {
println('Reminder: odd numbers are red and even are black.') println('Reminder: odd numbers are red and even are black.')
println('Type the number you want to bet on (between 0 and 49):') println('Type the number you want to bet on (between 0 and 49):')
line := os.get_line().trim_space() line := os.get_line().trim_space()
if line.len < 1 { if line.len < 1 {
println('error: empty line.') println('error: empty line.')
continue continue
} }
if !str_is_nbr(line) { if !str_is_nbr(line) {
println('error: $line is not a number.') println('error: $line is not a number.')
continue continue
} }
bet_nbr = line.int() bet_nbr = line.int()
if bet_nbr < 0 || bet_nbr > 49 { if bet_nbr < 0 || bet_nbr > 49 {
println('error: $line is not between 0 and 49.') println('error: $line is not between 0 and 49.')
bet_nbr = -1 bet_nbr = -1
continue continue
} }
} }
return bet_nbr return bet_nbr
} }
fn get_bet(money int) int { fn get_bet(money int) int {
mut bet := -1 mut bet := -1
for bet <= 0 || bet > money { for bet <= 0 || bet > money {
println('You\'ve $money V. Type in the amount of your bet:') println('You have $money V. Type in the amount of your bet:')
line := os.get_line().trim_space() line := os.get_line().trim_space()
if line.len < 1 { if line.len < 1 {
println('error: empty line.') println('error: empty line.')
continue continue
} }
if !str_is_nbr(line) { if !str_is_nbr(line) {
println('error: $line is not a number.') println('error: $line is not a number.')
continue continue
} }
bet = line.int() bet = line.int()
if bet <= 0 { if bet <= 0 {
println('error: $line is not higher than 1.') println('error: $line is not higher than 1.')
continue continue
} else if bet > money { } else if bet > money {
println('error: $line is more money than you have.') println('error: $line is more money than you have.')
} }
} }
return bet return bet
} }
fn run_wheel(bet_nbr int, _bet int) int { fn run_wheel(bet_nbr, _bet int) int {
mut bet := _bet mut bet := _bet
winning_nbr := rand.intn(50) winning_nbr := rand.intn(50)
print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ') print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ')
if winning_nbr % 2 == 1 { if winning_nbr % 2 == 1 {
println(odd) println(odd)
} else { } else {
println(even) println(even)
} }
if winning_nbr == bet_nbr { if winning_nbr == bet_nbr {
bet *= 3 bet *= 3
println('Congratulations! You get $bet V!') println('Congratulations! You get $bet V!')
} else if winning_nbr % 2 == bet_nbr % 2 { } else if winning_nbr % 2 == bet_nbr % 2 {
bet /= 2 bet /= 2
println('You bet the right color. You get $bet V!') println('You bet the right color. You get $bet V!')
} else { } else {
println('Sorry buddy. You lost $bet V!') println('Sorry buddy. You lost $bet V!')
bet *= -1 bet *= -1
} }
return bet return bet
} }
fn is_broke(money int) bool { fn is_broke(money int) bool {
if money <= 0 { if money <= 0 {
println('You\'re broke, the game is over..') println("You're broke, the game is over..")
return false return false
} else { }
quit := Options{'yes', 'y'} quit := Options{'yes', 'y'}
println('You\'ve $money V. Do you want to quit the casino with your winnings? (y/n)') println('You have $money V. Do you want to quit the casino with your winnings? (y/n)')
line := os.get_line().trim_space().to_lower() line := os.get_line().trim_space().to_lower()
if line== quit.long_opt || line== quit.short_opt { if line == quit.long_opt || line == quit.short_opt {
return false return false
} }
} return true
return true
} }
fn game_loop() { fn game_loop() {
mut can_play := true mut can_play := true
mut money := 1000 mut money := 1000
println(g_desc)
println(g_desc) println('You start the game with $money V.\n')
println('You start the game with $money V.\n') for can_play {
for can_play { bet_nbr := get_bet_nbr()
bet_nbr := get_bet_nbr() bet := get_bet(money)
bet := get_bet(money) money += run_wheel(bet_nbr, bet)
money += run_wheel(bet_nbr, bet) can_play = is_broke(money)
can_play = is_broke(money) }
}
} }
fn main() { fn main() {
if os.args.len >= 2 { if os.args.len >= 2 && option_parser() {
if option_parser() { return
return }
} game_loop()
}
game_loop()
} }