首页 > 解决方案 > 纸牌游戏对象数组问题

问题描述

我正在创建一个 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。我不确定我做错了什么,非常感谢一些指针/提示。

标签: c++

解决方案


推荐阅读