Corways Game solution
3 posters
Page 1 of 1
Corways Game solution
- Code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<windows.h>
using namespace std;
void display(int a[11][11]);
void check(int a[11][11],int i,int j);
void delay();
int count(int a[11][11],int i,int j);
/*gotoxy was removed from <conio.h>
Hence, the function is defined using
other inbuilt functions from <windows.h>*/
void gotoxy (int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int a[11][11]={};
/*create an array with all cells in the boundary
having 2 as their value(they will not be displayed).
Thus, we can generalise the condition checking for all cells.*/
for(int i=0,j=0;j<11;j++)
{
a[i][j]=2;
a[j][i]=2;
}
for(int i=10,j=10;i>0;i--)
{
a[i][j]=2;
a[j][i]=2;
}
int n;
/*enter ur initial conditions here or
include a block which allows u to do so*/
a[6][3]=1;
a[6][4]=1;
a[6][5]=1;
a[5][3]=1;
a[5][4]=1;
a[5][5]=1;
a[3][3]=1;
a[3][4]=1;
a[3][5]=1;
display(a);
cout<<"Enter the number of times to be run: ";
cin>>n;
for(int k=1;k<=n;k++)
{
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
check(a,i,j);
}
delay();
display(a);
cout<<"\n\n";
}
}
cout<<"\n\n\n";
return 0;
}
void check(int a[11][11],int i,int j)
{
if(a[i][j]==1)
{
int c=count(a,i,j);
if(c<2 || c>3)
{
a[i][j]=0;
}
}
if(a[i][j]==0)
{
int c=count(a,i,j);
if(c==3)
{
a[i][j]=1;
}
}
return;
}
void display(int a[11][11])
{
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
gotoxy(j,i);
cout<<a[i][j];
}
}
cout<<endl;
}
void delay()
{
for(int i=0;i<100;i++)
for(int j=0;j<100;j++)
for(int k=0;k<1000;k++);
}
int count(int a[11][11],int i,int j)
{
int count=0;
for(int k=i-1;k<=i+1;k++)
{
for(int l=j-1;l<=j+1;l++)
{
if((i!=k||j!=l)&&a[k][l]==1)
{
count++;
}
}
}
return count;
}
Last edited by Anup Atluri on Wed Aug 24, 2011 12:12 am; edited 1 time in total
Anup Atluri- Posts : 5
Points : 10
Reputation : 0
Join date : 2011-08-14
Re: Corways Game solution
#include<windows.h>
its windows programming can you tell me where can i execute a simple windows programing code.. as i tried in visual c++ iam getting some errors like include stdfix.h....
its windows programming can you tell me where can i execute a simple windows programing code.. as i tried in visual c++ iam getting some errors like include stdfix.h....
nagashayan- Posts : 2
Points : 4
Reputation : 0
Join date : 2011-08-16
Age : 32
Location : bangalore
Re: Corways Game solution
nagashayan wrote:#include<windows.h>
its windows programming can you tell me where can i execute a simple windows programing code.. as i tried in visual c++ iam getting some errors like include stdfix.h....
u can use codeblocks for windows !!! that's where the above program was run !!
Anup Atluri- Posts : 5
Points : 10
Reputation : 0
Join date : 2011-08-14
Re: Corways Game solution
anup, from next time can u please post the details of the question, i had to google it to know wat it is
tejassrinivasan- Posts : 1
Points : 1
Reputation : 0
Join date : 2011-08-30
Age : 32
Location : bangalore
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum