Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Created October 17, 2014 13:57
Show Gist options
  • Save EdgeCaseBerg/6da876d965bc57a33cbc to your computer and use it in GitHub Desktop.
Save EdgeCaseBerg/6da876d965bc57a33cbc to your computer and use it in GitHub Desktop.
A version of the 21 game without proper error checking or rule checking.
#include <stdio.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 * player2Turn = "It is player 2's turn";
const char * player1Win = "Player 1 wins!";
const char * player2Win = "Player 2 wins!";
const char * sticksLeftPrefixText = "There are ";
const char * sticksLeftSuffixText = " sticks left";
int main(){
cout << welcometext << endl;
int sticks = 21;
int currentPlayer = 1;
int continuePlaying = true;
int sticksToTake = 0;
while(continuePlaying){
cout << sticksLeftPrefixText << sticks << sticksLeftSuffixText << endl;
cout << (currentPlayer % 2 == 0 ? player1Turn : player2Turn) << endl;
cin >> sticksToTake;
sticks = sticks - sticksToTake;
if(sticks <= 0){
continuePlaying = false;
}
currentPlayer++;
}
cout << (currentPlayer % 2 == 0 ? player1Win : player2Win) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment