2013-05-06 73 views
-4

我在這裏有一個叫做mancala的遊戲,但我無法弄清楚爲什麼當一邊是空的時候爲什麼不把它添加到正確的bin。函數gameover應該添加beadArray [0-5]到邊1,beadArray [7-12]添加到邊2,如果邊1等於0,那麼它會添加邊2到beadArray [6],如果邊2有等於0,那麼它將第1邊添加到beadArray [13]。但是,如果其中一方倒空,那麼它就不會輸出贏家是誰。任何幫助將不勝感激! :) 寶石棋規則在這裏: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. 
+3

聽起來像是開始瞭解調試器的好時機。 – chris 2013-05-06 20:24:22

回答

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

通知你有贏家變量,一個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); 

順便說一句爲什麼你連續兩次調用gameOver?看不到任何理由。