Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <random>
  9. #include <ctime>
  10. #include <iomanip>
  11.  
  12. using namespace std;
  13.  
  14. class Card{
  15.  
  16. friend ostream & operator<<(ostream &output, const Card &C){
  17.  
  18. if(C.face && C.rank > 0){
  19.  
  20. if(C.rank == 1){
  21. output << "A";
  22. }
  23. else if (C.rank == 11){
  24. output << "J";
  25. }
  26. else if (C.rank == 12){
  27. output << "Q";
  28. }
  29. else if (C.rank == 13){
  30. output << "K";
  31. }
  32. else{
  33. output << C.rank;
  34. }
  35. }
  36. else{
  37. if (C.rank > 0)
  38. output << "X";
  39. else
  40. output << " ";
  41. }
  42.  
  43. return output;
  44.  
  45. }
  46.  
  47. public:
  48.  
  49.  
  50. Card(int r = 0, bool f = false){
  51. rank = r;
  52. face = f;
  53. }
  54.  
  55. int getRank(){
  56. if(face && (rank>0)){
  57. return rank;
  58. }
  59. else{
  60. return -1;
  61. }
  62. }
  63.  
  64. int getTrueRank(){
  65. return rank;
  66. }
  67.  
  68. void setRank(Card c){
  69. face = c.face;
  70. rank = c.rank;
  71.  
  72. }
  73. void setRank(){
  74. face = false;
  75. rank = 0;
  76. }
  77.  
  78. bool getFace(){
  79. return face;
  80. }
  81. void flip(){
  82. face = true;
  83. }
  84.  
  85.  
  86. private:
  87. int rank;
  88. bool face;
  89. };
  90.  
  91. vector<Card> writeDeck(int decks){
  92. vector<Card> deck;
  93. int repeats = decks*4;
  94. for(int i = 0;i<repeats;i++){
  95. for(int j = 1;j<=13;j++){
  96. Card c =Card(j);
  97. deck.push_back(c);
  98. }
  99. }
  100. random_shuffle(deck.begin(), deck.end());
  101.  
  102. return deck;
  103.  
  104. }
  105. vector<vector<Card>> makeBoard(vector<Card>& deck){
  106. Card t;
  107. vector<vector<Card>> board(5,vector<Card> (10,t));
  108. for(int i = 0;i<4;i++){
  109. for(int j = 0;j<10;j++){
  110. board[i][j] = deck.back();
  111. deck.pop_back();
  112. }
  113.  
  114. }
  115.  
  116. for(int i = 0;i<4;i++){
  117. board[4][i] = deck.back();
  118. deck.pop_back();
  119. }
  120.  
  121. for(int i =4;i<10;i++){
  122. deck.back().flip();
  123. board[4][i] = deck.back();
  124. deck.pop_back();
  125. }
  126.  
  127. board.resize(7,vector<Card>(10,t));
  128. for(int i =0;i<4;i++){
  129. deck.back().flip();
  130. board[5][i] = deck.back();
  131. deck.pop_back();
  132. }
  133.  
  134.  
  135. return board;
  136. }
  137.  
  138.  
  139. void checkAndRemoveMatch(vector<vector<Card>> b){
  140. // Not Tested
  141. bool match = true;
  142. for (int i = 0; i < 10; i ++){
  143. for (int j = 0; j < b.size(); j++){
  144. //when king found
  145.  
  146. if (b[j][i].getRank() == 13){
  147.  
  148. for (int k =j; k < b.size(); k++){
  149.  
  150. if (b[k+1][i].getRank() == 0){
  151. break;
  152. }
  153. if (!(b[k][i].getRank() == b[k+1][i].getRank()+1)){
  154. match = false;
  155. break;
  156. }
  157. }
  158. if (match){
  159. for (int k =b.size()-1; k >j; k--){
  160. b[k][i].setRank();
  161. }
  162. }
  163. }
  164. }
  165.  
  166. }
  167. }
  168.  
  169. bool checkWin(vector<vector<Card>> b, vector<Card> d){
  170. // slightly Tested
  171. bool win = true;
  172. for (int i = 0; i < b.size() -1; i++){
  173. for (int j = 0; j < 10; j++){
  174. if (b[i][j].getRank() != 0){
  175. win = false;
  176. break;
  177. }
  178. }
  179. if (!win){
  180. break;
  181. }
  182. }
  183.  
  184. if (win && d.size() == 0){
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190.  
  191. bool checkLose (vector<vector<Card>> b, vector<Card> d){
  192. // kill me
  193. // Not Tested
  194. // probably wont work, but might
  195. if (!(d.size() > 0)){
  196. bool canMoveSomething = false;
  197. for (int i = 0; i < 10; i++){
  198. for (int j = 0; j < b[i].size(); i++){
  199. bool placeable = false;
  200. bool moveable = true;
  201. if (b[i][j].getFace()){
  202. //card can be seen
  203. if (b[i][j].getRank() == 13){
  204. // is a king, moving wont matter
  205. moveable = false;
  206. }
  207. for (int k = j; j < b[i].size()-1; i++){
  208. // make sure everything below is moveable with the moving card
  209. if (!(b[i][k].getRank() == b[i][k+1].getRank()+1)){
  210. moveable = false;
  211. }
  212. }
  213. }else{
  214. placeable = false;
  215. }
  216. if (moveable){
  217. //card can be picked up
  218. // find where it can be placed
  219. int rankMatch = b[i][j].getRank() + 1;
  220. for (int k = 0; k < 10; k++){
  221. // ugly but looking for last card in each column and seeing if we can place stack there
  222. if (b[k][b[k].size()-1].getRank() == rankMatch){
  223. placeable==true;
  224. }
  225. }
  226. }
  227. if (placeable){
  228. // no need to look anymore in this column and hopefully should rule out moving a 6 on top of a 7 and counting it as a helpful move (it's not)
  229. canMoveSomething = true;
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. }
  236.  
  237.  
  238.  
  239. void displayBoard(vector<vector<Card>> b){
  240.  
  241. for(int i = 0;i<b.size();i++){
  242. for(int j = 0;j<10;j++){
  243. cout << " " << setw(3) << b[i][j];
  244. }
  245. cout << endl;
  246. }
  247. }
  248.  
  249. Card dealCard(vector<Card> &d){
  250.  
  251. Card r = d.back();
  252. d.pop_back();
  253. r.flip();
  254. return r;
  255.  
  256. }
  257.  
  258. void newGame(vector<vector<Card>> &b, vector<Card> &d){
  259.  
  260. cout <<"Please pick a difficulty: " << endl;
  261. cout <<"1) Easy (2 decks or 104 cards)" << endl;
  262. cout <<"2) Medium (4 decks or 208 cards)" << endl;
  263. cout <<"3) Hard (6 decks or 312 cards)" << endl;
  264. int difficulty;
  265. cin >> difficulty;
  266. while (difficulty < 1 || difficulty > 3){
  267. cout <<"Please pick a difficulty: " << endl;
  268. cout <<"1) Easy (2 decks or 104 cards)" << endl;
  269. cout <<"2) Medium (4 decks or 208 cards)" << endl;
  270. cout <<"3) Hard (6 decks or 312 cards)" << endl;
  271. cin >> difficulty;
  272. }
  273. d = writeDeck(2*difficulty);
  274.  
  275. b = makeBoard(d);
  276. }
  277.  
  278.  
  279.  
  280.  
  281. int main(){
  282. srand(time(NULL));
  283. Card t;
  284.  
  285. vector<vector<Card>> board;
  286. vector<Card> deck;
  287.  
  288. bool game = true, play = true;
  289.  
  290. cout << "Welcome to Spider Solitaire :D\n" << endl;
  291. cout << "Instructions: \n The goal of the game is to place King through ace in order on the board."<<endl;
  292. cout <<"You win the game when there are no more cards left in the deck or on the board. "<<endl;
  293. cout << "1. pick and action"<<endl;
  294. cout << "2. To move a card choose the row and column of the card you wish to move and the row and collum of the card you wish to place it on."<<endl;
  295. cout << "3. You can restart the game at anytime."<< endl;
  296.  
  297.  
  298. newGame(board, deck);
  299.  
  300. while(play){
  301.  
  302.  
  303. while(game){
  304.  
  305. displayBoard(board);
  306.  
  307. cout << "Options: " << endl;
  308. cout << "1) Move a card" << endl;
  309. cout << "2) New row from deck" << endl;
  310. cout << "3) Deck Size" << endl;
  311. cout << "4) Get a position from a value" << endl;
  312. cout << "5) New Game" << endl;
  313.  
  314. int input;
  315.  
  316. cin >> input;
  317.  
  318. switch (input){
  319. case 0:{
  320. cout << "Dealer Area accessed: " << endl;
  321.  
  322. cout << "1) See the deck " << endl;
  323. cout << "2) Size of board " << endl;
  324. cout << "3) See any card" << endl;
  325. cout << "4) Win the game" << endl;
  326. cout << "5) Free clear board" << endl;
  327. cout << "6) Lost game board " << endl;
  328.  
  329.  
  330. int ip;
  331. cin >> ip;
  332.  
  333. switch (ip){
  334. case 1:
  335. for(int i = 0;i<deck.size();i++){
  336. cout << i << ") " << deck.at(i).getTrueRank() << " ";
  337. break;
  338. }
  339.  
  340. case 2:
  341. cout << "Rows: " << board.size() << " " ;
  342. cout << "Columns: " << board[0].size();
  343. break;
  344.  
  345. case 3:{
  346. int r1, c1;
  347. cout << "Which row? " << endl;
  348. cin >> r1;
  349. cout << "Which column? " << endl;
  350. cin >> c1;
  351. r1--;c1--;
  352.  
  353. cout << "You picked a " << board[r1][c1].getTrueRank() << endl;
  354. break;
  355. }
  356. case 4:{
  357. game = false;
  358.  
  359. }
  360.  
  361. case 5:{
  362. Card t;
  363. board.resize(0,vector<Card>(10,t));
  364. board.resize(13,vector<Card>(10,t));
  365. for(int i = 0;i<12;i++){
  366.  
  367.  
  368. board[i][0] = Card(13-i, true);
  369.  
  370. }
  371. board[6][1] = Card(1, true);
  372. break;
  373. }
  374. case 6:{
  375. board.resize(0,vector<Card>(10,t));
  376. deck.resize(0);
  377. board.resize(13,vector<Card>(10,t));
  378.  
  379. for(int i =0;i<10;i++){
  380. board[0][i] = Card(1, true);
  381. }
  382.  
  383.  
  384.  
  385. }
  386.  
  387. cout << endl;
  388. }
  389.  
  390. break;
  391. }
  392. case 1:{
  393. int r1, c1, r2, c2;
  394. cout << "Which row to move from? " << endl;
  395. cin >> r1;
  396. cout << "Which column to move from? " << endl;
  397. cin >> c1;
  398. r1--;c1--;
  399.  
  400. bool validMove = false;
  401. //test for valid move
  402. if(r1 > board.size()){
  403. validMove = false;
  404. }
  405. else{
  406. if(board[r1][c1].getRank() != -1 && board[r1][c1].getRank() != 0){
  407. validMove = true;
  408. }
  409. bool checkBelow = true;
  410.  
  411. int i = r1;
  412. //Needs more testing, sometimes gives inifinate while loop and breaks
  413. while (!(board[i+1][c1].getRank() <= 0)&&!(i == board.size() -2)){
  414. int one = board[i][c1].getRank();
  415. int two = board[i+1][c1].getRank()+1;
  416. if(one != two){
  417. checkBelow = false;
  418.  
  419. }
  420. i++;
  421.  
  422. }
  423. validMove = checkBelow;
  424.  
  425. if (board[r1][c1].getFace() == false){
  426. validMove = false;
  427. }
  428. }
  429.  
  430. if (!validMove){
  431. cout <<"Error, that is not a valid move" << endl;
  432. }
  433. else{
  434. cout << "You chose a " << board[r1][c1] << endl;
  435.  
  436. cout << "Which row to move to? " << endl;
  437. cin >> r2;
  438. cout << "Which column to move to? " << endl;
  439. cin >> c2;
  440. r2--;c2--;
  441. bool validPlace = true;
  442. //test for valid place
  443. // is giving errors
  444. if(r2 > board.size()){
  445. validPlace = false;
  446. }
  447. else{
  448. if(board[r2+1][c2].getRank() > 0){
  449. validPlace = false;
  450.  
  451. }
  452. if(board[r2][c2].getRank() -1 != board[r1][c1].getRank()){
  453. validPlace = false;
  454.  
  455. }
  456. //Not Tested
  457. else if (r2 == 0 && board[r2][c2].getRank() == 0 && r2 < board.size()){
  458. //when the card spot is blank at the beginning (row 0) a card can be placed there
  459. validPlace = true;
  460. }
  461. }
  462.  
  463. if (!validPlace){
  464. cout <<"Error, that is not a valid move" << endl;
  465. }
  466. else {
  467. //Tried to add way for whole row to be moved with cards (like move 7 and 6)
  468. //tested slightly
  469. // had an error trying to move card in row 1
  470.  
  471.  
  472. vector<Card> row;
  473. for (int i = r1; i < board.size(); i++){
  474. if (board[i][c1].getRank() >0){
  475. board[i][c1].flip();
  476. row.push_back(board[i][c1]);
  477. board[i][c1].setRank();
  478. }
  479. }
  480. if (r1 > 0){
  481. board[r1-1][c1].flip();
  482. }
  483.  
  484. if (r2+row.size()+1 >= board.size()){
  485. board.resize(r2+row.size()+2,vector<Card>(10,t));
  486.  
  487. }
  488. for (int i = r2; i < r2+row.size(); i ++){
  489. board[i+1][c2] = row.at(i-r2);
  490. }
  491.  
  492. // old solution just in case
  493.  
  494. /*
  495. board[r2+1][c2].setRank(board[r1][c1].getTrueRank());
  496. board[r2+1][c2].flip();
  497. board[r1-1][c1].flip();
  498. board[r1][c1].setRank();
  499. */
  500.  
  501.  
  502. }
  503. }
  504.  
  505.  
  506. break;
  507. }
  508. case 2:{
  509.  
  510. // needs testing
  511. /*
  512. bool freeSpot = false;
  513. for (int i =0; i < 10; i++){
  514. if (board[0][i].getRank() == 0){
  515. freeSpot = true;
  516. }
  517. } */
  518.  
  519.  
  520.  
  521. if (deck.size() > 0){
  522. //each column
  523. for(int i = 0;i < 10;i++){
  524. //search column to find the last filled entry.
  525.  
  526. int j = 0;
  527. while((board[j][i].getTrueRank()!= 0)&&(j!=board.size())){
  528. j++;
  529. if (j == board.size()){
  530.  
  531. break;
  532. }
  533. }
  534. if (j == board.size()){
  535. board.resize(j+2,vector<Card>(10,t));
  536. }
  537.  
  538. if(board[j][i].getTrueRank()==-1){
  539. board[j+1][i] = dealCard(deck);
  540. }
  541. else{
  542. board[j][i] = dealCard(deck);
  543.  
  544. }
  545. if (j+1 == board.size()){
  546. board.resize(j+2,vector<Card>(10,t));
  547. }
  548. }
  549. } else {
  550. cout <<"error, there are no more cards left to place" << endl;
  551. }
  552.  
  553. }
  554. cout << endl;
  555. break;
  556. case 3:{
  557. cout << deck.size() << endl;
  558. }
  559.  
  560.  
  561.  
  562. //cout << 1 << endl;
  563.  
  564. break;
  565. case 4:
  566. int r1, c1;
  567. cout << "Which row? " << endl;
  568. cin >> r1;
  569. cout << "Which column? " << endl;
  570. cin >> c1;
  571. r1--;c1--;
  572.  
  573. cout << "You picked a " << board[r1][c1].getRank() << endl;
  574. break;
  575.  
  576. case 5:
  577. // new game
  578. //needs more test
  579. newGame(board, deck);
  580. break;
  581.  
  582. }
  583.  
  584. //slightlyyyy tested, none should crash program, hopefully
  585.  
  586.  
  587. checkAndRemoveMatch(board); // doesn't crash program
  588. /*
  589. if (checkWin(board, deck)){ // doesn't crash the program
  590. cout<<"You have cleared all the cards, congrats, you win"<< endl;
  591. }
  592.  
  593.  
  594. if (checkLose(board , deck)){ // doesn't crash the program
  595. cout <<"You suck, there are no more moves left, you lose" << endl;
  596. } */
  597.  
  598.  
  599.  
  600. }
  601.  
  602. cout << "1) Play again" << endl;
  603. cout << "2) Quit and close program" << endl;
  604.  
  605. int x;
  606. cin >> x;
  607.  
  608. if(x==1){
  609. game = true;
  610. newGame(board, deck);
  611. }
  612. if(x==2){
  613. play = false;
  614. }
  615. }
  616.  
  617.  
  618.  
  619.  
  620. /*
  621. Card c = Card(1);
  622. Card c1 = Card(3, true);
  623. Card c2 = Card(12, true);
  624. cout << c2;
  625. */
  626.  
  627.  
  628.  
  629.  
  630. return 0;
  631.  
  632. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement