c++ - 纸牌游戏对象数组问题
问题描述
我正在创建一个 UNO 游戏,将卡片对象从甲板转移到手上。每个玩家有一手牌,由 7 张随机牌组成。下面的代码显示了我尝试创建一个玩家并将玩家存储到一个可以改变回合顺序等的数组中。我用 Player* playerArray 初始化了我的数组
#pragma once
#include "Player.h"
#include "DevTest.h"
#include <string>
class Game
{
private:
int amountOfPlayers;
//std::vector<Player> playerArray;
Player* playerArray; //in charge of the order of turns etc
public:
Game();
void createPlayerArray();
void menu();
~Game();
};
这就是我实现数组的地方
#include "Game.h"
DevTest devtest;
extern Player player;
extern Deck deck;
Game::Game()
{
}
void Game::createPlayerArray()
{
std::string userName;
std::cout << "Please enter the amount of players: ";
std::cin >> amountOfPlayers;
if (amountOfPlayers < 2 || amountOfPlayers>10)
{
if (amountOfPlayers < 2)
{
std::cout << "Sorry! Not enough players." << std::endl;
}
if (amountOfPlayers > 10)
{
std::cout << "Sorry! Too many players." << std::endl;
}
}
else
{
playerArray = new Player[amountOfPlayers];
std::cout << "Please enter the names of each player." << std::endl;
for (int i = 0; i < amountOfPlayers; i++)
{
//Player* playerArray[i] = new Player;
std::cout << "Player " << i+1 << ": ";
std::cin.ignore();
std::getline(std::cin, userName);
playerArray[i].setUserName(userName);
for (int i = 0; i < player.getHANDSIZE(); i++)
{
deck.dealCard();
}
player.printHand();
}
}
return;
}
void Game::menu()
{
int choice;
std::cout << "UNO" << std::endl;
std::cout << "1. Play Game" << std::endl;
std::cout << "2. Read Rules" << std::endl;
std::cout << "3. Developer Testing" << std::endl;
std::cout << "4. Quit" << std::endl;
do
{
std::cin >> choice;
switch (choice)
{
case 1:
std::cout << "Play Game" << std::endl;
break;
case 2:
std::cout << "Read Rules" << std::endl;
break;
case 3:
std::cout << "Developer Testing" << std::endl;
devtest.testMenu();
break;
case 4:
exit(0);
break;
default:
std::cout << "Invalid choice. Please choose again." << std::endl;
}
//clear screen
} while (choice < 1 || choice >4);
}
Game::~Game()
{
}
播放器
#pragma once
#include "Deck.h"
#include <iostream>
class Player
{
private:
int length;
std::string username; //can indicate whos turn / wins
static const int HANDSIZE = 7;
Card* hand;
public:
Player();
void pushHand(Card deck);
void popHand();
void printHand();
int getHANDSIZE();
std::string getUserName();
void setUserName(std::string name);
~Player();
};
PLAYER.CPP
#include "Player.h"
#include "Card.h"
extern Deck deck;
//extern Game game;
Player::Player()
{
length = 0;
hand = new Card[HANDSIZE];
//game.createPlayerArray();
//new player?
}
void Player::pushHand(Card deck)
{
//push deck object into hand
hand[length] = deck;
length++;
}
void Player::printHand()
{
for (int i = 0; i < length; i++)
{
std::cout << "Length: " << i << std::endl;
std::cout << "Value: " << hand[i].value << std::endl;
std::cout << "Color: ";
deck.toString(hand[i].color);
}
}
int Player::getHANDSIZE()
{
return HANDSIZE;
}
std::string Player::getUserName()
{
return username;
}
void Player::setUserName(std::string name)
{
username = name;
}
Player::~Player()
{
}
My issue is, my array has a length of 0 even though I create the array with `playerArray = new Player[amountOfPlayers];`
which to my understanding creates an array of player objects with a given size. My question is, how do I create an array of Player objects correctly since either this declaration is flawed or my logic somewhere else is flawed.
Also, I cannot use vectors due to the instructor's instructions.
使用断点和 Debug>Windows>Locals,我可以看到我的播放器数组的长度为 0 而不是 2。我不确定我做错了什么,非常感谢一些指针/提示。
解决方案
推荐阅读
- mysql - 如何优化这个 SQL 查询?(在 MySQL 上运行)
- python - 为什么验证错误未显示在表单中
- string - 如何将“[int, int..]”形式的字符串转换为 int 列表?
- python-3.x - 获取上一季度月份的结束日期,并从 pyspark 中的日期列获取上一年的所有月份结束日期
- reactjs - 带有版本控制的缓存破坏似乎不起作用
- c# - 是否可以将 WPF ListBoxItem 的背景设置为存储为对象属性内的字符串的十六进制颜色?
- google-apps-script - 我可以使用 Google 表单更新 Google 表格电子表格吗?
- python - 当函数遇到问题时,比返回一堆无更好的做法?
- c - 读取txt文件并从输入问题中提取元素
- c# - 列表框在 xaml 查看器中看起来与在程序窗口中不同