Reducing the size and increasing efficiency of a huge block of code

So I am currently really confused. I have this HUGE block of code for the game Blackjack and have a few requirements.

1. Reduce the size and increase efficiency of the code by removing as many if/if else statements and consolidate them into functions
2. The dealer has to keep getting cards until their score is 17, they go over 21 or they take five cards
3. Must generate a new deck at the start of the game and shuffle the deck before each hand
4. Guest can play as many hands as they would like
5. If the dealer goes over 21 and a player goes over 21 the game is a push

I guess my main problem here is I don't know how to consolidate all these if/if else statements into functions. What functions should I create? I know arrays or enumeration may help, but I just don't know where to start. The most recent concept that came up before this exercise were arrays, so I'm guessing that will be crucial to completing this exercise.

Thanks in advance for helping out this noob!

https://pastebin.com/iYHXqXxW
Yeah, throw it away and start again.

There is no point even trying to polish a 1000+ line turd of a main() function like that.


> else if ((cardDeck[0] > 3) && (cardDeck[0] <= 7))
You can get rid of all these with some simple / and % arithmetic.
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
using namespace std;

int main ( ) {
  for ( int i = 0 ; i < 52 ; i++ ) {
    cout << "Suit=" << i/13 << ", Rank=" << i % 13 << endl;
  }
}


> if (cardDeck[0] <= 3)
Don't mix the players with the deck.

1
2
3
int deck[52];
int players[4][5]; // 4 players, each with up to 5 cards each
int dealer[5]; // the dealer has 5 cards. 


Then maybe add a function called 'deal' which moves ONE card from the deck into a given players hand.

You then deal 2 cards to each player.

Then start your 'stick or twist' logic (as a function), which you call in turn for each player.
agree, do over.
also figure out what you need. you need the ability to score a hand. you need the ability to determine who won a hand. you need to handle the money (if any) that was bet. You need a simple AI for the computer player, which is probably just a few simple rules. Usually computer ai goes after human and can see the human hand and hits until it beats the human or loses, sometimes with exceptions like stop at 17 or soft 17 (eg face+7 allows hit but 10+5+2 has to stop). You need a deck, ability to shuffle and deal from it (this can just be a vector, but if you want more you can do it).
if/then complexity can be eliminated or reduced via several techniques:
1) visual scan for junk (conditions that can't hit, or were already known at that point)
2) reverse logic (often can eliminate compound conditions by revering logic so that the flow ensures one of the conditions is known after)
3) defaults ... x = 3; if () x = 4; vs if () x = 4 else x = 3;
4) k-map it to spot redundancy
5) switch statement fall-through can avoid conditions and switches can be less bulky if done right
6) bool math / lookup table can eliminate simple conditionals eg result[] = {1,2} and later x = result[z != 11] //casts bool of condition to 0/1 and goes to the [0] or [1] location in the table

maybe what you are looking for is just a cleaner pattern..
switch(something)
case one: foo1(); break;
case two: foo2(); break;
^^^ condition is small and clean, and the functions make it modular/cleaner as well. also can use foo(one) foo(two) if the function for one and two is the same apart from the data operated upon.

even with a do-over if you wrote and understand the original you can probably surgically cut out good parts to reuse. but most of that thing looks like it needs to be tossed.
Last edited on
Topic archived. No new replies allowed.