Welcome! |
|
|
|
|
Site Forum |
|
|
|
|
|
Topic: Program Submission
|

|
| Program Submission |
| << Prev Topic | Next Topic >> |
| jumahu918 |
| Posted: 07-December-2009 at 2:09pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 01-December-2007 Location: United States Posts: 2
|
Here is the C++ code to a card game I wrote a few years back in Visual C++ Express 2008. Since the uploader is still not working, here it is for copy and paste purposes. Enjoy.
Code:
#include<iostream> #include<ctime> #include<string>
using namespace std;
bool cardtable[4][13] = {false};
string player[2] = {"Player 1", "Player 2"};
int gameplay(int &); void game(); void instruct(); void options(); void about();
const string cardsuit[4] = {"Spades", "Diamonds", "Clubs", "Hearts"}; const string cardnum[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; int cardvalue[13] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
int main() { int choice = 1; string player1 = "Player 1", player2 = "Player 2"; do { do { system("CLS"); cout << "\n----------BlackJack----------\n"; cout << " 1. Play Game\n"; cout << " 2. Instructions\n"; cout << " 3. Options\n"; cout << " 4. About\n"; cout << " 5. Exit\n"; cout << "-----------------------------\n"; if (choice < 1 || choice > 5) cout << " Please enter a valid selection (1 - 5)\n"; cout << " Selection : "; cin >> choice; }while(choice < 1 || choice > 5); switch(choice) { case 1 : game(); break; case 2 : instruct(); break; case 3 : options(); break; case 4: about(); break; case 5: break; default : break; } }while(choice != 5);
cout << "\nExiting..."; system("PAUSE");
return 0; }
int gameplay(int &count) { count++; int suit, card, sum = 0; bool used = false; do { int seed = time(0) / rand(); srand(seed); suit = rand() % 4; card = rand() % 13;
used = cardtable[suit][card]; }while (used == true); cardtable[suit][card] = true; cout << "\nCard : " << cardnum[card] << " of " << cardsuit[suit];
return card; }
void game() { bool ace[2] = {false, false}; char reset; int choice = 1; do { int sum[2] = {0}, card= 0, count = 0; for(int playnum = 0; playnum <=1; playnum++) { system("CLS"); cout << "----- " << player[playnum] << " -----\n"; card = gameplay(count);
if(card == 0) ace[playnum] = true;
sum[playnum] += cardvalue[card]; card = gameplay(count);
if(card == 0) ace[playnum] = true;
sum[playnum] += cardvalue[card]; cout << " \nTotal : " << sum[playnum] << endl << endl; cout << count << endl; system("PAUSE"); }
do { if(count == 30) { system("CLS"); cout << "---------- Hit? -----------\n"; cout << " Are you kidding me?\n\n"; cout << " Totaling Players...\n"; system("PAUSE"); break; }
if(count < 30) { do { system("CLS"); cout << "----- Hit? -----\n"; cout << " 1. " << player[0] << "\t" << sum[0] << endl; cout << " 2. " << player[1] << "\t" << sum[1] << endl; cout << " 3. Both Stay\n\n"; if(choice < 1 || choice > 3) cout << "Please enter a valid choice (1 - 3)\n"; cout << " Selection : "; cin >> choice;
}while(choice < 1 || choice > 3);
switch(choice) { case 1: system("CLS"); cout << "----- " << player[0] << " -----\n"; card = gameplay(count);
if(card == 0) ace[0] = true;
sum[0] += cardvalue[card]; cout << "\n Total : " << sum[0] << endl << endl; cout << count << endl; system("PAUSE"); break; case 2: system("CLS"); cout << "----- " << player[1] << " -----\n"; card = gameplay(count);
if(card == 0) ace[1] = true; sum[1] += cardvalue[card]; cout << "\n Total : " << sum[1] << endl << endl; cout << count << endl; system("PAUSE"); break; default: break; } } }while(choice != 3);
char choice; if(ace[0] == true) { system("CLS"); cout << "\nTotal : " << sum[0] << endl; cout << player[0] << ", would you like to use Ace as 11? (Y/N) : "; cin >> choice; if(choice == 'Y' || choice =='y') sum[0] += 10;
cout << "\n\tTotal : " << sum[0] << endl; system("PAUSE"); }
if(ace[1] == true) { system("CLS");
cout << "Total : " << sum[1] << endl; cout << player[1] << ", would you like to use Ace as 11? (Y/N) : "; cin >> choice;
if(choice == 'Y' || choice =='y') sum[1] += 10;
cout << "\n\tTotal : " << sum[1] << endl; system("PAUSE"); }
system("CLS"); cout << player[0] << "'s Total : " << sum[0] << endl; cout << player[1] << "'s Total : " << sum[1] << endl; if( sum[0] == sum[1] && sum[0] < 21 && sum[1] < 21) cout << "\n\tIt's a DRAW!\n"; if(sum[0] > sum[1] && sum[0] <= 21) cout << "\n\t" << player[0] << " Wins!\n"; else if(sum[1] > sum[0] && sum[1] <= 21) cout << "\n\t" << player[1] << " Wins!\n"; else if(sum[1] < sum[0] && sum[0] > 21) cout << "\n\t" << player[1] << " Wins!\n"; else if(sum[0] < sum[1] && sum[1] > 21) cout << "\n\t" << player[0] << " Wins!\n"; if(sum[0] > 21) cout << "\n\t" << player[0] << " busted\n"; if(sum[1] > 21) cout << "\n\t" << player[1] << " busted\n"; cout << "\nReshuffle Deck and Play Again? (Y/N) : "; cin >> reset;
}while(reset == 'Y' || reset == 'y'); }
void options() { int choice = 1; do { do { system("CLS"); cout << "\n-----------------------------\n"; cout << " 1. Change Player 1 Name\n"; cout << " 2. Change Player 2 Name\n"; cout << " 3. Default Names\n"; cout << " 4. Back to Main Menu\n"; cout << "-----------------------------\n"; if(choice < 1 || choice > 4) cout << "Please enter a valid selection (1 - 4)\n"; cout << " Selection : "; cin >> choice; cout << endl; }while(choice < 1 || choice > 4); switch(choice) { case 1 : cout << "New Player 1 Name : "; cin.ignore(); getline(cin, player[0]); break; case 2 : cout << "New Player 2 Name : "; cin.ignore(); getline(cin, player[1]); break; case 3 : player[0] = "Player 1"; player[1] = "Player 2"; break; case 4: break; default : break; } }while(choice != 4); }
void instruct() { system("CLS"); cout << "----------------------------------\n"; cout << " Players try to obtain a total\n"; cout << " of 21 without exceeding 21.\n"; cout << " Whomever has the highest\n"; cout << " total when a round ends\n"; cout << " is the winner.\n"; cout << " Ace is 1 by default, but after\n"; cout << " hitting ends, player has a\n"; cout << " choice to use it as 11\n"; cout << " that will add 10 to the\n"; cout << " current total\n"; cout << "----------------------------------\n";
system("PAUSE"); }
void about() { system("CLS"); cout << "----------------------------------\n"; cout << " Programmed by Justin Hughes\n"; cout << " February 5, 2008\n"; cout << "----------------------------------\n"; system("PAUSE"); }
/* void resetgame() { bool cardtable[4][13] = {false}; } */ |
|
|
|
Online Status: Offline
|
|
| |
| jumahu918 |
| Posted: 07-December-2009 at 2:18pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 01-December-2007 Location: United States Posts: 2
|
I just noticed a programming error. The card deck does not "reshuffle" as intended because the deck reset function was commented out. Heck, I don't even think I used it. Long story short, the random number generator has trouble finding an unused card when 40 or so cards have been used. Should be a simple fix.
|
Online Status: Offline
|
|
| |
| 1 User(s) are browsing this topic, 1 Guest(s) and 0 Member(s) |
| 0 Members: |
|
|
|
|
|
If you wish to post a reply to this topic you must first login If you are not already registered you must first register
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum
|
This page was generated in 0.2188 seconds. |