C++ Simple Game for Beginners
Complete Source Code...
100% running code
Simple C++ Projects For 1st 2nd Semester Students
========================================================================
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <time.h>
using namespace std;
main()
{ int Arr[10][10];
int x,y;
cout<<"Enter values in array\n";
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int a=rand()%9;
int b=rand()%9;
//cout<<a<<" "<<b;
for(int i=0;i<10;i++)
{
cout<<endl;
cout<<"\t\t\t";
for(int j=0;j<10;j++)
{
Arr[i][j]=1;
if(i==a&&j==b)
{
Arr[i][j]=0;
}
cout<<Arr[i][j]<<" ";
}
cout<<endl;
}
x=a;y=b;
while(1)
{
//move right
int f;
f=getch();
if (f == 77 || f == 75 || f == 80 || f == 72)
{
system("cls");
if(f==77)
{
swap(Arr[x][y],Arr[x][y+1]);
for(int i=0;i<10;i++)
{
cout<<endl;
cout<<"\t\t\t";
for(int j=0;j<10;j++)
{
cout<<Arr[i][j]<<" ";
}
cout<<endl;
}
y++;
}
else if(f==75)
{
swap(Arr[x][y-1],Arr[x][y]);
for(int i=0;i<10;i++)
{
cout<<endl;
cout<<"\t\t\t";
for(int j=0;j<10;j++)
{
cout<<Arr[i][j]<<" ";
}
cout<<endl;
}
y--;
}
//move down
else if(f==80)
{
swap(Arr[x][y],Arr[x+1][y]);
for(int i=0;i<10;i++)
{
cout<<endl;
cout<<"\t\t\t";
for(int j=0;j<10;j++)
{
cout<<Arr[i][j]<<" ";
}
cout<<endl;
}
x++;
}
//move up
else if(f==72)
{
swap(Arr[x-1][y],Arr[x][y]);
for(int i=0;i<10;i++)
{
cout<<endl;
cout<<"\t\t\t";
for(int j=0;j<10;j++)
{
cout<<Arr[i][j]<<" ";
}
cout<<endl;
}
x--;
}
}
}
}
========================================================================
0 comments:
Post a Comment