Commit a67ada9e authored by amir's avatar amir

some javadocs

parent 6da59d43
...@@ -10,9 +10,12 @@ public class Main { ...@@ -10,9 +10,12 @@ public class Main {
Player1 play1 = new Player1(); Player1 play1 = new Player1();
Player2 play2 = new Player2(); Player2 play2 = new Player2();
players.display(play1.getArr1(), play2.getArr2()); players.display(play1.getArr1(), play2.getArr2());
while ((play1.getNumberOfDisks1() + play2.getNumberOfDisks2() < 64) && while ((play1.getNumberOfDisks(play1.getArr1()) + play2.getNumberOfDisks(play2.getArr2()) < 64) &&
(play1.getNumberOfDisks1() != 0) && (play2.getNumberOfDisks2() != 0)) (play1.getNumberOfDisks(play1.getArr1()) != 0) && (play2.getNumberOfDisks(play2.getArr2()) != 0))
{ {
if ((play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) == 0) &&
(play2.numberOfPlayerPossibleMoves(play1.getArr1(), play2.getArr2()) == 0))
break;
turns++; turns++;
if (turns %2 == 1){ if (turns %2 == 1){
if (play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) != 0) { if (play1.numberOfPlayerPossibleMoves(play2.getArr2(), play1.getArr1()) != 0) {
...@@ -28,7 +31,6 @@ public class Main { ...@@ -28,7 +31,6 @@ public class Main {
System.out.println("Player2, it's your turn!"); System.out.println("Player2, it's your turn!");
else { else {
System.out.println("pass"); System.out.println("pass");
turns++;
continue; continue;
} }
} }
...@@ -50,6 +52,10 @@ public class Main { ...@@ -50,6 +52,10 @@ public class Main {
players.display(play1.getArr1(), play2.getArr2()); players.display(play1.getArr1(), play2.getArr2());
//System.out.println("player1: "+play1.getNumberOfDisks1()+" player2:"+play2.getNumberOfDisks2()); //System.out.println("player1: "+play1.getNumberOfDisks1()+" player2:"+play2.getNumberOfDisks2());
} }
if (play1.getNumberOfDisks(play1.getArr1()) > play2.getNumberOfDisks(play2.getArr2()))
System.out.println("Player1 wins!");
else
System.out.println("Player2 wins!");
} }
......
package com.company; package com.company;
public class Player1 extends Players { public class Player1 extends Players {
private int numberOfDisks1;
int[][] arr1; int[][] arr1;
public Player1(){ public Player1(){
arr1 = new int[9][9]; arr1 = new int[9][9];
arr1[4][5] = 1; arr1[4][5] = 1;
arr1[5][4] = 1; arr1[5][4] = 1;
numberOfDisks1 = 0;
} }
public int[][] getArr1() { public int[][] getArr1() {
return arr1; 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; package com.company;
public class Player2 extends Players { public class Player2 extends Players {
private int numberOfDisks2;
int[][] arr2; int[][] arr2;
public Player2(){ public Player2(){
arr2 = new int[9][9]; arr2 = new int[9][9];
arr2[4][4] = 1; arr2[4][4] = 1;
arr2[5][5] = 1; arr2[5][5] = 1;
numberOfDisks2 = 0;
} }
public int[][] getArr2() { public int[][] getArr2() {
return arr2; 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;
}
} }
...@@ -4,12 +4,27 @@ package com.company; ...@@ -4,12 +4,27 @@ package com.company;
import java.util.Scanner; import java.util.Scanner;
public class Players { public class Players {
// the number of possible players saves here
private int possibleMoves; private int possibleMoves;
// the player's number of disks
private int numberOfDisks;
/**
* Creates a player and initials the possible moves with zero
*/
public Players(){ public Players(){
possibleMoves = 0; possibleMoves = 0;
} }
/**
* gets an array(player1 or player2) and a coordinate and checks
* if the coordinate contains in the array
* @param arr the array to check it's values
* @param x the x of the coordinate
* @param y the y of the coordinate
* @return if contains returns true and otherwise returns false
*/
public boolean containsInArray(int[][] arr, int x, int y){ public boolean containsInArray(int[][] arr, int x, int y){
if (arr[x][y] != 0) if (arr[x][y] != 0)
return true; return true;
...@@ -17,6 +32,14 @@ public class Players { ...@@ -17,6 +32,14 @@ public class Players {
return false; return false;
} }
/**
* this method gets the coordinate of a blank space an checks if there is
* a opponent disc,and then if exists for each of opponent disks does the equalize method
* @param arrOpponent list of opponent discs' coordinates
* @param arr list of the player disc's coordinates
* @param x the x of the blank space
* @param y the y of the blank space
*/
public void searchInNeighbors(int[][] arrOpponent, int[][] arr, int x, int y){ public void searchInNeighbors(int[][] arrOpponent, int[][] arr, int x, int y){
if (x != 1){ if (x != 1){
if (containsInArray(arrOpponent, x - 1, y)){ if (containsInArray(arrOpponent, x - 1, y)){
...@@ -55,26 +78,30 @@ public class Players { ...@@ -55,26 +78,30 @@ public class Players {
if (containsInArray(arrOpponent, x, y + 1)) if (containsInArray(arrOpponent, x, y + 1))
equalize(arrOpponent, arr, x, y, 0, +1); equalize(arrOpponent, arr, x, y, 0, +1);
} }
} }
/**
* displays the place of black and white discs and blanks
* @param a1 array of the player1's discs
* @param a2 array of the player2's discs
*/
public void display(int[][] a1, int[][] a2){ public void display(int[][] a1, int[][] a2){
System.out.println(" A B C D E F G H"); System.out.println(" A B C D E F G H");
for (int i = 1; i <= 8; i++){ for (int i = 1; i <= 8; i++){
System.out.print(i+ " "); System.out.print(i+ " ");
for (int j = 1; j <= 8; j++) { for (int j = 1; j <= 8; j++) {
if (containsInArray(a2, j, i)) { if (containsInArray(a2, j, i)) {
// the unicode of white circle * // the unicode of white circle (there is a player2 disc)
System.out.print('\u25CB'); System.out.print('\u25CB');
} }
else { else {
if (containsInArray(a1, j, i)) { if (containsInArray(a1, j, i)) {
// the unicode of black circle // the unicode of black circle (there is a player1 disc)
System.out.print('\u26AB'); System.out.print('\u25CF');
} }
else else
// the unicode of white square // the unicode of white square (a blank)
System.out.print('\u2B1C'); System.out.print('\u25A1');
} }
System.out.print(" "); System.out.print(" ");
} }
...@@ -82,6 +109,15 @@ public class Players { ...@@ -82,6 +109,15 @@ public class Players {
} }
} }
/**
*
* @param arrOpponent
* @param arr
* @param x
* @param y
* @param moveX
* @param moveY
*/
public void equalize(int[][] arrOpponent, int[][]arr, int x, int y, int moveX, int moveY) { public void equalize(int[][] arrOpponent, int[][]arr, int x, int y, int moveX, int moveY) {
if (checkTheMoveIfPossible(arrOpponent, arr, x, y, moveX, moveY)) { if (checkTheMoveIfPossible(arrOpponent, arr, x, y, moveX, moveY)) {
arr[x][y] = 1; arr[x][y] = 1;
...@@ -95,6 +131,13 @@ public class Players { ...@@ -95,6 +131,13 @@ public class Players {
} }
} }
/**
* this method returns the number of possible moves by finding blanks and
* checking sides that if there is a possible move
* @param arrOpponent the coordinates of opponent player's discs
* @param arr the coordinates of the player's discs
* @return if there is a possible move returns 1 and else returns 0
*/
public int numberOfPlayerPossibleMoves(int[][] arrOpponent, int[][]arr){ public int numberOfPlayerPossibleMoves(int[][] arrOpponent, int[][]arr){
for (int i = 1; i < 9; i++){ for (int i = 1; i < 9; i++){
for (int j = 1; j < 9; j++){ for (int j = 1; j < 9; j++){
...@@ -121,6 +164,14 @@ public class Players { ...@@ -121,6 +164,14 @@ public class Players {
return possibleMoves; return possibleMoves;
} }
/**
*
* @param arrOpponent
* @param arr
* @param x
* @param y
* @return
*/
public boolean checkSides(int[][] arrOpponent, int[][] arr, int x, int y){ public boolean checkSides(int[][] arrOpponent, int[][] arr, int x, int y){
if (x != 1){ if (x != 1){
if (containsInArray(arrOpponent, x - 1, y)){ if (containsInArray(arrOpponent, x - 1, y)){
...@@ -170,6 +221,16 @@ public class Players { ...@@ -170,6 +221,16 @@ public class Players {
return false; return false;
} }
/**
*
* @param arrOpponent
* @param arr
* @param x
* @param y
* @param moveX
* @param moveY
* @return
*/
public boolean checkTheMoveIfPossible(int[][] arrOpponent, int[][]arr, int x, int y, int moveX, int moveY){ public boolean checkTheMoveIfPossible(int[][] arrOpponent, int[][]arr, int x, int y, int moveX, int moveY){
int x1 = x; int y1 = y; int x1 = x; int y1 = y;
x1 += moveX; x1 += moveX;
...@@ -189,4 +250,23 @@ public class Players { ...@@ -189,4 +250,23 @@ public class Players {
return false; return false;
} }
/**
* gets a player's array and returns the number of it's discs
* @param arr player1(2)'s array
* @return the number of discs
*/
public int getNumberOfDisks(int[][] arr) {
numberOfDisks = 0;
for (int i = 1; i <= 8; i++){
for (int j = 1; j <= 8; j++){
if (arr[j][i] != 0) {
//System.out.println("x = " + j + "y = " + i);
numberOfDisks++;
}
}
}
//System.out.println(numberOfDisks1);
return numberOfDisks;
}
} }
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