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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void fillStars( string upSide[4][4], char star);
void shuffleDeck(int unshuffled[4][4], int shuffled[4][4], string upSide[4][4]);
void pickCards(int &row1, int &row2, int &col1, int &col2);
void placeCard(int shuffled[4][4], string upSide[4][4], int &card1, int &card2,int &row1, int &row2, int &col1, int &col2);
int main()
{
int unshuffled[4][4] = { {1,1,2,2},
{3,3,4,4},
{5,5,6,6},
{7,7,8,8} };
int shuffled[4][4];
string upSide[4][4];
int card1 = 0;
int card2 = 0;
int row1 = 0;
int row2 = 0;
int col1 = 0;
int col2 = 0;
char star = '*';
fillStars(upSide, star);
shuffleDeck(unshuffled, shuffled, upSide);
pickCards(row1, row2, col1, col2);
placeCard(shuffled, upSide, card1, card2, row1, row2, col1, col2);
}
void fillStars(string upSide[4][4], char star)
{
int i = 0; //starts the game board by filling with stars
int j = 0;
for (i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
upSide[i][j] = star;
cout << upSide[i][j] << " ";
}
cout << endl;
}
cout << "\n";
}
void shuffleDeck(int unshuffled[4][4], int shuffled[4][4], string upSide[4][4])
{
int i = 0;
int j = 0; //there are 8 pairs of cards - the cards are
//assigned to the board randomly
srand (time(NULL));
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
shuffled[i][j] = unshuffled[rand()% 4][rand()% 4];
cout << shuffled[i][j] << " ";
}
cout << endl;
}
cout << "\n";
}
void pickCards(int &row1, int &row2, int &col1, int &col2)
{
cout << "Enter a row and column, separated with a space, for your first card: ";
cin >> row1 >> col1;
cout << "Enter a row and column, separated with a space, for your second card: ";
cin >> row2 >> col2;
//HERE DECREMENTING ROWS AND COLS TO ACCESS YOUR ARRAYS
row1--;
col1--;
row2--;
col2--;
}
void placeCard(int shuffled[4][4], string upSide[4][4], int &card1, int &card2, int &row1, int &row2, int &col1, int &col2)
{
int i = 0;
int j = 0;
card1 = shuffled[row1][col1]; //assign values to cards
card2 = shuffled[row2][col2];
cout << "\nCard1: " << card1 << "\nCard2: " << card2 << "\n";
upSide[row1][col1] = to_string(card1); //HERE CONVERTING INT TO STRING
upSide[row2][col2] = to_string(card2); //HERE CONVERTING INT TO STRING
cout << "\nupSide[" << row1 << "][" << col1 << "] is " << upSide[row1][col1]
<< "\nupSide[" << row2 << "][" << col2 << "] is " << upSide[row2][col2] << "\n\n";
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << upSide[i][j] << " ";
}
cout << endl;
}
}
|