初步实现了本人从面向过程到面向对象编程的转变 :)
程序功能:可以玩N回合(bout),每回合(bout)只要未分胜负就可以继续玩下去,或中途退出。
crashGame.h
/***************************
* 一個簡單的賭博遊戲,遊戲規則如下:
* 玩家擲兩個骰子,點數為1到6,
* 如果第一次點數和為7或11,則玩家勝,
* 如果點數和為2、3或12,則玩家輸,
* 如果和為其它點數,則記錄第一次的點數和,
* 然後繼續擲骰,直至點數和等於第一次擲出的點數和,則玩家勝,
* 如果在這之前擲出了點數和為7,則玩家輸。
*
* Write by Tayii, 20080620
*
****************************/
#include <iostream>
using std::cin ;
using std::cout ;
using std::endl ;
class
CrashGame
{
public:
CrashGame() ; // constructor
void crashMain() ; // main
void crash() ; // one total running
void rollFirst() ; // roll first time
void rollAgain() ; // roll after first time
void showResult(); // after current bout over, show the total result
unsigned gainPoint() ;
void queryPlayAgain() ; // query & set playAgain
// void setGameOver( bool ) ;
// bool getGameOver() ; // get gameOver
// char getPlayAgain() ; // get playAgain
private:
unsigned winCount ; // win times in toal bout times
unsigned lossCount ; // loss times in toal bout times
unsigned myPoint ; // gameplayer's point after one turn
char playAgain ; // query, indicate to call roolAgain() or not
bool gameOver ; // flag, result of program running, lead to call queryPlayAgain() or not
unsigned rollTimes ; // loop times i.e. roll times of this bout
unsigned boutTimes ; // total bout times
};
crashGame.cpp
#include <iostream> using std::cin ; using std::cout ; using std::endl ; #include <ctime> using std::time ; #include <cstdlib> using std::rand ; using std::srand ; #include <iomanip> using std::setw ; #include "crashGame.h" ; CrashGame::CrashGame() // constructor { winCount = 0 ; lossCount = 0 ; gameOver = 0 ; rollTimes = 1 ; //beacuse the argument is used in rollAgain() boutTimes = 0 ; } // main running void CrashGame::crashMain() { char answer ; do { cout << "Do you wanna playing CrashGame ? (Y/N) " ; cin >> answer ; } while ( ( 'y' != answer ) && ( 'Y' != answer ) && ( 'n' != answer ) && ( 'N' != answer ) ) ; cout << endl ; while ( ( 'y' == answer ) || ( 'Y' == answer ) ) { ++boutTimes ; cout << setw( 40 ) << "Play bout: " << boutTimes << endl ; crash() ; // run Crash() one time showResult() ; do { cout << "/nDo you wanna playing CrashGame again ? (Y/N) " ; cin >> answer ; } while ( ( 'y' != answer ) && ( 'Y' != answer ) && ( 'n' != answer ) && ( 'N' != answer ) ) ; cout << endl ; } cout << "GAME OVER!" << endl ; } //runnig crashGame once whole void CrashGame::crash() { srand ( time( 0 ) ) ; rollFirst() ; while ( !gameOver ) { queryPlayAgain() ; if ( !gameOver ) rollAgain() ; } gameOver = 0 ; } // roll first void CrashGame::rollFirst() { myPoint = gainPoint() ; // first time sum of 2 points if ( ( 7 == myPoint ) || ( 11 == myPoint ) ) { ++winCount ; cout << "You win this bout in the 1st time ! " << endl ; gameOver = 1 ; } else if ( ( 2 == myPoint ) || ( 3 == myPoint ) || ( 12 == myPoint ) ) { ++lossCount ; cout << "You loss this bout in the 1st time ! " << endl ; gameOver = 1 ; } else cout << "No win or loss!" << endl ; } void CrashGame::rollAgain() { unsigned sum_point ; // 2+ times sum of 2 points ++rollTimes ; // loop times add sum_point = gainPoint() ; if ( 7 == sum_point ) { ++lossCount ; cout << "You loss this bout in the " << rollTimes << "th time ! " << endl ; gameOver = 1 ; } else if ( myPoint == sum_point ) { ++winCount ; cout << "You win this bout in the " << rollTimes << "th time ! " << endl ; gameOver = 1 ; } else cout << "No win or loss!" << endl ; } void CrashGame::showResult() { cout << "/nIt's the rout " << boutTimes << " : /n/n" << setw( 15 ) << "Win" << setw( 15 ) << "Loss/n" << setw( 15 ) << winCount << setw( 15 ) << lossCount << endl ; } // gain sum of 2 dices unsigned CrashGame::gainPoint() { unsigned dice1, dice2 ; unsigned sumPoint ; dice1 = ( 1 + rand() % 6 ) ; dice2 = ( 1 + rand() % 6 ) ; sumPoint = dice1 + dice2 ; cout << "Your current Point = " << sumPoint << " , ( " << dice1 << " + " << dice2 << " )." << endl ; return sumPoint ; } void CrashGame::queryPlayAgain() { do { cout << "Continue playing ? (Y/N) " ; cin >> playAgain ; } while ( ( 'y' != playAgain ) && ( 'Y' != playAgain ) && ( 'n' != playAgain ) && ( 'N' != playAgain ) ) ; cout << endl ; if ( ( 'n' == playAgain ) || ( 'N' == playAgain ) ) { cout << "You stop playing." << endl ; gameOver = 1 ; } } /* void CrashGame::setGameOver( bool aa ) { gameOver = aa ; } bool CrashGame::getGameOver() { return gameOver ; } char CrashGame::getPlayAgain() { return playAgain ; } */
crashGame_Main.cpp
#include <iostream> using std::cout ; using std::endl ; using std::cin ; #include "crashGame.h" int main() { int wait ; CrashGame crash1 ; crash1.crashMain() ; cin >> wait ; return 0 ; }