Commit 5ae794c4 authored by nargessalehi98's avatar nargessalehi98

Add possible moves

parent c8e2482c
...@@ -6,12 +6,18 @@ public class Main { ...@@ -6,12 +6,18 @@ public class Main {
while (true) { while (true) {
String white = "| \u26AA |"; String white = "| \u26AA |";
String black = "| \u26AB |"; String black = "| \u26AB |";
System.out.println("Black's turn"); System.out.println("Black's turn :");
map.putDisk(black); boolean checkBlack = map.putDisk(black);
map.print(); map.print();
System.out.println("White's turn"); System.out.println("White's turn :");
map.putDisk(white); boolean checkWhite = map.putDisk(white);
if (checkBlack && checkWhite || map.fullMap()) {
System.out.println("game is over !");
map.findWinner();
break;
}
map.print(); map.print();
} }
} }
} }
...@@ -34,8 +34,15 @@ public class Map { ...@@ -34,8 +34,15 @@ public class Map {
} }
} }
public boolean getTurn() { public boolean fullMap() {
return turn; boolean check = true;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (checkColor(row, col).equals(empty))
check = false;
}
}
return check;
} }
public int getColNum(String c) { public int getColNum(String c) {
...@@ -69,21 +76,34 @@ public class Map { ...@@ -69,21 +76,34 @@ public class Map {
return empty; return empty;
} }
public boolean checkPutDisk(int row, int col) { public boolean checkPossibleMove(String Color) {
int counter = 0;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (checkColor(row, col).equals(empty)) {
if (checkPutDisk(row, col, Color)) {
counter++;
}
}
}
}
return counter != 0;
}
public boolean checkPutDisk(int row, int col, String Color) {
int counter = 0; int counter = 0;
for (int plusRow = -1; plusRow < 2; plusRow++) { for (int plusRow = -1; plusRow < 2; plusRow++) {
for (int plusCol = -1; plusCol < 2; plusCol++) { for (int plusCol = -1; plusCol < 2; plusCol++) {
String main = checkColor(row, col);
int Row = row + plusRow; int Row = row + plusRow;
int Col = col + plusCol; int Col = col + plusCol;
if (!(plusCol == 0 && plusRow == 0)) { if (!(plusCol == 0 && plusRow == 0)) {
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) { if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (!checkColor(Row, Col).equals(main) && !checkColor(Row, Col).equals(empty)) { if (!checkColor(Row, Col).equals(Color) && !checkColor(Row, Col).equals(empty)) {
while (true) { while (true) {
Row += plusRow; Row += plusRow;
Col += plusCol; Col += plusCol;
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) { if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (checkColor(Row, Col).equals(main)) { if (checkColor(Row, Col).equals(Color)) {
counter++; counter++;
break; break;
} }
...@@ -98,25 +118,55 @@ public class Map { ...@@ -98,25 +118,55 @@ public class Map {
return counter != 0; return counter != 0;
} }
public void putDisk(String Color) { public void findWinner() {
System.out.println("Enter Number of row and letter of column :"); int counterBlack = 0;
Scanner scan = new Scanner(System.in); int counterWhite = 0;
int row = scan.nextInt(); for (int row = 0; row < 8; row++) {
row = row - 1; for (int col = 0; col < 8; col++) {
String colLetter = scan.next(); if (checkColor(row, col).equals(black))
int col = getColNum(colLetter); counterBlack++;
if (checkPutDisk(row, col)) { if (checkColor(row, col).equals(white))
ArrayList<String> temp = map.get(row); counterWhite++;
if (temp.get(col).equals(empty)) { }
temp.remove(col); }
temp.add(col, Color); if (counterBlack > counterWhite)
map.remove(row); System.out.println("Black won ! Number of disks: " + counterBlack);
map.add(row, temp); if (counterWhite > counterBlack)
System.out.println("White won ! Number of disks: " + counterWhite);
if (counterBlack == counterWhite)
System.out.println("Equal !");
}
public boolean putDisk(String Color) {
if (checkPossibleMove(Color)) {
System.out.println("Enter Number of row and letter of column :");
Scanner scan = new Scanner(System.in);
int row = scan.nextInt();
row = row - 1;
String colLetter = scan.next();
int col = getColNum(colLetter);
if (col != 8) {
if (checkPutDisk(row, col, Color)) {
ArrayList<String> temp = map.get(row);
if (temp.get(col).equals(empty)) {
temp.remove(col);
temp.add(col, Color);
map.remove(row);
map.add(row, temp);
}
checkNeighbor(row, col);
} else {
System.out.println("choose right position !");
putDisk(Color);
}
} else {
System.out.println("wrong input !");
putDisk(Color);
} }
checkNeighbor(row, col); return false;
} else { } else {
System.out.println("choose right position"); System.out.println("pass !");
putDisk(Color); return true;
} }
} }
...@@ -130,40 +180,43 @@ public class Map { ...@@ -130,40 +180,43 @@ public class Map {
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) { if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (!checkColor(Row, Col).equals(main) && !checkColor(Row, Col).equals(empty)) { if (!checkColor(Row, Col).equals(main) && !checkColor(Row, Col).equals(empty)) {
boolean check = true; boolean check = true;
while (true) { while (true) {
if (check) { if (check) {
Row += plusRow; Row += plusRow;
Col += plusCol; Col += plusCol;
if (checkColor(Row, Col).equals(main)) { if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
while (true) { if (checkColor(Row, Col).equals(main)) {
Row -= plusRow; while (true) {
Col -= plusCol; Row -= plusRow;
if (checkColor(Row, Col).equals(main)) { Col -= plusCol;
check = false; if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
break; if (checkColor(Row, Col).equals(main)) {
} else { check = false;
ArrayList<String> temp = map.get(Row); break;
temp.remove(Col); } else {
temp.add(Col, main); ArrayList<String> temp = map.get(Row);
map.remove(Row); temp.remove(Col);
map.add(Row, temp); temp.add(Col, main);
map.remove(Row);
map.add(Row, temp);
}
}
} }
} }
} if (checkColor(Row, Col).equals(empty)) {
if (checkColor(Row, Col).equals(empty)) { // check = false;
check = false; break;
break; }
} // if (Row < 0 || Row > 7 || Col < 0 || Col > 7) {
if (Row < 0 || Row > 7 || Col < 0 || Col > 7) { // check = false;
check = false; // break;
break; // }
} }
} else } else
break; break;
} }
} }
} }
} }
} }
......
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