Final Project.exe中0x0F28EA8E(msvcr110d.dll)的未处理异常:0xC0000005:访问冲突读取位置0x9F5A339A

时间:2014-05-02 18:50:49

标签: c++ function

好吧,所以虽然我知道我错过了一些非常简单的东西,但我不知道它是什么。我已经查看了每个相关的问题,寻找我的答案,而我所能确定的就是我想到的某个地方超出了范围。我没有指针或任何东西。

今天我的问题出现了,因为我试图制作一个能够吸引玩家0和1的第一手牌的功能。这个程序将是一个非常愚蠢的MTG版本。

这是我的代码,我希望我能正确格式化。

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

void InitDeck (string Deck [40] [4])    // Makes the first player's deck
    //Column 0 is Name, 1 is Mana Cost, 2 is strengthm 3 is toughness.
{
    //Is there any way to make this a short and not a string
    Deck [0] [0] = "AEther Adept";              Deck [0] [1] = "3";         Deck [0] [2] = "3";     Deck [0] [3] = "2";
    Deck [1] [0] = "Alabaster Mage";            Deck [1] [1] = "2";         Deck [0] [2] = "2";     Deck [0] [3] = "1";
    Deck [2] [0] = "Arablest Elite";            Deck [2] [1] = "4";         Deck [0] [2] = "2";     Deck [0] [3] = "3";
    Deck [3] [0] = "Ardent Recruit";            Deck [3] [1] = "1";         Deck [0] [2] = "1";     Deck [0] [3] = "1";
    Deck [4] [0] = "Auramancer";                Deck [4] [1] = "3";         Deck [0] [2] = "2";     Deck [0] [3] = "2";
    Deck [5] [0] = "Auriok Edgewright";         Deck [5] [1] = "2";         Deck [0] [2] = "2";     Deck [0] [3] = "2";
    Deck [6] [0] = "Auriok Senchaser";          Deck [6] [1] = "2";         Deck [0] [2] = "1";     Deck [0] [3] = "1";
    Deck [7] [0] = "Auriok Survivors";          Deck [7] [1] = "6";         Deck [0] [2] = "4";     Deck [0] [3] = "6";
    Deck [8] [0] = "Azure Mage";                Deck [8] [1] = "3";         Deck [0] [2] = "2";     Deck [0] [3] = "1";
    Deck [9] [0] = "Benalish Veteran";          Deck [9] [1] = "3";         Deck [0] [2] = "2";     Deck [0] [3] = "2";
    Deck [10] [0] = "Blade Splicer";            Deck [10] [1] = "3";        Deck [0] [2] = "1";     Deck [0] [3] = "1";
    Deck [11] [0] = "Blade-Tribe Bersekers";    Deck [11] [1] = "4";        Deck [0] [2] = "3";     Deck [0] [3] = "3";
    Deck [12] [0] = "Blighted Agent";           Deck [12] [1] = "2";        Deck [0] [2] = "1";     Deck [0] [3] = "1";
    Deck [13] [0] = "Blind Zealot";             Deck [13] [1] = "3";        Deck [0] [2] = "2";     Deck [0] [3] = "2";
    Deck [14] [0] = "Crimson Mage";             Deck [14] [1] = "2";        Deck [0] [2] = "2";     Deck [0] [3] = "2";
    Deck [15] [0] = "Elite Vanguard";           Deck [15] [1] = "1";        Deck [0] [2] = "2";     Deck [0] [3] = "1";
    Deck [16] [0] = "Embersmith";               Deck [16] [1] = "2";        Deck [0] [2] = "2";     Deck [0] [3] = "1";
    Deck [17] [0] = "Fallen Ferromancer";       Deck [17] [1] = "4";        Deck [0] [2] = "1";     Deck [0] [3] = "1";
    Deck [18] [0] = "Gideon's Lawkeeper";       Deck [18] [1] = "3";        Deck [0] [2] = "2";     Deck [0] [3] = "2";
    Deck [19] [0] = "Grand Abolisher";          Deck [19] [1] = "2";        Deck [0] [2] = "2";     Deck [0] [3] = "2";

    for (int i = 20; i < 40; i++)
    {
        Deck [i] [0] = "Mana";
        Deck [i] [1] = "Mana";
        Deck [i] [2] = "Mana";
        Deck [i] [3] = "Mana";
    }

}

void InitLife (short life [2])      //Sets the starting life for each player
{
    life [0] = 20;
    life [1] = 20;
}

int CoinFlip (short coinReturn)     //Flips the coin to determine which player starts
{
    srand (unsigned int(time (NULL)));
    short flipResult = rand() % 2;

    return flipResult;
}

/*void DumpArray (string array [40] [4])                    //dumps the names of everything in the deck, or it would if it was working
{
    for (int i = 0; i < 40; i++)
    {
        cout << array [i][0] << endl;
    }
}*/

void FirstHand (string Hand [7][4],string Deck [40][4])
{
    for (int i = 0; i < 7; i++)
    {
        short deckCoutner = 40;

        srand (unsigned int(time (NULL)));
        short randCard = rand() % (deckCoutner);

        Hand [i] [0] = Deck [deckCoutner] [0];
        Hand [i] [1] = Deck [deckCoutner] [1];
        Hand [i] [2] = Deck [deckCoutner] [2];
        Hand [i] [3] = Deck [deckCoutner] [3];  

        deckCoutner-- ;
    }
}

int Draw ()
{

    return 0;
}

void main ()
{
    //int atoi (const char * str); 
    //This can be used to translate from string to int

    //Variables Start
    string Deck0 [40] [4];      //Player 0's Deck
    string Deck1 [40] [4];      //Player 1's Deck
    string Hand0 [7] [4];
    string Hand1 [7] [4];
    short life [2];             //Both PLayers life points. Player 0's life is in bin 0
    short turn = 0;             //who's turn is it? 0=P0, 1=P1, -1=Game is over
    //Variables End

    //**************************************************************************************GAME START**************************************************************************

    InitLife(life);
    short coinReturn = -1;                      //this is -1 just to deubg and see that it wasnt returning the number that it was initialized as
    coinReturn = CoinFlip (coinReturn);         //for some reason this always returns the same exact "random" number

    InitDeck (Deck0);                           //Initiallized Deck 0 for player 0
    InitDeck (Deck1);                           //Initiallized Deck 1 for player 1

    if (coinReturn == 0 )                       //decides who's turn it is based on the coinReturn result
    {
        turn = 0;
    }else
    {
        turn = 1;
    }

    //It has now been decided whos turn it is. I now need to make the two first hands for each player

    FirstHand (Hand0, Deck0);                   // Makes the first two hands out of the first two decks
    FirstHand (Hand1, Deck1);


    cout << coinReturn<<endl;

    system ("pause");
    return;
}

在那里有一个转储数组函数,我只是没有费心去实现功能,所以我不担心我的。这是一个学校项目,所以我不仅仅是在寻求答案,而是在过去的2个小时里我一直在靠墙试图解决这个问题。正如我之前所说,我很肯定我的问题的答案是在我读过的另一篇文章中,我似乎无法弄清楚在哪里。非常感谢你的帮助。我绝对觉得这是我对缺少的功能知识的一些差距。

我还修改了第99行

void Upkeep (string board [10] [5]) //a function that does each players' upkeep
                                    //-1 is tapped, 0 is untapped. The 5th feild in the array is for tapped/untapped status
{
    for (int i = 0; i < 10; i++)
    {
            board [i] [4] = "0";
    }
    return;
}

2 个答案:

答案 0 :(得分:2)

short deckCoutner = 40; - 数组中的最后一个元素是39,而不是40。

此外,只需在程序开头调用srand()一次。如果你继续调用它,那么你将获得相同的随机数,因为time()的分辨率足够低,每次都会返回相同的值,所以你将继续使用相同的值来播种随机数生成器值。

答案 1 :(得分:0)

short deckCoutner = 40;

    srand (unsigned int(time (NULL)));
    short randCard = rand() % (deckCoutner);

    Hand [i] [0] = Deck [deckCoutner] [0];
    Hand [i] [1] = Deck [deckCoutner] [1];
    Hand [i] [2] = Deck [deckCoutner] [2];
    Hand [i] [3] = Deck [deckCoutner] [3];  

40太大了索引,因为你只用40个元素启动它。 40将指向第41个元素