我正在尝试用C ++实现战争卡牌游戏。我有以下数据文件,从中提取卡的价值和卡套。
7 hearts
Q spades
9 clubs
A hearts
10 spades
3 spades
K diamonds
10 hearts
6 spades
3 clubs
K hearts
9 spades
6 clubs
3 diamonds
A spades
J clubs
8 diamonds
5 hearts
A clubs
J diamonds
8 hearts
4 spades
A diamonds
J hearts
7 spades
4 clubs
7 clubs
4 diamonds
K spades
10 clubs
7 diamonds
4 hearts
K clubs
10 diamond
6 diamonds
3 hearts
Q clubs
9 diamonds
8 spades
5 clubs
2 diamonds
J spades
8 clubs
5 diamonds
2 hearts
6 hearts
2 spades
Q diamonds
9 hearts
5 spades
2 clubs
Q hearts
7 hearts
Q spades
9 clubs
A hearts
10 spades
3 spades
K diamonds
10 hearts
6 spades
3 clubs
K hearts
9 spades
6 clubs
3 diamonds
A spades
J clubs
8 diamonds
5 hearts
A clubs
J diamonds
8 hearts
4 spades
A diamonds
J hearts
7 spades
4 clubs
7 clubs
4 diamonds
K spades
10 clubs
7 diamonds
4 hearts
K clubs
10 diamond
6 diamonds
3 hearts
Q clubs
9 diamonds
8 spades
5 clubs
2 diamonds
J spades
8 clubs
5 diamonds
2 hearts
6 hearts
2 spades
Q diamonds
9 hearts
5 spades
2 clubs
Q hearts
[首先,我通过将数据文件拆分为一半并将两半的数据放入2个不同的数组中,然后通过合并数组进行混洗来对卡进行一次改组。然后,我使用筹码将这些卡推入4张筹码中(代表我的游戏中有4位玩家。)该算法非常简单,因为我计算了游戏中每个玩家的总面额并计算了总人数以宣布获胜者。我在代码中收到以下错误:“没有从'string'(aka'basic_string,allocator>')到'const carddata'的可行转换”。请提供有关其含义的见解。以下是代码示例:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
//to represent value of each card and the suit of each card
struct carddata{
string denomination;
string suits;
};
//creating template and prototype for using stack
template <typename T, typename vectorData = std::vector<T> >
class Stack
{
public:
Stack(int);
bool isFull() const;
bool isEmpty() const;
void push(T const&);
T pop();
int Size();
void makeEmpty();
T peek();
private:
int top;
int size;
vectorData data;
};
class StackException: public std::logic_error {
public:
StackException(const std::string& message = "")
:logic_error(message.c_str()){
}
};
template <typename T, typename vectorData>
Stack<T, vectorData>::Stack(int s){
data.resize(size);
size = s;
top = -1;
}
template <typename T, typename vectorData>
bool Stack<T, vectorData>::isEmpty() const {
return (top == -1);
}
template <typename T, typename vectorData>
int Stack<T, vectorData>::Size() {
return size;
}
template <typename T, typename vectorData>
bool Stack<T, vectorData>::isFull() const {
return (top == size -1);
}
template <typename T, typename vectorData>
void Stack<T, vectorData>::push(T const& newItem){
if (isFull()){
throw StackException("Full Size limit exceeded");
}
else {
top += 1;
if (data.size() == 0){
data.resize(size);
}
data[top] = newItem;
}
}
template <typename T, typename vectorData>
T Stack<T, vectorData>::pop() {
if (isEmpty()){
throw StackException("Stack Empty");
} else {
top--;
}
}
template <typename T, typename vectorData>
void Stack<T, vectorData>::makeEmpty() {
if (!isEmpty()){
data.resize(0);
size = 0;
top = -1;
}
}
template <typename T, typename vectorData>
T Stack<T, vectorData>::peek(){
if (isEmpty()){
throw StackException("Stack Empty");
}
return data[top];
}
int main(){
carddata game;//declaring the struct
ifstream myfile;//create file object
myfile.open("infile.txt");//opening the file
carddata firsthalf[26];//declare array to store 1st half of the carddata present in the infile
carddata secondhalf[26];//declare array to store 2nd half of the carddata present in the infile
carddata entirecard[52];//declare array to merge and shuffle the card
for (int i = 0; i< 26; i++){
myfile >> game.denomination >> game.suits;
firsthalf[i] = game;
}
for (int j = 0; j < 26; j++){
myfile >> game.denomination >> game.suits;
secondhalf[j] = game;
}
//this function is used to merge and create a new set of cards containing 52 cards (to shuffle).
int i =0;
int j = 0;
while (i<26 && j<52){
entirecard[j] = firsthalf[i];
j ++;
entirecard[j] = secondhalf[i];
i++;
j++;
}
//we need to put data onto the stack. These stacks represent 4 people in my game
Stack<carddata> stack1(13);
Stack<carddata> stack2(13);
Stack<carddata> stack3(13);
Stack<carddata> stack4(13);
int a = 0;
int b = 0;
int c = 0;
int d = 0;
// i am trying to push card's denomination, but get a problem
while (a < 13 && b < 13 && c < 13 && d < 13){
stack1.push(entirecard[a].denomination);
stack1.push(entirecard[b].denomination);
stack1.push(entirecard[c].denomination);
stack1.push(entirecard[d].denomination);
a++;b++;c++;d++;
}
int player1 = 0;
int player2 = 0;
int player3 = 0;
int player4 = 0;
//to add values of each card for each player
for (int i = 0; i < 13; i++){
if(stack1.pop()=="J"){
player1 += 10;
}
else if (stack2.pop()=="J"){
player2 += 10;
}
else if (stack3.pop()=="J"){
player3 += 10;
}
else if(stack1.pop()=="Q"){
player1 += 11;
}
else if (stack2.pop()=="Q"){
player2 += 11;
}
else if (stack3.pop()=="Q"){
player3 += 11;
}
else if(stack1.pop()=="K"){
player1 += 12;
}
else if (stack2.pop()=="K"){
player2 += 12;
}
else if (stack3.pop()=="K"){
player3 += 12;
}
else if (stack1.pop()=="A"){
player1 += 13;
}
else if (stack2.pop()=="A"){
player2 += 13;
}
else if (stack3.pop()=="A"){
player3 += 13;
}
else{
player1 += stack1.pop();
player2 += stack2.pop();
player3 += stack3.pop();
player4 += stack4.pop();
}
}
if (player1>player2&&player1>player3 && player1 >> player4){
cout << "Player1 wins";
}
else if (player2>player1&&player2>player3 && player2>> player4){
cout << "Player2 wins";
}
else if (player3>player1&&player3>player2 && player3>> player4){
cout << "Player3 wins";
}
else if (player4>player1&&player4>player2 && player4 >> player3){
cout << "Player4 wins";
}
else if (player1==player2==player3==player4){
cout << "Player1 wins";
}
myfile.close();
return 0;
}
@@ Shrijan Aryal我可以得到这些代码吗?请其紧急。