ok I said you could have my code if you posted yours so here goes.... sorry about the length but I dont have a zip util on me at the mo so I will cut and paste.
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
Code:
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";
}
}
}
Code:
ScreenFuncs.h
void clrscr();
void gotoxy(int,int);
Code:
ScreenFuncs.cpp
#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);
}
Code:
Main.cpp
#include "Board.h"
int main()
{
Board Game;
Game.Play();
return 0;
}
Enjoy.