A version of the 21 game without proper error checking or rule checking.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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