I have to write a slot machine program where theres 3 wheels.
wheel 1 = cherry, cherry, cherry, plum, plum, bell, bar
wheel 2 = cherry, cherry, plum, plum plum, bell, bar
wheel 3 = plum, plum, bell, bar, cherry cherry.
when wheels "spin", output should show three items from each wheel (each wheel is a column of output).
player can put in 1 -3 coins, and 0 to exit
when 1 coin is enetered, middle row is evaluated; 2 coins, top two rows; 3 coins, all rows.

can anyone help me with this? this is what I have so far:

#include <iostream>
#include <cstdlib>  //for rand & srand functions
#include <ctime>  //for time function
#include <cassert> //for assert function
using namespace std;

void spin(int wheel[], int wheelsize, int column, int position[][3], int positionsize);
void NumConversion(int);
int evalpayout(int A[][], int B[][], int C[][]);

int main ()
{
	char exitkey;
	int total = 5, coinsin, coinswon = 0, winnings;
	int wheel1[7] = {1, 1, 1, 2, 2, 3, 3}, wheel2[7] = {1, 1, 2, 2, 2, 3, 4}, wheel3[7] = {2, 2, 3, 3, 4, 1, 1};
	int spin_result[3][3];

	cout << "How many coins would you like to insert?";
	cout << "You can play up to 3 coins. Enter 0 to exit." << endl;
	cin >> coinsin;

	assert ( coinsin >= 0 && coinsin <= 3);  //program terminates if invalid number of coins played is entered

	do
	{
		
		total = total - coinsin;

		spin(wheel1, 7, 0, spin_result, 3);     //generates numerical spin results for each wheel
		spin(wheel2, 7, 1, spin_result, 3);
		spin(wheel3, 7, 2, spin_result, 3);

		cout << "The wheels spin. The show the following: "<<endl;
		cout << endl;

		for(int i = 0; i < 3; i++)         //outputs spin results
		{
			for (int j = 0; j < 3; j++) 
				NumConversion(spin_result[i][j]);     //converts the numerical spin results to word results
			cout << endl;
		}

		if(coinsin == 1)
		{
			evalpayout(spin_result[1][0], spin_result[1][1], spin_result[1][2]); //evaluates middle row
			coinswon = winnings;
		}
		else if (coinsin == 2)
		{
			evalpayout(spin_result[0][0], spin_result[0][1], spin_result[0][2]);  //evaluations first row
			coinswon = winnings; 
			evalpayout(spin_result[1][0], spin_result[1][1], spin_result[1][2]);  //evaluations middle row
			coinswon = winnings;  //adds first row winnings to middle row winnings for total coins won
		}
		else 
		{
			evalpayout(spin_result[0][0], spin_result[0][1], spin_result[0][2]);  //evaluations first row
			coinswon = winnings;
			evalpayout(spin_result[1][0], spin_result[1][1], spin_result[1][2]);  //evaluations middle row
			coinswon = winnings;   //adds first row winnings to middle row winnings 
			evalpayout(spin_result[2][0], spin_result[2][1], spin_result[2][2]);  //evaluations last row
			coinswon = winnings;   // adds last row winnings to middle + first row winnings for total coins won
		}

		
		cout << endl;
		cout << "You win " << coinswon << " coin(s)." << endl;
		cout << endl;

		total = total + coinswon;

		cout<<"How many coins would you like to insert?";
		cin >> coinsin;

		assert ( coinsin >=0 && coinsin <=3);

	} while (coinsin != 0);

	cout<<endl;
	cout<<"Your final balance is " << total <<" coins."<<endl;
	cout << endl;
	cout << "Press any key then <enter> to exit: ";
	cin >> exitkey;
	cout << "Good Bye!";

	return 0;
}

void spin(int wheel[], int wheelsize, int column, int position[][3], int positionsize)
{
	int next = 0;
	srand(time(0));     //seed random number generator with system time
	next = rand() % 6;    

	for(int index =0; index < positionsize; index++)
	{
		position[index][column] = wheel[next];
		if ((next + 1) > (wheelsize - 1))
			next = 0; 
		else
			next = next + 1;
	}
}

void NumConversion(int x)     //converts all the random nubers generated into word outputs.
{
	switch(x)
	{
		case 0:
		cout << setw(8) << "Cherry";    
		break;

		case 1:
		cout << setw(8) << "Plum";
		break;

		case 2:
		cout << setw(8) << "Bell";
		break;

		case 3:
		cout << setw(8) << "Bar";
		break;
	}

}


int evalpayout( int A[][], int B[][], int C[][]) //takes in a specific row to evaluate payout
{
	int winnings = 0;
	if(A[x][0] = 2 && B[x][1] = 2 && C[x][2] = 2)
		winnings = winnings + 6;
	if(A[x][0] = 3 && B[x][1] = 3 && C[x][2] = 3)
		winnings = winnings + 9;
	if(A[x][0] = 4 && B[x][1] = 4 && C[x][2] = 4)
		winnings = winnings + 30;
	if(A[x][0] = 2 && B[x][1] = 2)
		winnings = winnings + 4;
	if(A[x][0] = 3 && B[x][1] = 3)
		winnings = winnings + 6;
	if(A[x][0] = 4 && B[x][1] = 4)
		winnings = winnings + 10;
	if (A[x][0] = 2)
		winnings = winnings + 2;
	if (A[x][0] = 3)
		winnings = winnings + 3;
	if (A[x][0] = 4)
		winnings = winnings + 5;
	return winnings;               //returns winnings to use towards calculating total coins won.
}

Edited 5 Years Ago by __avd: Added [code] tags.

What exactly is the problem?

Hint, lines 53, 60, 62, your comments do not match what you have written there. You are overwriting the value, not adding to it.

1. Slot machine has 3 wheels each with 7 positions, as described before
2. Program must ask user how many coins are being inserted, either 1, 2, or 3. any number outside of range ends the program, and 0 is to exit program.
3. Player starts with 5 coins. Number of coins entered is subtracted from that amount and any winnings are added. the balance may become negative and the final numer of coins must be printed at the end of the game.
4. sample spin should displav a 3x3 array, with each column corresponding to a wheel (1st column is 1st wheen, 2nd col is 2nd wheel, etc.) where each wheel spins independetly.
5. payout: 1 coin entered, the middle row is evaluated; 2 coins, top 2 rows; 3 coins, all rows
plum in 1st position = + 2
plum in first 2 positions = +4
plum in all three = +6
bell in first = +3
bell in first two = +6
bell in all three = +9
bar in first = +5
bar in first two = +10
bar in all three = 30


*and thank you for noticing those lines, i have changed it so they read "coinswon = coinswon + winnings"

In evalpayout look at the line if(A[x][0] = 2 && B[x][1] = 2 && C[x][2] = 2) and all of the others like it. You have used the equality operator (=) instead of the comparison operator (==).

thanks! completely forgot about that. it seems to me that the one thing I am really having trouble with is evalpayout function. Asside from the equal operator instead of the comparison operator, is the code correct? I want the function to take in a row and evaluate it, but I am not quite sure I did that correctly. Can you help me with that?

actually, what if i changed the function evalpayout to the following:

int evalpayout(int a, int b, int c) //takes in a specific row to evaluate payout
{
	int winnings = 0;
	if(a == 2 && b == 2 && c == 2)
		winnings = winnings + 6;
	if(a == 3 && b == 3 && c == 3)
		winnings = winnings + 9;
	if(a == 4 && b == 4 && c == 4)
		winnings = winnings + 30;
	if(a == 2 && b == 2)
		winnings = winnings + 4;
	if(a == 3 && b == 3)
		winnings = winnings + 6;
	if(a == 4 && b == 4)
		winnings = winnings + 10;
	if (a == 2)
		winnings = winnings + 2;
	if (a == 3)
		winnings = winnings + 3;
	if (a == 4)
		winnings = winnings + 5;
	return winnings;               //returns winnings to use towards calculating total coins won.
}

because the function call in the main program takes the values of each row, which are all integer values. Would this work?

I was just looking at that, and I think that would work (I did not test it yet). You'll have to adjust your function prototype at the top too. Also, you were missing <iomanip> from your headers.

Also, take the return from your function into winnings up in main(). The local variable in the function bears no relation to the one in main().

coinswon = evalPayout(spin_result[1][0], spin_result[1][1], spin_result[1][2]);
coinswon+= evalPayout(*those for the other row*);

Edited 5 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.