Skip to content
This repository was archived by the owner on May 17, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 58 additions & 59 deletions ActualChips.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <iostream>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>

using std::string;
using std::ofstream;
using std::cout;
using std::cin;
using std::ios;
using std::cout;
using std::endl;
using std::ios;
using std::ofstream;
using std::string;

const float MAX_TURN = .5;
const int MAX_CHIPS = 100;
Expand Down Expand Up @@ -38,43 +38,42 @@ int main()
char userChoice;
string playerNames[2];

//Output file variables
//Output file variables
ofstream outFile;
outFile.open("Winners.txt", ios::app);

//seed the random number generator
srand(time(0));

//ask the players for their names, then store in an array
getUserNames(playerNames);
player1.name = playerNames[0];
player2.name = playerNames[1];
player1.numWins = 0;
player2.numWins = 0;


//start the game with a random number of chips in the pile
do
{
chipsInPile = (rand() % MAX_CHIPS) + 1;
chipsInPile = (rand() % MAX_CHIPS) + 1;

cout << "This round will start with " << chipsInPile << " chips in the pile\n";
cout << "This round will start with " << chipsInPile << " chips in the pile\n";
gameOver = false;
moveCounter = 0;
while (gameOver == false)
{
{
chipsTaken = askMove(player1Turn, chipsInPile, playerNames);
chipsInPile = chipsInPile - chipsTaken;
cout << "There are " << chipsInPile << " left in the pile\n";
player1Turn = !player1Turn;
moveCounter++;
if (chipsInPile == 0)
{
gameOver = true;
cout << FindPlayerName(playerNames, player1Turn) << ", congratulations you won" << endl;
outFile << FindPlayerName(playerNames, player1Turn) << " won in " << moveCounter << " moves" << endl;
}
}
chipsInPile = chipsInPile - chipsTaken;
cout << "There are " << chipsInPile << " left in the pile\n";
player1Turn = !player1Turn;
moveCounter++;
if (chipsInPile == 0)
{
gameOver = true;
cout << FindPlayerName(playerNames, player1Turn) << ", congratulations you won" << endl;
outFile << FindPlayerName(playerNames, player1Turn) << " won in " << moveCounter << " moves" << endl;
}
}
if (FindPlayerName(playerNames, player1Turn) == playerNames[0])
{
player1.numWins = player1.numWins + 1;
Expand All @@ -83,15 +82,15 @@ int main()
{
player2.numWins = player2.numWins + 1;
}
cout << "\nDo you wish to play again? (Y/N)" << endl;
cout << "\nDo you wish to play again? (Y/N)" << endl;
cin >> userChoice;
userChoice = toupper(userChoice);
if (userChoice == 'N')
{
cout << player1.name << " had " << player1.numWins << " total wins this round" << endl;
cout << player2.name << " had " << player2.numWins << " total wins this round" << endl;
}
}while ( userChoice == 'Y');
} while (userChoice == 'Y');
outFile.close();
}

Expand All @@ -108,44 +107,44 @@ void getUserNames(string players[])

string FindPlayerName(string names[], bool playerTurn)
{
if (playerTurn == true)
return names[0];
else
return names[1];
if (playerTurn == true)
return names[0];
else
return names[1];
}

int askMove(bool player1Turn, int chipsInPile, string names[])
{
int chipsTaken;
int maxPerTurn = MAX_TURN * chipsInPile;
do
int chipsTaken;
int maxPerTurn = MAX_TURN * chipsInPile;
do
{
cout << FindPlayerName(names, player1Turn) << " How many chips would you like?\n";

cout << "You can take up to ";
if (( maxPerTurn ) == 0)
{
cout << " 1\n";
}
else
{
cout << maxPerTurn << endl;
}
if (FindPlayerName(names, player1Turn) == "AI")
cout << FindPlayerName(names, player1Turn) << " How many chips would you like?\n";

cout << "You can take up to ";
if ((maxPerTurn) == 0)
{
if (maxPerTurn == 0)
{
chipsTaken = 1;
}
else
{
chipsTaken = (rand() % maxPerTurn) + 1;
}
}
else
{
cin >> chipsTaken;
}
}while ((chipsTaken > maxPerTurn ) && (chipsInPile > 1));
cout << " 1\n";
}
else
{
cout << maxPerTurn << endl;
}
if (FindPlayerName(names, player1Turn) == "AI")
{
if (maxPerTurn == 0)
{
chipsTaken = 1;
}
else
{
chipsTaken = (rand() % maxPerTurn) + 1;
}
}
else
{
cin >> chipsTaken;
}
} while ((chipsTaken > maxPerTurn) && (chipsInPile > 1));
return chipsTaken;
}
}
Binary file modified ActualChips.exe
Binary file not shown.
Empty file removed Winners.txt
Empty file.