Commit 9ffcfd7e authored by ferhat tamer's avatar ferhat tamer 💬
Browse files

Initial commit

parents
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int tablo [4][4];
int tabloSatir[] ={1,0,-1,0};
int tabloKolon []={0,1,0,-1};
pair<int,int> BosAlanOlustur(){
int Alan=1, satir, kolon;
while(Alan){
satir=rand() % 4;
kolon=rand() % 4;
if(tablo[satir][kolon] == 0)
Alan=0;
}
return make_pair(satir,kolon);
}
void Ekle(){
pair<int, int> gonder = BosAlanOlustur();
tablo[gonder.first][gonder.second] = 2;
}
void YeniOyun()
{
for(int i=0;i<4;++i)
for(int j=0;j<4;++j)
tablo[i][j]=0;
Ekle();
}
void printMenu(){
system("cls");
for(int i=0;i<4;++i){
for(int j=0;j<4;++j)
if(tablo[i][j]==0)
cout<< setw(4) << ".";
else
cout<< setw(4)<<tablo[i][j];
cout<< "\n";
}
cout<<"n: yeni oyun, w: yukari, s: asagi, d: sag, a: sol, q: quit \n";
}
bool hareketEttir(int satir, int kolon,int nextSatir, int nextKolon){
if(nextSatir < 0 || nextKolon < 0 || nextSatir >= 4 || nextKolon >= 4 || tablo[satir][kolon] != tablo[nextSatir][nextKolon] && tablo[nextSatir][nextKolon] != 0)
return false;
return true;
}
void yonlendir(int yon){
int baslaSatir = 0, baslaKolon = 0, satirSira = 1, kolonSira = 1;
if(yon == 0){
baslaSatir=3;
satirSira = -1;
}
if(yon == 1){
baslaKolon = 3;
kolonSira = -1;
}
int hareket, Ekle1 = 0;
do{
hareket=0;
for(int i =baslaSatir;i >= 0 && i < 4;i += satirSira)
for(int j=baslaKolon;j >= 0 && j < 4;j += kolonSira)
{
int nextI = i + tabloSatir[yon], nextJ = j + tabloKolon[yon];
if(tablo[i][j] && hareketEttir(i,j,nextI,nextJ)) {
tablo[nextI][nextJ] += tablo[i][j];
tablo[i][j] = 0;
hareket=Ekle1=1;
}
}
printMenu();
}while(hareket);
if(Ekle1)
Ekle();
}
int main()
{
srand(time(0));
char islemCevir[128];
islemCevir['s']=0;
islemCevir['d']=1;
islemCevir['w']=2;
islemCevir['a']=3;
YeniOyun();
while(true) {
printMenu();
char islem;
cin>>islem;
if(islem == 'n')
YeniOyun();
else if (islem == 'q')
break;
else {
int yonAl = islemCevir[islem];
yonlendir(yonAl);
}
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment