我无法弄清楚为什么我的阵列无法正常工作

时间:2013-05-06 20:20:41

标签: c++ arrays visual-studio-2010

我这里有一个名为mancala的游戏,但我无法弄清楚为什么当一方为空时为什么它不会将它添加到正确的bin中。函数gameover应该将beadArray [0-5]添加到第1边,将beadArray [7-12]添加到第2边,如果第1边等于0,则将第2边添加到beadArray [6]并且如果第2边等于0然后将第1侧添加到beadArray [13]。但是,当其中一方倒空然后它不会输出胜者是谁时,它永远不会这样做。任何帮助将不胜感激! :) Mancala在这里规定:http://boardgames.about.com/cs/mancala/ht/play_mancala.htm 除了我的它顺时针方向

#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.

1 个答案:

答案 0 :(得分:2)

问题出在这里

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);

请注意,您有两个获胜者变量,一个在do while循环之前,一个在do while循环之内。控制do while循环的变量是第一个,这就是你永远不会获胜的原因。

修复非常简单,更改代码,因此您只有一个获胜者变量(我确信这一直是您的意思)。

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);

BTW为什么你连续两次打电话给gameOver?看不出任何理由。