#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]