Blackjack (condensing/loops)

So for the first project, we need to make a simple blackjack game. This is the sample program output example given.

Welcome to the COMP 51 Casino!
Let’s Play Blackjack!

You have been dealt a 5 and a 9. Your hand is 14.
Would you like to (h)it or (s)tay: h

You have been dealt a 1. Your hand is 15.
Would you like to (h)it or (s)tay: H

You have been dealt a 10. Your hand is 25.
Would you like to (h)it or (s)tay: h

You have been dealt a 3. Your hand is 28.
You can’t hit anymore. I hope your hand is enough!

The dealer’s hand is 22.
You busted! I’m sorry, you lose!



As we haven't done loops yet, I'm not expecting him to grade whether or not we use it. However, without it, my code seems so bulky, and when I read on tutorials about loops, I can't seem to figure out how to apply it to my code for hit/stay. If using only If/Else statements, I would imagine the code would end up very long if I add 2-3 more hits. Or am I missing something? So I'm basically asking what I can do to simplify my code, reading material would be great if anything. FYI, this is only my third week into my first programming class, so my knowledge is a bit limited, sorry if I'm a little slow.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <string>
#include <time.h>
#include <Windows.h>

using namespace std;

int main()
{
	int rand1, rand2, dealerrand, playerhand;

	cout<<"Welcome to simple BlackJack!\n";

	//START SEED
	srand(time(NULL));

	//PLAYER HAND GEN
	rand1 = rand()%10 +1;
	rand2 = rand()%10 +1;
	
	//Generating first and second card for player

	cout<<"First Card: "<<rand1<< "\n";
	cout<<"Second Card: "<<rand2<< "\n";
	
	playerhand = rand1 + rand2;
	Sleep(1000); // Pause for read time
	cout<<"You got: " <<playerhand<< endl;

	//DEALER HAND GEN
	dealerrand = rand()%8 +17;
	Sleep(1000); // Pause for read time
	cout<<"Dealer got: " <<dealerrand<< endl;

	//HIT OR STAY FUNCTIONS
	int hitstay;
	int hit1, newhand;
	cout<<"Hit [1] or Stay[2]?\n";  //SET VALUE 1 for HIT /// SET VALUE 2 FOR STAY //
	cin>>hitstay;

	if(hitstay == 1) //IF HIT
	{
	hit1 = rand()%10 +1;
	newhand = rand1 + rand2 + hit1;
	cout<<"You hit and got: " << hit1 << endl;
	cout<<"You now have: "<< newhand << endl;

	//GAMEPLAY FOR HIT
	if((newhand > dealerrand) && (newhand <= 21))
	{
		cout<<"Congratulations! You Win!\n";
		if(newhand == 21) //Sum of hand is 21 call BlackJack
		{
			cout<<"BlackJack!\n";
			cin.get();
		}
		cin.get();
	}
	else if(newhand == dealerrand)
	{
		cout<<"You Pushed!\n";
		cin.get();
	}
	else if(dealerrand > 21)
	{
		cout<<"Dealer Bust! You Win!\n";
		cin.get();
	}
	else
	{
		cout<<"You Lose!\n";
		cin.get();
	}
		cin.get();
	}

    //END HIT GAMEPLAY

	else(hitstay == 2); //IF STAY
	{
	//GAMEPLAY FOR STAY
	if(playerhand > dealerrand)
	{
		cout<<"Congratulations! You Win!\n";
		if(playerhand == 21) //Sum of hand is 21 call BlackJack
		{
			cout<<"BlackJack!\n"; 
			cin.get();
		}
		cin.get();
	}
	else if(playerhand == dealerrand)
	{
		cout<<"You Pushed!\n";
		cin.get();
	}
	else if(dealerrand > 21)
	{
		cout<<"Dealer Bust! You Win!\n";
		cin.get();
	}
	else
	{
		cout<<"You Lose!\n";
		cin.get();
	}
		
	}
	cin.get(); 
	//END STAY GAMEPLAY
}]
Last edited on
Topic archived. No new replies allowed.