Commit 49970e8c authored by nargessalehi98's avatar nargessalehi98

Add inhertitance

parent c15a4c10
import java.util.ArrayList;
import java.util.Scanner;
public class GameRules { public class GameRules {
private Map map;
private String empty;
private String white;
private String black;
public GameRules() {
empty = "| |";
white = "| ⚪ |";
black = "| ⚫ |";
map = new Map();
}
public String checkColor(int row, int col) {
ArrayList<String> temp = map.getRow(row);
if (temp.get(col).equals(white))
return white;
if (temp.get(col).equals(black))
return black;
else
return empty;
}
public boolean checkFullMap() {
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 boolean checkPutDisk(int row, int col, String Color) {
int counter = 0;
for (int plusRow = -1; plusRow < 2; plusRow++) {
for (int plusCol = -1; plusCol < 2; plusCol++) {
int Row = row + plusRow;
int Col = col + plusCol;
if (!(plusCol == 0 && plusRow == 0)) {
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (!checkColor(Row, Col).equals(Color) && !checkColor(Row, Col).equals(empty)) {
while (true) {
Row += plusRow;
Col += plusCol;
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (checkColor(Row, Col).equals(Color)) {
counter++;
break;
}
} else
break;
}
}
}
}
}
}
return counter != 0;
}
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 void findWinner() {
int counterBlack = 0;
int counterWhite = 0;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (checkColor(row, col).equals(black))
counterBlack++;
if (checkColor(row, col).equals(white))
counterWhite++;
}
}
if (counterBlack > counterWhite)
System.out.println("Black won ! Number of disks: " + counterBlack);
if (counterWhite > counterBlack)
System.out.println("White won ! Number of disks: " + counterWhite);
if (counterBlack == counterWhite)
System.out.println("Equal !");
}
public void checkNeighbor(int row, int col) {
for (int plusRow = -1; plusRow < 2; plusRow++) {
for (int plusCol = -1; plusCol < 2; plusCol++) {
String main = checkColor(row, col);
int Row = row + plusRow;
int Col = col + plusCol;
if (!(plusCol == 0 && plusRow == 0)) {
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (!checkColor(Row, Col).equals(main) && !checkColor(Row, Col).equals(empty)) {
boolean check = true;
while (true) {
if (check) {
Row += plusRow;
Col += plusCol;
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (checkColor(Row, Col).equals(main)) {
while (true) {
Row -= plusRow;
Col -= plusCol;
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (checkColor(Row, Col).equals(main)) {
check = false;
break;
} else {
ArrayList<String> temp = map.getRow(Row);
temp.remove(Col);
temp.add(Col, main);
map.removeRow(Row);
map.addRow(Row, temp);
}
}
}
}
if (checkColor(Row, Col).equals(empty))
break;
} else
break;
} else
break;
}
}
}
}
}
}
}
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 = map.getColNum(colLetter);
if (col != 8) {
if (checkPutDisk(row, col, Color)) {
ArrayList<String> temp = map.getRow(row);
if (temp.get(col).equals(empty)) {
temp.remove(col);
temp.add(col, Color);
map.removeRow(row);
map.addRow(row, temp);
}
checkNeighbor(row, col);
} else {
System.out.println("choose right position !");
putDisk(Color);
}
} else {
System.out.println("wrong input !");
putDisk(Color);
}
return false;
} else {
System.out.println("pass !");
return true;
}
}
public void print() {
map.print();
}
} }
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Map map = new Map(); GameRules game = new GameRules();
map.print(); game.print();
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 :");
boolean checkBlack = map.putDisk(black); boolean checkBlack = game.putDisk(black);
map.print(); game.print();
System.out.println("White's turn :"); System.out.println("White's turn :");
boolean checkWhite = map.putDisk(white); boolean checkWhite = game.putDisk(white);
if (checkBlack && checkWhite || map.fullMap()) { if (checkBlack && checkWhite || game.checkFullMap()) {
System.out.println("game is over !"); System.out.println("game is over !");
map.findWinner(); game.findWinner();
game.print();
break; break;
} }
map.print(); game.print();
} }
} }
} }
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.SortedMap;
public class Map extends GameRules { public class Map {
private ArrayList<ArrayList> map; private ArrayList<ArrayList> map;
private String empty; private String empty;
private String white; private String white;
...@@ -33,17 +30,6 @@ public class Map extends GameRules { ...@@ -33,17 +30,6 @@ public class Map extends GameRules {
} }
} }
public boolean fullMap() {
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) {
if (c.equals("A")) if (c.equals("A"))
return 0; return 0;
...@@ -65,164 +51,18 @@ public class Map extends GameRules { ...@@ -65,164 +51,18 @@ public class Map extends GameRules {
return 8; return 8;
} }
public String checkColor(int row, int col) { public ArrayList<String> getRow(int index){
ArrayList<String> temp = map.get(row); return map.get(index);
if (temp.get(col).equals(white))
return white;
if (temp.get(col).equals(black))
return black;
else
return empty;
}
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;
for (int plusRow = -1; plusRow < 2; plusRow++) {
for (int plusCol = -1; plusCol < 2; plusCol++) {
int Row = row + plusRow;
int Col = col + plusCol;
if (!(plusCol == 0 && plusRow == 0)) {
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (!checkColor(Row, Col).equals(Color) && !checkColor(Row, Col).equals(empty)) {
while (true) {
Row += plusRow;
Col += plusCol;
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (checkColor(Row, Col).equals(Color)) {
counter++;
break;
}
} else
break;
}
}
}
}
}
}
return counter != 0;
} }
public void findWinner() { public void removeRow(int index){
int counterBlack = 0; map.remove(index);
int counterWhite = 0;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (checkColor(row, col).equals(black))
counterBlack++;
if (checkColor(row, col).equals(white))
counterWhite++;
}
}
if (counterBlack > counterWhite)
System.out.println("Black won ! Number of disks: " + counterBlack);
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);
} }
return false;
} else {
System.out.println("pass !");
return true;
}
}
public void checkNeighbor(int row, int col) {
for (int plusRow = -1; plusRow < 2; plusRow++) {
for (int plusCol = -1; plusCol < 2; plusCol++) {
String main = checkColor(row, col);
int Row = row + plusRow;
int Col = col + plusCol;
if (!(plusCol == 0 && plusRow == 0)) {
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (!checkColor(Row, Col).equals(main) && !checkColor(Row, Col).equals(empty)) {
boolean check = true;
while (true) { public void addRow(int index,ArrayList temp){
if (check) { map.add(index,temp);
Row += plusRow;
Col += plusCol;
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (checkColor(Row, Col).equals(main)) {
while (true) {
Row -= plusRow;
Col -= plusCol;
if (Row >= 0 && Row < 8 && Col >= 0 && Col < 8) {
if (checkColor(Row, Col).equals(main)) {
check = false;
break;
} else {
ArrayList<String> temp = map.get(Row);
temp.remove(Col);
temp.add(Col, main);
map.remove(Row);
map.add(Row, temp);
}
}
}
}
if (checkColor(Row, Col).equals(empty)) {
// check = false;
break;
}
// if (Row < 0 || Row > 7 || Col < 0 || Col > 7) {
// check = false;
// break;
// }
} }
} else
break;
}
}
}
}
}
}
}
public void print() { public void print() {
System.out.println(" A B C D E F G H "); System.out.println(" A B C D E F G H ");
System.out.println(" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+"); System.out.println(" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+");
...@@ -233,5 +73,5 @@ public class Map extends GameRules { ...@@ -233,5 +73,5 @@ public class Map extends GameRules {
System.out.println(" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+"); System.out.println(" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+");
} }
} }
}
}
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