简体   繁体   中英

War Card Game C++

I have an assignment that creates the card game War and uses rand_range for the card numbers. If the numbers 11, 12, 13, or 14 pop up I am supposed to change them to J, K, Q, and A, respectively. I cannot figure out how to change the unsigned short for the card numbers to a const char or string.

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
#include<stdlib.h>
#include<string>
using namespace std;

unsigned short rand_range(unsigned short min, unsigned short max)
{
    return rand() % (max - min + 1) + min;
}
int main(void)
{srand((unsigned)time(0));
unsigned char suit1 = ' ', suit2 = ' ';                         
unsigned char  verticalLine = ' ', horizontalDouble = ' ';
unsigned char doubleCornerr = ' ', doubleCornerl = ' ', doubleCornerlb = ' ';
unsigned char doubleCornerrb = ' ', topLine = ' ', verticalDouble = ' ', topCornerr = ' ';
unsigned char topCornerl = ' ', bottomCornerl = ' ', bottomCornerr = ' ', hyphen = ' ';

unsigned short cardNumber1 = rand_range(2,14);
unsigned short cardNumber2 = rand_range(2,14);

suit1 = rand_range(3,6);
suit2 = rand_range(3,6);

verticalLine = 179;
horizontalDouble = 205;
doubleCornerr = 187;
doubleCornerl = 201;
doubleCornerlb = 200;
doubleCornerrb = 188;
topLine = 196;
verticalDouble = 186;
topCornerr = 191;
topCornerl = 218;
bottomCornerl = 192;
bottomCornerr = 217;

//The next portion of code will ensure that the suits are different
if (suit1 == suit2)
    suit1 += 1;
if (suit1 == 6)
    suit1 = 3;
//The next portion will change the numbers 11,12,13,14 
// J, K, Q, A, respectively. 
string royalcard;

switch (cardNumber1 & cardNumber2) {
case 11:
royalcard = "J";
break;

case 12:
royalcard = "Q";
break;

case 13: 
royalcard = "K";
break;

case 14:
royalcard = "A";
break;

default:

break;
}

Use an if or select...case to find out. Hope this is what you were looking for.

// x holds the random number you have generated
string royalcard;

switch (x) {
case 11:
royalcard = "Jack";
break;

case 12:
royalcard = "Queen";
break;

case 13: 
royalcard = "King"
break;

case 14:
royalcard = "Ace"
break;

default:

break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM