/*第1题 扑克牌游戏--源代码及关键源代码注解如下:*/
//* This Program was written entirely by the author Frank Vokoun.
//*******************preprocessor directives***********************
//
#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>      // used to seed the random number generator
//
/
/***********************************************************************
class Playing_Card    //扑克类
{
private:
int m_Value;
char m_Face[3];  //扑克的数字大小
char m_Suit ;    //扑克的花(黑、红、梅、方)
public:
Playing_Card();        // CONSTRUCTOR
void showcard();      // Displays an object of class Playing_Card
void Card_Make(int);
};
class Deck //一副扑克(52张)         
{
private:
Playing_Card m_cardarray[52];    // Object array of class Playing_Card
int m_lastdelt;
public:
Deck();      // CONSTRUCTOR initializes empty Deck object
void MakeDeck() ;    // makes a complete object of class Deck
void Deal_One();      // Deals a card from top of the deck
void ShowDeck();      // Displays a Deck object to the  screen
void DeckShuff(int);    // "Shuffles" a Deck object for int times
void Remove_Card();    // Removes used card from Deck Prevents
};          //  mistakenly using a nonexistant card
//  can bu used to send a card ??
//***************************prototypes**********************************
void Program_Init();    // Program information
int main();
void Center_Text(char []);    // Centers text
int get_number();      // Gets a integer from user
char Get_Key();      // Gets a char from user, pauses program
void DeckMakeDriver();    // Tests Several Functions including Makedeck
int getRandInt(int min, int max);      // Random number generator
void prog_close();      // pauses before program termination
//****************************Main*************************************
int main()
//**************************************************************************
//* int main:  Main Function Calls other functions
//*
//* expects:  None.
//* Returns:  Integer 0;
//* Side effects  None
//*     
/
/* Tasks    (1) Seeds the Random number generator
//*    (2) Calls Program Init function for Title etc.
//*    (3) Call DeckMakeDriver to Test MakeDeck and DeckShuff.
//*
//***********************************************************************
{
srand( (unsigned)time( NULL ) );      // Seeds GetRandInt 
int Card_Number = 0;
Program_Init();          // Showd title etc.
DeckMakeDriver();    // The Main Function Driver Tests Deck and
//  Playing_Card classes
prog_close();      // pauses screen output     
return 1;       
}
Playing_Card::Playing_Card()      // CONSTRUCTOR
//**********************************************************************
****
//* Playing_Card Playing_Card CONSTRUCTOR FOR CL
ASS Playing_Card
//*
//* expects:  None.
//* Returns:  None
/
/* Side effects  None
//*     
//* Tasks    Constructs object of class playing_card replaces default constructor
//*********************************************************************    // Constructor replaces default constructor
{
int i;
for(i=1;i<=3;)    // inits string variable to blanks
{
m_Face[i] = ' ';
i++;
}
m_Suit = ' ';    // inits char variable to blank
m_Value = 0;
}
void Program_Init()
//*************************************************************************
//* void Program_Init():Shows Title
//* 
//* expects  None
//* returns:  None
//* Side Effects:  Text is centered on screen
//*
/
/* Task(s)  Output program information to screen.
//*
//***********************************************************************
{
Center_Text("Lab #5");
cout << endl;
Center_Text("A Deck Driver");
cout << endl <<"\n" ;
Center_Text("By Frank Vokoun");
cout << endl <<"\n" ;
Center_Text("(C) 2001, Frank Vokoun");
cout << endl <<"\n" ;
Center_Text("Hit the <Return> key to Continue..");
<();
}
char Get_Key()
//***********************************************************************
//* char Get_Key():  Uses a input prompt to get a char
//* 
//* expects:  None.
//* Returns:  Input char.
//* Side effects:  Pauses Program execution (desired effect).
/
/*
//* Task(s):  Gets a char from the user..
//*
//**********************************************************************
{
char x;
x = ();
cout << endl;
return x;
}
void Playing_Card::showcard()
/
/*****************************************************************
//* void showcard(card):Displays a Playing Cards object to the screen
//*
//* Expects:  None -> Uses calling members Playing_Card object
//* Returns:  None
//* Side Effects:      Displays
//*
//* Task(s):  Displays the object of class Playing_Card to screen
//*     
//***********************************************************************
cout << "  ";
cout << m_Face  ;
cout.width(1);
cout <<  m_Suit;
cout << " ";
}
void Center_Text(char ShellText[80])
//**********************************************************************
//*Void Center_Text: Displays text centered on the screen.
//*
//* expects:  The text to display. 
/
/* Returns:  None.
//* Side effects:  Outputs Centered text on screen. 
//*
//*******************************************************************
{
int length;
int center;
length= strlen(ShellText);
center = (80 - length)/2;
for(;center!=0;center--)
{
cputs(" ");
}
cputs(ShellText);
}
int get_number()
//
********************************************************************
//*
//* int get_number:  Gets an integer from the user.
//* expects:  None. 
//* Returns:  Integer.
/
/* Side effects:  None. 
//* Task(s):  Asks the user to enter an integer then returns
that integer.
//*      . 
//**********************************************************************
{
int Input_Integer = 0;
Center_Text("Please enter an integer between 0 and 51. 52 to quit.");
cout << endl;
cin >> Input_Integer;
return Input_Integer;
}
void Playing_Card::Card_Make(int num)
//*********************************************************************
//* Get_Card(int):  Creates a Playing_Card object based on an interger input.
//* Expects:  int 
//* Returns:  None -> uses Calling members Playing_Card object
//* Side Effects:      None
//*
//*Tasks:    1. assign a face value
//*    2. assign a m_Suit(char type)
//*    3. assign a point value
/
/*
//********************************************************************
{
int i = 0;
char j;
int face_num = num % 13;   
switch(face_num)      // Assigns a Face value for string cards
{
case 0: strcpy(m_Face," A");break;
case 9: strcpy(m_Face,"10");break;
case 10: strcpy(m_Face," J");break;
case 11: strcpy(m_Face," Q");break;
case 12: strcpy(m_Face," K");break;
default:
j = char(face_num + 49);  // Fills string with number mod 13
if(i<3)
{
m_Face[i] = ' '; i++;
m_Face[i] = j;i++;
m_Face[i] = NULL;
break;
}
}
if(num <= 12)m_Suit = 6;    // assigns suit use ascii values for
if(num >12 && num <= 25)m_Suit = 3; //  card symbols
if(num >25 && num <= 38)m_Suit = 5;
if(num >38 && num <= 51)m_Suit = 4;
if(face_num <= 9)m_Value  = face_num + 1;  // Card value's Ace = 1
if(face_num >= 10)m_Value = 10;
}
void DeckMakeDriver()
//********************************************************************
//* DeckMakeDriver(): Used to test the various deck functions Deckmake,
/
/*      Deckshuff, Deckcopy etc.
//* Expects:  None.
//* Returns:  None.
//* Side effects:      None
//*
//* Tasks:        1. Call make Deck
//*    2. Show Deck
//*    3. Call shuffle
//*    4. Call show deck
//*    5. While !Done Call
//*      a. Deal one
/
/*      b. Show card
//* 
//*Note the dot operator is need to access object because this is not a member of the class
//**********************************************************************
{
Deck deck_1;
deck_1.MakeDeck();    // Creates the Deck.
deck_1.ShowDeck();    // Displays deck to screen.
Get_Key();      // Pauses Program.
deck_1.DeckShuff(250);    // Shuffles the deck 250 times
deck_1.ShowDeck();    // Displays deck to screen.
cout << endl <<endl << endl;
char y;         
do
{
deck_1.Deal_One();
cout << endl;
cout << endl << endl << "Y to Deal One more N to quit"<< endl;
cin >> y;
y = toupper(y);
}while(y == 'Y' );
}   
void Deck::MakeDeck()      // creates a full deck not a construct
//**************************************************************************
//* Deck Make Deck(): Creates a Deck Object
//* Expects:  none -> uses calling functions deck object
//*
Returns:  none -> uses calling functions Deck object 
//* Side effects:      none
//* Tasks:    1. Create a Full Deck structure of unique card structures
//*      a. have the decks last delt variable = to the top card
//***********************************************************************
{
m_lastdelt = 51;        // Set Deck to empty
while(m_lastdelt >-1)      // iterate until deck is full.
{         
m_cardarray[m_lastdelt].Card_Make(m_lastdelt);  // call card make for every card object
m_lastdelt--;      // inside the deck object ie cardarray   
}         
游戏免费源码分享网站}
void Deck::ShowDeck()
//********************************************************************
//* void ShowDeck:  Displays the deck in a organized readable fashion to the screen
//* Expects:  none shows calling functions object of class deck
/
/* Returns:  none
//* Side Effects:      None
//* Tasks:    1. set an index to lastdealt + 1
//*    2. call ShowCard passing in card[index]
//*********************************************************************
{
int index = m_lastdelt + 1;
int newline = 0;
Center_Text("Current Deck of Cards from top to bottom");
cout << endl;
while(index<=51)
{
if(newline % 11 == 0) cout << endl;
m_cardarray[index].showcard();    // calls showcard for every card in
newline++;        // the cardarray object
index++;
}
}
int getRandInt(int min, int max)
//*********************************************************************
//*int GetRandInt:  Returns a random number between min and max
//*
/
/* BORROWED FUNCTION:Used with permission from Prof. Steve Samuelson.
//*
//* Expects:  min -- smallest possible number to return
//*    max -- largest possible number to return
//* Returns:  A number between min and max
//* Side Effects:      None
//*
//* Tasks:    1. set a num = to random number generator.
//*      2. make num = num % (max - min +1); add min.
//*      3. Return the num
//*******************************************************************
{
int numToReturn;
numToReturn = rand();
numToReturn = numToReturn % (max - min + 1) + min;
return numToReturn;
}
void Deck::DeckShuff( int times)  //洗牌次数
//********************************************************************
//* Deck DeckShuff(Deck, int): Simulates the shuffling of a deck of cards
//* expects:  none -> uses calling functions Deck object
//* Returns:  none -&g
t; manipulates calling functions object of class Deck USING
//*        A THIS POINTER   
//* Side effects:  no unintended effects.
//*
//* Task(s) 1. To randomly arrange the elements of the Deck structure Playing_Card
//*        array.
//*    A. split the Deck structure into two halves at a random location
//*        between 20 and 35.
//*    B. Alternating between the two halves move a random number of
//*      Playing_Card structures to the original deck structure, Until
//*        it is full of cards (52
cards in a deck).* this is also the
//*        number of cards available in the to halves.
//*    C. Since it is a full unused deck set the lastdelt variable to -1
//*     
//*     
//*********************************************************************
{
int x, split;  //split是分开成两部分的位置,如上部分、下部分
Center_Text("Shuffling Deck");
cout << endl;
for(x=0;x<=times;x++)      // iterate input number of times
{
split = getRandInt(20,35);    // Get split location
Deck topdeck;        // Create 2 new unfilled decks
Deck bottomdeck;
int i;
int bottomdeckindex = 1;
int topdeckindex = 1;
for(i=0;i<=split - 1;)     
{
topdeck.m_cardarray[topdeckindex] = this->m_cardarray[i];
topdeckindex++;
i++;
}
for(i=(split);i< 52;)    // move remaining cards to bottom deck
{
bottomdeck.m_cardarray[bottomdeckindex] = this->m_cardarray[i];
bottomdeckindex++;
i++;
}
int deckoutindex = 0;    // set deck to fill's index to zero
int numcardstomovetop;
int numcardstomovebottom;
int j;
int h = 0;
bottomdeckindex = 52 - split;  // set index to num cards in bottom
topdeckindex =split;    // set index to num cards in top
while(deckoutindex <= 51)
{
numcardstomovetop = getRandInt(2,7);
//从上部分抽取的张数,是2-7之间的随机数
numcardstomovebottom = getRandInt(2,7);
// Move a random number of cards(2-7) 
for(j=0;j <=numcardstomovebottom;j++) // from bottomdeck to original deck
{
if(bottomdeckindex > 0)    // check for available cards
{
this->m_cardarray[deckoutindex] = bottomdeck.m_cardarray[bottomdeckindex];
deckoutindex++;
bottomdeckindex--;
}
for(h=0;h<=numcardstomovetop;h++)
// Move a random number of cards (2 to 7)
{  //  from topdeck to original deck.
if((topdeckindex > 0) && (deckoutindex <=52))
/
/ check for available cards
{  //  and slots
this->m_cardarray[deckoutindex]=topdeck.m_cardarray[topdeckindex];
deckoutindex++;
topdeckindex--;
}
}
}
}
}
this->m_lastdelt = -1;    // Return a complete shuffled deck
}
void prog_close()
//*******************************************************************
//* void prog_close: Waits for user input to end
//* Inputs:    None 
//* Returns:  None
//* Side effects:  Output text to screen/waits for user input to 
//*      end program.
//*
//* task(s

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。