微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在过程之间传递变量

如何解决在过程之间传递变量

我正在开发一个 C++ 纸牌游戏,但我遇到了一个问题,我在不同的过程中有变量,但它们似乎没有在每种方法中保留它们的值。

在处理程序方面,我完全是初学者,因此对于看起来如此简单的问题,我深表歉意。

这是我的代码供参考:

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;
/* run this program using the console pauser or add your own getch,system("pause") or input loop */

class player {
public:
    string name = "";
    int credit = 0;
    int bet = 0;
};

class cards {
public:
    string suit;
    int number = 0;
};

void Initialise() {
    //initialise Vars
    string suits[4] = { "Coins","Flasks","Sabers","Staves" };
    int values[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
}

void DefineCards(cards Card[60]) {
    
    //Define Card Values
    for (int c = 0; c < 60; c++) {

        if (c >= 0 && c <= 14) {
            Card[c].suit = "Coins";
            Card[c].number = c;
        }
        else if (c >= 15 && c <= 29) {
            Card[c].suit = "Flasks";
            Card[c].number = c - 14;
        }
        else if (c >= 30 && c <= 44) {
            Card[c].suit = "Sabers";
            Card[c].number = c - 29;
        }
        else if (c >= 45 && c <= 60) {
            Card[c].suit = "Staves";
            Card[c].number = c - 44;
        }

    }
}

void CreatePlayer(player CurrentPlayer) {
    

    bool correct = false;
    string choice;

    do {
        cout << "Please input your name: " << endl;
        cin >> CurrentPlayer.name;

        cout << "Your name is " << CurrentPlayer.name << "? (Y/N)" << endl;
        cin >> choice;

        if (choice == "y" || choice == "Y") {
            correct = true;
        }
        else {
            correct = false;
        }
    } while (correct == false);
    
    CurrentPlayer.credit = 1000;
}

//Game Procedeures

void Betting(player CurrentPlayer){
    int pot = 0;
    int bet = 0;

    bool correct = false;
    do {
        cout << "Current Bet: " << CurrentPlayer.bet << " Total Pot: " << pot << " Credit: " << CurrentPlayer.credit << "\n\n";
        cout << "Please enter a bet to begin: " << endl;
        cin >> bet;

        if (bet <= CurrentPlayer.credit) {
            correct = true;
        }
        else if (bet > CurrentPlayer.credit) {
            cout << "You don't have enough credits.";
        }
        else if (bet < 1) {
            cout << "That's an impossible value,Please try again.";
        }
    } while (correct == false);
    pot = pot + bet;
}

int main(int argc,char** argv) {
    cards Card[60];
    player CurrentPlayer;
    
    //Initialisation procedeures
    Initialise();
    DefineCards(Card);
    CreatePlayer(CurrentPlayer);
    
    //Game
    Betting(CurrentPlayer);
    
    //display Cards

    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。