-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
101 lines (94 loc) · 3.09 KB
/
Main.cpp
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
//George Vargas
//CECS 282-07
//Prog 1-Solitaire Prime
//Feb 12, 2020
#include <iostream>
#include "Card.h"
#include "Deck.h"
using namespace std;
/**
*Create your own function called "isPrime" (cannot use C++ library function). One is not a prime number.
*/
bool isPrime(int num) {
if (num <= 1)
return false;
for (int i = 2; i < num; i++)
if (num % i == 0)
return false;
return true;
}
/*
*This function deals out the cards and checks if the piles are prime.
*/
void play(Deck deck) {
Card card;
int sum = 0;
int piles = 0;
while (deck.cardsLeft()!= 0) {
//Take the top card from the deck.
card = deck.deal();
//Display the card using 2 feilds.
card.showCard();
//This returns the point value of the card and adds them into sum.
sum += card.getValue();
//If the sum is not prime, take the next card from the top of the deck.
//When you aquire a Prime pile, print out the value (prime number) and start over on the next line.
if (isPrime(sum)) {
cout << "Prime: " << sum << "\n";
sum = 0;
piles++;
}
//If the last card from the deck gives you a prime pile, then you win.
if (deck.cardsLeft() == 0 && sum == 0)
cout << "\nWinner in " << piles << " piles!\n";
//If the last card from the deck does not give you a prime pile, then you lose.
if (deck.cardsLeft() == 0 && sum != 0)
cout << "Loser\n";
}
cout << "There are no more cards to deal, start a new deck.\n";
}
/**
*The main runs the entire program.
*/
int main() {
int choice=0;
Deck deck;
while (choice!=5) {
cout << "\nWelcome to Solitaire Prime!\n1) New Deck\n2) Display Deck\n3) Shuffle Deck"
"\n4) Play Solitaire Prime\n5) Exit\n";
cin >> choice;
//This will create an unshuffled deck in the following order: Spades, Hearts, Diamonds, Clubs...Ace, 2, 3,..., 10, Jack, Queen, King.
if (choice==1) {
cout << "\nThe deck of cards has been reorganized.\n";
deck.refreshDeck();
}
//This will display all cards in a grid: 13 columns by 4 rows.
else if (choice == 2) {
cout << "\nThe game's current deck of cards.\n";
deck.showDeck();
}
//This will randomly shuffle all the cards in the deck.
else if (choice == 3) {
cout << "\nThe deck of cards has been shuffled.\n";
deck.shuffle();
}
//This will play the game as described above.
else if (choice == 4) {
cout << "\nYour deck of cards.\n";
deck.showDeck();
cout << "\nYour hand has been dealt, goodluck!\n";
play(deck);
}
//This will exit the program.
else if (choice == 5) {
cout << "\nThank you for playing Solitaire Prime!\n";
}
else {
cout << "Invalid choice, please enter 1 through 5. \n";
cin.clear();
cin.ignore(10000, '\n');
}
}
getchar();
return 0;
}