# C++ Vector Question



## Kramer55 (Jan 18, 2005)

I'm (trying to) writing a game called mancala, but I am running into a few problems.

I have a Move(int&, int) function that is supposed to take away stones from pits moving in a counter-clockwise direction, but I can't seem to be able to get it to work.

Header File...

```
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Mancala{
        private:
                vector<int> Pits;
        public:
                Mancala(int);
                ~Mancala(){};

                void Move(int&, int);      
                void PrintBoard();
                void PrintResult(){};
               // bool IsValidMove(int,int){};
              //  bool EndGame(void){};         
};

Mancala::Mancala(int NumOfStones){
	for(int i=0;i<14;++i)
	{
		Pits.push_back(NumOfStones);
	}
   Pits[6]=Pits[13]=0;
}

void Mancala::Move(int& Player, int ChosenPit)
{
	for(int i = 0; i <= ChosenPit; --i)
	{
		 Pits.push_back(i); 
	}
}

void Mancala::PrintBoard(void){        //Ugly, but it works
        string Line(55, '-');
        string MidLine(37, '-');
        string Spaces(2, ' ');

        cout << '\t' <<'\t' << "12" << "   " << "11" << "   " << "10" << "   " << "9" << "   ";
        cout << "8" << "   " << "7" << endl;

        cout << Line << endl;

        cout <<'\t' <<"     " << "| " << Pits[12] << "   " << "| " << Pits[11] << "  " << "| " << Pits[10];
        cout << "  " << "| " << Pits[9] << " " << "| " << Pits[8] << " " << "| " << Pits[7] << endl;

        cout << "13 | " << Pits[13]  << Spaces << MidLine << Spaces << Pits[6] << " | 6" << endl;

		cout << '\t' <<"      " << Pits[0] << "     " << Pits[1] << "    " << Pits[2] << "   " << Pits[3] << "   ";
        cout << Pits[4] << "    " << Pits[5] << endl;

		 cout << Line << endl;

        cout <<'\t' <<"     " << "| " << "0" << "   " << "| " << "1" << "  " << "| " << "2";
        cout << "  " << "| " << "3" << " " << "| " << "4" << " " << "| " << "5" << endl;

            
        cout << endl << endl << endl;
}
```
.cpp File

```
#include "mancala.h"

int main(){
       
        bool GameOver=false;
        int Player=1,LastPlayer=2,StoneNum;
       
        do{
                cout<<"Input the number of stones in each pit:";
                cin>> StoneNum;
                if(StoneNum<=0)cout<<"Number of stones should be a positive value"<<endl;
        }while(StoneNum<=0 );

        Mancala MancalaGame(StoneNum);

        while(!GameOver){

                int PitNumber;

                MancalaGame.PrintBoard();

                if(LastPlayer == Player)cout<< "Player "<< Player<< " gets one more chance:";
                else cout<< "Player " << Player<< "'s turn, choose a pit(-1 to end game):";

                cin >> PitNumber;
                cout << endl;

                if(PitNumber==-1){
                        cout<<"Player "<<Player<<" choose to end the game. Thanks for playing..."<<endl;
                        GameOver=true;
                        continue;
                }

               /* if(!MancalaGame.IsValidMove(Player, PitNumber)){
                        cout<< endl << "Not a valid move. Try again." << endl << endl;
                        continue;
                }*/

                LastPlayer = Player;
                MancalaGame.Move(Player, PitNumber);
               // GameOver = MancalaGame.EndGame();

        }

        MancalaGame.PrintBoard();
        MancalaGame.PrintResult();

        system("Pause");
}
```
In the Move() function, the first variable is passed by reference to see if the player gets to take another turn. Note: Players can take another turn if the last stone lands in their score-keeping pit.

For those of you who don't know what mancala is, it's a game with 2 players, each with 6 "pits" on their side of the board to hold stones, plus one more pit to keep score.

Players move the stones counter-clockwise around the board, dropping a stone off in each pit until they run out stones. If while dropping the stones, the last stone dropped lands in an empty pit on the player's own side, they can take the stones of their opponent if there are any in the pit across the board from where their last stone landed.

Thanks for your help.


----------



## Shadow2531 (Apr 30, 2001)

Don't know the game, but ...

In Mancala::Move, the loop runs forever. Did you mean ++i instead?
Also, in that function you're not doing anything with Player (at least yet) and you're just adding more elements to Pits.

Anyway, for example, if I enter 5 stones in each pit, the pits vector will look like this:

pits[0] = 5;
pits[1] = 5;
pits[2] = 5;
pits[3] = 5;
pits[4] = 5;
pits[5] = 5
pits[6] = 0;
pits[7] = 5;
pits[8] = 5;
pits[9] = 5;
pits[10] = 5;
pits[11] = 5;
pits[12] = 5;
pits[13] = 0;

If player one chooses, pit 3, what do you want the pits vector to look like after move() is called?


----------

