Commit 6da59d43 authored by amir's avatar amir

Initials

parents
Pipeline #3148 failed with stages
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int turns = 0;
Players players = new Players();
Player1 play1 = new Player1();
Player2 play2 = new Player2();
players.display(play1.getArr1(), play2.getArr2());
while ((play1.getNumberOfDisks1() + play2.getNumberOfDisks2() < 64) &&
(play1.getNumberOfDisks1() != 0) && (play2.getNumberOfDisks2() != 0))
{
turns++;
if (turns %2 == 1){
if (play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) != 0) {
System.out.println("Player1, it's your turn!");
}
else {
System.out.println("pass");
turns++;
}
}
if (turns %2 == 0){
if (play2.numberOfPlayerPossibleMoves(play1.getArr1(), play2.getArr2()) != 0)
System.out.println("Player2, it's your turn!");
else {
System.out.println("pass");
turns++;
continue;
}
}
Scanner scanner = new Scanner(System.in);
String nl = scanner.nextLine();
char[] f = nl.toCharArray();
int y = f[0] - '1' + 1;
int x = f[2] - 'A' + 1;
if ((x >= 1) && (x < 9) && (y >= 1) && (y < 9)) {
if ((!play1.containsInArray(play1.getArr1(), x, y)) && (!play2.containsInArray(play2.getArr2(), x, y))){
if (turns %2 == 1){
play1.searchInNeighbors(play2.getArr2(), play1.getArr1(), x, y);
}
else
play2.searchInNeighbors(play1.getArr1(), play2.getArr2(), x, y);
}
}
players.display(play1.getArr1(), play2.getArr2());
//System.out.println("player1: "+play1.getNumberOfDisks1()+" player2:"+play2.getNumberOfDisks2());
}
}
}
package com.company;
public class Player1 extends Players {
private int numberOfDisks1;
int[][] arr1;
public Player1(){
arr1 = new int[9][9];
arr1[4][5] = 1;
arr1[5][4] = 1;
numberOfDisks1 = 0;
}
public int[][] getArr1() {
return arr1;
}
public int getNumberOfDisks1() {
numberOfDisks1 = 0;
for (int i = 1; i <= 8; i++){
for (int j = 1; j <= 8; j++){
if (arr1[j][i] != 0) {
//System.out.println("x = " + j + "y = " + i);
numberOfDisks1++;
}
}
}
//System.out.println(numberOfDisks1);
return numberOfDisks1;
}
}
package com.company;
public class Player2 extends Players {
private int numberOfDisks2;
int[][] arr2;
public Player2(){
arr2 = new int[9][9];
arr2[4][4] = 1;
arr2[5][5] = 1;
numberOfDisks2 = 0;
}
public int[][] getArr2() {
return arr2;
}
public int getNumberOfDisks2() {
numberOfDisks2 = 0;
for (int i = 1; i <= 8; i++){
for (int j = 1; j <= 8; j++){
if (arr2[j][i] != 0) {
//System.out.println("x = " + j + "y = " + i);
numberOfDisks2++;
}
}
}
//System.out.println(numberOfDisks2);
return numberOfDisks2;
}
}
package com.company;
import java.util.Scanner;
public class Players {
private int possibleMoves;
public Players(){
possibleMoves = 0;
}
public boolean containsInArray(int[][] arr, int x, int y){
if (arr[x][y] != 0)
return true;
else
return false;
}
public void searchInNeighbors(int[][] arrOpponent, int[][] arr, int x, int y){
if (x != 1){
if (containsInArray(arrOpponent, x - 1, y)){
equalize(arrOpponent, arr, x, y, -1, 0);
}
if (y != 1){
if (containsInArray(arrOpponent, x - 1, y - 1))
equalize(arrOpponent, arr, x, y, -1, -1);
}
if (y != 8){
if (containsInArray(arrOpponent, x - 1, y + 1))
equalize(arrOpponent, arr, x, y, -1, +1);
}
}
if (x != 8){
if (containsInArray(arrOpponent, x + 1, y)){
equalize(arrOpponent, arr, x, y, +1, 0);
}
if (y != 1){
if (containsInArray(arrOpponent, x + 1, y - 1))
equalize(arrOpponent, arr, x, y, +1, -1);
}
if (y != 8){
if (containsInArray(arrOpponent, x + 1, y + 1))
equalize(arrOpponent, arr, x, y, +1, +1);
}
}
if (y != 1){
if (containsInArray(arrOpponent, x, y - 1))
equalize(arrOpponent, arr, x, y, 0, -1);
}
if (y != 8){
if (containsInArray(arrOpponent, x, y + 1))
equalize(arrOpponent, arr, x, y, 0, +1);
}
}
public void display(int[][] a1, int[][] a2){
System.out.println(" A B C D E F G H");
for (int i = 1; i <= 8; i++){
System.out.print(i+ " ");
for (int j = 1; j <= 8; j++) {
if (containsInArray(a2, j, i)) {
// the unicode of white circle *
System.out.print('\u25CB');
}
else {
if (containsInArray(a1, j, i)) {
// the unicode of black circle
System.out.print('\u26AB');
}
else
// the unicode of white square
System.out.print('\u2B1C');
}
System.out.print(" ");
}
System.out.println();
}
}
public void equalize(int[][] arrOpponent, int[][]arr, int x, int y, int moveX, int moveY) {
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, moveX, moveY)) {
arr[x][y] = 1;
while (((x >= 1) && (x <= 8) && (y >= 1) && (y <= 8))
&& (!containsInArray(arr, x + moveX, y + moveY))) {
x += moveX;
y += moveY;
arr[x][y] = 1;
arrOpponent[x][y] = 0;
}
}
}
public int numberOfPlayerPossibleMoves(int[][] arrOpponent, int[][]arr){
for (int i = 1; i < 9; i++){
for (int j = 1; j < 9; j++){
if ((!containsInArray(arr, j, i)) && (!containsInArray(arrOpponent, j, i))){
//System.out.print(j);
//System.out.println(i);
if (checkSides(arrOpponent, arr, j, i)) {
//System.out.println("x ="+j+"y ="+i);
return 1;
}
}
/*else {
if (containsInArray(arr, j, i))
System.out.println("khoditrue");
else
System.out.println("khodifalse");
if (containsInArray(arrOpponent, j, i))
System.out.println("ghkhoditrue");
else
System.out.println("ghkhodifalse");
}*/
}
}
return possibleMoves;
}
public boolean checkSides(int[][] arrOpponent, int[][] arr, int x, int y){
if (x != 1){
if (containsInArray(arrOpponent, x - 1, y)){
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, -1, 0))
return true;
}
if (y != 1){
if (containsInArray(arrOpponent, x - 1, y - 1))
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, -1, -1))
return true;
}
if (y != 8){
if (containsInArray(arrOpponent, x - 1, y + 1))
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, -1, +1))
return true;
}
}
if (x != 8){
if (containsInArray(arrOpponent, x + 1, y)){
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, +1, 0))
return true;
}
if (y != 1){
if (containsInArray(arrOpponent, x + 1, y - 1))
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, +1, -1))
return true;
}
if (y != 8){
if (containsInArray(arrOpponent, x + 1, y + 1)) {
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, +1, +1))
return true;
}
}
}
if (y != 1){
if (containsInArray(arrOpponent, x, y - 1))
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, 0, -1))
return true;
}
if (y != 8){
if (containsInArray(arrOpponent, x, y + 1)) {
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, 0, +1))
return true;
}
}
return false;
}
public boolean checkTheMoveIfPossible(int[][] arrOpponent, int[][]arr, int x, int y, int moveX, int moveY){
int x1 = x; int y1 = y;
x1 += moveX;
y1 += moveY;
while ((x1 > 0) && (x1 < 9) && (y1 > 0) && (y1 < 9)){
if (containsInArray(arrOpponent, x1, y1)){
x1 += moveX;
y1 += moveY;
}
else{
if (containsInArray(arr, x1, y1))
return true;
else
break;
}
}
return false;
}
}
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