/*
Title: Slot machine
Filename: inlupp_2.cpp
Description: A "slot machine" where you bet money and that betmoney will be
multiplied if you got any rows, otherwise lost.
Input: Money, money bet, replay answer
Output: Game board, rows player got, money betted, total money, money won/lost.
Author: Patrik Freij
*/
#include <iostream>
#include <ctime>
using namespace std;
/*
Creating player specific variables like money, betted money and
money won/lost and "wins-per-round"
*/
struct player
{
int
money,
moneybet,
moneywl,
wins;
};
/*
Creating function "boardcheck" which checks for wins on the board. Returns wins.
*/
int boardcheck(int number[][3])
{
int wins = 0;
/*
We check each "3 in a row" path to see if the second and third value in
a row is thesame as the first one, if it is, add 1 to "wins".
*/
// Horizontal check
if((number[0][0] == number[0][1]) && (number[0][0] == number[0][2])) {
wins++;
}
if((number[1][0] == number[1][1]) && (number[1][0] == number[1][2])) {
wins++;
}
if((number[2][0] == number[2][1]) && (number[2][0] == number[2][2])) {
wins++;
}
// Vertical check
if((number[0][0] == number[1][0]) && (number[0][0] == number[2][0])) {
wins++;
}
if((number[0][1] == number[1][1]) && (number[0][1] == number[2][1])) {
wins++;
}
if((number[0][2] == number[1][2]) && (number[0][2] == number[2][2])) {
wins++;
}
// "cross" check
if((number[0][0] == number[1][1]) && (number[0][0] == number[2][2])) {
wins++;
}
if((number[2][0] == number[1][1]) && (number[2][0] == number[0][2])) {
wins++;
}
return wins;
}
/*
Tells the game how many times to multiplicate "moneybet" depending on how
many rows you got in the game. If no rows, return -1 since you will loose money.
*/
int rowmultiply(int &wins)
{
switch(wins)
{
case 1:
return 2;
break;
case 2:
return 3;
break;
case 3:
return 4;
break;
case 4:
return 5;
break;
case 5:
return 7;
break;
case 6:
return 8;
break;
case 7:
return 9;
break;
case 8:
return 10;
break;
default:
return -1;
break;
}
}
// Starting the program
int main()
{
// Creating the player and setting default values
player patrik;
patrik.money = 0;
patrik.moneybet = 0;
patrik.moneywl = 0;
// Creating variables
const int cells = 3;
int
replay = 1,
betvalid = 0,
number[cells][cells];
char
letter = 'a';
srand(time(0));
// If the player wants to play again after he have played once time, make this true and play again.
while(replay)
{
/*
If the player has less than 1 krona, make betvalid false.
if betvalid is false let the player add more money.
The value has to be between 100, 300 or 500 kronor to be valid.
*/
if(patrik.money < 1) { betvalid = 0; }
while(betvalid != 1)
{
cout<<"You don't have enough money to play, please cash in some money: (100, 300, 500) ";
cin>>patrik.money;
if(patrik.money == 100 || patrik.money == 300 || patrik.money == 500)
{
betvalid = 1;
} else {
cout<<"Invalid value"<<endl;
}
}
cout<<"You currently got "<<patrik.money<<" kronor."<<endl;
betvalid = 0;
/*
If betvalid is not true, let the player bet money.
If the player has bet an amount that isn't 100, 300 or 500 or doesn't
have that amount of money it will run again.
*/
while(betvalid != 1)
{
cout<<"Place your bets: ";
cin>>patrik.moneybet;
if(patrik.money >= patrik.moneybet && patrik.moneybet > 0)
{
betvalid = 1;
} else {
cout<<"Invalid bet"<<endl;
}
}
/*
Print out the board with 2 for loops (one for y axis and one for x axis).
Add the current "cordinate" to the current cell and then
add a random number between 1 and 3 in each field. Then replace the number with a letter.
*/
for(int y=0;y<cells;y++)
{
cout<<"-------------"<<endl;
for(int x=0;x<cells;x++)
{
if(x==0) { cout<<"| "; }
number[y][x] = rand() % 3 + 1;
switch(number[y][x])
{
case 1:
letter = 'O';
break;
case 2:
letter = 'X';
break;
default:
letter = 'A';
break;
}
cout<<letter<<" | ";
}
if(y==2) { cout<<endl<<"-------------"; }
cout<<endl;
}
/*
Check for wins with "boardcheck", then multiply your bet depending
on how many rows you got with "rowmultiply".
*/
patrik.wins = boardcheck(number);
cout<<"You got "<<patrik.wins<<" in a row!"<<endl;
cout<<"You betted "<<patrik.moneybet;
patrik.moneybet = patrik.moneybet * rowmultiply(patrik.wins);
if(patrik.moneybet > 0)
{
cout<<" kronor and won "<<patrik.moneybet<<" kronor!"<<endl;
} else {
cout<<" kronor and lost "<<patrik.moneybet*-1<<" kronor!"<<endl;
}
// Add won/lost money to your "moneywl" and current money
patrik.moneywl = patrik.moneywl + patrik.moneybet;
patrik.money = patrik.money + patrik.moneybet;
cout<<"You currently got "<<patrik.money<<" kronor."<<endl;
// Ask the player to replay the game.
cout<<"Replay? (1/0) ";
cin>>replay;
}
// Tell the player how much he won/lost and that it will be given to/taken from his account.
if(patrik.moneywl > 0)
{
cout<<"You won "<<patrik.moneywl<<" kronor and will now be cashed in to your account"<<endl;
} else {
cout<<"You lost "<<patrik.moneywl*-1<<" kronor! Better luck next time."<<endl;
}
// Shut it down! After you have pressed any button of course.
system("PAUSE");
return 0;
}