При проверке красный крест на пункте:
"Если метод checkFutureWin() вернул true, нужно вызвать метод setSignAndCheck().". Пишет: "Не выполнены все требования задачи".
Запуск без проверки проходит. Все работает в соответствии с алгоритмом. Но пройти дальше не могу.
Что делать?
package com.javarush.games.ticktacktoe;
import com.javarush.engine.cell.*;
public class TicTacToeGame extends Game {
private int[][] model = new int[3][3];
private int currentPlayer;
private boolean isGameStopped;
public void initialize() {
setScreenSize(3, 3);
startGame();
updateView();
}
public void startGame() {
for (int x=0; x<3; x++) {
for (int y=0; y<3; y++) {
model[x][y] = 0;
}
}
currentPlayer = 1;
isGameStopped = false;
}
public void updateCellView(int x, int y, int value) {
switch (value) {
case 1: setCellValueEx(x, y, Color.WHITE, "X", Color.RED);
break;
case 2: setCellValueEx(x, y, Color.WHITE, "O", Color.BLUE);
break;
default: setCellValueEx(x, y, Color.WHITE, " ", Color.WHITE);
}
}
public void updateView() {
for (int x=0; x<3; x++) {
for (int y=0; y<3; y++) {
updateCellView(x, y, model[x][y]);
}
}
}
public void setSignAndCheck(int x, int y) {
model[x][y] = currentPlayer;
updateView();
if (checkWin(x, y, currentPlayer)) {
isGameStopped = true;
if (currentPlayer == 1) {
showMessageDialog(Color.NONE, "You Win!", Color.GREEN, 75);
}
else {
showMessageDialog(Color.NONE, "Game Over", Color.RED, 75);
}
}
else if (!hasEmptyCell()) {
isGameStopped = true;
showMessageDialog(Color.NONE, " Draw!", Color.BLUE, 75);
}
}
public void onMouseLeftClick(int x, int y) {
if (model[x][y] == 0 && isGameStopped == false) {
setSignAndCheck(x, y);
currentPlayer = 3 - currentPlayer;
computerTurn();
currentPlayer = 3 - currentPlayer;
}
}
public boolean checkVertical(int y, int n) {
boolean check = true;
for (int x=0; x<3; x++) {
check = check && (model[x][y] == n);
}
return check;
}
public boolean checkHorizontal(int x, int n) {
boolean check = true;
for (int y=0; y<3; y++) {
check = check && (model[x][y] == n);
}
return check;
}
public boolean checkDiagonal(int x, int y, int n) {
boolean check = false;
if ((x + y) == 2 || x == y) {
check = true;
for (int i=0; i<3; i++) {
check = check && (model[2 - i][i] == n);
}
if (check) {
return check;
}
check = true;
for (int i=0; i<3; i++) {
check = check && (model[i][i] == n);
}
}
return check;
}
public boolean checkWin(int x, int y, int n) {
if (checkVertical(y, n)) {
return true;
}
else if (checkHorizontal(x, n)) {
return true;
}
else if (checkDiagonal(x, y, n)) {
return true;
}
return false;
}
public boolean hasEmptyCell() {
for (int x=0; x<3; x++) {
for (int y=0; y<3; y++) {
if (model[x][y] == 0) {
return true;
}
}
}
return false;
}
public void onKeyPress(Key key) {
if (key == Key.SPACE) {
if (isGameStopped) {
startGame();
updateView();
}
}
else if (key == Key.ESCAPE) {
startGame();
updateView();
}
}
public void computerTurn() {
int emptyX = 100;
int emptyY = 100;
int x = 1;
int y = 1;
if (model[x][y] == 0) {
setSignAndCheck(x, y);
}
else {
for (x=0; x<3; x++) {
for (y=0; y<3; y++) {
if (model[x][y] == 0) {
if (emptyX == 100 && emptyY == 100) {
emptyX = x;
emptyY = y;
}
if (checkFutureWin(x, y, currentPlayer)) {
setSignAndCheck(x, y);
return;
}
}
}
}
setSignAndCheck(emptyX, emptyY);
}
}
public boolean checkFutureWin(int x, int y, int n) {
if (model[x][y] == 0) {
model[x][y] = n;
boolean check = checkWin(x, y, n);
model[x][y] = 0;
return check;
}
return false;
}
}