【发布时间】:2021-04-10 00:29:33
【问题描述】:
背景
我们正在创建一个二十一点游戏,我们想向玩家发牌。我们已经创建了一个创建数字向量的函数,并且我们还能够“洗牌”这些数字。我们现在要做的是获取向量 shuffled_cardvals 并将值分配给庄家和玩家。
我们的问题是我们不能让 shuffled_cardvals 在函数之外工作。通过阅读其他帖子,我们相当肯定我们的问题是因为 shuffled_cardvals 在函数 shuffle() 内部使用,所以它不存在于该函数之外。
因此,我们知道问题所在,但我们不知道如何解决它。
代码
这是我们的代码。我们正在使用源文件和头文件,因为我们认为这会使事情变得更简洁。
main.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include "blackjack_header_functions.hpp"
#include "BlackjackClass.hpp"
int main() {
//extern int choice;
// Intro
std::cout << "Good Evening. This is Samuel L. Jackson. Welcome to BlackJack(son). \n";
// output initial face value of cards
std::cout << "Initial face values for each player: ";
int facevalue = 0;
std::cout << "\n";
//shuffle the cards and output the results
shuffle();
// take the player to "the hub"
selectionMenu();
//std::cout << "your choice is " << choice << " biatch\n";
return 0;
}
blackjack_header_functions.hpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
void selectionMenu();
void DetermineWinner();
void shuffle();
std::vector <std::string> cards = { "AC" , "AH" , "AS" , "AD" ,"KC" , "KH" , "KS" , "KD" ,
"QC" , "QH" , "QS" , "QD", "JC" , "JH" , "JS" , "JD" , "10C" , "10H" , "10S" , "10D" ,
"9C" , "9H" , "9S" , "9D" ,"8C" , "8H" , "8S" , "8D" , "7C" , "7H" , "7S" , "7D" ,
"6C" , "6H" , "6S" , "6D" , "5C" , "5H" , "5S" , "5D" , "4C" , "4H" , "4S" , "4D" ,
"3C" ,"3H" , "3S" , "3D" , "2C" , "2H" , "2S" , "2D" };
std::vector <int> cardvals = {
2, 2, 2, 2,
3, 3, 3, 3,
4, 4, 4, 4,
5, 5, 5, 5,
6, 6, 6, 6,
7, 7, 7, 7,
8, 8, 8, 8,
9, 9, 9, 9,
10, 10, 10, 10,
10, 10, 10, 10,
10, 10, 10, 10,
10, 10, 10, 10,};
std::vector <int> shuffled_cardvals = {};
std::vector <int> &x;
void shuffle(){
// First, generate a random seed for the card shuffling process.
// If we don't do this step, every time we call random_shuffle(),
// we'll get the same order of cards for our shuffled deck, making
// it not very random.
std::srand(std::time(0));
// Next, shuffle the cards and their face values
std::random_shuffle(cards.begin(), cards.end());
std::random_shuffle(cardvals.begin(), cardvals.end());
// then, put the shuffled cards in a new vector called shuffled_deck
for(int i = 0; i < cards.size(); i++) {shuffled_deck.push_back(cards[i]);}
// do it for the face values too
std::vector <int> shuffled_cardvals = {};
for(int i = 0; i < cardvals.size(); i++) {shuffled_cardvals.push_back(cardvals[i]);}
// finally, output the shuffled deck to the console
std::cout << "Shuffled deck: \n";
for(int i = 0; i < shuffled_deck.size(); i++) {std::cout << shuffled_deck[i] << " ";}
std::cout << "\n" << "Shuffled card values: \n";
for(int i = 0; i < shuffled_cardvals.size(); i++) {std::cout << shuffled_cardvals[i] << " ";}
}
void Deal(std::vector <int> &x) {
int dealerhand = 0;
int playerhand = 0;
for(int i = 0; i < 4; i++) {
dealerhand = dealerhand + x[i];
playerhand = playerhand + x[i+1];
}
std::cout << "\n" << dealerhand << "\n" << playerhand;
}
控制台输出(编辑/修复 1/4/21)
In file included from main.cpp:100:
blackjack_header_functions.hpp:38:20: error: 'x' declared as reference but
not initialized
38 | std::vector <int> &x;
| ^
blackjack_header_functions.hpp: In function 'void shuffle()':
blackjack_header_functions.hpp:54:45: error: 'shuffled_deck' was not
declared in this scope
54 | for(int i = 0; i < cards.size(); i++)
{shuffled_deck.push_back(cards[i]);}
| ^~~~~~~~~~~~~
blackjack_header_functions.hpp:62:25: error: 'shuffled_deck' was not
declared in this scope
62 | for(int i = 0; i < shuffled_deck.size(); i++) {std::cout <<
shuffled_deck[i] << " ";} ^~~~~~~~~~~~~
|
免责声明
我和我的朋友正试图在寒假开始一起学习 C++,我们完全是菜鸟。现在的代码处于非常未完成的状态,我们确信它已经有很多问题。任何帮助表示赞赏!
【问题讨论】:
-
&shuffled_cardvals = {}不在您发布的代码中,请发布minimal reproducible example -
您已将 shuffled_cardvals 声明为 shuffle() 中的本地和全局。您可能需要将它们作为参考传递给 shuffle,例如 Deal 中的 x。此外,您在 hpp 文件中有一个未分配的全局引用 x,编译器会为此给出错误。
-
正如您使用 C++ 的一般建议:为什么不创建一个包含所有相关卡片信息和方法的类
Card和一个包含多张卡片并具有shuffle()等方法 -
@AlanBirtles 在我清理代码并摆脱东西之前,我不小心包含了一条旧的错误消息。刚刚修好了!
-
@Ian4264 本质上,我应该只将 shuffled_cardvals 创建为全局变量,然后将其传递给 shuffle()?