The Daily Click ::. Forums ::. Non-Klik Coding Help ::. Mancala in an Hour
 

Post Reply  Post Oekaki 
 

Posted By Message

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
19th April, 2004 at 22:30:57 -

I was bored today, so I whipped up a short 2-player Mancala/Bantumi game in C++ in about an hour. Pretty easy to make, so if you're new to C++ then it might be good to have a browse through, or try creating your own, otherwise all you need to do is put the code into the files specified in bold, then compile to play.

mancala.cpp

// Mancala

// Michael Hadzic

#include "mancala.h"

int main()
{
system(CLEAR);
cout << HEDNG;

string strPlr1 = getString("Player 1 Name: ");
string strPlr2 = getString("Player 2 Name: ");

board objBord(strPlr1, strPlr2);

while(objBord.isPlaying())
{
objBord.getNextMove();
}

objBord.getResults();
system(PAUSE);

return 0;
}

string getString(string prmMesg)
{
string strInpt;

cout << prmMesg;
cin >> strInpt;

return strInpt;
}
mancala.h

#include <iostream>

#include <string>
#include "board.h"

using namespace std;

#define CLEAR "cls"
#define PAUSE "pause"
#define HEDNG "---------\n Mancala\n---------\n\n"

string getString(string);
board.cpp

#include "board.h"


board::board(string prmPlr1, string prmPlr2)
:strPlr1(prmPlr1), strPlr2(prmPlr2), blnPlr1(true)
{
for(int a = 0; a < 14; a++)
intPots[a] = 4;

intPots[getTranslation(-1, true)] = 0;
intPots[getTranslation(-1, false)] = 0;
}

int board::getAdjacent(int prmNmbr)
{
switch(prmNmbr)
{
case 0: return 12;
case 1: return 11;
case 2: return 10;
case 3: return 9;
case 4: return 8;
case 5: return 7;
case 6: return 13;
case 7: return 5;
case 8: return 4;
case 9: return 3;
case 10: return 2;
case 11: return 1;
case 12: return 0;
case 13: return 6;
default: return -1;
}
}

void board::getHeading()
{
system(CLEAR);

for(int a = 0; a < strPlr1.length() + strPlr2.length() + 15; a++)
cout << "-";

cout << "\n Mancala: " << strPlr1 << " vs " << strPlr2 << "\n";

for(a = 0; a < strPlr1.length() + strPlr2.length() + 15; a++)
cout << "-";

cout << "\n\n";
return;
}

void board::getMove()
{
int intMove;

do
{
intMove = getNumber();
} while(intPots[getTranslation(intMove)] == 0);

setMove(intMove);
return;
}

void board::getNextMove()
{
print();
getMove();
getNextTurn();

return;
}

void board::getNextTurn()
{
if(blnPlr1)
blnPlr1 = false;
else
blnPlr1 = true;

return;
}

int board::getNumber()
{
int intInpt;
string strInpt;

do
{
if(blnPlr1)
cout << strPlr1 << ", Select a Pot (1-6): ";
else
cout << strPlr2 << ", Select a Pot (1-6): ";

cin >> strInpt;
intInpt = atoi(strInpt.c_str());
} while(intInpt < 1 || intInpt > 6);

return intInpt;
}

void board::getPots()
{
int a;

// Player 1

if(blnPlr1)
for(a = 1; a < 7; a++)
cout << " " << a;

cout << "\n";

for(a = 1; a < 7; a++)
if(intPots[getTranslation(a, true)] < 10)
cout << " [ " << intPots[getTranslation(a, true)] << "]";
else
cout << " [" << intPots[getTranslation(a, true)] << "]";

cout << "\n\n";

// Scoring Pots

if(intPots[getTranslation(-1, true)] < 10)
cout << "[ " << intPots[getTranslation(-1, true)] << "]";
else
cout << "[" << intPots[getTranslation(-1, true)] << "]";

for(a = 0; a < 30; a++)
cout << " ";

if(intPots[getTranslation(-1, false)] < 10)
cout << "[ " << intPots[getTranslation(a, false)] << "] ";
else
cout << "[" << intPots[getTranslation(a, false)] << "] ";

cout << "\n\n";

// Player 2

for(a = 6; a > 0; a--)
if(intPots[getTranslation(a, false)] < 10)
cout << " [ " << intPots[getTranslation(a, false)] << "]";
else
cout << " [" << intPots[getTranslation(a, false)] << "]";

cout << "\n";

if(!blnPlr1)
for(a = 6; a > 0; a--)
cout << " " << a;

// Final

cout << "\n\n";
return;
}

void board::getResults()
{
setResults();
getHeading();
cout << strPlr1 << ":\t" << intPots[getTranslation(-1, true)] << "\n";
cout << strPlr2 << ":\t" << intPots[getTranslation(-1, false)] << "\n\n";

if(intPots[getTranslation(-1, true)] > intPots[getTranslation(-1, false)])
cout << strPlr1 << " is the winner!" << endl;
else if(intPots[getTranslation(-1, false)] > intPots[getTranslation(-1, true)])
cout << strPlr2 << " is the winner!" << endl;
else
cout << "It's a tie!" << endl;

return;
}

void board::getStatus(int prmLast)
{
pause();
print(0);

if(prmLast == getTranslation(-1))
{
getNextTurn();
return;
}

if(intPots[prmLast] == 1 && isOwnSide(prmLast))
{
getSteal(prmLast);
return;
}

return;
}

void board::getSteal(int prmLast)
{
intPots[prmLast] = 0;
intPots[getTranslation(-1)] += intPots[getAdjacent(prmLast)] + 1;
intPots[getAdjacent(prmLast)] = 0;

return;
}

int board::getTranslation(int prmNmbr)
{
return getTranslation(prmNmbr, blnPlr1);
}

int board::getTranslation(int prmNmbr, bool prmPlr1)
{
if(prmPlr1)
{
switch(prmNmbr)
{
case 1: return 5;
case 2: return 4;
case 3: return 3;
case 4: return 2;
case 5: return 1;
case 6: return 0;
default: return 6;
}
}
else
{
switch(prmNmbr)
{
case 1: return 12;
case 2: return 11;
case 3: return 10;
case 4: return 9;
case 5: return 8;
case 6: return 7;
default: return 13;
}
}
}

int board::getWrap(int prmNmbr)
{
int intNmbr = prmNmbr;

while(intNmbr >= 14)
intNmbr -= 14;

return intNmbr;
}

bool board::isOwnSide(int prmNmbr)
{
if(blnPlr1 && prmNmbr >= getTranslation(1, true) && prmNmbr <= getTranslation(6, true))
return true;

if(!blnPlr1 && prmNmbr >= getTranslation(1, false) && prmNmbr <= getTranslation(6, false))
return true;

return false;
}

bool board::isPlaying()
{
bool blnMov1 = false;
bool blnMov2 = false;

for(int a = 1; a < 7; a++)
{

if(intPots[getTranslation(a, true)] > 0)
blnMov1 = true;

if(intPots[getTranslation(a, false)] > 0)
blnMov2 = true;
}

if(blnMov1 && blnMov2)
return true;

return false;
}

void board:ause()
{
int intTime = time(NULL);
while(time(NULL) - intTime < 1);
return;
}

void board:rint()
{
getHeading();
getPots();

return;
}

void board:rint(int prmMovs)
{
print();
cout << "Moves Left: " << prmMovs << endl;

return;
}

void board::setMove(int prmMove)
{
int intTran = getTranslation(prmMove);
int intMovs = intPots[intTran];
int intLast = intTran;

intPots[intTran] = 0;

for(int a = 1; a <= intMovs; a++)
{
int intPosn = getWrap(intTran + a);
intLast = intPosn;


if(blnPlr1 && intPosn == getTranslation(-1, false))
{
intMovs++;
}
else if(!blnPlr1 && intPosn == getTranslation(-1, true))
{
intMovs++;
}
else
{
pause();
intPots[intPosn]++;
}

print(intMovs - a);
}

getStatus(intLast);
}

void board::setResults()
{
for(int a = 1; a <= 6; a++)
{
intPots[getTranslation(-1, true)] += intPots[getTranslation(a, true)];
intPots[getTranslation(-1, false)] += intPots[getTranslation(a, false)];
}

return;
}
board.h

#include <iostream>

#include <string>
#include <ctime>

using namespace std;

#define CLEAR "cls"

class board
{
string strPlr1;
string strPlr2;
bool blnPlr1;
int intPots[14];
public:
board(string, string);
int getAdjacent(int);
void getHeading();
void getMove();
void getNextMove();
void getNextTurn();
int getNumber();
void getPots();
void getResults();
void getStatus(int);
void getSteal(int);
int getTranslation(int);
int getTranslation(int, bool);
int getWrap(int);
bool isOwnSide(int);
bool isPlaying();
void pause();
void print();
void print(int);
void setMove(int);
void setResults();
};
Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
19th April, 2004 at 22:34:38 -

Please note I said "new to C++" not "new to programming"

Also, dont post complaining that something doesnt work properly, or whatever. Like I said, I made it in an hour so I didnt do a great deal of testing.

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
20th April, 2004 at 04:08:08 -

COMPILED EXE AND SOURCE CODE:
http://www.create-games.com/shadowcaster/mancala.zip

INSTRUCTIONS:
It seems some people have never heard of Mancala or Bantumi (obviously, then, you dont have a Nokia phone ) so here are some instructions...

1. Players start with six pots containing four beans each. Players also have a seventh "scoring pot" which starts empty. The layout looks like this:

  [ 4]  [ 4]  [ 4]  [ 4]  [ 4]  [ 4]

[ 0] [ 0]
[ 4] [ 4] [ 4] [ 4] [ 4] [ 4]
2. Players take turns selecting a pot from which the beans are taken, and placed one-by-one into the other pots in an anti-clockwise direction. For example:

  [ 5]  [ 5]  [ 5]  [ 0]  [ 4]  [ 4]

[ 1] [ 0]
[ 4] [ 4] [ 4] [ 4] [ 4] [ 4]
3. The player with the most beans in the scoring pot at the end wins.

4. Beans cannot be placed in the other players scoring pot.

5. If the last bean ends in the scoring pot (as in the above example), the player receives another turn.

6. If the last bean ends in a pot on the same players side which contains no other beans, the final bean is moved to the scoring pot along with all the beans in the other players adjacent pot. For example, if the pot with 2 beans in the below example is selected, then the player will receive 10 points:

  [ 0]  [ 5]  [ 2]  [ 0]  [ 4]  [ 5]

[ 2] [ 1]
[ 9] [ 5] [ 5] [ 0] [ 6] [ 5]
7. A game ends when either player has no beans left in any of their six pots. If, at this time, the other player still has beans remaining, then those beans are added to the other players score.

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

Cazra

Crazy?

Registered
  24/07/2002
Points
  4472

Game of the Week WinnerVIP Member
20th April, 2004 at 05:35:41 -

neat game there. Can't compile the source code though since Borland is being idiotic again.

 
n/a

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
20th April, 2004 at 05:43:15 -

I've never used Borland, only VC++, LCC, GCC and G++ for compiling applications in C/C++.

LCC, GCC and G++ are free, but I guess since the EXE is in the zip anyway you dont really need to compile the code unless you want to change it ^__^ You get some people that swear by Borland, so I guess it must be pretty good. I'll have to check it out sometime.

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

Deleted User
20th April, 2004 at 09:22:50 -

Lol, void board:rint(int prmMovs)



 

AsparagusTrevor

Mine's a pint of the black stuff

Registered
  20/08/2002
Points
  2364

Game of the Week WinnerHas Donated, Thank You!VIP MemberEvil kliker
20th April, 2004 at 10:26:44 -

I used to love Bantumi on my old Nokia 3330 phone, but then I got a new phone and they've bloody took it off. Thanks for making a PC version.

 
Image

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
20th April, 2004 at 14:25:07 -

Haha, i had this game on my Nokia as well. Couldn't figure it out, but it was fun

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
21st April, 2004 at 02:44:10 -

Thanks guys ^__^

Pyry: ?

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert
   

Post Reply



 



Advertisement

Worth A Click