C ++函数返回错误的返回值?

时间:2015-09-30 23:36:28

标签: c++ function while-loop return return-value

我基于Kaiji Ep制作了一个简单的命令行应用程序。 16模仿游戏Emperor Card(我们在基本编程中的中期)。我被困在我认为是一个简单的问题,但我似乎无法自己解决它。我有这个函数“winChecker(List * root,Node * head),”它可以检查哪些牌被抽出以及谁赢了谁。

似乎每当我绘制公民时它都会返回错误的返回值,并且对手也会吸引公民。根据我的代码,它应该循环,因为Citizen vs Citizen是平局。

你能帮我理解我在这里做错了吗?此外,如果你看到其他一些错误,请随意指出它们。我愿意学习。

PS:我只使用struct for Node和List,因为我们不允许在我们的midterms中使用Class。我为每个人提供了1个头文件。

Source.cpp

    #include "Node.h"
#include "List.h"
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

Node *createKaijiDeck(int round);
Node *selectKaijiCard(Node *head, int index);
Node *deleteSelectedKaijiCard(Node *head, int index);
List* createTonegawaDeck(int round);
List *selectTonegawaCard(List *root, int indexT);
List *deleteSelectedTonegawaCard(List *root, int indexT);
bool betCheck(int betLength, int remainingLength);
int prizeCheck(int betLength, int round);
void printDeck(Node * head);
int winCheck(List *root, Node *head, int cardIndex, int randTonegawa);
void gameOver(int cash, int round, int input, int remaining);

int main() {

    // Seed the RNG
    srand((unsigned int)time(0));

    // Initilizing variables
    int input, cardIndex, randTonegawa, countTonegawa = 0, cash = 0, remaining = 30;

    // Round loop
    for (int round = 1; round < 12; round++) {

        cout << "===============================================" << endl;
        cout << "                ROUND " << round << endl;
        cout << "===============================================" << endl;
        cout << "How much would you like to bet, in milimeters?" << endl;
        cout << "(You still have " << remaining << " milimeters left.)" << endl;
        cin >> input;
        betCheck(input, remaining);

        // Match loop
        if (betCheck(input, remaining) == true) {

            cout << "You can win " << prizeCheck(input, round) << " Yen." << endl << endl;
            cout << "Cash currently at hand: " << cash << endl;
            Node* head = createKaijiDeck(round);
            List* root = createTonegawaDeck(round);
            do {
                printDeck(head);

                cout << "Select a card to play [1 - 5]: ";
                cin >> cardIndex;
                randTonegawa = (rand() % (5 - countTonegawa));
                Node* selectKaijiCardAtIndex = selectKaijiCard(head, cardIndex);
                cout << "You chose the card: " << selectKaijiCardAtIndex->cards << endl;
                List* selectTonegawaCardAtIndex = selectTonegawaCard(root, randTonegawa);
                cout << "Tonegawa chose the card: " << selectTonegawaCardAtIndex->card << endl;
                cout << endl;
                countTonegawa++;
            } while (winCheck(root, head, cardIndex, randTonegawa) == 0);

            // Match up checker (Emperor > Citizen > Slave > Emperor)
            if (winCheck(root, head, cardIndex, randTonegawa) == 1) {

                cash = cash + prizeCheck(input, round);
                cout << "Round " << round << " winner is Kaiji." << endl;
                cout << "You won " << prizeCheck(input, round) << " Yen!" << endl;
            }
            else if (winCheck(root, head, cardIndex, randTonegawa) == 2) {

                remaining = remaining - input;
                cout << "Round " << round << " winner is Tonegawa." << endl;
                cout << "The pin moved by " << input << " milimeters!" << endl;
            }

        }
        else if (betCheck(input, remaining) == false)
        {
            cout << "You lose! You already lost your ear!" << endl;
            system("pause");
            exit(0);
        }
    }
    return 0;
}

Node *createKaijiDeck(int round) {

    Node* head = NULL;
    Node* curr = NULL;
    Node* prev = NULL;

    if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) {
        curr = new Node;
        curr->cards = "Emperor";
        prev = curr;
        head = curr;

        for (int i = 0; i < 3; i++) {

            curr = new Node;
            curr->cards = "Citizen";
            prev->next = curr;
            prev = curr;
        }

        curr = new Node;
        curr->cards = "Citizen";
        prev->next = curr;

    }

    if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) {
        curr = new Node;
        curr->cards = "Slave";
        prev = curr;
        head = curr;

        for (int i = 0; i < 3; i++) {

            curr = new Node;
            curr->cards = "Citizen";
            prev->next = curr;
            prev = curr;
        }

        curr = new Node;
        curr->cards = "Citizen";
        prev->next = curr;

    }

    return head;

}
Node *selectKaijiCard(Node *head, int indexK) {

    for (int i = 0; i < indexK - 1; i++) {

        head = head->next;
    }

    return head;
}
Node *deleteSelectedKaijiCard(Node *head, int indexK) {

    Node *curr = NULL;
    if (indexK == 1)
    {
        curr = head;
        head = head->next;
        delete curr;
        return head;
    }

    Node *deleteCard = head;

    for (int i = 0; i < indexK - 1; i++) {

        curr = deleteCard;
        deleteCard = deleteCard->next;
    }
    curr->next = deleteCard->next;
    delete deleteCard;
    return head;
}
List *createTonegawaDeck(int round) {

    List *root = NULL;
    List *front = NULL;
    List *tail = NULL;

    if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) {
        front = new List;
        front->card = "Slave";
        tail = front;
        root = front;

        for (int i = 0; i < 3; i++) {

            front = new List;
            front->card = "Citizen";
            tail->next = front;
            tail = front;
        }

        front = new List;
        front->card = "Citizen";
        tail->next = front;
    }

    if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) {
        front = new List;
        front->card = "Emperor";
        tail = front;
        root = front;

        for (int i = 0; i < 3; i++) {

            front = new List;
            front->card = "Citizen";
            tail->next = front;
            tail = front;
        }

        front = new List;
        front->card = "Citizen";
        tail->next = front;
        front->next = root;
    }

    return root;
}
List *selectTonegawaCard(List *root, int indexT) {
    for (int i = 0; i < indexT; i++) {

        root = root->next;
    }

    return root;

}
List *deleteSelectedTonegawaCard(List *root, int indexT) {

    List *front = NULL;
    if (indexT == 0)
    {
        front = root;
        root = root->next;
        delete front;
        return root;
    }

    List *deleteTonegawaCard = root;

    for (int i = 0; i < indexT; i++) {

        front = deleteTonegawaCard;
        deleteTonegawaCard = deleteTonegawaCard->next;
    }
    front->next = deleteTonegawaCard->next;
    delete deleteTonegawaCard;
    return root;
}
bool betCheck(int betLength, int remainingLength) {

    bool flag;
    if (betLength <= remainingLength) {
        flag = true;
    }
    else if (betLength > remainingLength) {
        flag = false;
    }
    return flag;
}
int prizeCheck(int betLength, int round) {

    int yen;
    if (round == 1 || round == 2 || round == 3 || round == 7 || round == 8 || round == 9) {

        yen = betLength * 100000;
    }
    if (round == 4 || round == 5 || round == 6 || round == 10 || round == 11 || round == 12) {

        yen = betLength * 500000;
    }
    return yen;
}
void printDeck(Node * head) {

    int count = 1;
    cout << "===============" << endl;
    cout << "Kaiji's cards" << endl;
    cout << "===============" << endl << endl;
    while (head != NULL) {
        cout << count << ". " << head->cards << endl;
        head = head->next;
        count++;
    }
    cout << endl;
}
int winCheck(List *root, Node *head, int cardIndex, int randTonegawa) {

    int result = 0;

    if ((head->cards == "Citizen") && (root->card == "Citizen")) {

        result = 0;
    }
    else if ((head->cards == "Emperor") && (root->card == "Citizen") || (head->cards == "Slave") && (root->card == "Emeperor") || (head->cards == "Citizen") && (root->card == "Slave")) {

        result = 1;
    }
    else if ((root->card == "Emperor") && (head->cards == "Citizen") || root->card == "Slave" && head->cards == "Emperor" || root->card == "Citizen" && head->cards == "Slave") {

        result = 2;
    }
    head = deleteSelectedKaijiCard(head, cardIndex);
    root = deleteSelectedTonegawaCard(root, randTonegawa);
    return result;
}
void gameOver(int cash, int round, int input, int remaining) {

    if (round <= 12 && cash == 20000000 && betCheck(input, remaining) == true) {
        cout << "You did not entirely win! You only got " << cash << " Yen in 12 rounds!" << endl;
        system("pause");
        exit(0);
    }
    else if (round == 12 && cash < 20000000 && betCheck(input, remaining) == false) {

        cout << "You won! You got" << cash << " Yen at Round " << round << endl;
    }

}

List.h

#pragma once
#include <string>
using namespace std;

struct List {
    string card;
    List* next = NULL;
};

Node.h

#pragma once
#include <string>
using namespace std;

struct Node {
    string cards;
    Node* next = NULL;
    Node* prev = NULL;  
};

1 个答案:

答案 0 :(得分:0)

看起来当您选择要播放的牌时,您可以从牌组中删除该牌,然后比较两张牌的第一张牌。这看起来不对,你肯定要比较你刚才选择的牌吗?

如果是这样,您应该使用winCheckselectKaijiCardAtIndex致电selectTonegawaCardAtIndex。您还希望之前从卡片组中删除卡片,因为deleteSelectedKaijiCard功能实际上删除了卡片,因此您无法在此之后使用它。

这可能意味着重新安排您的代码,而不是在很多地方调用wincheck