Troubles with mancala
May 31, 2013 at 4:49pm May 31, 2013 at 4:49pm UTC
For my class I'm making a mancala game, but I'm having troubles with the actual movement of marbles in the game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#include <iostream>
#include <vector.h>
bool finished(vector <int > displayboard);
void displayboard(vector <int > boardvalue);
void makemove(vector <int > &boardvalue, int player);
using namespace std;
void makemove(vector <int > &boardvalue, int player)
{
char position; int numposition;
cout << "\nWhich column do you want to pick from? " ;
cin >> position;
if (player == 1)
{
switch (position)
{
case 'A' :
numposition=1;
break ;
case 'B' :
numposition=2;
break ;
case 'C' :
numposition=3;
break ;
case 'D' :
numposition=4;
break ;
case 'E' :
numposition=5;
break ;
case 'F' :
numposition= 6;
break ;
default :
cout << "Enter the letter of one of the pits" ;
break ;
}
}
if (player == 2)
{
switch (position)
{
case 'A' :
numposition=13;
break ;
case 'B' :
numposition=12;
break ;
case 'C' :
numposition=11;
break ;
case 'D' :
numposition=10;
break ;
case 'E' :
numposition=9;
break ;
case 'F' :
numposition=8;
break ;
default :
cout << "Enter the letter of one of the pits" ;
break ;
}
}
int numstones = boardvalue[numposition];
boardvalue[numposition]=0;
for (int i = 1; i < numstones; i++)
{
boardvalue[(numposition + i)] += 1;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool finished(vector <int > displayboard)
{
if (displayboard[1] ==0 && displayboard[2] ==0 && displayboard[3] ==0 && displayboard[4] ==0 && displayboard[5] ==0 && displayboard[6] ==0 ||displayboard[13] ==0 && displayboard[8] ==0 && displayboard[9] ==0 && displayboard[10] ==0 && displayboard[11] ==0 && displayboard[12] ==0)
return true ;
else
return false ;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void displayboard(vector <int > boardvalue)
{
cout << " " << boardvalue[13] << " " << boardvalue[12] << " " << boardvalue[11] << " " << boardvalue[10] << " " << boardvalue[9] << " " << boardvalue[8];
cout << endl << boardvalue[0] << " " << boardvalue [7] << endl;
cout << " " << boardvalue[1] << " " << boardvalue[2] << " " << boardvalue[3] << " " << boardvalue[4] << " " << boardvalue[5] << " " << boardvalue[6];
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int main()
{
vector <int > boardvalue(14);
int player = 1;
for (int i = 0; i <= boardvalue.size(); i ++)
{
boardvalue[i] = 4;
}
boardvalue[0] = 0; boardvalue[7] = 0;
displayboard(boardvalue);
cout << "\n A B C D E F\n" ;
while (!finished(boardvalue))
{
if (player == 1)
{
cout << "\nPlayer 1's turn!" ;
}
else
{
cout << "\nPlayer 2's turn!" ;
}
makemove(boardvalue, player);
if (player == 1)
{
cout << " A B C D E F\n" ;
}
displayboard(boardvalue);
if (player == 2)
{
cout << "\n A B C D E F\n" ;
}
if (player == 1)
{
player = 2;
}
else
player = 1;
}
return (0);
}
Specifically in makemove towards the end at
1 2 3 4 5 6
int numstones = boardvalue[numposition];
boardvalue[numposition]=0;
for (int i = 1; i < numstones; i++)
{
boardvalue[(numposition + i)] += 1;
}
It ends when the vector gets to 13, though it should keep looping until it runs out of stones.
I know there's a simple solution to the problem, and I feel really dumb because it is escaping my grasp right now.
Any other advice would be appreciated as well.
Topic archived. No new replies allowed.