السلام عليكم و رحمه الله
عندي سوال في لعبة X O .... انا كتبت methods اللي تحدد الفايز ,,, يعني checkdiagonal و checkcheckhorizontal و checkvertical اذا اللاعب الاول كمل الخط المفروض اللعبة تلقائيا توقف و تتطلع نتيجة انه اللاعب الاول فاز
المشكلة ماشي خطأ واضح ... بس مع ذلك الmethods ما تشتغل يعني لو اللاعب الاول فاز اللعبة ماتوقف !!
و هذا الكود و انا استخدم برنامج ال NetBeans:
package Tictactoe;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ituser
*/
import java.util.*;
public class Tictactoe {
private static final int ROWS = 3;
private static final int COLUMNS = 3;
private String[][] board;
//construct an empty board
public Tictactoe() {
board = new String[ROWS][COLUMNS];
//Fill with spaces
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
board[j] = " ";
}
}
}
//set the fields in the board
public void set(int i, int j, String player) {
if (board[j].equals(" ")) {
board[j] = player;
}
}
//set the fields in the board
public void check(String player) {
boolean found = false;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[j] == player) {
if (j == COLUMNS - 1) {
found = true;
}
continue;
} else {
break;
}
}
}
}
public boolean checkAll(String player) {
if (checkdiagonalsLeft(player)) { return true;
}if (checkdiagonalsRight(player)){ return true;
}if (checkcheckhorizontal(player)){ return true;
}if (checkvertical(player)){ return true;
}else {
return false;
}
}
public boolean checkdiagonalsLeft(String player) {
boolean found = false;
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board == player) {
count = i;
continue;
} else {
break;
}
}
if (count == ROWS - 1) {
found = true;
}
}
return found;
}
public boolean checkdiagonalsRight(String player) {
boolean found = false;
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[2-i] == player) {
count = i;
continue;
} else {
break;
}
}
if (count == ROWS - 1) {
found = true;
}
}
return found;
}
public boolean checkcheckhorizontal(String player) {
boolean found = false;
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[j] == player) {
count = i;
continue;
} else {
break;
}
}
if (count == ROWS - 1) {
found = true;
}
}
return found;
}
public boolean checkvertical(String player) {
boolean found = false;
int count = 0;
for (int j = 0; j < COLUMNS; j++) {
for (int i = 0; i < ROWS; i++) {
if (board[j] == player) {
count = i;
continue;
} else {
break;
}
}
if (count == ROWS - 1) {
found = true;
}
}
return found;
}
/**creates a string representation of the board
* @return the string representation
*/
public String toString() {
String r = "";
for (int i = 0; i < ROWS; i++) {
r += "|";
for (int j = 0; j < COLUMNS; j++) {
r += board[j];
}
r += "|\n";
}
return r;
}
public static void main(String[] args) {
int counter = 0;
Scanner in = new Scanner(System.in);
String player = "x";
Tictactoe game = new Tictactoe();
boolean done = false;
while (!done) {
System.out.println(game.toString());
System.out.print("Row for " + player + " (-1 to exit): ");
int row = in.nextInt();
if (row < 0) {
done = true;
} else {
System.out.print("Column for " + player + ": ");
int column = in.nextInt();
game.set(row, column, player);
counter++;
game.check(player);
if (player.equals("x")) {
player = "o";
} else {
player = "x";
}
}
if (game.checkAll(player)) {
break;
}
}//while
System.out.println("Result: ");
}
}أتمنى حد يقدر يساعدني و يحددلي الخطأ وين بالضبط
