Relive the C + +---dice game---shinepans

Source: Internet
Author: User
Tags random seed sleep function

This was a program witch played by two people//two people game, if the first throw dice, throw two times and for 7 or 11 then the first person direct victory, the second person direct failure//If the first person throws the dice, throw two times and 2, 3 or 12, Then the first person directly fails, the second person wins//if the first person throws a sieve, throws two times and above are not, then by the second person throws, until one party wins # include <iostream> #include <stdlib.h> #include <ctime>//Get the system time header file # include <windows.h>//sleep function # include <conio.h>//_kbhit get keyboard percussion using namespace     Std;int Rolldice (); function prototype void Setseed (), void PlayGame (), int rolldice ()//throw dice function {int die1 = 1 + rand ()% 6;int Die2 = 1 + rand ()% 6;int sum = Die1 + die2;cout << "Player rooled" << die1 << "+" << die2 << "=" << sum << E Ndl;return sum;}  void Setseed ()//Set the SEED function to use system time to set the random seed {srand ((unsigned) times (NULL));  Get system Time settings seed}enum gamestatus{win,lose,playing};   Enumerates game states; void PlayGame () {int SumA = 0, Sumb = 0, game = 1;  The game is a flag, and when it is 1 o'clock continuously throws gamestatus status = playing;while (status = = PLAYING) {setseed (); Set seed cout << "please aaaaaaaa throw: \ n (Press any key to start throwing, press any key to end the throw) \ n"; system ("pause"); while (game) {SumA = Rolldice (); SleeP (+); if (_kbhit ()) {game = 0;}} Game = 1;if (SumA = = 7 | | sumA = =) {status = Win;cout << "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa won" << Endl ;} else if (SumA = = 2 | | sumA = = 3 | | sumA = =) {status = Lose;cout << "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB won" &lt ;< Endl;} Else{cout << "Please bbbbbbbbbb throw: \ n (Press any key to start throwing, press any key to end the throw) \ n"; system ("pause"); while (game) {Sleep]; sumb = Rolldice () ; if (_kbhit ()) {game = 0;}} Game = 1;if (Sumb = = 7 | | sumb = =) {status = Win;cout << "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB won" << Endl;} else if (Sumb = = 2 | | sumb = = 3 | | sumb = =) {status = Win;cout << "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa won" & lt;< Endl;} Else  Do nothing; Continue next round}}}int main () {char x; PlayGame (); System ("pause"); cout << "Do you want another plate?" \ n "<<" (y/n): \ n "<< endl;while (1) {if (_kbhit ()) {x = _getch (); if ((x = = ' Y ') | | (x = = ' Y ')) {PlayGame ();} if ((x = = ' n ') | | (x = = ' N ')) {System ("pause"); return 0;}}} return 0;}

Relive the C + +---dice game---shinepans

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.