Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我不明白为什么我的数组不能正常工作_C++_Arrays_Visual Studio 2010 - Fatal编程技术网

C++ 我不明白为什么我的数组不能正常工作

C++ 我不明白为什么我的数组不能正常工作,c++,arrays,visual-studio-2010,C++,Arrays,Visual Studio 2010,我这里有一个叫曼卡拉的游戏,但我不明白为什么当一方是空的,为什么它不把它添加到正确的垃圾箱。gameover函数应该将beadArray[0-5]添加到第1面,将beadArray[7-12]添加到第2面,如果第1面等于0,则将第2面添加到beadArray[6],如果第2面等于0,则将第1面添加到beadArray[13]。但当其中一方空了,它就不会输出谁是赢家时,它也不会这样做。任何帮助都将不胜感激!:) 曼卡拉规则如下: 除了我的,它是顺时针的 #include <iostream&

我这里有一个叫曼卡拉的游戏,但我不明白为什么当一方是空的,为什么它不把它添加到正确的垃圾箱。gameover函数应该将beadArray[0-5]添加到第1面,将beadArray[7-12]添加到第2面,如果第1面等于0,则将第2面添加到beadArray[6],如果第2面等于0,则将第1面添加到beadArray[13]。但当其中一方空了,它就不会输出谁是赢家时,它也不会这样做。任何帮助都将不胜感激!:) 曼卡拉规则如下: 除了我的,它是顺时针的

#include <iostream>
#include <iomanip>
using namespace std;
const int MAX=14;
void startArray(int beadArray[MAX]);//Creates the array that gets printed out
void printArray(int beadArray[MAX]);//Prints out the array
void board(int beadArray[MAX]);     //Creates the board out of stars
void makeSolidLine(int numStars);   //makeSolidLine makes a line out of numStars
void line();                        // A line of stars with 6 spaces inbetween
void topBinNum();                   //Numbers in top of the board
void bottomBinNum();                //Numbers in the bottom bin
void topBinNumValue();                  //Values of each slot in the top bin
void bottomBinNumValue();               //Values of each slot in the bottom bin
void binSelection(int beadArray[MAX], int player, int &binNum, int winner);
void binDrop(int beadArray[MAX], int &binSelection, int player);
int gameOver(int beadArray[MAX]);       //Declares the winner by adding bins and comparing the totals
int answer;                         //Players answer to who goes first
int response;

/*Calls the functions in proper order so the code will run smoothly
 parameter=n/a
 return value=n/a
 */
int main()
{
    cout<<"Who goes first?(1=player1, 2=player2)\n";
    cin>>answer;
    int player=answer;                  //Players aswer to who goes first
    int binNum;                         //The number of each bin location
    int beadArray[MAX];                 //The values of each bin location
    startArray(beadArray);              //Begins the array to build the board
    board(beadArray);                   //Creates the board itself
    int winner=gameOver(beadArray);     //Sees if there is a winner or if game still needs to be played
    gameOver(beadArray);
    do
    {
        binSelection(beadArray, player, binNum, winner);
        binDrop(beadArray, binNum, player);
        board(beadArray);
        int winner=gameOver(beadArray);
        gameOver(beadArray);
        if (winner==-1)
        {

            if (binNum==13||binNum==6)
            {

            }
            else if(player==1)
            {
                player=2;

            }
            else
            {
                player=1;

            }
        }
    }while(winner==-1);
    cout<<winner;
    system ("pause");
    return 0;
}
/*The function topBinNum creates the top bin numbers for the board by using the for loop by  outputting a star with four spaces then outputting a number which starts at 0 then one is added every time it loops, then the number is followed by 2 spaces and a star which is the end of the for loop that stops looping once the number reaches 5.
 parameter=n/a
 return value=n/a
 */
void topBinNum()
{
    cout<<"*      ";
    for(int i=0; i<6; i++)
    {
        cout<<"*"<<setw(4)<<i<<setw(2)<<" ";
    }
    cout<<"*";
    cout<<"      *\n";

}
/*The function bottomBinNum creates the top bin numbers for the board by using the for loop by  outputting a star with four spaces then outputting a number which starts at 12 then one is subtracted every time it loops, then the number is followed by 2 spaces and a star which is the end of the for loop that stops looping once the number reaches 5.
 parameter=n/a
 return value=n/a
 */
void bottomBinNum()
{
    cout<<"*      ";
    for(int i=12; i>6; i--)
    {
        cout<<"*"<<setw(4)<<i<<setw(2)<<" ";
    }
    cout<<"*";
    cout<<"      *\n";
}
/*Prints out a solid line of *
 parameter=int numStars
 return value=n/a
 */
void makeSolidLine(int numStars)
{
    for (int count=0; count<numStars; count++)
    {
        cout<<"*";
    }
}
/*Prints out a line of * with six spaces inbetween
 parameter=n/a
 return value=n/a
 */
void line()
{
    for (int count=0; count<8; count++)
    {
        cout<<"*";
        for(int count=0; count<6; count++)
        {
            cout<<" ";
        }
    }
    cout<<"*\n";
}
/*gives each slot a value
 parameter=it takes the function int beadArray[MAX] and allows startArray to use beadArray in its function
 return value=n/a
 */
void startArray (int beadArray[MAX])
{
    for(int i=0; i<MAX; ++i)
    {
        beadArray[i]=4;
    }
    beadArray[6]=0;
    beadArray[13]=0;
}
/*Finds data needed to print out the numbers in the correct order
 parameter=it takes the function int beadArray[MAX] and allows printArray to use beadArray in its function
 return value=n/a
 */
void printArray (int beadArray[MAX])
{

    for(int i=0; i<MAX; i++)
    {
        cout<<beadArray[i];
        cout<<endl<<endl;
    }
}
/*
 topBinNumValue calls the parameter int beadArray[MAX] which is the slot scores from 0-4 and outputs five 4's with no return value
 parameter=it takes the function int beadArray[MAX] and allows topBinNumValue to use beadArray in its function
 return value=n/a
 */
void topBinNumValue(int beadArray[MAX])
{
    cout<<"*      ";
    for(int i=0; i<6; i++)
    {
        cout<<"*"<<setw(4)<<beadArray[i]<<setw(2)<<"  ";
    }

    cout<<"*";
    cout<<"      *\n";
}
/*
 bottomBinNumValue calls the parameter int bead array[max] which is the slot scores from 6-13 and outputs a 0 then five 4's and another 0 with no return value
 parameter=it takes the function int beadArray[MAX] and allows bottomBinNumValue to use beadArray in its function
 return value=n/a
 */
void bottomBinNumValue(int beadArray[MAX])
{
    for(int i=13; i>5; i--)
    {
        cout<<"*"<<setw(4)<<beadArray[i] <<setw(2)<<" ";
    }

    cout<<"*\n";

}
/*Creates the board with numbers in proper location by calling all the previously created codes the print out the board.
 parameter=it takes the function int beadArray[MAX] and allows board to use beadArray in its function
 return value=n/a
 */
void board(int beadArray[MAX])
{
    makeSolidLine(57);
    cout<<endl;
    line();
    topBinNum();
    line();
    topBinNumValue(beadArray);
    line();
    cout<<"*  13  ";
    makeSolidLine(43);
    cout<<"   6  *";
    cout<<endl;
    line();
    bottomBinNum();
    line();
    bottomBinNumValue(beadArray);
    line();
    makeSolidLine(57);
    cout<<endl;
}
/*Adds the totals to beadArray[13 & 6] checks to see which slot is higher and displays the winner if there is one.
 parameter=int beadArray[MAX] keeps array from going above the MAX
 return value=winner, who ever won the game is the return value
 */
int gameOver(int beadArray[MAX])
{
    int winner=0;
    int side1=0;
    int side2=0;
    for(int i=0; i<6; i++)
    {
        side1=side1+beadArray[i];
    }
    for(int i=12; i>6; i--)
    {
        side2=side2+beadArray[i];
    }
    if(side1==0||side2==0)
    {

        beadArray[6]=beadArray[6]+side1;
        beadArray[13]=beadArray[13]+side2;
        if(beadArray[6]>beadArray[13])
        {

            winner=1;
        }
        else
        {


            winner=2;
        }
    }
    else
    {
        winner= -1;
    }
    return winner;

}
/*
 Asks the player which bin they would like to select and making sure that their bin selection is in the correct range and then passes back the binNum by reference.
 paramter=int beadArray[MAX] so the function can use the array, int player chooses which players turn it is, int&binNum includes the players choice of bin in the function
 return value=n/a
 */
void binSelection(int beadArray[MAX], int player, int &binNum, int winner)
{
    cout<<"Player "<<player<<" Choose bin.\n"<<winner;
    cin>>binNum;

    while(player==1 && ((binNum<0||binNum>5) || beadArray[binNum]<1))
    {
        if(beadArray[binNum]==0)
        {
            cout<<"Make selection with value other then 0 and ";
        }
        cout<<"Make selection with beads between 0 and 5.\n";
        cin>>binNum;
    }
    while(player==2 && ((binNum>12||binNum<7) || beadArray[binNum]<1))
    {
        if(beadArray[binNum]==0)
        {
            cout<<"Make selection with value other then 0 and ";
        }
        cout<<"Make selection with beads between 7 and 12.\n";
        cin>>binNum;
    }
}
/*
 Drops beads into the bin when a player makes a binselection, making sure each bead is distrubuted correctly
 return value=n/a
 parameteres= int beadArray[MAX] allowing the function to use the values of the bin in the function, int &binSelection allows the function use the bin that player has selected to move, int player allows the function to use which player is going in the function
 */
void binDrop(int beadArray[MAX], int &binSelection, int player)
{

    int hand=0;
    do
    {
        hand=beadArray[binSelection];
        beadArray[binSelection]=0;
        while(hand>0)
        {
            binSelection++;
            if (player==1&& binSelection==13)
            {
                binSelection=0;
            }
            if (player==2&& binSelection==6)
            {
                binSelection=7;
            }
            if(binSelection>13)
            {
                binSelection=0;
            }
            beadArray[binSelection]++;
            hand--;
            board(beadArray);
        }
    }while(binSelection!=13 && binSelection!=6 && beadArray[binSelection]>1);
}
//I cant figure out how why it wont add all beads from player 2's side to player 1's total if player 1 clears their side first and vice versa.
#包括
#包括
使用名称空间std;
常数int MAX=14;
void startArray(int beadArray[MAX])//创建打印出来的数组
void printary(int beadArray[MAX])//打印出阵列
空心板(int beadArray[MAX])//用星星创建棋盘
void makeSolidLine(int numStars)//makeSolidLine用numStars生成一行
虚线();//中间有6个空格的一行星星
void topBinNum()//最上面的数字
void bottomBinNum()//底部箱子中的数字
void topBinNumValue()//顶部存储箱中每个插槽的值
void bottomBinNumValue()//底部箱中每个插槽的值
无效选择(int-beadArray[MAX],int-player,int&binNum,int-winner);
void binDrop(int beadArray[MAX],int&binSelection,int player);
int gameOver(int beadArray[MAX])//通过添加箱子并比较总数来宣布获胜者
int答案//球员们决定谁先上场
int响应;
/*以正确的顺序调用函数,以便代码能够顺利运行
参数=不适用
返回值=不适用
*/
int main()
{
库坦斯沃;
int player=answer;//玩家问谁先去
int binNum;//每个存储箱位置的编号
int beadArray[MAX];//每个箱子位置的值
startArray(beadArray);//开始阵列以构建板
线路板(beadArray);//创建线路板本身
int winner=gameOver(beadArray);//查看是否有赢家或是否仍需要玩游戏
加梅奥弗(比达雷);
做
{
binSelection(beadArray,玩家,binNum,获胜者);
宾德罗普(比达雷、宾纳姆、球员);
董事会(beadArray);
int winner=gameOver(beadArray);
加梅奥弗(比达雷);
如果(获胜者==-1)
{
如果(binNum==13 | | binNum==6)
{
}
else if(player==1)
{
玩家=2;
}
其他的
{
玩家=1;
}
}
}而(获胜者==-1);
问题就在这里

int winner=gameOver(beadArray);
gameOver(beadArray);
do
{
    binSelection(beadArray, player, binNum, winner);
    binDrop(beadArray, binNum, player);
    board(beadArray);
    int winner=gameOver(beadArray);
    gameOver(beadArray);
    ...
}while(winner==-1);
请注意,您有两个赢家变量,一个在执行while循环之前,一个在执行while循环内部。控制执行while循环的变量是第一个,这就是为什么您永远不会获得赢家

修复非常简单,更改代码,使您只有一个winner变量(我相信这就是您一直以来的意思)


顺便说一句,你为什么连续两次调用gameOver?看不出任何原因。

听起来是开始了解调试器的好时机。
int winner=gameOver(beadArray);
gameOver(beadArray);
do
{
    binSelection(beadArray, player, binNum, winner);
    binDrop(beadArray, binNum, player);
    board(beadArray);
    winner=gameOver(beadArray);
    gameOver(beadArray);
    ...
}while(winner==-1);