Hello,
I was wandering if you had any good code that you could share with me? I would like code that does something like a game or something. I dont think I can run graphics though.
Thanks....![]()
![]()
This is a discussion on does anyone have good code???? within the C++ Programming forums, part of the General Programming Boards category; Hello, I was wandering if you had any good code that you could share with me? I would like code ...
Hello,
I was wandering if you had any good code that you could share with me? I would like code that does something like a game or something. I dont think I can run graphics though.
Thanks....![]()
![]()
heres some that i did for someone the other day. There is nothing secret about it lol so you can have it.
Code:Board.h #ifndef BOARD_H #define BOARD_H class Board { private: char TheBoard [10][10]; int HurkleX; int HurkleY; void Miss(int,int); void Hit(); void Display(int,int)const; void Win(int,int)const; char* Direction(int,int)const; public: Board(); ~Board() {} void Play(); }; #endif Board.cpp #include "Board.h" #include "ScreenFuncs.h" #include <iostream> #include <ctime> #include <cstdlib> #include <cctype> using namespace std; Board::Board() { srand ((unsigned)time(NULL)); HurkleX=rand()%10; HurkleY=rand()%10; for (int i=0;i<10;i++) for(int j=0;j<10;j++) TheBoard [i][j]='.'; } void Board::Miss(int x,int y) { TheBoard [y][x]='X'; gotoxy(2,20); cout<<"Missed the Hurkle but he's "<<Direction(x,y)<<" from there. "<<flush; } void Board::Hit() { TheBoard [HurkleY][HurkleX]='H'; } void Board::Win(int x,int y)const { gotoxy(x,y); cout<<"You have won!!!! You found the Hurkle!!!! "<<endl; } void Board::Display(int x,int y)const { gotoxy(x,y); cout<<" 0 1 2 3 4 5 6 7 8 9"<<flush; y++; gotoxy(x,y); for(int i=0;i<10;i++) { cout<<i<<" "<<flush; for(int j=0;j<10;j++) { cout<<TheBoard[i][j]<<" "; if (j==9) { cout<<flush; y++; } } gotoxy(x,y); } } void Board::Play() { int Tries=10,x=0,y=0; do { Display(2,2); gotoxy(2,14); cout<<"Where do you think the Hurkle lies...."<<endl<<" Enter X Position (0-9) ? \b\b\b\b\b"; cin>>x; cout<<" Enter Y Position (0-9) ? \b\b\b\b\b"; cin>>y; if (x<0 || x>9)continue; if (y<0 || y>9)continue; if (x==HurkleX && y==HurkleY) { Hit(); Display(2,2); Win(2,20); break; } else { Miss(x,y); } Tries--; gotoxy(2,18); cout<<"Tries left = "<<Tries; } while(Tries); if (x==HurkleX && y==HurkleY) return; gotoxy(2,20); cout<<"Sorry you have lost.Better luck next time. "<<endl<<" The Hurkle was at X-Pos :- "<<HurkleX<<" and Y-Pos :- "<<HurkleY<<endl; } char* Board::Direction(int x,int y)const { int DistX=(HurkleX-x); int DistY=(HurkleY-y); if(abs(DistX)>abs(DistY)) if (DistX < 0) return "LEFT"; else return "RIGHT"; if(abs(DistX)<abs(DistY)) if (DistY < 0) return "UP"; else return "DOWN"; if (abs(DistX)==abs(DistY)) { int a=rand()%2; if (a) { if (DistX < 0) return "LEFT"; else return "RIGHT"; } else { if (DistY < 0) return "UP"; else return "DOWN"; } } } ScreenFuncs.h // function prototypes void clrscr(); void gotoxy(int,int); ScreenFuncs.cpp // includes #include <windows.h> #include "ScreenFuncs.h" // clearscreen function void clrscr() { COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); } // gotoxy function void gotoxy(int x, int y) { COORD point; point.X = x; point.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point); } Main.cpp #include "Board.h" int main() { Board Game; Game.Play(); return 0; }
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi