Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ferhat tamer
2048
Commits
9ffcfd7e
Commit
9ffcfd7e
authored
May 29, 2022
by
ferhat tamer
💬
Browse files
Initial commit
parents
Changes
1
Show whitespace changes
Inline
Side-by-side
2048.cpp
0 → 100644
View file @
9ffcfd7e
#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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment