/*
yilan-oyunu v1.0 Klasik yilan oyunu (Turbo C ile derlenip DOS altında calistirilmali).
Copyright (C) 2005 Engin KUZU

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

//http://www.gnu.org/copyleft/gpl.html
//Turkce cevirisi: http://www.belgeler.org/howto/gpl_copy.html
//Web: http://www.enginkuzu.org

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

char dizi[4000][2];
char yem[2];
char son[2];

int max;
int yon;
int sayac;

int i;
char c,hata;

void ilk_degerleri_ata()
{
	dizi[0][0]=9;
	dizi[0][1]=12;
	dizi[1][0]=8;
	dizi[1][1]=12;
	dizi[2][0]=7;
	dizi[2][1]=12;
	dizi[3][0]=6;
	dizi[3][1]=12;
	dizi[4][0]=5;
	dizi[4][1]=12;
	dizi[5][0]=4;
	dizi[5][1]=12;
	dizi[6][0]=3;
	dizi[6][1]=12;
	
	yem[0]=22;
	yem[1]=22;
	
	max=6;
	yon=80;
	randomize();
	_setcursortype(_NOCURSOR);
}
void karsilama_ekrani()
{
	clrscr();
	gotoxy(22,10);	printf("Oyunda yon tuslari ile ilerlenir");
	gotoxy(20,12);	printf("ESC ==> Cikis   P ==> Oyunu duraklat");
	gotoxy(28,16);	printf("Yilan Oyunu v1.0");
	gotoxy(26,35);	printf("ENTER ile devam edin...");
	getch();
}
void cerceveyi_ciz()
{
	clrscr();
	gotoxy(1,1);	printf("%c",201);
	gotoxy(80,1);	printf("%c",187);
	gotoxy(1,49);	printf("%c",200);
	gotoxy(80,49);	printf("%c",188);
	
	for(i=2;i<80;i++)
	{
		gotoxy(i,1);	printf("%c",205);
		gotoxy(i,49);	printf("%c",205);
	}
	for(i=2;i<49;i++)
	{
		gotoxy(1,i);	printf("%c",186);
		gotoxy(80,i);	printf("%c",186);
	}    
}
void yilan_ve_yemi_ciz()
{
	for(i=0;i<max;i++)
	{
		gotoxy(dizi[i][0],dizi[i][1]);
		printf("%c",'*');
	}
	gotoxy(yem[0],yem[1]);
	printf("%c",'+');
}
void bekle_ve_tuslari_yakala()
{
    sayac=0;
    while(sayac<32000)	//bekleme ve basilan tusu alma bolumu
	{
		if( kbhit() )
		{
			c=getch();
			if( c==77 && yon!=75 )
				yon=77;
			else if ( c==72 && yon!=80 )
				yon=72;
			else if ( c==80 && yon!=72 )
				yon=80;
			else if ( c==75 && yon!=77 )
				yon=75;
			else if ( c==27 )
			{
				gotoxy(22,22);
				printf("ESC tusu ile oyundan ciktiniz.");
				getch();
				exit(0);
			}
			else if ( c=='p' )
			{
				while(1)
				{
					if( kbhit() )
					{
						c=getch();
						if( c=='p')
							break;
					}
				}
			}
		}
		sayac++;
	}    
}
void yilan_bogumlarinin_koordinatlarini_ilerlet()
{
	son[0]=dizi[max][0];
	son[1]=dizi[max][1];
	for(i=max;i>0;i--)
	{
		dizi[i][0]=dizi[i-1][0];
		dizi[i][1]=dizi[i-1][1];
	}    
}    
void yilanin_ilk_bogumunu_ilerletme_islemleri()
{
    if( yon==77 )
		dizi[0][0]++;
	else if( yon==75 )
		dizi[0][0]--;
	else if( yon==72 )
		dizi[0][1]--;
	else
		dizi[0][1]++;
}
void yilan_duvara_carpma_kontrol()
{
	if( dizi[0][0]<2 || dizi[0][0]>79 )
	{
        gotoxy(22,22);
		printf("Duvara carptin! Oyun sona erdi.");
		getch();
		exit(0);
	}
	else if( dizi[0][1]<2 || dizi[0][1]>48 )
	{
		gotoxy(22,22);
		printf("Duvara carptin! Oyun sona erdi.");
		getch();
		exit(0);
	}
}
void yilan_kendine_carpma_kontrol()
{
	for(i=max;i>0;i--)
	{
		if( dizi[i][0]==dizi[0][0] && dizi[i][1]==dizi[0][1] )
		{
			gotoxy(22,22);
			printf("Yilan kendisine carpti! Oyun sona erdi.");
			getch();
			exit(0);
		}
	}
}
void yilanin_en_arka_bogumunu_ekrandan_sil()
{
	gotoxy(son[0],son[1]);
	printf(" ");
}
void yilan_yemi_alma_kontrol()
{
    if( yem[0]==dizi[0][0] && yem[1]==dizi[0][1] )
	{
		hata=1;
		max++;
		dizi[max][0]=son[0];
		dizi[max][1]=son[1];

		while(hata)
		{
			hata=0;
			yem[0]=rand()%78+2;
			yem[1]=rand()%47+2;
			for(i=0;i<max+1;i++)
			{
				if( dizi[i][0]==yem[0] && dizi[i][1]==yem[1] )
				{
					hata=1;
					break;
				}
			}
			if(!hata)
			{
				gotoxy(yem[0],yem[1]);
				printf("%c",'+');
			}
		}
    }
	else
		yilanin_en_arka_bogumunu_ekrandan_sil();
}
void yilani_ekrana_ciz()
{
	gotoxy(dizi[0][0],dizi[0][1]);
	printf("%c",'*');
}

int main()
{
	ilk_degerleri_ata();
	karsilama_ekrani();
	cerceveyi_ciz();
	yilan_ve_yemi_ciz();

	while(1)
	{
        bekle_ve_tuslari_yakala();
		yilan_bogumlarinin_koordinatlarini_ilerlet();
		yilanin_ilk_bogumunu_ilerletme_islemleri();
		yilan_duvara_carpma_kontrol();
		yilan_kendine_carpma_kontrol();
		yilan_yemi_alma_kontrol();
	    yilani_ekrana_ciz();
	}
	
	getch();
	return 0;
}
