2019-06-27 03:05:05 +03:00
|
|
|
import rand
|
2019-07-13 19:17:45 +03:00
|
|
|
import time
|
2019-06-27 03:05:05 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
const (HelpText = ' Usage:\t./VCasino\n
|
|
|
|
Description:\n VCasino is a little game only made to learn V.\n')
|
|
|
|
const (GDesc = ' 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 same color as the ball one, you\'ll get your bet /2.
|
|
|
|
Otherwise, you will lose your bet.\n')
|
|
|
|
const (Odd = 'Red' Even = 'Black')
|
|
|
|
|
2019-09-28 22:26:44 +03:00
|
|
|
struct Options {
|
2019-06-27 03:05:05 +03:00
|
|
|
long_opt string
|
|
|
|
short_opt string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn display_help() {
|
|
|
|
println(HelpText + GDesc)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn option_parser() bool {
|
2019-09-28 22:26:44 +03:00
|
|
|
help := Options{'--help', '-h'}
|
2019-06-27 03:05:05 +03:00
|
|
|
for i := 0; i < os.args.len; i++ {
|
2019-06-27 14:14:59 +03:00
|
|
|
if os.args[i]== help.long_opt || os.args[i]== help.short_opt {
|
2019-06-27 03:05:05 +03:00
|
|
|
display_help()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn str_is_nbr(s string) bool {
|
|
|
|
for i := 0; i < s.len; i++ {
|
|
|
|
if !s[i].is_digit() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_bet_nbr() int {
|
|
|
|
mut bet_nbr := -1
|
|
|
|
for bet_nbr < 0 || bet_nbr > 49 {
|
|
|
|
println('Reminder: Odd numbers are red and even are black.')
|
|
|
|
println('Type the number you want to bet on (between 0 and 49):')
|
|
|
|
line := os.get_line().trim_space()
|
|
|
|
if line.len < 1 {
|
|
|
|
println('error: empty line.')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !str_is_nbr(line) {
|
|
|
|
println('error: $line is not a number.')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bet_nbr = line.int()
|
|
|
|
if bet_nbr < 0 || bet_nbr > 49 {
|
|
|
|
println('error: $line is not between 0 and 49.')
|
|
|
|
bet_nbr = -1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bet_nbr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_bet(money int) int {
|
|
|
|
mut bet := -1
|
|
|
|
for bet <= 0 || bet > money {
|
|
|
|
println('You\'ve $money V. Type in the amount of your bet:')
|
|
|
|
line := os.get_line().trim_space()
|
|
|
|
if line.len < 1 {
|
|
|
|
println('error: empty line.')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !str_is_nbr(line) {
|
|
|
|
println('error: $line is not a number.')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bet = line.int()
|
|
|
|
if bet <= 0 {
|
|
|
|
println('error: $line is not heigher than 1.')
|
|
|
|
continue
|
|
|
|
} else if bet > money {
|
2019-07-15 17:43:22 +03:00
|
|
|
println('error: $line is more money than you have.')
|
2019-06-27 03:05:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return bet
|
|
|
|
}
|
|
|
|
|
2019-08-07 15:16:10 +03:00
|
|
|
fn run_wheel(bet_nbr int, _bet int) int {
|
2019-09-28 22:26:44 +03:00
|
|
|
mut bet := _bet
|
2019-07-13 19:17:45 +03:00
|
|
|
rand.seed(time.now().uni)
|
2019-06-27 03:05:05 +03:00
|
|
|
winning_nbr := rand.next(50)
|
|
|
|
print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ')
|
|
|
|
if winning_nbr % 2 == 1 {
|
|
|
|
println(Odd)
|
|
|
|
} else {
|
|
|
|
println(Even)
|
|
|
|
}
|
|
|
|
if winning_nbr == bet_nbr {
|
|
|
|
bet *= 3
|
|
|
|
println('Congratulations! You get $bet V!')
|
|
|
|
} else if winning_nbr % 2 == bet_nbr % 2 {
|
|
|
|
bet /= 2
|
|
|
|
println('You bet the right color. You get $bet V!')
|
|
|
|
} else {
|
|
|
|
println('Sorry buddy. You lost $bet V!')
|
|
|
|
bet *= -1
|
|
|
|
}
|
|
|
|
return bet
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_broke(money int) bool {
|
|
|
|
if money <= 0 {
|
|
|
|
println('You\'broke, the game is over..')
|
|
|
|
return false
|
|
|
|
} else {
|
2019-09-28 22:26:44 +03:00
|
|
|
quit := Options{'yes', 'y'}
|
2019-06-27 03:05:05 +03:00
|
|
|
println('You\'ve $money V. Do you want to quit the casino with your winnings? (y/n)')
|
|
|
|
line := os.get_line().trim_space().to_lower()
|
2019-06-27 14:14:59 +03:00
|
|
|
if line== quit.long_opt || line== quit.short_opt {
|
2019-06-27 03:05:05 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn game_loop() {
|
|
|
|
mut can_play := true
|
|
|
|
mut money := 1000
|
|
|
|
|
|
|
|
println(GDesc)
|
|
|
|
println('You start the game with $money V.\n')
|
|
|
|
for can_play {
|
|
|
|
bet_nbr := get_bet_nbr()
|
|
|
|
bet := get_bet(money)
|
|
|
|
money += run_wheel(bet_nbr, bet)
|
|
|
|
can_play = is_broke(money)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
if os.args.len >= 2 {
|
|
|
|
if option_parser() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
game_loop()
|
2019-06-27 13:27:46 +03:00
|
|
|
}
|