Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <time.h>
  4. #include <iomanip>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int drawL = 60, discL = 0;
  10.  
  11. void swap(int& a, int& b) {
  12.         int temp = a;
  13.         a = b;
  14.         b = temp;
  15. }
  16.  
  17. void assignCards(int drawPile[], int rack[]) {
  18.         for (int i = 0; i < 10; i++) {
  19.                 rack[i] = drawPile[drawL - 1];
  20.                 drawL--;
  21.         }
  22. }
  23.  
  24. void arrayShuffle(int array[]) {
  25.         srand (time(NULL));
  26.         for (int i = drawL - 1; i > 0; i--) {
  27.                 int q = rand() % (i + 1);
  28.                 swap(array[i], array[q]);
  29.         }
  30. }
  31.  
  32.  
  33. void arrayAssign(int arrayA[]) {
  34.         for (int i = 0; i < drawL; i++) {
  35.                 arrayA[i] = i + 1;
  36.         }
  37. }
  38.  
  39. void printCards(int rack[]){
  40.         for (int i = 0; i < 10; i++) {
  41.                 cout << "(" << rack[i] << "," << i << ")" << setw(3);
  42.         }
  43.         cout << endl;
  44. }
  45.  
  46. bool sortCheck(int rack[]) {
  47.         for (int i = 1; i < 10; i ++) {
  48.                 if (rack[i] < rack[i - 1]) {
  49.                         return false;
  50.                 }
  51.         }
  52.         return true;
  53. }
  54.  
  55. int turn(int deck[], int rack[], int disc[], char resp) {
  56.         int inp;
  57.         cout << endl << "-------------------------------" << endl << endl;
  58.         cout << "Player "<< resp << " has:"<<endl;
  59.         printCards(rack);
  60.         cout << endl << "Where do you want to draw from?" << endl;
  61.         cout << "1) Discard Pile (" << disc[discL] << ")" << endl;
  62.         cout << "2) Deck ???" << endl;
  63.         cin >> inp;
  64.         if (inp == 1) {
  65.                 cout << "What position card do you want to replace this with?" << endl;
  66.                 cout << "Input the position of the card you want to get rid of: ";
  67.                 cin >> inp;
  68.                 if (inp > 10 || inp < 0) {
  69.                         cout << "Error Input" << endl;
  70.                         exit(0);
  71.                 }
  72.                 else {
  73.                         int temp = rack[inp];
  74.                         rack[inp] = disc[discL];
  75.                         discL--;
  76.                         if (discL < 0) {
  77.                                 cout << "The game is tied because there are no more cards in the discard pile." << endl;
  78.                                 exit(0);
  79.                         }
  80.                         else {
  81.                                 discL++;
  82.                                 disc[discL] = temp;
  83.                                 bool d = sortCheck(rack);
  84.                                 if (d) {
  85.                                         return resp;
  86.                                 }
  87.                         }
  88.                 }
  89.         }
  90.         else if (inp == 2) {
  91.                 char rep;
  92.                 cout << "You drew a: " << deck[drawL] << endl;
  93.                 cout << "Would you like to replace it with one from the rack?(y/n)" << endl;
  94.                 cin >> rep;
  95.                 if (rep == 'y') {
  96.                         cout << "Input the position of the card you want to get rid of: ";
  97.                         cin >> inp;
  98.                         int temp = rack[inp];
  99.                         rack[inp] = deck[drawL];
  100.                         drawL--;
  101.                         if (drawL < 0){
  102.                                 cout << "The game is tied because there are no more cards in the draw pile.";
  103.                                 exit(0);
  104.                         }
  105.                         else {
  106.                                 discL++;
  107.                                 disc[discL] = temp;
  108.                                 cout << "Print: " << disc[discL] << endl;
  109.                                 bool d = sortCheck(rack);
  110.                                 cout << d;
  111.                                 if (d) {
  112.                                         return resp;
  113.                                 }
  114.                         }
  115.                 }
  116.                 else if (rep == 'n') {
  117.                         discL++;
  118.                         disc[discL] = deck[drawL];
  119.                         drawL--;
  120.                 }
  121.                 else {
  122.                         cout << "Error input";
  123.                         exit(0);
  124.                 }
  125.         }
  126.         else {
  127.                 cout << "Error input";
  128.                 exit(0);
  129.         }
  130.         return 'n';
  131. }
  132.  
  133. int main() {
  134.         int deck[drawL], rackA[10], rackB[10], discard[drawL];
  135.         char ret = 'n';
  136.         arrayAssign(deck);
  137.         arrayShuffle(deck);
  138.         assignCards(deck, rackA);
  139.         assignCards(deck, rackB);
  140.         discard[discL] = deck[drawL];
  141.         //discL++;
  142.         drawL++;
  143.         cout << endl << "--- Welcome to Rack-0 ---" << endl;
  144.         cout << "Find another player and assign teams A or B" << endl;
  145.         cout << "(number on the card , position of the card in the rack)" << endl;
  146.         while (ret == 'n'){
  147.                 ret = turn(deck, rackA, discard, 'A');
  148.                 if (ret == 'A') {
  149.                         cout << "Player A wins" << endl;
  150.                         exit(0);
  151.                 }
  152.                 ret = turn(deck, rackB, discard, 'B');
  153.                 if (ret == 'B') {
  154.                         cout << "Player B wins" << endl;
  155.                         exit(0);
  156.                 }
  157.         }
  158.         return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement