I'm currently writing a program that will half simulate a modified version of poker. I am in a very early stage of the program which will be incorporated in a larger program later on. I have a couple of functions in Poker.h that when I submit a number will give me the suit and a number from 1-13. I want to be able to convert that number to either say, "Ace", "Jack", "Queen", "King", or the number. I don't want to have to say, "if (z==2) {a="2"} else if (z==3) {a="3")" etc. If anyone has ideas, that would be helpful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "Poker.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define size 4
usingnamespace std;
char response[size];
int main(int argc, constchar * argv[])
{
string v = getSuit(13);
int z = getValue(13);
cout << z << endl;
cout << v << endl;
}
#ifndef AdventureRPG_Poker_h
#define AdventureRPG_Poker_h
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
usingnamespace std;
string cardOut;
int valueOut;
string suitOut;
string of = " of ";
int getValue(int a) {
int z = a % 13;
z++;
int x = a-z;
x /= 13;
int b = z;
valueOut = b;
return valueOut;
}
string getSuit(int a) {
int z = a % 13;
int x = a-z;
x /= 13;
if (x==0) {
suitOut="Diamond";
} elseif (x==1) {
suitOut="Heart";
} elseif (x==2) {
suitOut="Spade";
} elseif (x==3) {
suitOut="Club";
}
return suitOut;
}
#endif
Well, what I mean by an array is an array of strings, with ace being at the 0th memory spot and king being at the 12th. When you plug in the card value (minus one), it will give you what card it is, i.e. ace, two, three, et cetera. No need for a long switch or if-else chain.
You could do a switch/case with a default statement based on the result of getValue(), but that is basically the same as writing a few if/else branches. You could try making a map (though since you have integer keys it can be a standard array), as I (think) was being suggested above, that goes from a number to the string you want to output.