Skip to content

Commit 37158e4

Browse files
authored
Add files via upload
1 parent 9b7c9d9 commit 37158e4

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

Java/MineSweeper/bin/Main.class

1.04 KB
Binary file not shown.
2.7 KB
Binary file not shown.

Java/MineSweeper/src/Main.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// bu videodan yapılmıştır: https://www.youtube.com/watch?v=1vBEwDZrN_U&list=PLEcJSEQK_cD5KHgg9sXumeg659hAr2j4W&index=58
2+
import java.util.Scanner;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) {
7+
8+
Scanner scan = new Scanner(System.in);
9+
int row, col;
10+
11+
System.out.println("Mayın Tarlası Oyununa Hoşgeldiniz.");
12+
System.out.println("Lütfen oynamak istediğiniz oyunun boyutları giriniz:(örn: satır 5, sutun 4)");
13+
System.out.print("Satır Sayısı : ");
14+
row = scan.nextInt();
15+
System.out.print("Sutun Sayısı : ");
16+
col = scan.nextInt();
17+
18+
mineSweeper mine = new mineSweeper(row, col);
19+
mine.run();
20+
21+
}
22+
23+
24+
}

Java/MineSweeper/src/mineSweeper.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import java.util.Random;
2+
import java.util.Scanner;
3+
4+
public class mineSweeper {
5+
6+
int rowNumber,colNumber,size;
7+
int [][] map; //mayınların nerede tutulduğunu bildiğimiz tarla
8+
int [][] board;//boş tarla - kullanıcının oynadığı
9+
boolean game = true;
10+
11+
Random random = new Random(); //mayınları random yerleşt.
12+
Scanner scan = new Scanner(System.in);
13+
14+
mineSweeper(int rowNumber, int colNumber) {
15+
this.rowNumber = rowNumber;
16+
this.colNumber = colNumber;
17+
this.map = new int[rowNumber][colNumber];
18+
this.board = new int[rowNumber][colNumber];
19+
this.size = rowNumber * colNumber;
20+
}
21+
22+
public void run() {
23+
int row,col,success = 0;
24+
prepareGame();
25+
System.out.println("---------------");
26+
System.out.println("oyun başladı");
27+
28+
while(game) {
29+
print(board);
30+
System.out.println(" ");
31+
System.out.print("Satır : ");
32+
row = scan.nextInt();
33+
System.out.print("Sutun : ");
34+
col = scan.nextInt();
35+
36+
if(row < 0 || row >= rowNumber) {
37+
System.out.println("Geçersiz koordinat girdiniz");
38+
continue;
39+
}
40+
41+
if(col < 0 || col >= colNumber) {
42+
System.out.println("Geçersiz koordinat girdiniz");
43+
continue;
44+
}
45+
46+
if(map[row][col] != -1) {
47+
check(row,col);
48+
success++;
49+
if(success == (size - (size / 4))) {
50+
System.out.println("Başarılı, Hİçbir Mayına Basmadınız.");
51+
break;
52+
}
53+
}else {
54+
game = false;
55+
System.out.println("---------------");
56+
System.out.println("---GAME OVER---");
57+
System.out.println("---------------");
58+
System.out.println("Mayınların Konumu ");
59+
print(map);
60+
}
61+
}
62+
}
63+
64+
public void check(int row, int col) {
65+
if(board[row][col] == 0) {
66+
if((col < colNumber -1) && (map[row][col+1] == -1)) {
67+
board[row][col]++;
68+
}
69+
if((row < rowNumber-1) && (map[row+1][col] == -1)) {
70+
board[row][col]++;
71+
}
72+
if((row > 0) && (map[row-1][col] == -1)) {
73+
board[row][col]++;
74+
}
75+
if((col > 0)&& (map[row][col-1] == -1)) {
76+
board[row][col]++;
77+
}
78+
if(board[row][col] == 0) {
79+
board[row][col] = -2;
80+
}
81+
}
82+
}
83+
84+
//rastgele mayin oluşturcağız
85+
public void prepareGame() {
86+
int randomRow, randomCol, count = 0;
87+
while(count != (size / 4)) {
88+
randomRow = random.nextInt(rowNumber);
89+
randomCol = random.nextInt(colNumber);
90+
if(map[randomRow][randomCol] !=-1) {
91+
map[randomRow][randomCol] = -1;
92+
count++;
93+
}
94+
}
95+
96+
}
97+
98+
// Tarlanın mayınlı konumunu ekrana yazdırma
99+
public void print(int[][] arr) {
100+
for(int i = 0; i < arr.length; i++) {
101+
for(int j = 0; j < arr[0].length; j++) {
102+
if(arr[i][j] >= 0 )
103+
System.out.print(" ");
104+
System.out.print(arr[i][j]+ " ");
105+
}
106+
System.out.println();
107+
}
108+
109+
}
110+
111+
}

0 commit comments

Comments
 (0)