Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- using namespace std;
- #define SPADE "\xE2\x99\xA0"
- #define HEART "\xE2\x99\xA5"
- #define DIAMOND "\xE2\x99\xA6"
- #define CLUB "\xE2\x99\xA3"
- struct Card{
- int color;
- int number;
- };
- void init(Card *c){
- int idx = 0;
- for(int i=1;i<=13;i++){
- for(int j=0;j<4;j++){
- c[idx].number = i;
- c[idx].color = j;
- idx++;
- }
- }
- }
- void shuffle(Card *c){
- srand(time(NULL));
- for(int i=0;i<52;i++){
- int randNum = rand()%52;
- Card temp = c[randNum];
- c[randNum] = c[i];
- c[i] = temp;
- }
- }
- void deal(Card *c, Card p[][13]){
- int idx = 0;
- for(int i=0;i<4;i++){
- for(int j=0;j<13;j++){
- p[i][j] = c[idx++];
- }
- }
- }
- void sort(Card p[][13]){
- for(int i=0;i<4;i++){
- for(int j=0;j<12;j++){
- for(int k=0;k<12-j;k++){
- if(p[i][k].number>p[i][k+1].number || p[i][k].number==p[i][k+1].number && p[i][k].color>p[i][k+1].color){
- Card temp = p[i][k];
- p[i][k] = p[i][k+1];
- p[i][k+1] = temp;
- }
- }
- }
- }
- }
- void show(Card p[][13]){
- for(int i=0;i<4;i++){
- for(int j=0;j<13;j++){
- if(p[i][j].color==0){
- cout << SPADE;
- }else if(p[i][j].color==1){
- cout << HEART;
- }else if(p[i][j].color==2){
- cout << DIAMOND;
- }else{
- cout << CLUB;
- }
- if(p[i][j].number==1){
- cout << "A ";
- }else if(p[i][j].number==11){
- cout << "J ";
- }else if(p[i][j].number==12){
- cout << "Q ";
- }else if(p[i][j].number==13){
- cout << "K ";
- }else{
- cout << p[i][j].number << " ";
- }
- }
- cout << endl;
- }
- }
- int main(){
- Card c[52], p[4][13];
- srand(time(NULL));
- init(c);
- shuffle(c);
- deal(c, p);
- cout << "Before sorting: " << endl;
- show(p);
- sort(p);
- cout << endl;
- cout << "After sorting: " << endl;
- show(p);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement