CRASH 一个赌博小游戏的编写
来源:互联网 发布:强国人 知乎 编辑:程序博客网 时间:2024/04/30 04:30
初步实现了本人从面向过程到面向对象编程的转变 :)
程序功能:可以玩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 ;classCrashGame{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 runningvoid 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 wholevoidCrashGame::crash(){ srand ( time( 0 ) ) ; rollFirst() ; while ( !gameOver ) { queryPlayAgain() ; if ( !gameOver ) rollAgain() ; } gameOver = 0 ;}// roll firstvoidCrashGame::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 ;}voidCrashGame::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 dicesunsigned 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 ;}voidCrashGame::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 ; }}/*voidCrashGame::setGameOver( bool aa ){ gameOver = aa ;}boolCrashGame::getGameOver(){ return gameOver ;}charCrashGame::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 ;}
- CRASH 一个赌博小游戏的编写
- ios 自己编写的一个弹球小游戏
- 用processing编写的一个小游戏
- Pygame:编写一个小游戏
- ABAP编写的小游戏
- HTML5编写的小游戏
- HTML5编写的小游戏
- 一个好玩的小游戏(纯C语言编写)
- 【扫雷】编写一个小游戏--扫雷的c语言实现
- HTML5编写的小游戏集锦
- Java编写的五子棋小游戏
- c语言编写的小游戏
- #Python 初学之二编写一个小游戏#
- 一个简单的小游戏
- 一个C的小游戏
- 一个html5的小游戏
- 一个自制的小游戏
- 一个赌博面试题的研究(未解答出来)
- 六月十九
- 马云为什么收购 PHPWIND?
- oracle 查询数据库重复的字段
- 累了--一个女程序员的心酸和无奈
- 这道面试题貌似很简单噢!
- CRASH 一个赌博小游戏的编写
- 安装Microsoft SQL Server 2005出现COM+出错问题及解决
- JavaScript使用技巧精萃
- 手机屏幕 and 分辨率
- 备忘:用Visual C++编写DLL
- 2003系统上传文件不能超过200K的解决方案
- 运放“虚短"
- VC++中操作word
- 2008-6-20