TicTacToe simple array
parent
8a81e02559
commit
5ea64d5ac3
|
|
@ -0,0 +1,189 @@
|
||||||
|
package Uebung_02;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class TicTacToe_AC1 {
|
||||||
|
|
||||||
|
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 final boolean HUMAN = true;
|
||||||
|
public static final boolean BOT = false;
|
||||||
|
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
updateStatus(randomNumber, BOT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void humanMove(Scanner scanner) {
|
||||||
|
printOptionsMenu();
|
||||||
|
int choice = -1;
|
||||||
|
|
||||||
|
do {
|
||||||
|
choice = scanner.nextInt();
|
||||||
|
} while(!isFieldChoosable(choice-1));
|
||||||
|
|
||||||
|
updateStatus(choice-1, HUMAN);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 void updateStatus(int index, boolean isHumanMove) {
|
||||||
|
if (isHumanMove) {
|
||||||
|
gameFields[index] = O;
|
||||||
|
} else {
|
||||||
|
gameFields[index] = X;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean gameHasEnded() {
|
||||||
|
return noMovesLeft() || gameWasWon();
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (!gameFields[0].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[0].equals(gameFields[4]) &&
|
||||||
|
gameFields[0].equals(gameFields[8])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
if (!gameFields[2].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[2].equals(gameFields[4]) &&
|
||||||
|
gameFields[2].equals(gameFields[6])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isHorizontalWin() {
|
||||||
|
if (!gameFields[0].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[0].equals(gameFields[1]) &&
|
||||||
|
gameFields[0].equals(gameFields[2])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
if (!gameFields[3].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[3].equals(gameFields[4]) &&
|
||||||
|
gameFields[3].equals(gameFields[5])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
if (!gameFields[6].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[6].equals(gameFields[7]) &&
|
||||||
|
gameFields[6].equals(gameFields[8])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isVerticalWin() {
|
||||||
|
if (!gameFields[0].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[0].equals(gameFields[3]) &&
|
||||||
|
gameFields[0].equals(gameFields[6])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
if (!gameFields[1].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[1].equals(gameFields[4]) &&
|
||||||
|
gameFields[1].equals(gameFields[7])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
if (!gameFields[2].equals(EMPTYFIELD) &&
|
||||||
|
gameFields[2].equals(gameFields[5]) &&
|
||||||
|
gameFields[2].equals(gameFields[8])
|
||||||
|
) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//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 += printSquare(gameFields[i]);
|
||||||
|
}
|
||||||
|
System.out.println(grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String printSquare(String c) {
|
||||||
|
return "[" + c + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
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 += choosableOption(i);
|
||||||
|
}
|
||||||
|
System.out.println(grid+"\nGib die Zahl ein, wo du deinen Kreis setzen willst: ");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String choosableOption(int index) {
|
||||||
|
if (isFieldChoosable(index)) {
|
||||||
|
return (index + 1) + " ";
|
||||||
|
} else {
|
||||||
|
return " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue