Archived

This topic is now archived and is closed to further replies.

Will someone look at my game code?

This topic is 5174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I have just finished my first game. Its nothing special at all really, but I have only been c++ programming for about a month or so. It is a console version of the triangle puzzle that you can play at Crack Barrel. For those who havent played it, you hop pins over another pin until you are left with only one. Maybe someone can look at my code and tell me what I could do better or what I''ve done wrong. Thanks.

Share this post


Link to post
Share on other sites
OK here is my code. It is written using DevC++. Please dont make fun of me if it is badly written, I am new to programming. Any helpful advice much appreciated, or how I could make the game work simpler, ect. It works though.


#include <iostream>

using namespace::std;

int pins=15;


int pos[5][5]={
{1,2,3,4,5},
{6,7,8,9,0},
{11,12,13,0,0},
{16,17,0,0,0},
{21,0,0,0,0}
};


int drawgame();
int start();
int validmoves();
int checkmove(int,int);
int move();
int removepin(int);
int addpin(int);
int final(int);

int start()
{
drawgame();
cout<<"\n\nEnter start position : ";
int start;
cin>>start;
start=start-1;
int xstart, ystart;
ystart=(start/5);
xstart=(start%5);
pos[ystart][xstart]=-1;
pins--;
}

int main()
{

start ();

int gamerun=0;
int numberofvalidmoves;

while (gamerun==0)
{
numberofvalidmoves=validmoves();

if (numberofvalidmoves==0)
{
final(pins);
}

move();
}
}

int validmoves()//how many valid moves the player can make

{
int validmoves=0;
cout<<"\n";
int x,y,c;

for(int n=1;n<25; n++)
{
c=n-1;
y= c/5;
x=(c%5);



//up

if ((n==pos[y][x])&&(pos[y-2][x]==-1)&&(y-2>=0)&&(pos[y-1][x]>0))
{


validmoves++;
}
//down

if ((n==pos[y][x])&&(pos[y+2][x]==-1)&&(y+2<5)&&(pos[y+1][x]>0))
{


validmoves++;
}
//left

if ((n==pos[y][x])&&(pos[y][x-2]==-1)&&(x-2>=0)&&(pos[y][x-1]>0))
{


validmoves++;
}
//right

if ((n==pos[y][x])&&(pos[y][x+2]==-1)&&(x+2<5)&&(pos[y][x+1]>0))
{


validmoves++;
}

//upright

if ((n==pos[y][x])&&(pos[y-2][x+2]==-1)&&(x+2<5)&&(y-2>=0)&&(pos[y-1][x+1]>0))
{


validmoves++;
}

//downleft


if ((n==pos[y][x])&&(pos[y+2][x-2]==-1)&&(y+2<5)&&(x-2>=0)&&(pos[y+1][x-1]>0))
{


validmoves++;
}

}

return validmoves;
}

int drawgame()//draws the gamespace

{

cout<<"\n";
string spaces=" ";


for (int n=0;n<5; n++)
{
cout<<spaces;
for (int i=0;i<5;i++)
{
if (!pos[n][i]==0)//if number is 0 then no pinhole is there

{
//Pin present, number less than 10

if ((pos[n][i]<10)&&((n*5)+i+1<10)&&(pos[n][i]>-1))
{
cout<<"0"<<pos[n][i]<<" "<<char(248)<<" ";
}

//Pin present, number greater equal than 10

if ((pos[n][i]>=10)&&((n*5)+i+1>=10)&&(pos[n][i]>-1))
{
cout<<pos[n][i]<<" "<<char(248)<<" ";
}

//Pin not present, number less than 10


if ((pos[n][i]<10)&&((n*5)+i+1<10)&&(pos[n][i]==-1))
{
cout<<"0"<<(n*5)+i+1<<" ";
}

//Pin not present, number greater equal than 10

if ((pos[n][i]<10)&&((n*5)+i+1>=10)&&(pos[n][i]==-1))
{
cout<<(n*5)+i+1<<" ";
}
}
}

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

int move()
{
drawgame();
cout<<"\nNumber of pins left :"<<pins;
cout<<"\nTurn Number :"<<turn;

int p1,p2;



cout<<"\nEnter pin to move (0 to end game): ";
cin>>p1;

if (p1==0)//exits on 0

{
final(pins);
}

cout<<"Enter position to move to : ";
cin>>p2;

int check=checkmove(p1,p2);
if (check==0)
{
cout<<"\nInvalid Move!";
move();
}
else
{

//clear pins from here

removepin(check);
removepin(p1);
addpin(p2);
pins--;
}




}

int removepin(int pin)
{
int pinno,pinx,piny;
pinno=pin-1;
piny=pinno/5;
pinx=pinno%5;
pos[piny][pinx]=-1;
}
int addpin(int pin)
{
int pinno,pinx,piny;
pinno=pin-1;
piny=pinno/5;
pinx=pinno%5;
pos[piny][pinx]=pin;
}

int checkmove(int p1, int p2)
{
int p1x,p1y,p2x,p2y;
int c,d;
int middlepin=0;
int movemade=0;

c=p1-1;
p1y= c/5;
p1x=(c%5);

d=p2-1;
p2x=d/5;
p2y=d%5;

//up

if ((p1==pos[p1y][p1x])&&(pos[p1y-2][p1x]==-1)&&(pos[p1y-2][p1x]==pos[p2x][p2y])&&(p1y-2>=0)&&(pos[p1y-1][p1x]>0)&&(movemade==0))
{

middlepin=(p1-p2)/2+p2;
movemade=1;
}
//down

if ((p1==pos[p1y][p1x])&&(pos[p1y+2][p1x]==-1)&&(pos[p1y+2][p1x]==pos[p2x][p2y])&&(p1y+2<5)&&(pos[p1y+1][p1x]>0)&&(movemade==0))
{

middlepin=(p2-p1)/2+p1;
movemade=1;
}
//left

if ((p1==pos[p1y][p1x])&&(pos[p1y][p1x-2]==-1)&&(pos[p1y][p1x-2]==pos[p2x][p2y])&&(p1x-2>=0)&&(pos[p1y][p1x-1]>0)&&(movemade==0))
{

middlepin=(p1-p2)/2+p2;
movemade=1;
}
//right

if ((p1==pos[p1y][p1x])&&(pos[p1y][p1x+2]==-1)&&(pos[p1y][p1x+2]==pos[p2x][p2y])&&(p1x+2<5)&&(pos[p1y][p1x+1]>0)&&(movemade==0))
{

middlepin=(p2-p1)/2+p1;
cout<<endl<<middlepin;
movemade=1;
}

//upright

if ((p1==pos[p1y][p1x])&&(pos[p1y-2][p1x+2]==-1)&&(pos[p1y-2][p1x+2]==pos[p2x][p2y])&&(p1x+2<5)&&(p1y-2>=0)&&(pos[p1y-1][p1x+1]>0)&&(movemade==0))
{

middlepin=(p1-p2)/2+p2;
movemade=1;
}

//downleft


if ((p1==pos[p1y][p1x])&&(pos[p1y+2][p1x-2]==-1)&&(pos[p1y+2][p1x-2]==pos[p2x][p2y])&&(p1y+2<5)&&(p1x-2>=0)&&(pos[p1y+1][p1x-1]>0)&&(movemade==0))
{

middlepin=(p2-p1)/2+p1;
movemade=1;
}

return middlepin;
}

int final(int pinsleft)// end of the game

{
drawgame();
cout<<"\nYou finished the game with "<<pins<<" pins left.";
switch(pinsleft)
{

case 1: cout<<"\nYou cheated!"; break;
case 2: cout<<"\nYou could do better."; break;
case 3: cout<<"\nYou are not that bright."; break;
case 4: cout<<"\nYou are somewhat dumb."; break;
case 5: cout<<"\nI've met smarter cantelopes."; break;
case 6: cout<<"\nJackass."; break;
case 7: cout<<"\nYou suck!"; break;
case 8: cout<<"\nYou are so stupid I hate you."; break;
case 9: cout<<"\nWhy were you born?"; break;
case 10: cout<<"\nYou are crap at this game."; break;
case 11: cout<<"\nShoot yourself immediately."; break;
case 12: cout<<"\nIdiot"; break;
case 13: cout<<"\nI am not your friend anymore."; break;
case 14: cout<<"\nYour so bad I crapped my pants."; break;
case 15: cout<<"\nI have no response to your crapness."; break;
}
exit(0);
}





[edited by - asdasd12345 on October 16, 2003 8:34:40 PM]

Share this post


Link to post
Share on other sites
suggestion:

next time use the source tags.
ie:

[ source]

//code


[ /source]


plus when using cout it's better to use endl than \n.

oh use the tags without the spaces inbetween.



[edited by - Alpha_ProgDes on October 16, 2003 8:20:13 PM]

Share this post


Link to post
Share on other sites