`package program;
import java.util.Scanner;
import entities.Tabuleiro;
public class Dama {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Tabuleiro tab = new Tabuleiro();
tab.createTable();
tab.positioPieces();
System.out.println("White plays first!");
tab.showTable();
do {
System.out.println("Please insert the column and then the line from 1 to 8:");
boolean move = false;
while(move == false){
int piecex = sc.nextInt();
int piecey = sc.nextInt();
int movex = sc.nextInt();
int movey = sc.nextInt();
move = tab.checkMovement(piecex, piecey, movex, movey);
}
tab.showTable();
}while(tab.checkVitory() == false);
System.out.println("Please insert the column and then the line from 1 to 8:");
sc.close();
}
}
`
package entities;
public class Tabuleiro {
private Integer rodada = 0;
private String table = “”;
private char[][] matriz = new char[8][8];
private char P = ‘ ‘;
private char OP = ‘ ‘;
public Tabuleiro() {
}
public void createTable() {
;
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz.length; j++) {
matriz[i][j] = '-';
}
}
}
public void showTable() {
String msg = "";
String columns = " ";
for (int s = 0; s < matriz.length; s++) {
// alphabet.charAt(s)
columns += s + " ";
}
columns += "<- Y";
for (int i = 0; i < matriz.length; i++) {
msg += " " + i + " ";
for (int j = 0; j < matriz.length; j++) {
msg += (matriz[i][j] + " ");
}
msg += "\n";
}
msg += " ^\n |\n X";
table = (columns + "\n" + msg);
System.out.println(table);
}
public void positioPieces() {
for (int i = 0; i < matriz.length; i++) {
if (i == 0 || i == matriz.length - 2) {
for (int b = 0; b < matriz.length; b += 2) {
if (i == matriz.length - 2) {
matriz[i][b] = 'W';
} else {
matriz[i][b] = 'B';
}
}
} else if (i == matriz.length - 1 || i == 1) {
for (int w = 1; w < matriz.length; w += 2) {
if (i == matriz.length - 1) {
matriz[i][w] = 'W';
} else {
matriz[i][w] = 'B';
}
}
} else {
for (int j = 0; j < matriz.length; j++) {
matriz[i][j] = '-';
}
}
}
showTable();
}
public void doMovement(int piecex, int piecey, int movex, int movey) {
// Checkando se a rodada é das brancas
if (rodada % 2 == 0) {
P = 'W';
OP = 'B';
} else {
P = 'B';
OP = 'W';
}
matriz[piecex][piecey] = '-';
matriz[movex][movey] = P;
rodada++;
}
public boolean checkMovement(int piecex, int piecey, int movex, int movey) {
// Checkando se a rodada é das brancas
if (rodada % 2 == 0) {
P = 'W';
OP = 'B';
} else {
P = 'B';
OP = 'W';
}
if (movex < 0 || movex > 7 || movey < 0 || movey > 7) {
System.out.println("Impossible move!Insert Again:");
return false;
} else {
// Checkando se o a peça escolhida é das Brancas
if (matriz[piecex][piecey] != P) {
System.out.println("Impossible move!Insert Again:");
return false;
} else {
// Checkando se a casa do movimento está vazia
if (matriz[movex][movey] != '-') {
System.out.println("Impossible move!Insert Again:");
return false;
} else {
if (checkToEat(movex, movey, piecex, piecey) == true) {
return true;
} else {
if (P == 'W') {
// Checkando se o movimento é na diagonal, de apenas uma casa e para cima
if (movex != piecex - 1 || movey != piecey + 1 && movey != piecey - 1) {
System.out.println("Impossible move!Insert Again:");
return false;
} else {
doMovement(piecex, piecey, movex, movey);
return true;
}
} else {
// Checkando se o movimento é na diagonal, de apenas uma casa e para baixo
if (movex != piecex + 1 || movey != piecey + 1 && movey != piecey - 1) {
System.out.println("Impossible move!Insert Again:");
return false;
} else {
doMovement(piecex, piecey, movex, movey);
return true;
}
}
}
}
}
}
}
public boolean checkToEat(int movex, int movey, int piecex, int piecey) {
// Checkando se a rodada é das brancas
if (rodada % 2 == 0) {
P = 'W';
OP = 'B';
} else {
P = 'B';
OP = 'W';
}
// Encontrando onde a peça poderá ser comida
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i != 0 && j != 0) {
if (piecex + (i) <= 0 || piecey + (i) <= 0 || piecex + (i) >= 7 || piecey + (i) >= 7) {
return false;
} else {
if (matriz[piecex + (i)][piecey + (j)] == OP) {
// Checkando se esse era o movimento esperado
if (piecex + (i) + (i) == movex && piecey + (j) + (j) == movey) {
// Movendo as peças
matriz[piecex][piecey] = '-';
matriz[piecex + (i)][piecey + (j)] = '-';
matriz[movex][movey] = P;
rodada++;
return true;
}
}
}
}
}
}
return false;
}
public boolean checkVitory() {
int contw = 0;
int contb = 0;
int contm = 0;
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz.length; j++) {
if (matriz[i][j] == 'W') {
contw++;
} else if (matriz[i][j] == 'B') {
contb++;
}
}
}
if (contw == 0) {
System.out.println("White wins!");
return true;
} else if (contb == 0) {
System.out.println("Black wins!");
return true;
} else {
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz.length; j++) {
if (matriz[i][j] == 'W') {
contm = 0;
for (int wi = -1; wi < 2; wi++) {
for (int wj = -1; wj < 2; wj++) {
if (wi != 0 && wj != 0) {
if (matriz[i + wi][j + wj] != '-') {
contm++;
}
}
}
}
if (contm == 0) {
System.out.println("White Wins");
return true;
} else {
return false;
}
} else if (matriz[i][j] == 'B') {
contm = 0;
for (int wi = -1; wi < 2; wi++) {
for (int wj = -1; wj < 2; wj++) {
if (wi != 0 && wj != 0) {
if (matriz[i + wi][j + wj] != '-') {
contm++;
}
}
}
}
if (contm == 0) {
System.out.println("Black Wins");
return true;
} else {
return false;
}
}
}
}
}
return false;
}
}