Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdgeCaseBerg/c97d64f01eb7b29256eb to your computer and use it in GitHub Desktop.
Save EdgeCaseBerg/c97d64f01eb7b29256eb to your computer and use it in GitHub Desktop.
This is a version of the 21 game with error checking, a computer player, scoreboard, and the ability to play multiple times.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
const char * welcometext =
"Welcome to the game of 21! \n\
Rules: \n\
First choose who goes first, the game starts with 21 sticks in\n\
a pile, the player forced to take the last stick loses. \n\
At each turn, you can remove up to 3 sticks.";
const char * player1Turn = "It is player 1's turn";
const char * computerTurn = "It the computer's turn";
const char * player1Win = "Player 1 wins!";
const char * computerWin = "The Computer wins!";
const char * sticksLeftPrefixText = "There are ";
const char * sticksLeftSuffixText = " sticks left";
const char * errorInput = "Invalid input! Please enter a numeric value";
const char * errorBounds = "Invalid input! Please enter a number [1-3]";
const char * getFirstPlayerError = "Please enter h or c";
const char * yesNoError = "Please enter y or n";
//Note that you will lose everytime if the computer goes first
int ai(int sticks, int lastmove){
// +[1-3] = sticks % 4 == 0 ?
int toTake = 1;
for (int i = 1; i < 4; ++i){
if((lastmove + i) % 4 == 0){
return i;
}
}
return toTake;
}
int chooseFirstPlayer(){
string input;
cout << "Who will go first? [h]uman or [c]omputer:" << endl;
do {
getline(cin, input);
if(input.length() == 0){
cout << getFirstPlayerError << endl;
continue;
}
if(input[0] == 'h' || input[0] == 'H'){
return 2;
}
if(input[0] == 'c' || input[0] == 'C'){
return 1;
}
cout << getFirstPlayerError << endl;
}while(1);
}
int humanTurn(){
string input;
int sticksToTake = 1;
do{
cout << "How many sticks will you take?: ";
getline(cin, input);
if(input.length() != 1){
cout << "Please enter 1 numeric value" << endl;
continue;
}
int sticksToTake = input[0] - '0';
if(!(0 < sticksToTake && sticksToTake <= 3)){
cout << errorBounds << endl;
}else{
break;
}
}while(1);
return sticksToTake;
}
int playAgain(){
string input;
cout << "Would you like to play again? [y]es or [n]o: " << endl;
do {
getline(cin, input);
if(input.length() == 0){
cout << yesNoError << endl;
continue;
}
if(input[0] == 'y' || input[0] == 'Y'){
return true;
}
if(input[0] == 'n' || input[0] == 'N'){
return false;
}
cout << yesNoError << endl;
}while(1);
}
int main(){
cout << welcometext << endl;
int sticks = 21;
int currentPlayer = 2;
int continuePlaying = true;
int userWantsToPlay = true;
int sticksToTake = 0;
int validInput = false;
string input;
int userScore = 0;
int computerScore = 0;
do{
currentPlayer = chooseFirstPlayer();
while(continuePlaying){
cout << "There " << (sticks == 1 ? "is " : "are ") << sticks << " stick" << (sticks == 1 ? " " : "s ") << "left" << endl;
cout << (currentPlayer % 2 == 0 ? player1Turn : computerTurn) << endl;
if(currentPlayer % 2 == 0){
sticksToTake = humanTurn();
}else{
sticksToTake = ai(sticks, sticksToTake);
cout << "Computer takes " << sticksToTake << " stick" << (sticksToTake == 1 ? "" : "s") << endl;
}
sticks = sticks - sticksToTake;
if(sticks <= 0){
continuePlaying = false;
}
currentPlayer++;
}
cout << (currentPlayer % 2 == 0 ? player1Win : computerWin) << endl;
if( currentPlayer % 2 == 0 ){
userScore++;
}else{
computerScore++;
}
cout << "You've won " << userScore << " games to the computers " << computerScore << endl;
if(userWantsToPlay = playAgain()){
//Reset Game parameters
sticks = 21;
sticksToTake = 0;
continuePlaying = true;
}
}while(userWantsToPlay);
cout << "Thanks for playing!" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment