Jump to content
  • Advertisement
Sign in to follow this  
link161

i'm so proud right now, Tell me how this c++ is(it's a mess)

This topic is 2855 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

ok I made this program, my first time with classes tell me how I can shape it up

#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

class mboard{
private:
char board[8][8];
public:
mboard(){
cout<<"\nCreating the board...\n\n";
}
~mboard(){
}
//setting the positions
void setb(char pl,int plx,int ply){
for(int i=0;i<=7;i++)
for(int j=0;j<=7;j++)
board[j]=0;
board[0][7]='*';
for(int i=0;i<=7;i++)
board[0]='*';
for(int i=0;i<=7;i++)
board[0]='*';
for(int i=0;i<=7;i++)
board[7]='*';
for(int i=0;i<=7;i++)
board[7]='*';

for(int i = 0;i<=5;i++)
board[2]='*';
for(int i=4;i<=7;i++)
board[2]='*';
for(int i=4;i<=7;i++)
board[4]='*';
for(int i=5;i<=7;i++)
board[4]='*';

board[ply][plx]=pl; //setting the player position


}

//Drawing the board
void drawboard(){
for(int i=0;i<=7;i++){
for(int j=0;j<=7;j++)
cout<<board[j];
cout<<endl;
}
}

//checking for collision based on the movement key from the movement function
bool coll(int x,int y,char move){



if( move=='w')
if(board[y-1][x]!='*' )
return false;


if( move=='s')
if(board[y+1][x]!='*' )
return false;

if( move=='a')
if(board[y][x-1]!='*' )
return false;

if( move=='d')
if(board[y][x+1]!='*' )
return false;

return true;
}

};



int main(){
int plx=3,ply=6;
char pl='0',sel;
mboard mainb;

//main loop while the selection is not 'q'
do{
mainb.setb(pl,plx,ply);
mainb.drawboard();
cout<<endl<<"x= "<<plx<<" y= "<<ply<<endl<<endl;
sel=getch();

//movement code
if(sel=='w'&&mainb.coll(plx,ply,sel)==false)
ply--;

if(sel=='s'&&mainb.coll(plx,ply,sel)==false)
ply++;

if(sel=='a'&&mainb.coll(plx,ply,sel)==false)
plx--;

if(sel=='d'&&mainb.coll(plx,ply,sel)==false)
plx++;
system("cls");

}while(sel!='q');
system("pause");
}


[Edited by - link161 on December 27, 2010 9:10:56 PM]

Share this post


Link to post
Share on other sites
Advertisement
It would get better results if you put your code in source tags. Right now it isn't the easiest to read and kinda hurts the eyes.

Share this post


Link to post
Share on other sites
First you can put your code into source blocks so it isn't a big non-indented mess. :)

Good job for first time. Off to a good start. keep doing and learning. Get a good book on object oriented programming and you'll be right on track.

(EDIT: Ninja'd like a champ! )
[/source]

Share this post


Link to post
Share on other sites
ha sorry about that, here that should be better. So I guess next I want to be able to update the screen without the input. and hopefully soon, a pong game than pacman clone.

Share this post


Link to post
Share on other sites
Just my personal opinion and how I prefer to code is don't be afraid to use brackets, it can help to read your code for others and yourself especially when you have a lot of nested statements and loops.

so for example instead of

for(int x = 0; x < 10; x++)
for(int y = 0; y < 10; y++)
that[x][y] = 0




to be honest one isn't so bad, but when you have lots it can start to look messy. So instead of the above, you could try.

for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 10; y++)
{
that[x][y] = 0
}
}



Again, this is just my personal opinion on making the readability of the code easier for the reader.

EDIT** Also congrats on using classes for the first time!

Share this post


Link to post
Share on other sites
Actually wicked, I completely agree with you, it wasn't until I took this programming class last semester that I got in to that habit. Which just came from practicing ifs so much and doing the exercises in class to finish em' first, it's my speedy programming habit lol

Share this post


Link to post
Share on other sites
I don't know if you're still looking for feedback, but if so, perhaps you could repost your code. (For some reason, adding 'source' tags to existing text - or something like that - causes angle brackets to be displayed as their HTML equivalents, which makes the code difficult to read.)

Share this post


Link to post
Share on other sites
I'll do it for him:
#include<iostream>
#include<string>
#include<conio.h>

using namespace std;

class mboard{
private:
char board[8][8];
public:
mboard(){
cout<<"\nCreating the board...\n\n";
}
~mboard(){
}
//setting the positions
void setb(char pl,int plx,int ply){
for(int i=0;i<=7;i++)
for(int j=0;j<=7;j++)
board[j]=0;
board[0][7]='*';
for(int i=0;i<=7;i++)
board[0]='*';
for(int i=0;i<=7;i++)
board[0]='*';
for(int i=0;i<=7;i++)
board[7]='*';
for(int i=0;i<=7;i++)
board[7]='*';

for(int i = 0;i<=5;i++)
board[2]='*';
for(int i=4;i<=7;i++)
board[2]='*';
for(int i=4;i<=7;i++)
board[4]='*';
for(int i=5;i<=7;i++)
board[4]='*';

board[ply][plx]=pl; //setting the player position


}

//Drawing the board
void drawboard(){
for(int i=0;i<=7;i++){
for(int j=0;j<=7;j++)
cout<<board[j];
cout<<endl;
}
}

//checking for collision based on the movement key from the movement function
bool coll(int x,int y,char move){



if( move=='w')
if(board[y-1][x]!='*' )
return false;


if( move=='s')
if(board[y+1][x]!='*' )
return false;

if( move=='a')
if(board[y][x-1]!='*' )
return false;

if( move=='d')
if(board[y][x+1]!='*' )
return false;

return true;
}

};



int main(){
int plx=3,ply=6;
char pl='0',sel;
mboard mainb;

//main loop while the selection is not 'q'
do{
mainb.setb(pl,plx,ply);
mainb.drawboard();
cout<<endl<<"x= "<<plx<<" y= "<<ply<<endl<<endl;
sel=getch();

//movement code
if(sel=='w'&&mainb.coll(plx,ply,sel)==false)
ply--;

if(sel=='s'&&mainb.coll(plx,ply,sel)==false)
ply++;

if(sel=='a'&&mainb.coll(plx,ply,sel)==false)
plx--;

if(sel=='d'&&mainb.coll(plx,ply,sel)==false)
plx++;
system("cls");

}while(sel!='q');
system("pause");
}

Share this post


Link to post
Share on other sites
Oh, thank you alpha. Yes I'm always looking for more feedback, or things you would have done differently and why etc. Just to get a better feel for what direction to take when I rewrite all this

Share this post


Link to post
Share on other sites
Well, it's a bit hard to read. Documentation and spacing are important (if not others, for yourself and your professors). Most of it is just formatting, but it's still fairly clear what the program is and how it works. I know how it's like to work with classes in c++ (it's not the nicest thing in the world), and this is certainly a start. You seem to have followed proper OO design, too (unless I missed something). Keep working at it.

On a side note, just to get a scope of college/university programming, what did you cover in your previous programming class?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!