Witam,
napisałam grę kółko i krzyżyk. Chciałam, żeby po zakończeniu gry wyświetliły się wszystkie ruchy po kolei jakie zostały wykonane w grze(pętla for each na końcu Main), niestety nie wiem jak to napisać, żeby każdy kolejny move był elementem mojej tablicy i funkcje w klasie Statistic działały. Program już mówi o błędzie w linijce 66 klasy Main (pierwsze użycie get.Statistic).
Proszę też o zerknięcie na kod, co poprawić, generalnie zastanawiałam się jeszcze nad klasą board.
package pl.com.marzena.first;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Witaj w grze kółko i krzyżyk");
System.out.println("Położenie pól w planszy kształtuje się jak na obrazku poniżej, gdzie pierwsza cyfra ozanacza położenie poziome, druga pionowe.");
System.out.println("0, 0" + "|" + "0, 1" + "|" + "0, 2");
System.out.println("----+----+----");
System.out.println("1, 0" + "|" + "1, 1" + "|" + "1, 2");
System.out.println("----+----+----");
System.out.println("2, 0" + "|" + "2, 1" + "|" + "2, 2");
Player[] players = new Player[2];
System.out.println("Podaj imię gracza nr 1: ");
String firstName = scan.nextLine();
players[0] = new Player(firstName);
System.out.println("Podaj imię gracza nr 2: ");
String secondName = scan.nextLine();
players[1] = new Player(secondName);
System.out.println("Losuję gracza...");
Game game = new Game(players);
Player playerWhoStarts = game.getPlayerWhoStarts();
System.out.println("Zaczyna gracz: " + playerWhoStarts.getName());
int i = 0;
Player secondPlayer;
if (players[0] == playerWhoStarts) {
secondPlayer = players[1];
} else {
secondPlayer = players[0];
}
Integer coordinateHorizontal;
Integer coordinateVertical;
// Move move = null;
System.out.println(i);
do {
char value;
Player playerWhoMoves;
if (i % 2 == 0) {
playerWhoMoves = playerWhoStarts;
System.out.println("Gracz: " + playerWhoMoves.getName() + "-krzyżyk");
value = 'X';
} else {
playerWhoMoves = secondPlayer;
System.out.println("Gracz: " + playerWhoMoves.getName() + "- kółko");
value = 'O';
}
System.out.println("Podaj lokalizację poziomą: ");
coordinateHorizontal = scan.nextInt();
System.out.println("Podaj lokalizację pionową: ");
coordinateVertical = scan.nextInt();
Move move = new Move(playerWhoMoves, coordinateHorizontal, coordinateVertical, value);
game.getStatistic().add(move, i);
if (move.checkIfCoordinatesAreCorrect(coordinateHorizontal, coordinateVertical)) {
if (!game.checkTheFieldIsEmpty(coordinateHorizontal, coordinateVertical)) {
game.setField(coordinateHorizontal, coordinateVertical, value);
System.out.println(game.getField(0, 0) + "|" + game.getField(0, 1) + "|" + game.getField(0, 2));
System.out.println("-+-+-");
System.out.println(game.getField(1, 0) + "|" + game.getField(1, 1) + "|" + game.getField(1, 2));
System.out.println("-+-+-");
System.out.println(game.getField(2, 0) + "|" + game.getField(2, 1) + "|" + game.getField(2, 2));
if (game.checkAllFieldsForWin()) {
System.out.println("Wygrał gracz: " + playerWhoMoves.getName());
System.exit(0);
} else if (game.checkBoardForDeadHeat()) {
System.out.println("Remis. Koniec gry");
System.exit(0);
}
i++;
} else {
System.out.println("Ta pozycja jest już zajęta, ponów ruch");
}
} else {
System.out.println("Podałeś błędną lokalizację, ponów ruch");
}
} while (!game.checkAllFieldsForWin() || !game.checkBoardForDeadHeat());
for (Move mov : game.getStatistic().findAll())
System.out.println(mov.getPlayer().getName() + " " + mov.getCoordinateHorizontal() + " " + mov.getCoordinateVertical() + " " + mov.getValue());
}
}
import java.util.Random;
public class Game {
private Player[] players;
private Statistic statistic;
private char[][] board = new char[3][3];
public Game(Player[] players) {
this.players = players;
}
public Statistic getStatistic() {
return statistic;
}
public Player getPlayerWhoStarts() {
Random random = new Random();
int indexOfPlayer = random.nextInt(players.length);
return players[indexOfPlayer];
}
public void setField(Integer horizontalCoordinate, Integer verticalCoordinate, char value) {
board[horizontalCoordinate][verticalCoordinate] = value;
}
public char getField(Integer horizontalCoordinate, Integer verticalCoordinate) {
return board[horizontalCoordinate][verticalCoordinate];
}
public boolean checkIfTheFieldsAreTheSame(char firstField, char secondField, char thirdField) {
return (firstField != '\u0000') && (firstField == secondField) && (secondField == thirdField);
}
public boolean checkColumnsForWin() {
for (int i = 0; i <= 2; i++) {
if (checkIfTheFieldsAreTheSame(board[i][0], board[i][1], board[i][2])) {
return true;
}
}
return false;
}
public boolean checkRowForWin() {
for (int i = 0; i <= 2; i++) {
if (checkIfTheFieldsAreTheSame(board[0][i], board[1][0], board[2][i])) return true;
}
return false;
}
public boolean checkDiagonalsForWin() {
return ((checkIfTheFieldsAreTheSame(board[0][0], board[1][1], board[2][2])) || (checkIfTheFieldsAreTheSame(board[0][2], board[1][1], board[2][0])));
}
public boolean checkBoardForDeadHeat() {
char[][] boardSecondary = new char[3][3];
if (getField(0, 0) != '\u0000' && getField(0, 1) != '\u0000' && getField(0, 2) != '\u0000' && getField(1, 0) != '\u0000' && getField(1, 1) != '\u0000' && getField(1, 2) != '\u0000' && getField(2, 0) != '\u0000' && getField(2, 1) != '\u0000' && getField(2, 2) != '\u0000') {
return true;
}
return false;
}
public boolean checkAllFieldsForWin() {
if (checkColumnsForWin() || checkDiagonalsForWin() || checkRowForWin()) {
return true;
}
return false;
}
public boolean checkTheFieldIsEmpty(Integer horizontalCoordinate, Integer verticalCoordinate) {
return (board[horizontalCoordinate][verticalCoordinate] == 'X' || board[horizontalCoordinate][verticalCoordinate] == 'O');
}
}
public class Move {
private Player player;
private Integer coordinateHorizontal;
private Integer coordinateVertical;
private char value;
public Move(Player player, Integer coordinateHorizontal, Integer coordinateVertical, char value) {
this.player = player;
this.coordinateHorizontal = coordinateHorizontal;
this.coordinateVertical = coordinateVertical;
this.value = value;
}
public Player getPlayer() {
return player;
}
public Integer getCoordinateHorizontal() {
return coordinateHorizontal;
}
public Integer getCoordinateVertical() {
return coordinateVertical;
}
public char getValue() {
return value;
}
public boolean checkIfCoordinatesAreCorrect(Integer coordinateHorizontal, Integer coordinateVertical) {
return (coordinateHorizontal <= 2 && coordinateHorizontal >= 0 && coordinateVertical <= 2 && coordinateVertical >= 0);
}
}
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Statistic {
private Move[] moves;
public Statistic(Move[] moves) {
this.moves = moves;
}
public void add(Move move, int i) {
moves[i] = move;
}
public Move[] findAll() {
return moves;
}
}