战争卡片游戏C ++

时间:2014-03-31 03:14:31

标签: c++ random

我有一个创建纸牌游戏War的作业,并使用rand_range作为卡号。如果弹出数字11,12,13或14,我应该分别将它们改为J,K,Q和A.我无法弄清楚如何将卡号的unsigned short更改为const char或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;
}

1 个答案:

答案 0 :(得分:0)

使用if或select ...大小写来查找。希望这是你想要的。

// 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;
}