v / examples / vcasino
Raw file | 146 loc (135 sloc) | 3.16 KB | Latest commit hash 017ace6ea
1import rand
2import os
3
4const (
5 help_text = ' Usage:\t./VCasino\n
6 Description:\n VCasino is a little game only made to learn V.\n'
7 g_desc = " The object of Roulette is to pick the number where the spinning ball will land on the wheel.
8 If your number is the good one, you'll get your bet x3.
9 If your number is the same color as the ball one, you'll get your bet /2.
10 Otherwise, you will lose your bet.\n"
11 odd = 'red'
12 even = 'black'
13)
14
15struct Options {
16 long_opt string
17 short_opt string
18}
19
20fn display_help() {
21 println(help_text + g_desc)
22}
23
24fn option_parser() bool {
25 help := Options{'--help', '-h'}
26 for i in 0 .. os.args.len {
27 if os.args[i] == help.long_opt || os.args[i] == help.short_opt {
28 display_help()
29 return true
30 }
31 }
32 return false
33}
34
35fn str_is_nbr(s string) bool {
36 for i in 0 .. s.len {
37 if !s[i].is_digit() {
38 return false
39 }
40 }
41 return true
42}
43
44fn get_bet_nbr() int {
45 mut bet_nbr := -1
46 for bet_nbr < 0 || bet_nbr > 49 {
47 println('Reminder: odd numbers are red and even are black.')
48 println('Type the number you want to bet on (between 0 and 49):')
49 line := os.get_line().trim_space()
50 if line.len < 1 {
51 println('error: empty line.')
52 continue
53 }
54 if !str_is_nbr(line) {
55 println('error: ${line} is not a number.')
56 continue
57 }
58 bet_nbr = line.int()
59 if bet_nbr < 0 || bet_nbr > 49 {
60 println('error: ${line} is not between 0 and 49.')
61 bet_nbr = -1
62 continue
63 }
64 }
65 return bet_nbr
66}
67
68fn get_bet(money int) int {
69 mut bet := -1
70 for bet <= 0 || bet > money {
71 println('You have ${money} V. Type in the amount of your bet:')
72 line := os.get_line().trim_space()
73 if line.len < 1 {
74 println('error: empty line.')
75 continue
76 }
77 if !str_is_nbr(line) {
78 println('error: ${line} is not a number.')
79 continue
80 }
81 bet = line.int()
82 if bet <= 0 {
83 println('error: ${line} is not higher than 1.')
84 continue
85 } else if bet > money {
86 println('error: ${line} is more money than you have.')
87 }
88 }
89 return bet
90}
91
92fn run_wheel(bet_nbr int, _bet int) int {
93 mut bet := _bet
94 winning_nbr := rand.intn(50) or { 0 }
95 print('Roulette Wheel spinning... and stops on the number ${winning_nbr} which is a ')
96 if winning_nbr % 2 == 1 {
97 println(odd)
98 } else {
99 println(even)
100 }
101 if winning_nbr == bet_nbr {
102 bet *= 3
103 println('Congratulations! You get ${bet} V!')
104 } else if winning_nbr % 2 == bet_nbr % 2 {
105 bet /= 2
106 println('You bet the right color. You get ${bet} V!')
107 } else {
108 println('Sorry buddy. You lost ${bet} V!')
109 bet *= -1
110 }
111 return bet
112}
113
114fn is_broke(money int) bool {
115 if money <= 0 {
116 println("You're broke, the game is over..")
117 return false
118 }
119 quit := Options{'yes', 'y'}
120 println('You have ${money} V. Do you want to quit the casino with your winnings? (y/n)')
121 line := os.get_line().trim_space().to_lower()
122 if line == quit.long_opt || line == quit.short_opt {
123 return false
124 }
125 return true
126}
127
128fn game_loop() {
129 mut can_play := true
130 mut money := 1000
131 println(g_desc)
132 println('You start the game with ${money} V.\n')
133 for can_play {
134 bet_nbr := get_bet_nbr()
135 bet := get_bet(money)
136 money += run_wheel(bet_nbr, bet)
137 can_play = is_broke(money)
138 }
139}
140
141fn main() {
142 if os.args.len >= 2 && option_parser() {
143 return
144 }
145 game_loop()
146}