V2 is V1 shortened
parent
5ea64d5ac3
commit
e7f3576bb8
|
|
@ -84,7 +84,7 @@ public class TicTacToe_AC1 {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean gameHasEnded() {
|
public static boolean gameHasEnded() {
|
||||||
return noMovesLeft() || gameWasWon();
|
return gameWasWon() || noMovesLeft();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean noMovesLeft() {
|
public static boolean noMovesLeft() {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,130 @@
|
||||||
|
package Uebung_02;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class TicTacToe_AC2 {
|
||||||
|
|
||||||
|
public static final String EMPTYFIELD = " ";
|
||||||
|
public static final String X = "X";
|
||||||
|
public static final String O = "O";
|
||||||
|
public static String[] gameFields = new String[9];
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
initializeGameStatus();
|
||||||
|
computerMove();
|
||||||
|
printGrid();
|
||||||
|
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
while(true) {
|
||||||
|
humanMove(scanner);
|
||||||
|
printGrid();
|
||||||
|
if(gameHasEnded()) {
|
||||||
|
System.out.println("\nDu hast gewonnen!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
computerMove();
|
||||||
|
printGrid();
|
||||||
|
if(gameHasEnded()) {
|
||||||
|
System.out.println("\nDer Roboter hat gewonnen!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void initializeGameStatus() {
|
||||||
|
Arrays.fill(gameFields, EMPTYFIELD);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void computerMove() {
|
||||||
|
int randomNumber = -1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
randomNumber = generateNumber();
|
||||||
|
} while (!isFieldChoosable(randomNumber));
|
||||||
|
|
||||||
|
gameFields[randomNumber] = X;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void humanMove(Scanner scanner) {
|
||||||
|
printOptionsMenu();
|
||||||
|
int choice = -1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
choice = scanner.nextInt();
|
||||||
|
} while (!isFieldChoosable(choice-1));
|
||||||
|
|
||||||
|
gameFields[choice-1] = O;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int generateNumber() {
|
||||||
|
Random random = new Random();
|
||||||
|
return random.nextInt(9);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isFieldChoosable(int index) {
|
||||||
|
return gameFields[index].equals(EMPTYFIELD);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean gameHasEnded() {
|
||||||
|
return gameWasWon() || noMovesLeft();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean noMovesLeft() {
|
||||||
|
for (String s: gameFields) {
|
||||||
|
if (s.equals(EMPTYFIELD)) return false;
|
||||||
|
}
|
||||||
|
System.out.println("\nUnentschieden!");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean gameWasWon() {
|
||||||
|
return isDiagonalWin() || isHorizontalWin() || isVerticalWin();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isDiagonalWin() {
|
||||||
|
if (isLineComplete(0,4,8) || isLineComplete(2,4,6)) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isHorizontalWin() {
|
||||||
|
if (isLineComplete(0,1,2) || isLineComplete(3,4,5) || isLineComplete(6,7,8)) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isVerticalWin() {
|
||||||
|
if (isLineComplete(0,3,6) || isLineComplete(1,4,7) || isLineComplete(2,5,8)) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isLineComplete(int first, int second, int third) {
|
||||||
|
return !gameFields[first].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[first].equals(gameFields[second]) &&
|
||||||
|
gameFields[first].equals(gameFields[third]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Below is section for helper methods - Strings and outputs//
|
||||||
|
public static void printGrid() {
|
||||||
|
System.out.println("______________________________________________________________________");
|
||||||
|
String grid = "";
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
if (i % 3 == 0) grid += "\n";
|
||||||
|
grid += "[" + gameFields[i] + "]";
|
||||||
|
}
|
||||||
|
System.out.println(grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printOptionsMenu() {
|
||||||
|
System.out.println("\n\nDu hast die folgende leeren Felder: ");
|
||||||
|
String grid = "";
|
||||||
|
for (int i = 0; i < 9; i++) {
|
||||||
|
if (i % 3 == 0) grid += "\n";
|
||||||
|
grid += isFieldChoosable(i) ? (i + 1)+" " : " ";
|
||||||
|
}
|
||||||
|
System.out.println(grid+"\nGib die Zahl ein, wo du deinen Kreis setzen willst: ");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue