首页 > 解决方案 > 在过程之间传递变量

问题描述

我正在开发一个 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;
}

标签: c++classoopvariablesprocedural-programming

解决方案


推荐阅读