Loesung mit char array plus die Loesung von Vlad

main
Andres Alberto Cruz Aguirre 2024-11-09 08:07:25 +01:00
parent e7f3576bb8
commit 4b47525e95
4 changed files with 336 additions and 46 deletions

View File

@ -0,0 +1,179 @@
package Uebung_02;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] moveArray = {" ", " ", " ", " ", " ", " ", " ", " ", " "};
boolean gameGoing = true;
String savingMemory;
// printBoard(moveArray); //Not needed for the first run, actually.
while(gameGoing){
printBoard(playerMove(moveArray));
gameGoing = isGameGoing(moveArray, gameGoing);
if(gameGoing == false){
break;
}
printBoard(enemyMove(moveArray));
gameGoing = isGameGoing(moveArray, gameGoing);
}
}
private static boolean isGameGoing(String[] moveArray, boolean gameGoing) {
String savingMemory;
savingMemory = checkWin(moveArray); //Yes, yes, I bowed to the mighty IntelliJ refactoring.
if (savingMemory == "Player win!") {
gameGoing = false;
System.out.println(savingMemory);
}
else if (savingMemory == "PC win!") {
gameGoing = false;
System.out.println(savingMemory);
}
else if (savingMemory == "DRAW!!!!") {
gameGoing = false;
System.out.println(savingMemory);
}
else System.out.println(savingMemory);
return gameGoing;
}
private static void printBoard(String[] moveArray){
System.out.print("[" + moveArray[0] + "]" + "[" + moveArray[1] + "]" + "[" + moveArray[2] + "]" + "\n" +
"[" + moveArray[3] + "]" + "[" + moveArray[4] + "]" + "[" + moveArray[5] + "]" + "\n" +
"[" + moveArray[6] + "]" + "[" + moveArray[7] + "]" + "[" + moveArray[8] + "]" + "\n" + "\n");
}
private static String[] enemyMove( String[] moveArray){
boolean argh = true; //so I know when to stop generating moves
while (argh == true) { //Yes. I know. I told IntelliJ to leave me alone as well. You will not make me budge either.
Random random = new Random();
int randomInt = random.nextInt(9);
if(Objects.equals(moveArray[randomInt], " ")){
moveArray[randomInt] = "O";
argh = false;
}
else {
System.out.print("Thinking.");
try {
Thread.sleep(300);
} catch (InterruptedException e) {
System.err.println("Thread was interrupted!");
}
System.out.print(".");
try {
Thread.sleep(300);
} catch (InterruptedException e) {
System.err.println("Thread was interrupted!");
}
System.out.print("."+ "\n");
}
}
return moveArray;
}
private static String[] playerMove ( String[] moveArray){
boolean validInput = true;
while (validInput) { //Yes, I know its counterintuitive. No, I don't care.
System.out.println("Where do you wanna out in your move?");
System.out.print("\n"+"[1]" + "[2]" + "[3]" + "\n" +
"[4]" + "[5]" + "[6]" + "\n" +
"[7]" + "[8]" + "[9]" + "\n");
Scanner scanner = new Scanner(System.in);
int playerMove = scanner.nextInt() - 1;
if(playerMove<=8){
System.out.println("This is a correct move!");
moveArray[playerMove] = "X";
validInput = false;
}
else {
System.out.println("No, this is not a valid input, ya git!"); //useful comments are for the weak.
}
}
return moveArray;
}
private static String checkWin(String[] moveArray){
String playerChar = "X"; //Yes, I know it can be made simpler. But I was too lazy to type the logic myself, so I let ChatGpt write it and then had to bastardise it to fit into my code. Sue me.
String pcChar = "O";
// Check for player win
//rows
if ((moveArray[0] == playerChar && moveArray[1] == playerChar && moveArray[2] == playerChar) || // Row 1
(moveArray[3] == playerChar && moveArray[4] == playerChar && moveArray[5] == playerChar) || // Row 2
(moveArray[6] == playerChar && moveArray[7] == playerChar && moveArray[8] == playerChar)) { // Row 3
return "Player win!";
}
// Check columns
if ((moveArray[0] == playerChar && moveArray[3] == playerChar && moveArray[6] == playerChar) || // Column 1
(moveArray[1] == playerChar && moveArray[4] == playerChar && moveArray[7] == playerChar) || // Column 2
(moveArray[2] == playerChar && moveArray[5] == playerChar && moveArray[8] == playerChar)) { // Column 3
return "Player win!";
}
// Check diagonals
if ((moveArray[0] == playerChar && moveArray[4] == playerChar && moveArray[8] == playerChar) || // Diagonal top-left to bottom-right
(moveArray[2] == playerChar && moveArray[4] == playerChar && moveArray[6] == playerChar)) { // Diagonal top-right to bottom-left
return "Player win!";
}
//Check for PC win
//rows
if ((moveArray[0] == pcChar && moveArray[1] == pcChar && moveArray[2] == pcChar) || // Row 1
(moveArray[3] == pcChar && moveArray[4] == pcChar && moveArray[5] == pcChar) || // Row 2
(moveArray[6] == pcChar && moveArray[7] == pcChar && moveArray[8] == pcChar)) { // Row 3
return "PC win!";
}
// Check columns
if ((moveArray[0] == pcChar && moveArray[3] == pcChar && moveArray[6] == pcChar) || // Column 1
(moveArray[1] == pcChar && moveArray[4] == pcChar && moveArray[7] == pcChar) || // Column 2
(moveArray[2] == pcChar && moveArray[5] == pcChar && moveArray[8] == pcChar)) { // Column 3
return "PC win!";
}
// Check diagonals
if ((moveArray[0] == pcChar && moveArray[4] == pcChar && moveArray[8] == pcChar) || // Diagonal top-left to bottom-right
(moveArray[2] == pcChar && moveArray[4] == pcChar && moveArray[6] == pcChar)) { // Diagonal top-right to bottom-left
return "PC win!";
}
//And of course check for draw
if ( moveArray[0] != " " && moveArray[1] != " "&& moveArray[2] != " "&& moveArray[3] != " "&& moveArray[4] != " "&& moveArray[5] != " "&& moveArray[6] != " "&& moveArray[7] != " "&& moveArray[8] != " "){
return "DRAW!!!!";
}
//else return "Something went wrong. Please consult your local Witch Doctor.";
else return "You are not done yet! The enemy is still standing!";
}
}

View File

@ -21,22 +21,21 @@ public class TicTacToe_AC1 {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
while(true) { while(canMakeMove()) {
humanMove(scanner); humanMove(scanner);
printGrid(); printGrid();
if(gameHasEnded()) { if(gameWasWon()) {
System.out.println("\nDu hast gewonnen!"); System.out.println("\nDu hast gewonnen!");
break; break;
} }
computerMove(); computerMove();
printGrid(); printGrid();
if(gameHasEnded()) { if(gameWasWon()) {
System.out.println("\nDer Roboter hat gewonnen!"); System.out.println("\nDer Roboter hat gewonnen!");
break; break;
} }
} }
scanner.close(); scanner.close();
} }
@ -82,17 +81,13 @@ public class TicTacToe_AC1 {
gameFields[index] = X; gameFields[index] = X;
} }
} }
public static boolean gameHasEnded() {
return gameWasWon() || noMovesLeft();
}
public static boolean noMovesLeft() { public static boolean canMakeMove() {
for (String s: gameFields) { for (String s: gameFields) {
if(s.equals(EMPTYFIELD)) return false; if(s.equals(EMPTYFIELD)) return true;
} }
System.out.println("\nUnentschieden!"); System.out.println("\nUnentschieden!");
return true; return false;
} }
public static boolean gameWasWon() { public static boolean gameWasWon() {

View File

@ -9,25 +9,25 @@ public class TicTacToe_AC2 {
public static final String EMPTYFIELD = " "; public static final String EMPTYFIELD = " ";
public static final String X = "X"; public static final String X = "X";
public static final String O = "O"; public static final String O = "O";
public static String[] gameFields = new String[9]; public static String[] boardFields = new String[9];
public static void main(String[] args) { public static void main(String[] args) {
initializeGameStatus(); initializeGameBoard();
computerMove(); computerMove();
printGrid(); printBoard();
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
while(true) { while(canMakeMove()) {
humanMove(scanner); humanMove(scanner);
printGrid(); printBoard();
if(gameHasEnded()) { if(gameWasWon()) {
System.out.println("\nDu hast gewonnen!"); System.out.println("\nDu hast gewonnen!");
break; break;
} }
computerMove(); computerMove();
printGrid(); printBoard();
if(gameHasEnded()) { if(gameWasWon()) {
System.out.println("\nDer Roboter hat gewonnen!"); System.out.println("\nDer Roboter hat gewonnen!");
break; break;
} }
@ -36,8 +36,8 @@ public class TicTacToe_AC2 {
} }
public static void initializeGameStatus() { public static void initializeGameBoard() {
Arrays.fill(gameFields, EMPTYFIELD); Arrays.fill(boardFields, EMPTYFIELD);
} }
public static void computerMove() { public static void computerMove() {
@ -47,7 +47,7 @@ public class TicTacToe_AC2 {
randomNumber = generateNumber(); randomNumber = generateNumber();
} while (!isFieldChoosable(randomNumber)); } while (!isFieldChoosable(randomNumber));
gameFields[randomNumber] = X; boardFields[randomNumber] = X;
} }
public static void humanMove(Scanner scanner) { public static void humanMove(Scanner scanner) {
@ -58,7 +58,7 @@ public class TicTacToe_AC2 {
choice = scanner.nextInt(); choice = scanner.nextInt();
} while (!isFieldChoosable(choice-1)); } while (!isFieldChoosable(choice-1));
gameFields[choice-1] = O; boardFields[choice-1] = O;
} }
public static int generateNumber() { public static int generateNumber() {
@ -67,53 +67,46 @@ public class TicTacToe_AC2 {
} }
public static boolean isFieldChoosable(int index) { public static boolean isFieldChoosable(int index) {
return gameFields[index].equals(EMPTYFIELD); return boardFields[index].equals(EMPTYFIELD);
}
public static boolean gameHasEnded() {
return gameWasWon() || noMovesLeft();
} }
public static boolean noMovesLeft() { public static boolean canMakeMove() {
for (String s: gameFields) { for (String s: boardFields) {
if (s.equals(EMPTYFIELD)) return false; if (s.equals(EMPTYFIELD)) return true;
} }
System.out.println("\nUnentschieden!"); System.out.println("\nUnentschieden!");
return true; return false;
} }
public static boolean gameWasWon() { public static boolean gameWasWon() {
return isDiagonalWin() || isHorizontalWin() || isVerticalWin(); return isDiagonalWin() || isHorizontalWin() || isVerticalWin();
} }
public static boolean isDiagonalWin() { public static boolean isDiagonalWin() {
if (isLineComplete(0,4,8) || isLineComplete(2,4,6)) return true; return isLineComplete(0,4,8) || isLineComplete(2,4,6);
return false;
} }
public static boolean isHorizontalWin() { public static boolean isHorizontalWin() {
if (isLineComplete(0,1,2) || isLineComplete(3,4,5) || isLineComplete(6,7,8)) return true; return isLineComplete(0,1,2) || isLineComplete(3,4,5) || isLineComplete(6,7,8);
return false;
} }
public static boolean isVerticalWin() { public static boolean isVerticalWin() {
if (isLineComplete(0,3,6) || isLineComplete(1,4,7) || isLineComplete(2,5,8)) return true; return isLineComplete(0,3,6) || isLineComplete(1,4,7) || isLineComplete(2,5,8);
return false;
} }
public static boolean isLineComplete(int first, int second, int third) { public static boolean isLineComplete(int first, int second, int third) {
return !gameFields[first].equals(EMPTYFIELD) && return !boardFields[first].equals(EMPTYFIELD) &&
gameFields[first].equals(gameFields[second]) && boardFields[first].equals(boardFields[second]) &&
gameFields[first].equals(gameFields[third]); boardFields[first].equals(boardFields[third]);
} }
//Below is section for helper methods - Strings and outputs// //Below is section for helper methods - Strings and outputs//
public static void printGrid() { public static void printBoard() {
System.out.println("______________________________________________________________________"); System.out.println("______________________________________________________________________");
String grid = ""; String grid = "";
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++) {
if (i % 3 == 0) grid += "\n"; if (i % 3 == 0) grid += "\n";
grid += "[" + gameFields[i] + "]"; grid += "[" + boardFields[i] + "]";
} }
System.out.println(grid); System.out.println(grid);
} }

View File

@ -0,0 +1,123 @@
package Uebung_02;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class TicTacToe_AC3 {
public static final char EMPTYFIELD = ' ';
public static final char X = 'X';
public static final char O = 'O';
public static char[] boardFields = new char[9];
public static void main(String[] args) {
initializeGameBoard();
computerMove();
printBoard();
Scanner scanner = new Scanner(System.in);
while(canMakeMove()) {
humanMove(scanner);
printBoard();
if(gameWasWon()) {
System.out.println("\nDu hast gewonnen!");
break;
}
computerMove();
printBoard();
if(gameWasWon()) {
System.out.println("\nDer Roboter hat gewonnen!");
break;
}
}
scanner.close();
}
public static void initializeGameBoard() {
Arrays.fill(boardFields, EMPTYFIELD);
}
public static void computerMove() {
int randomNumber = -1;
do {
randomNumber = generateNumber();
} while (!isFieldChoosable(randomNumber));
boardFields[randomNumber] = X;
}
public static void humanMove(Scanner scanner) {
printOptionsMenu();
int choice = -1;
do {
choice = scanner.nextInt();
} while (!isFieldChoosable(choice-1));
boardFields[choice-1] = O;
}
public static int generateNumber() {
Random random = new Random();
return random.nextInt(9);
}
public static boolean isFieldChoosable(int index) {
return boardFields[index] == (EMPTYFIELD);
}
public static boolean canMakeMove() {
for (char f: boardFields) {
if (f == EMPTYFIELD) return true;
}
System.out.println("\nUnentschieden!");
return false;
}
public static boolean gameWasWon() {
return isDiagonalWin() || isHorizontalWin() || isVerticalWin();
}
public static boolean isDiagonalWin() {
return isLineComplete(0,4,8) || isLineComplete(2,4,6);
}
public static boolean isHorizontalWin() {
return isLineComplete(0,1,2) || isLineComplete(3,4,5) || isLineComplete(6,7,8);
}
public static boolean isVerticalWin() {
return isLineComplete(0,3,6) || isLineComplete(1,4,7) || isLineComplete(2,5,8);
}
public static boolean isLineComplete(int first, int second, int third) {
return !(boardFields[first] == EMPTYFIELD) &&
boardFields[first] == boardFields[second] &&
boardFields[first] == boardFields[third];
}
//Below is section for helper methods - Strings and outputs//
public static void printBoard() {
System.out.println("______________________________________________________________________");
String grid = "";
for (int i = 0; i < 9; i++) {
if (i % 3 == 0) grid += "\n";
grid += "[" + boardFields[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: ");
}
}