v / examples / vcasino / vcasino.v
145 lines · 132 sloc · 3.16 KB · 7eef64f88544a2165216c2fe87662105d8f995e0
Raw
1import rand
2import os
3
4const help_text = ' Usage:\t./VCasino\n
5 Description:\n VCasino is a little game only made to learn V.\n'
6
7const 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
12const odd = 'red'
13const even = 'black'
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 == '' {
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 }
63 }
64 return bet_nbr
65}
66
67fn get_bet(money int) int {
68 mut bet := -1
69 for bet <= 0 || bet > money {
70 println('You have ${money} V. Type in the amount of your bet:')
71 line := os.get_line().trim_space()
72 if line == '' {
73 println('error: empty line.')
74 continue
75 }
76 if !str_is_nbr(line) {
77 println('error: ${line} is not a number.')
78 continue
79 }
80 bet = line.int()
81 if bet <= 0 {
82 println('error: ${line} is not higher than 1.')
83 continue
84 } else if bet > money {
85 println('error: ${line} is more money than you have.')
86 }
87 }
88 return bet
89}
90
91fn run_wheel(bet_nbr int, _bet int) int {
92 mut bet := _bet
93 winning_nbr := rand.intn(50) or { 0 }
94 print('Roulette Wheel spinning... and stops on the number ${winning_nbr} which is a ')
95 if winning_nbr % 2 == 1 {
96 println(odd)
97 } else {
98 println(even)
99 }
100 if winning_nbr == bet_nbr {
101 bet *= 3
102 println('Congratulations! You get ${bet} V!')
103 } else if winning_nbr % 2 == bet_nbr % 2 {
104 bet /= 2
105 println('You bet the right color. You get ${bet} V!')
106 } else {
107 println('Sorry buddy. You lost ${bet} V!')
108 bet *= -1
109 }
110 return bet
111}
112
113fn is_broke(money int) bool {
114 if money <= 0 {
115 println("You're broke, the game is over..")
116 return false
117 }
118 quit := Options{'yes', 'y'}
119 println('You have ${money} V. Do you want to quit the casino with your winnings? (y/n)')
120 line := os.get_line().trim_space().to_lower()
121 if line == quit.long_opt || line == quit.short_opt {
122 return false
123 }
124 return true
125}
126
127fn game_loop() {
128 mut can_play := true
129 mut money := 1000
130 println(g_desc)
131 println('You start the game with ${money} V.\n')
132 for can_play {
133 bet_nbr := get_bet_nbr()
134 bet := get_bet(money)
135 money += run_wheel(bet_nbr, bet)
136 can_play = is_broke(money)
137 }
138}
139
140fn main() {
141 if os.args.len >= 2 && option_parser() {
142 return
143 }
144 game_loop()
145}
146