-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMayinTarlasi.java
176 lines (121 loc) · 5.1 KB
/
MayinTarlasi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package projeler;
import java.util.Random;
import java.util.Scanner;
public class MayinTarlasi {
public static void main(String[] args) {
/*
MAYIN TARLASI OYUNUNU JAVA İLE YAPMA.
KOD KISIMLARINDA İLGİLİ KODUN NE YAPTIĞINA DAİR AÇIKLAMALAR BULUNMAKTADIR
*/
Scanner input = new Scanner(System.in);
int satir = 0, sutun = 0;
do {
System.out.println("kaca kaclik alanda oynamak istersiniz ? (satir ve sutun sayisi ayni olsun lutfen)\n");
System.out.print("satir : ");
satir = input.nextInt();
System.out.print("sutun : ");
sutun = input.nextInt();
} while (satir != sutun);// MAYIN TARLASI BOYUTUNU ALMA BOLUMU
int[][] array = new int[satir + 2][sutun + 2]; // MAYINLI ARRAY
int[][] GosterilenArray = new int[satir + 2][sutun + 2]; // OYUN ICINDE GOSTERILEN MAYINSIZ ARRAY
Random random = new Random();
for (int counter3 = 0; counter3 < satir * sutun / 4; counter3++) {
int x = random.nextInt(array.length - 2) + 1;
int y = random.nextInt(array.length - 2) + 1;
if (array[x][y] == 1) {
counter3--;
} else {
array[x][y] = 1;
} // MAYINI RASTGELE KORDINATLARA YERLESTIRME BOLUMU
// System.out.println(x + " " + y); //MAYININ KORDINATLARI
} // MAYİN EKLEME BOLUMU
/* for (int counter = 1; counter < array.length - 1; counter++) {
for (int counter2 = 1; counter2 < array.length - 1; counter2++) {
System.out.print(array[counter][counter2] + " ");
}
System.out.println();} // KONTROL ETME DURUMU GEREKTIGINDE MAYIN TARLASINI GOSTEREN BOLUM*/
System.out.println();
int KazanmaSayaci = 0;
int kordinatx = array.length - 1, kordinaty = array.length - 1;
try {
while (array[kordinatx][kordinaty] != 1) {
for (int counter = 1; counter < array.length - 1; counter++) {
System.out.print("\t\t\t\t");
for (int counter2 = 1; counter2 < array.length - 1; counter2++) {
System.out.print(GosterilenArray[counter][counter2] + " ");
}
System.out.println("\n");
} // OYUN ICINDEKI MAYIN HARITASINI GOSTERME BOLUMU
System.out.println();
System.out.print("x kordinatini giriniz : ");
kordinatx = input.nextInt();
System.out.print("y kordinatini giriniz : ");
kordinaty = input.nextInt();
kordinatx += 1;
kordinaty += 1;// KORDINAT ALMA BOLUMU
int sayac=1;
while (GosterilenArray[kordinatx][kordinaty] != 0) {
sayac++;
System.out.println("AYNI KORDINATI "+ sayac +" KEZ GIRDINIZ TEKRAR DENEYIN : ");
System.out.print("x kordinatini giriniz : ");
kordinatx = input.nextInt();
System.out.print("y kordinatini giriniz : ");
kordinaty = input.nextInt();
kordinatx += 1;
kordinaty += 1;
} // KORDINAT ALMA BOLUMU
if (array[kordinatx][kordinaty] == 1) {
System.out.println("\n\nGIRDIGINIZ KORDINATTA MAYIN VAR ELENDINIZ UZGUNUMM :( \n\n");
} // ELENME DURUMU
if (array[kordinatx][kordinaty] != 1) {
KazanmaSayaci++;
if (KazanmaSayaci == (satir*sutun) - (satir * sutun / 4) ) {
System.out.println("TEBRIKLERR KAZANDINIZZZ. HICBIR MAYINA BASMADAN OYUNU TAMAMLADINIZ. ");
break;
}
}
if (array[kordinatx][kordinaty] == 0) {
if (array[kordinatx + 1][kordinaty] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
if (array[kordinatx - 1][kordinaty] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
if (array[kordinatx][kordinaty + 1] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
if (array[kordinatx][kordinaty - 1] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
if (array[kordinatx + 1][kordinaty + 1] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
if (array[kordinatx - 1][kordinaty + 1] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
if (array[kordinatx - 1][kordinaty - 1] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
if (array[kordinatx + 1][kordinaty - 1] == 1) {
GosterilenArray[kordinatx][kordinaty] += 1;
}
} // KONTROL BOLUMU
if (array[kordinatx][kordinaty] == 0) {
GosterilenArray[kordinatx][kordinaty]=-1;
System.out.println("\n\nTEBRIKLER GIRDIGINIZ KORDINATTA MAYIN YOK YENI KORDINATLARI GIRINIZ\n\n");// DEVAM ETME DURUMU
}
}
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("\n\nGIREBILECEGINIZ SAYILARIN DISINA CIKTINIZ LUTFEN 0 (0 DAHIL) ILE "
+ (array.length - 2) + " ARASINDA SAYI GIRINIZ \n\n");
} // KULLANCIDAN KORDINATLARI ALMA VE KONTROL BOLUMU
for (int counter = 1; counter < array.length - 1; counter++) {
System.out.print("\t\t\t\t");
for (int counter2 = 1; counter2 < array.length - 1; counter2++) {
System.out.print(array[counter][counter2] + " ");
}
System.out.println("\n");
} // OYUN SONU MAYIN TARLASINI GOSTERME BOLUMU
}
}