Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created October 15, 2012 07:39
Show Gist options
  • Save itsjohncs/3891236 to your computer and use it in GitHub Desktop.
Save itsjohncs/3891236 to your computer and use it in GitHub Desktop.
CS 10 SI Lab Session 3 - Roulette Exercise
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
// What would happen if we replaced time(0) here with a number, like 2?
srand(time(0));
// The two question marks below mean that you should figure out the correct
// types for each of the variables.
// If the user bets on a number, this will be the user's choice, otherwise
// it will be 0.
? user_num = 0;
// This will be true if the user bet on red.
? user_red;
// Hook the user into playing our devious game.
cout << "Search your pockets! Place your bets! Pray to your deities! Let's "
"play roulette!\n"
"You sir! Yes you. Come here.\n"
"Would you like to place a bet on a number or color? ";
string user_input;
cin >> user_input;
if (user_input == "number") {
cout << "\nWhich number sir (1-35)? ";
cin >> user_num;
} else if (user_input == "color") {
cout << "\nWhich color sir? ";
string user_color_input;
cin >> user_color_input;
// Notice the use of a boolean expression here. What will user_red hold if
// the user enters yellow?
user_red = user_color_input == "red";
}
// Randomly choose a number that our virtual ball has landed on.
// The question mark here means that you should replace it with an expression
// that would make sense here.
int result_num = ?;
// We are going to pretend that odd numbers are red and even numbers are black
// despite roulette having more complicated rules than that.
// Notice I'm using the modulo operator to detect whether the number is even
// or odd, how does this work?
bool result_red = result_num % 2 == 0;
cout << "\nThe ball landed on " << result_num;
// If we just printed out the value of result_red here, what would be
// displayed?
if (result_red) {
cout << " red. ";
} else {
cout << " black. ";
}
// Here we figure out if the user won. Determine what should replace the
// questions marks in the below statements. Hint: They will be boolean
// expressions.
bool user_won;
if (user_num == 0) {
user_won = ?;
} else {
user_won = ?;
}
if (user_won){
cout << "Which means you win! Congratulations sir!\n";
} else {
cout << "Which means you lose! I'm sorry sir!\n";
}
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
// What would happen if we replaced time(0) here with a number, like 2?
srand(time(0));
// The two question marks below mean that you should figure out the correct
// types for each of the variables.
// If the user bets on a number, this will be the user's choice, otherwise
// it will be 0.
int user_num = 0;
// This will be true if the user bet on red.
bool user_red;
// Hook the user into playing our devious game.
cout << "Search your pockets! Place your bets! Pray to your deities! Let's "
"play roulette!\n"
"You sir! Yes you. Come here.\n"
"Would you like to place a bet on a number or color? ";
string user_input;
cin >> user_input;
if (user_input == "number") {
cout << "\nWhich number sir (1-35)? ";
cin >> user_num;
} else if (user_input == "color") {
cout << "\nWhich color sir? ";
string user_color_input;
cin >> user_color_input;
// Notice the use of a boolean expression here. What will user_red hold if
// the user enters yellow?
user_red = user_color_input == "red";
}
// Randomly choose a number that our virtual ball has landed on.
// The question mark here means that you should replace it with an expression
// that would make sense here.
int result_num = rand() % 35 + 1;
// We are going to pretend that odd numbers are red and even numbers are black
// despite roulette having more complicated rules than that.
// Notice I'm using the modulo operator to detect whether the number is even
// or odd, how does this work?
bool result_red = result_num % 2 == 0;
cout << "\nThe ball landed on " << result_num;
// If we just printed out the value of result_red here, what would be
// displayed?
if (result_red) {
cout << " red. ";
} else {
cout << " black. ";
}
// Here we figure out if the user won. Determine what should replace the
// questions marks in the below statements. Hint: They will be boolean
// expressions.
bool user_won;
if (user_num == 0) {
user_won = user_red == result_red;
} else {
user_won = user_num == result_num;
}
if (user_won){
cout << "Which means you win! Congratulations sir!\n";
} else {
cout << "Which means you lose! I'm sorry sir!\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment