-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The MineSweeper project was created completely from scratch with a String structure Comments line updates Fixed the selected poisiton not being 0 Mines -1 status replaced with * createBoardManager and createBoardUser method deleted and a new methods added Added width main variable to fix counter bug Null value error fixed
- Loading branch information
Showing
2 changed files
with
143 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
import java.util.Scanner; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int rowNumber,colNumber; | ||
int rowNumber, colNumber; | ||
|
||
// En az 2x2 boyutunda oyun alanı oluşturulabilir | ||
while (true){ // 7. Madde : Oyun alanının satır boyutu kullanıcı tarafından, 2 den küçük olmama şartı ile alınır | ||
System.out.println("ENTER HEIGHT: "); | ||
while (true) { // 7. Madde : Oyun alanının satır boyutu kullanıcı tarafından, 2 den küçük olmama şartı ile alınır | ||
System.out.print("ENTER HEIGHT: "); | ||
rowNumber = scanner.nextInt(); | ||
if(rowNumber < 2){ | ||
|
||
if (rowNumber < 2) { | ||
System.out.println("PLEASE ENTER A BIGGER THEN 2"); | ||
continue; | ||
} | ||
break; | ||
} | ||
while (true){ // 7. Madde : Oyun alanının sütun boyutu kullanıcı tarafından, 2 den küçük olmama şartı ile alınır | ||
System.out.println("ENTER WIDTH: "); | ||
while (true) { // 7. Madde : Oyun alanının sütun boyutu kullanıcı tarafından, 2 den küçük olmama şartı ile alınır | ||
System.out.print("ENTER WIDTH: "); | ||
colNumber = scanner.nextInt(); | ||
if(colNumber < 2){ | ||
if (colNumber < 2) { | ||
System.out.println("PLEASE ENTER A BIGGER THEN 2"); | ||
continue; | ||
} | ||
break; | ||
} | ||
MineSweeper gameCreate = new MineSweeper(rowNumber,colNumber); // 5. Madde : MineSweeper class'ı çağırılarak oyun başlatılabilir | ||
|
||
MineSweeper gameCreate = new MineSweeper(rowNumber, colNumber); // 5. Madde : MineSweeper class'ı çağırılarak oyun başlatılabilir | ||
gameCreate.goPlay(); // Oyunu başlatan metot | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,148 +1,194 @@ | ||
/** | ||
* @author Furkan Aydemir, deerborg | ||
* @since 2024 | ||
/* | ||
@author Furkan Aydemir , deerborg | ||
@since 2024 | ||
*/ | ||
|
||
import java.util.Random; | ||
import java.util.Scanner; | ||
import java.util.*; | ||
|
||
public class MineSweeper { | ||
|
||
String[][] managerMap; | ||
String[][] playerMap; | ||
int rowNumber; | ||
int colNumber; | ||
int boardSize; | ||
int[][] managerMap; | ||
int[][] userMap; | ||
public MineSweeper(int rowNumber, int colNumber) { // 5. Madde : MineSweeper kurucu metotdu içerisinde değişkenler tanımlandı | ||
this.rowNumber = rowNumber; | ||
this.colNumber = colNumber; | ||
this.managerMap = new int[rowNumber][colNumber]; | ||
this.userMap = new int[rowNumber][colNumber]; | ||
this.boardSize = rowNumber * colNumber; | ||
int mineCounter = 0; | ||
|
||
// 5. Madde : MineSweeper kurucu metodu içerisinde değişkenler tanımlandı | ||
public MineSweeper(int rowValue, int colValue) { | ||
this.managerMap = new String[rowValue][colValue]; | ||
this.playerMap = new String[rowValue][colValue]; | ||
this.rowNumber = rowValue; | ||
this.colNumber = colValue; | ||
this.boardSize = rowValue * colValue; | ||
} | ||
Random giveMine = new Random(); | ||
|
||
Random mineValue = new Random(); | ||
Scanner userValue = new Scanner(System.in); | ||
public void goPlay() { // 6. Madde : Oyun başlatma metodu | ||
|
||
int goRow , goCol,clearChoice = 0,checkAgainRow = -1,chekAgainCol = -1; | ||
// 6. Madde : Oyun başlatma metodu | ||
public void goPlay() { | ||
|
||
int goRow, goCol, clearChoice = 0, checkAgainRow = -1, chekAgainCol = -1; | ||
|
||
addMine(); | ||
createBoardManager(managerMap); // Mayınlı haritanın çıktısı | ||
System.out.println("---------------"); | ||
while(true){ | ||
createBoardPlayer(userMap); // 11. Madde : Kullanıcının her hamlesinde haritayı günceller | ||
addMines(); | ||
createBoardMap(managerMap); // Tek seferlik Mayınların gösterildiği harita | ||
System.out.println("=================="); | ||
createBoardMap(playerMap); // Tek seferlik kullanıcıya gösterilecek harita | ||
|
||
System.out.println("ENTER ROW: "); | ||
goRow = userValue.nextInt(); // 9. Madde : Kullanıcıdan alınacak olan işaretleme bilgisi | ||
while (true) { | ||
|
||
if(goRow >= rowNumber || goRow < 0){ // 10. Madde : Girilen satır bilgisinin toplam dizi alanı dışında olma durumu | ||
System.out.print("ENTER ROW: "); | ||
goRow = userValue.nextInt(); // 9. Madde : Kullanıcıdan alınacak olan işaretleme bilgisi | ||
|
||
if (goRow < 0 || goRow > rowNumber - 1) { // 10. Madde : Girilen sütun bilgisinin toplam dizi alanı dışında olma durumu | ||
System.out.println("INVALID VALUE, TRY AGAIN"); | ||
continue; | ||
} | ||
|
||
System.out.println("ENTER COL: "); | ||
goCol = userValue.nextInt(); // 9. Madde : Kullanıcıdan alınacak olan işaretleme bilgisi | ||
System.out.print("ENTER COL: "); | ||
goCol = userValue.nextInt(); // 9. Madde : Kullanıcıdan alınacak olan işaretleme bilgisi | ||
|
||
if(goCol >= colNumber || goCol < 0){ // 10. Madde : Girilen sütun bilgisinin toplam dizi alanı dışında olma durumu | ||
if (goCol < 0 || goCol > rowNumber - 1) { // 10. Madde : Girilen sütun bilgisinin toplam dizi alanı dışında olma durumu | ||
System.out.println("INVALID VALUE, TRY AGAIN"); | ||
continue; | ||
} | ||
|
||
if (checkAgainRow == goRow && chekAgainCol == goCol) { // Aynı değerleri tur içinde girerse tekrar değer girmesini ister | ||
System.out.println("SAME VALUE, TRY AGAIN"); | ||
if (chekAgainCol == goCol && checkAgainRow == goRow) { // Aynı değerleri tur içinde girerse tekrar değer girmesini ister | ||
System.out.println("YOU HAVE ENTERED REPEATED VALUES, ENTER DIFFERENT VALUES"); | ||
continue; | ||
} | ||
|
||
chekAgainCol = goCol; | ||
checkAgainRow = goRow; | ||
|
||
if(userMap[goRow][goCol] > 0){ // Daha önceden çervesinde mayın olan bir konumu açarsa ve tekrar aynı konumu girerse, tekrar değer girmesini ister | ||
System.out.println("SAME VALUE ! REPEAT SELECTION MAY BREAK GAME MECHANICS!\nGO ENTER NEW VALUE"); | ||
if (!playerMap[goRow][goCol].equals("-")) { // Daha önceden girdiği konumu tekrar girerse tekrar değer girmesini ister | ||
System.out.println("YOU ENTERED THE SAME LOCATIONS!"); | ||
continue; | ||
} | ||
// Mayın kontrolü | ||
checkMine(goRow,goCol); | ||
|
||
if(managerMap[goRow][goCol] != -1){ | ||
|
||
controlMine(goRow, goCol); | ||
if (managerMap[goRow][goCol].equals("*")) { // 13. Madde : Seçilen konumda mayın var ise oyunun sonlanacağı blok | ||
System.out.println("ITS MINE... BOOOM! GAME OVER!"); | ||
|
||
break; | ||
} else { | ||
|
||
checkMineMap(goRow, goCol); // 11. Madde : Kullanıcının her hamlesinde haritayı günceller ve mayın kontrollerini burada gösterir | ||
|
||
mineCounter = 0; | ||
|
||
System.out.println("GOOD CHOICE"); | ||
clearChoice++; | ||
if(clearChoice == boardSize - ((boardSize*25) / 100)){ // 14. Madde: %75 alanı doğru ve temiz girerse (tek seferde oyunu bitirirse) koşul bloğu | ||
|
||
if (clearChoice == boardSize - ((boardSize * 25) / 100)) { // 14. Madde : %75 alanı doğru ve temiz girerse (tek seferde oyunu bitirirse) koşul bloğu | ||
System.out.println("CONGRATULATIONS, YOU WON!!!"); | ||
userValue.close(); | ||
break; | ||
} | ||
} | ||
else{ // 13. Madde : Seçilen konumda mayın (-1) varsa kaybedeceği koşul bloğu | ||
System.out.println("GAME OVER! BAD CHOICE"); | ||
userValue.close(); | ||
break; | ||
} | ||
} | ||
} | ||
public void createBoardManager(int[][] board) { // 6. Madde : Mayınların ekleneceği ve gösterilecek olan (managerMap) metod | ||
for (int[] rowValue : board) { | ||
for (int colValue : rowValue) { | ||
if (colValue != -1) { | ||
System.out.print(" - "); | ||
} else { | ||
System.out.print(" * "); | ||
|
||
// 6. Madde : Mayınların gösterildiği ve mayınsız gösterilecek tek seferlik haritaları oluşturan metot | ||
public void createBoardMap(String[][] board) { | ||
|
||
for (int i = 0; i < board.length; i++) { | ||
for (int j = 0; j < board[i].length; j++) { | ||
if ("*".equals(board[i][j])) { | ||
System.out.print(board[i][j] + " "); | ||
} | ||
if (!"*".equals(board[i][j])) { | ||
board[i][j] = "-"; | ||
System.out.print(board[i][j] + " "); | ||
} | ||
|
||
} | ||
System.out.println(); | ||
} | ||
} | ||
public void createBoardPlayer(int[][] board) { // 6. Madde : Mayınların gösterilmediği, oyuncunun ilerleyeceği ve gösterilecek olan (userMap) metod | ||
for (int i = 0; i < board.length; i++) { | ||
for (int j = 0; j < board[i].length; j++) { | ||
if(userMap[i][j] == 0){ | ||
System.out.print(" - "); // 12. Madde : Etrafında mayın yoksa " - " olarak gözükür. | ||
}else{ | ||
System.out.print(" "+board[i][j]+" "); // 12. Madde : Kontrol edilirken çevresindeki mayınlara göre değeri yazar | ||
} | ||
|
||
// 6. Madde ve 8. Madde : Alanın 4/1 kadar rastgele mayın ekleme metodu | ||
public void addMines() { | ||
int rowMine, colMine, mineCount = 0; | ||
|
||
while (mineCount != (boardSize * 25 / 100)) { | ||
|
||
rowMine = mineValue.nextInt(rowNumber); | ||
colMine = mineValue.nextInt(colNumber); | ||
|
||
if (!"*".equals(managerMap[rowMine][colMine])) { | ||
managerMap[rowMine][colMine] = "*"; | ||
mineCount++; | ||
} | ||
System.out.println(); | ||
|
||
} | ||
|
||
} | ||
public void addMine() { // 6. Madde ve 8. Madde : Alanın 4/1 kadar rastgele mayın ekleme metodu | ||
|
||
int rowMine, colMine, count = 0; | ||
while (count != ((boardSize * 25) / 100)) { | ||
rowMine = giveMine.nextInt(rowNumber); | ||
colMine = giveMine.nextInt(colNumber); | ||
if (managerMap[rowMine][colMine] != -1) { | ||
managerMap[rowMine][colMine] = -1; | ||
count++; | ||
// 6. Madde : Mayınların gösterilmediği, oyuncunun ilerleyeceği ve güncellenecek harita metodu | ||
public void checkMineMap(int goRow, int goCol) { | ||
|
||
for (int i = 0; i < playerMap.length; i++) { | ||
for (int j = 0; j < playerMap[i].length; j++) { | ||
if (playerMap[i][j].equals(playerMap[goRow][goCol])) { | ||
playerMap[goRow][goCol] = String.valueOf(mineCounter); | ||
System.out.print(playerMap[goRow][goCol] + " "); | ||
continue; | ||
} | ||
|
||
System.out.print(playerMap[i][j] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
public void checkMine(int controlRow, int controlCol) { // 6. Madde ve 12. Madde : Oyun sırasında verilen konumun dikey olarak sağ, sol, alt, üst ve çapraz olarak sağ, sol, alt, üst konumların kontrolü metodu | ||
// Mayın olmama koşulu | ||
if (managerMap[controlRow][controlCol] == 0) { | ||
|
||
// 6. Madde ve 12. Madde : Oyun sırasında verilen konumun dikey olarak sağ, sol, alt, üst ve çapraz olarak sağ, sol, alt, üst konumların kontrolü metodu | ||
public void controlMine(int controlRow, int controlCol) { | ||
|
||
|
||
if (managerMap[controlRow][controlCol].equals("-")) { | ||
|
||
// Dikey konumlarda sağ, sol, alt, üst konumların mayın kontrolü | ||
if ((controlCol < (colNumber - 1)) && (managerMap[controlRow][controlCol + 1]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
if ((controlCol < (colNumber - 1)) && (managerMap[controlRow][controlCol + 1]).equals("*")) { | ||
mineCounter++; | ||
} | ||
if ((controlRow < (rowNumber - 1)) && (managerMap[controlRow + 1][controlCol]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
|
||
if ((controlRow < (rowNumber - 1)) && (managerMap[controlRow + 1][controlCol]).equals("*")) { | ||
mineCounter++; | ||
} | ||
if ((controlCol > 0) && (managerMap[controlRow][controlCol - 1]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
|
||
if ((controlCol > 0) && (managerMap[controlRow][controlCol - 1]).equals("*")) { | ||
mineCounter++; | ||
} | ||
if ((controlRow > 0) && (managerMap[controlRow - 1][controlCol]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
|
||
if ((controlRow > 0) && (managerMap[controlRow - 1][controlCol]).equals("*")) { | ||
mineCounter++; | ||
} | ||
|
||
// Çapraz konumlarda sağ, sol, alt, üst konumların mayın kontrolü | ||
if ((controlRow > 0) && (controlCol < (colNumber - 1)) && (managerMap[controlRow - 1][controlCol + 1]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
if ((controlRow > 0) && (controlCol < (colNumber - 1)) && (managerMap[controlRow - 1][controlCol + 1]).equals("*")) { | ||
mineCounter++; | ||
} | ||
if ((controlCol > 0) && (controlRow > 0) && (managerMap[controlRow - 1][controlCol - 1]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
|
||
if ((controlCol > 0) && (controlRow > 0) && (managerMap[controlRow - 1][controlCol - 1]).equals("*")) { | ||
mineCounter++; | ||
} | ||
if ((controlCol > 0) && (controlRow < (rowNumber - 1)) && (managerMap[controlRow + 1][controlCol - 1]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
|
||
if ((controlCol > 0) && (controlRow < (rowNumber - 1)) && (managerMap[controlRow + 1][controlCol - 1]).equals("*")) { | ||
mineCounter++; | ||
} | ||
if ((controlRow < (rowNumber - 1)) && (controlCol < (colNumber - 1)) && (managerMap[controlRow + 1][controlCol + 1]) == -1) { | ||
userMap[controlRow][controlCol]++; | ||
|
||
if ((controlRow < (rowNumber - 1)) && (controlCol < (colNumber - 1)) && (managerMap[controlRow + 1][controlCol + 1]).equals("*")) { | ||
mineCounter++; | ||
} | ||
|
||
playerMap[controlRow][controlCol] = String.valueOf(mineCounter); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|