صباح الورد
انا كتبت method اللي تحدد حركة الكمبيوتر ... بس مب عارفه اسوي لهم call في main method
اتمنى حد يوضحهم لي
هذي الmethod :
public int[] nextMove(String player) {
int counter = 0;
int pos[] = new int[18];
for (int i = 0; i < 18; i++) {
pos = -1;
}
/* lucky position in the center of board*/
if (board[1][1] == " ") {
return new int[]{1, 1};
}
/* choose available move */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[j] == " ") {
pos[counter] = i;
}
pos[counter + 1] = j;
counter = counter + 2;
}
}
/* no move is available */
return pos;
}
public String preventAll() {
String block=preventdiagonalsLeft();
if (block.equals("")) {
block=preventdiagonalsRight();
if (block.equals("")) {
block=preventhorizontal();
if (block.equals("")) {
block=preventvertical();
}
}
}
return block;
}
public String preventdiagonalsLeft() {
String player = "x";
boolean found = false;
int i = 0;
int j = 0;
int count = 0;
String block="";
while (i < ROWS) {
if (board[j] == player) {
count++;
} else if (board[j] == " ") {
block = "Block: "+i+" "+j;
}
i++;
j++;
}
if (count == 2) {
found = true;
return block;
}
return "";
}
public String preventdiagonalsRight() {
String player="x";
boolean found = false;
int i = 0;
int j = 2;
int count = 0;
String block="";
while (i < ROWS) {
if (board[j] == player) {
count++;
} else if (board[j] == " ") {
block = "Block: "+i+" "+j;
}
i++;
j--;
}
if (count == 2) {
found = true;
return block;
}
return "";
}
public String preventhorizontal() {
String player="x";
boolean found = false;
int count = 0;
String block="";
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[j] == player) {
count++;
} else if (board[j] == " ") {
block = "Block: "+i+" "+j;
}
}
if (count == 2) {
found = true;
return block;
} else {
count = 0;
}
}
return "";
}
public String preventvertical() {
String player="x";
boolean found = false;
int count = 0;
String block="";
for (int j = 0; j < COLUMNS; j++) {
for (int i = 0; i < ROWS; i++) {
if (board[j] == player) {
count++;
} else if (board[j] == " ") {
block = "Block: "+i+" "+j;
}
}
if (count == 2) {
found = true;
return block;
} else {
count = 0;
}
}
return "";
}و هذا كود الmain method :
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String player = "x";
Tictactoe_cpu game = new Tictactoe_cpu();
boolean done = false;
System.out.println(game.toString());
while (!done) {
int row = 0, column = 0;
if (player.equals("o")) {
/// هني لازم اسوي call لل method
System.out.println(game.preventAll());
} else {
System.out.print("Row for " + player + " (-1 to exit): ");
row = in.nextInt();
if (row < 0) {
done = true;
break;
} else {
System.out.print("Column for " + player + ": ");
column = in.nextInt();
}}
if (game.set(row, column, player).equals(player)) {
System.out.println(game.toString());
if (game.checkAll(player)) {
System.out.println("Result: The Winner is " + player);
break;
}
if (player.equals("x")) {
player = "o";
} else {
player = "x";
}
} else {
System.out.println("Position selected!");
}
}//while
}
}