1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
// Game of Pig.cpp : main project file.
// Pigdice.cpp : the dice game of pig.
// Author: Ken Thompson
//Last modified: 9/13/12
#include "stdafx.h"
#include <string>
#include <conio.h>
#include <iostream>
#include <ctime>
#include <Windows.h>
using namespace std;
int rollDice();
char playerRollsOrHolds();
char playAgain();
int playerTurn(int playerTotalScore, string playername);
int computerTurn(int computerTotalScore);
void ClearScreen();
int main()
{
time_t t;
srand((unsigned) time(&t));
char keepPlaying = 'X';
int playerCurrentScore=0;
int computerCurrentScore=0;
int playerTotalScore=0;
int computerTotalScore=0;
string playername = "Human";
string Line(80,'_');
string Pig(80,'=');
char playGame;
char playAgain();
cout << "What is your name? \n";
cin >> playername;
cout << "Shall we play a game " << playername << "? Y or N :\n";
cin >> playGame;
playGame = toupper(playGame);
if (playGame == 'N')
{
cout << "You chose not to play today. Goodbye " << playername << endl;
_getch();
return 0;
}
ClearScreen();
cout << " Game of Pig \n" ;
cout << "================================================================== \n" ;
cout << "The goal of Pig is to reach 100 points or more before the other player does.\n";
cout << "Each time you roll the playerRoll points are added to your turn total.\n";
cout << "If you roll a 2-6, then you can choose to roll again or hold \n";
cout << "If you hold, then the points are added to your score.\n";
cout << "But if you roll a 1, then all your points for that turn are lost!\n";
cout << endl;
cout << endl;
cout << "Press Enter to continue.\n";
_getch();
do
{
do
{
ClearScreen();
cout << " Game of Pig \n";
cout << Pig;
cout << Line << endl << "\t\t" << playername << "'s total score = "
<< playerTotalScore << "\tComputer's total score = " << computerTotalScore
<< endl << Line << endl;
if (computerTotalScore<100)
playerTotalScore=playerTurn(playerTotalScore, playername);
if (playerTotalScore<100)
computerTotalScore=computerTurn(computerTotalScore);
}while (playerTotalScore < 100 && computerTotalScore < 100);
if (playerTotalScore>=100 && computerTotalScore<=99)
{
cout << " Congratulations " << playername << ", you win!" << endl;
}
if (playerTotalScore<=99 && computerTotalScore>=100)
{
cout << "Sorry " << playername << ", you lost." << endl;
}
keepPlaying = playAgain();
if ( keepPlaying == 'N')
{
cout << Line << endl;
cout << "A strange game. The only winning move is not to play." << endl;
cout << "How about a nice game of chess?" << endl;
cout << Line << endl;
_getch();
}
playerTotalScore = 0;
computerTotalScore = 0;
}while (keepPlaying == 'Y');
}
int playerTurn(int playerTotalScore, string playername)
{
int playerCurrentScore =0;
int rollDice();
char playerResponse;
do
{
playerResponse = 'R';
int rollDice();
int playerRoll = rollDice();
cout << playername << " rolled a " << playerRoll << endl;
if (playerRoll == 1)
{
cout << playername << "'s score for this round was lost." << endl;
playerCurrentScore = 0;
break;
}
else
{
playerCurrentScore += playerRoll;
cout << playername << "'s current total : " << playerCurrentScore << ". \n";
playerResponse = playerRollsOrHolds();
if (playerResponse != 'R')
{
playerTotalScore+=playerCurrentScore;
cout << endl << playername << "'s total score : " << playerTotalScore << endl;
break;
}
else
continue;
}
} while (playerResponse == 'R');
return playerTotalScore;
}
int computerTurn(int computerTotalScore)
{
int computerCurrentScore=0;
do
{
int ComputerRoll = rollDice();
cout << "Computer rolled a " << ComputerRoll << ". \n";
Sleep(600); // Small delay so you watch screen.
if (ComputerRoll == 1)
{
cout << endl << "Computer's score for this round was lost." << endl;
computerCurrentScore = 0;
break;
}
else
{
computerCurrentScore+= ComputerRoll;
cout << endl << "Computer's current total is " << computerCurrentScore << endl;
if (computerCurrentScore>=20)
{
computerTotalScore+=computerCurrentScore;
cout << endl << "Computer chose hold. Computer's total score : " << computerTotalScore << endl;
}
}
}
while (computerCurrentScore<=19);
{
return computerTotalScore;
}
}
//Functions
int rollDice()
{
return rand()%6+1;
}
char playerRollsOrHolds()
{
char playerResponse;
cout << endl << "Would you like to Roll or Hold (R or H): ";
do
{
cin >> playerResponse;
playerResponse = toupper(playerResponse);
if (playerResponse!='R' && playerResponse != 'H')
cout << "Enter an 'R' or an 'H', only" << endl << endl;
} while ( playerResponse !='R' && playerResponse != 'H');
return playerResponse;
}
char playAgain()
{
char keepPlaying;
cout << "Would you like to play again? (y or n) ";
do
{
cin >> keepPlaying;
keepPlaying = toupper(keepPlaying);
if (keepPlaying!='R' && keepPlaying != 'H')
cout << "Enter only a 'Y' or an 'N', please!" << endl << endl;
} while ( keepPlaying !='Y' && keepPlaying != 'N');
return keepPlaying;
}
void ClearScreen()
{
DWORD n;
DWORD size;
COORD coord = {0};
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
size = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
SetConsoleCursorPosition ( h, coord );
}
|