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