Musterlösungen zu den Übungsblättern
parent
779659229d
commit
9038f0b283
|
@ -1,6 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,50 @@
|
|||
package Uebung_01;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @author Hanin Aljalab
|
||||
* Anmerkung: in dieser Version findet nur die Umwandlung von
|
||||
* Dezimal zu Römisch statt.
|
||||
* Daher muss die Umwandlung von Römisch zu Dezimal erweitert werden
|
||||
*/
|
||||
public class DecToRoem_Version_01 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
int zahl, position = 0;
|
||||
|
||||
//Übersetzungstabellen für die Zahlenwerte
|
||||
String[] roemisch = {"M", "CM", "D", "CD", "C", "XC", "L", "XL",
|
||||
"X", "IX", "V", "IV", "I"};
|
||||
|
||||
int[] arabisch = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
|
||||
|
||||
//"Verbindung" zur Tastatur
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
System.out.println("Konvertierung Dezimal -> Römisch");
|
||||
|
||||
//Eingabe von x über die Tastatur einlesen
|
||||
System.out.println("Eingabe von x (1 <= x <= 3999): ");
|
||||
zahl = input.nextInt();
|
||||
|
||||
//Umwandlung
|
||||
//Fehleingaben abfragen
|
||||
if (zahl < 1 || zahl > 3999) {
|
||||
System.out.println("Eingabe liegt außerhalb des zulässigen Bereichs!");
|
||||
} else {
|
||||
System.out.println("Ausgabe: ");
|
||||
// Die arabischen Zahlenwert-Entsprechungen von oben nach unten arbeiten
|
||||
while (zahl > 0 && position < arabisch.length) {
|
||||
if (zahl >= arabisch[position]) { // Anpassung der Bedingung
|
||||
zahl -= arabisch[position];
|
||||
System.out.print(roemisch[position]); // Ausgabe in einer Zeile
|
||||
} else {
|
||||
position++;
|
||||
}
|
||||
}
|
||||
System.out.println(); // Neue Zeile nach der Ausgabe
|
||||
}
|
||||
input.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,217 @@
|
|||
package Uebung_01;
|
||||
|
||||
/**
|
||||
* @author Hanin Aljalab
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class DecToRom_Version_02 {
|
||||
|
||||
// Methode mit Rückgabewert von der Datentyp int
|
||||
public static int RomToDez(String rom) {
|
||||
|
||||
int dez = 0;
|
||||
|
||||
for (int i = 0; rom.length() > i; i++) {
|
||||
|
||||
char eingabe = rom.charAt(i); //einzelne String Zeichen ausselen
|
||||
char zeichen;
|
||||
|
||||
if (i + 1 != rom.length()) {
|
||||
|
||||
zeichen = rom.charAt(i + 1);
|
||||
|
||||
// Subtraktion
|
||||
if (eingabe == 'I' && (zeichen == 'V' || zeichen == 'X' || zeichen == 'L' || zeichen == 'C'
|
||||
|| zeichen == 'D' || zeichen == 'M')) {
|
||||
|
||||
dez = dez - 1;
|
||||
continue; // Wenn das zutrifft, wird die aktuelle Durchlauf der Schleife übersprungen und
|
||||
// der nächsten durchgeführt.
|
||||
}
|
||||
|
||||
if (eingabe == 'V'
|
||||
&& (zeichen == 'X' || zeichen == 'L' || zeichen == 'C' || zeichen == 'D' || zeichen == 'M')) {
|
||||
dez = dez - 5;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (eingabe == 'X' && (zeichen == 'L' || zeichen == 'C' || zeichen == 'D' || zeichen == 'M')) {
|
||||
dez = dez - 10;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (eingabe == 'L' && (zeichen == 'C' || zeichen == 'D' || zeichen == 'M')) {
|
||||
dez = dez - 50;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (eingabe == 'C' && (zeichen == 'D' || zeichen == 'M')) {
|
||||
dez = dez - 100;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (eingabe == 'D' && zeichen == 'M') {
|
||||
dez = dez - 500;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
switch (eingabe) {
|
||||
|
||||
case 'I':
|
||||
dez = dez + 1;
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
dez = dez + 5;
|
||||
break;
|
||||
case 'X':
|
||||
dez = dez + 10;
|
||||
break;
|
||||
case 'L':
|
||||
dez = dez + 50;
|
||||
break;
|
||||
case 'C':
|
||||
dez = dez + 100;
|
||||
break;
|
||||
case 'D':
|
||||
dez = dez + 500;
|
||||
break;
|
||||
case 'M':
|
||||
dez = dez + 1000;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return dez;
|
||||
|
||||
}
|
||||
|
||||
// Methode mit Rückgabewert von der Datentyp String
|
||||
public static String DecInRom(int dez) {
|
||||
|
||||
String newRom = "";
|
||||
|
||||
while (dez > 0) {
|
||||
|
||||
if (dez >= 1000) {
|
||||
|
||||
newRom = newRom + "M";
|
||||
dez = dez - 1000;
|
||||
|
||||
}
|
||||
|
||||
else if (dez >= 900) {
|
||||
newRom = newRom + "CM";
|
||||
dez = dez - 900;
|
||||
}
|
||||
|
||||
else if (dez >= 500) {
|
||||
|
||||
dez = dez - 500;
|
||||
newRom = newRom + "D";
|
||||
}
|
||||
|
||||
else if (dez >= 400) {
|
||||
newRom = newRom + "CD";
|
||||
dez = dez - 400;
|
||||
}
|
||||
|
||||
else if (dez >= 100) {
|
||||
dez = dez - 100;
|
||||
newRom = newRom + "C";
|
||||
}
|
||||
|
||||
else if (dez >= 90) {
|
||||
newRom = newRom + "XC";
|
||||
dez = dez - 90;
|
||||
}
|
||||
|
||||
else if (dez >= 50) {
|
||||
dez = dez - 50;
|
||||
newRom = newRom + "L";
|
||||
}
|
||||
|
||||
else if (dez >= 40) {
|
||||
newRom = newRom + "XL";
|
||||
dez = dez - 40;
|
||||
}
|
||||
|
||||
else if (dez >= 10) {
|
||||
dez = dez - 10;
|
||||
newRom = newRom + "X";
|
||||
}
|
||||
|
||||
else if (dez >= 9) {
|
||||
newRom = newRom + "IX";
|
||||
dez = dez - 9;
|
||||
}
|
||||
|
||||
else if (dez >= 5) {
|
||||
dez = dez - 5;
|
||||
newRom = newRom + "V";
|
||||
}
|
||||
|
||||
else if (dez >= 4) {
|
||||
newRom = newRom + "IV";
|
||||
dez = dez - 4;
|
||||
}
|
||||
|
||||
else if (dez >= 1) {
|
||||
dez = dez - 1;
|
||||
newRom = newRom + "I";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return newRom;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
String[] romNumber = { "I", "V", "X", "L", "C", "D", "M" };
|
||||
int[] dezNumber = { 1, 5, 10, 50, 100, 500, 1000 };
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
while (true) {
|
||||
|
||||
System.out.println(
|
||||
"Was möchten Sie tun? \n Geben Sie 1 oder 2: (1 für Römisch -> Dez und 2 für Dez -> Römisch)");
|
||||
String eingabe = sc.nextLine();
|
||||
|
||||
if (eingabe.equals("1")) {
|
||||
|
||||
System.out.println("Geben Sie ein römischen Zahl ein: ");
|
||||
String rom = sc.nextLine();
|
||||
|
||||
int dez = RomToDez(rom);
|
||||
System.out.println("Der Dezimalzahl lautet: " + dez);
|
||||
}
|
||||
|
||||
else {
|
||||
System.out.println("Geben Sie den Dezimalzahl ein:");
|
||||
int dez = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
String newRom = DecInRom(dez);
|
||||
|
||||
System.out.println("Der Römischen Zahl lautet: " + newRom);
|
||||
|
||||
}
|
||||
|
||||
System.out.println("Möchten Sie andere Umrechnung durchführen? \nGeben Sie J/N: ");
|
||||
eingabe = sc.nextLine();
|
||||
if (!eingabe.equals("J")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Fertig");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,130 @@
|
|||
package Uebung_02;
|
||||
|
||||
/**
|
||||
* @author: Hanin Aljalab
|
||||
* Tic Tac Toe Game
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TicTacToe {
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Decleration of the variables
|
||||
char[][] board = {{' ', ' ', ' '},
|
||||
{' ', ' ', ' '},
|
||||
{' ', ' ', ' '}
|
||||
};
|
||||
int player;
|
||||
boolean win;
|
||||
|
||||
// Initialize the variables
|
||||
player = 1;
|
||||
win = false;
|
||||
|
||||
// creating the "Scanner"
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
// Start the game
|
||||
while (!win && !isBoardFull(board)) {
|
||||
spielfeldAusgeben(board);
|
||||
|
||||
if (player == 1) {
|
||||
// Player's turn
|
||||
spielerZug(board, sc);
|
||||
} else {
|
||||
// Computer's turn
|
||||
computerZug(board);
|
||||
}
|
||||
|
||||
// Check for a win or draw
|
||||
win = isWin(board);
|
||||
if (!win) {
|
||||
player = (player == 1) ? 2 : 1; // change the player
|
||||
}
|
||||
}
|
||||
|
||||
spielfeldAusgeben(board);
|
||||
|
||||
if (win) {
|
||||
System.out.println("Spieler " + player + " gewinnt!");
|
||||
} else {
|
||||
System.out.println("Unentschieden!");
|
||||
}
|
||||
}
|
||||
|
||||
// print the game board
|
||||
public static void spielfeldAusgeben(char[][] board) {
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
System.out.print(" [" + board[i][j] + "] ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
public static void spielerZug(char[][] board, Scanner sc) {
|
||||
System.out.println("Spieler, geben Sie Ihre Position ein (1-9): ");
|
||||
int input = sc.nextInt();
|
||||
|
||||
// Convert input in row and column
|
||||
int row = (input - 1) / 3;
|
||||
int col = (input - 1) % 3;
|
||||
|
||||
// Check whether the position is valid and the field is not occupied
|
||||
if (input >= 1 && input <= 9 && board[row][col] == ' ') {
|
||||
board[row][col] = 'O';
|
||||
} else {
|
||||
System.out.println("Ungültige Position. Bitte erneut versuchen.");
|
||||
spielerZug(board, sc); // Recursive call for new input
|
||||
}
|
||||
}
|
||||
|
||||
public static void computerZug(char[][] board) {
|
||||
Random ra = new Random();
|
||||
int row, col;
|
||||
|
||||
do {
|
||||
row = ra.nextInt(board.length);
|
||||
col = ra.nextInt(board.length);
|
||||
} while (board[row][col] != ' ');
|
||||
|
||||
board[row][col] = 'X';
|
||||
}
|
||||
|
||||
public static boolean isWin(char[][] board) {
|
||||
// Check for profit in rows, columns and diagonals
|
||||
for (int i = 0; i < 3; i++) {
|
||||
// Profit in rows
|
||||
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
|
||||
return true;
|
||||
}
|
||||
// Profit in columns
|
||||
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Profit in diagonals
|
||||
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') || (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isBoardFull(char[][] board) {
|
||||
// Check whether the pitch is full (draw)
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
if (board[i][j] == ' ') {
|
||||
return false; // There are still empty positions
|
||||
}
|
||||
}
|
||||
}
|
||||
return true; // All positions have been filled
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package Uebung_03;
|
||||
|
||||
//Import the scanner class for user input
|
||||
|
||||
public class BracketChecker {
|
||||
|
||||
public void bracketChecker(String expression) {
|
||||
|
||||
//Check the brackets in the expression and output the result
|
||||
if (checkBrackets(expression)) {
|
||||
System.out.println("Klammern sind gültig.");
|
||||
} else {
|
||||
System.out.println("Klammern sind ungültig.");
|
||||
}
|
||||
}
|
||||
|
||||
//Method for checking the brackets in the expression
|
||||
public boolean checkBrackets(String expression) {
|
||||
//A stack object is created to check the brackets
|
||||
Stack stack = new Stack();
|
||||
|
||||
//Loop runs through each letter of the entered expression
|
||||
for (int i = 0; i < expression.length(); i++) {
|
||||
char currentChar = expression.charAt(i);
|
||||
|
||||
//If the current character is an opening bracket, it is added to the stack
|
||||
if (isOpenBracket(currentChar)) {
|
||||
stack.push(currentChar);
|
||||
}
|
||||
//If the current character is a closing bracket
|
||||
else if (isCloseBracket(currentChar)) {
|
||||
//Check whether the stack is empty or whether the closing bracket matches the
|
||||
//last added opening bracket matches
|
||||
if (stack.isEmpty() || !isMatchingBracket(stack.pop(), currentChar)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check whether the stack is empty after running the expression
|
||||
return stack.isEmpty();
|
||||
}
|
||||
|
||||
//Method to determine whether the character is an opening bracket
|
||||
public boolean isOpenBracket(char bracket) {
|
||||
return bracket == '(' || bracket == '[' || bracket == '{';
|
||||
}
|
||||
|
||||
//Method to determine whether the character is a closing bracket
|
||||
public boolean isCloseBracket(char bracket) {
|
||||
|
||||
return bracket == ')' || bracket == ']' || bracket == '}';
|
||||
}
|
||||
|
||||
//Method to determine whether the opening and closing clamps fit together
|
||||
public boolean isMatchingBracket(char openBracket, char closeBracket) {
|
||||
return (openBracket == '(' && closeBracket == ')')
|
||||
|| (openBracket == '[' && closeBracket == ']')
|
||||
|| (openBracket == '{' && closeBracket == '}');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package Uebung_03;
|
||||
|
||||
/**
|
||||
* Stack
|
||||
* @author Hanin Aljalab
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//A scanner is created
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
//User is requested to enter an expression
|
||||
System.out.println("Geben Sie einen Ausdruck ein:");
|
||||
//The user input is saved in the "expression" variable
|
||||
String expression = scanner.nextLine();
|
||||
|
||||
BracketChecker br = new BracketChecker();
|
||||
|
||||
br.bracketChecker(expression);
|
||||
br.checkBrackets(expression);
|
||||
|
||||
|
||||
Stack stack = new Stack();
|
||||
// Place characters on the stack
|
||||
stack.push('A');
|
||||
stack.push('B');
|
||||
stack.push('C');
|
||||
|
||||
// Remove character from stack and output
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.println("Pop: " + stack.pop());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package Uebung_03;
|
||||
|
||||
|
||||
public class Stack {
|
||||
// maximum size of the stack.
|
||||
private int size = 100;
|
||||
//A char array that provides the actual storage space for the stack elements.
|
||||
private char[] stack;
|
||||
//An index that points to the next available position in the stack array.
|
||||
private int stackPointer;
|
||||
|
||||
// Constructor: Initializes the empty stack
|
||||
public Stack() {
|
||||
stack = new char[size];
|
||||
stackPointer = 0; // The stack pointer points to the first element of the array
|
||||
}
|
||||
|
||||
// Method to add a character to the stack
|
||||
public void push(char c) {
|
||||
if (stackPointer < size) {
|
||||
stack[stackPointer] = c;
|
||||
stackPointer++; // Increments/Increases the stack pointer after adding
|
||||
} else {
|
||||
System.out.println("Stack-Overflow: Der Stack ist voll.");
|
||||
}
|
||||
}
|
||||
|
||||
// Method for removing and returning the last character added to the stack
|
||||
public char pop() {
|
||||
if (stackPointer > 0) {
|
||||
stackPointer--; // Decrement the stack pointer before reading
|
||||
return stack[stackPointer];
|
||||
} else {
|
||||
System.out.println("Stack-Underflow: Der Stack ist leer.");
|
||||
return '\0'; // '\0' stands for an empty character (null character)
|
||||
}
|
||||
}
|
||||
|
||||
// Method to check whether the stack is empty
|
||||
public boolean isEmpty() {
|
||||
|
||||
return stackPointer == 0;
|
||||
}
|
||||
|
||||
// Method to check whether the stack is full
|
||||
public boolean isFull() {
|
||||
return stackPointer == size;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package Uebung_04;
|
||||
|
||||
public class Board {
|
||||
private char[][] board;
|
||||
|
||||
// Constructor to initialize an empty 3x3 board
|
||||
public Board() {
|
||||
board = new char[][]{
|
||||
{' ', ' ', ' '},
|
||||
{' ', ' ', ' '},
|
||||
{' ', ' ', ' '}
|
||||
};
|
||||
}
|
||||
|
||||
// Getters and Setters
|
||||
public char[][] getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
public void setBoard(char[][] board) {
|
||||
this.board = board;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
package Uebung_04;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class ComputerPlayer extends Player {
|
||||
private Random random;
|
||||
|
||||
// Constructor
|
||||
public ComputerPlayer(char symbol) {
|
||||
super(symbol);
|
||||
this.random = new Random();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeMove(Board board) {
|
||||
int[] bestMove = getBestMove(board, 10000); // Number of simulations
|
||||
int
|
||||
|
||||
row = bestMove[0];
|
||||
int col = bestMove[1];
|
||||
|
||||
if (board.getBoard()[row][col] == ' ') {
|
||||
board.getBoard()[row][col] = symbol;
|
||||
} else {
|
||||
System.out.println("Error: Invalid move. Choosing the first available empty space.");
|
||||
makeRandomMove(board);
|
||||
}
|
||||
}
|
||||
|
||||
private int[] getBestMove(Board board, int simulations) {
|
||||
int[] bestMove = new int[]{-1, -1};
|
||||
double bestWinRate = -1;
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
if (board.getBoard()[i][j] == ' ') {
|
||||
double winRate = monteCarlo(board, i, j, simulations);
|
||||
|
||||
if (winRate > bestWinRate) {
|
||||
bestWinRate = winRate;
|
||||
bestMove[0] = i;
|
||||
bestMove[1] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.printf("Best Move: (%d, %d) with Win Rate = %.2f%n", bestMove[0] + 1, bestMove[1] + 1, bestWinRate);
|
||||
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private double monteCarlo(Board board, int row, int col, int simulations) {
|
||||
int winCount = 0;
|
||||
|
||||
for (int k = 0; k < simulations; k++) {
|
||||
Board copyBoard = copyBoard(board);
|
||||
copyBoard.getBoard()[row][col] = symbol;
|
||||
|
||||
if (simulateGame(copyBoard)) {
|
||||
winCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return (double) winCount / simulations;
|
||||
}
|
||||
|
||||
private void makeRandomMove(Board board) {
|
||||
// Fallback method if no valid moves are found
|
||||
int row, col;
|
||||
do {
|
||||
row = random.nextInt(3);
|
||||
col = random.nextInt(3);
|
||||
} while (board.getBoard()[row][col] != ' ');
|
||||
|
||||
board.getBoard()[row][col] = symbol;
|
||||
}
|
||||
|
||||
private boolean simulateGame(Board board) {
|
||||
// Check for a win in rows
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (board.getBoard()[i][0] == symbol &&
|
||||
board.getBoard()[i][1] == symbol &&
|
||||
board.getBoard()[i][2] == symbol) {
|
||||
return true; // Computer wins
|
||||
}
|
||||
}
|
||||
|
||||
// Check for a win in columns
|
||||
for (int j = 0; j < 3; j++) {
|
||||
if (board.getBoard()[0][j] == symbol &&
|
||||
board.getBoard()[1][j] == symbol &&
|
||||
board.getBoard()[2][j] == symbol) {
|
||||
return true; // Computer wins
|
||||
}
|
||||
}
|
||||
|
||||
// Check for a win in diagonals
|
||||
if (board.getBoard()[0][0] == symbol &&
|
||||
board.getBoard()[1][1] == symbol &&
|
||||
board.getBoard()[2][2] == symbol) {
|
||||
return true; // Computer wins
|
||||
}
|
||||
|
||||
if (board.getBoard()[0][2] == symbol &&
|
||||
board.getBoard()[1][1] == symbol &&
|
||||
board.getBoard()[2][0] == symbol) {
|
||||
return true; // Computer wins
|
||||
}
|
||||
|
||||
return false; // No win
|
||||
}
|
||||
|
||||
private Board copyBoard(Board original) {
|
||||
Board copy = new Board();
|
||||
char[][] originalBoard = original.getBoard();
|
||||
char[][] copyBoard = copy.getBoard();
|
||||
|
||||
for (int i = 0; i < originalBoard.length; i++) {
|
||||
for (int j = 0; j < originalBoard[i].length; j++) {
|
||||
copyBoard[i][j] = originalBoard[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return copy;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,102 @@
|
|||
package Uebung_04;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
class Game {
|
||||
private Board board;
|
||||
private Player player1;
|
||||
private Player player2;
|
||||
|
||||
// Constructor to initialize the game with two players
|
||||
public Game(Player player1, Player player2) {
|
||||
this.board = new Board();
|
||||
this.player1 = player1;
|
||||
this.player2 = player2;
|
||||
}
|
||||
|
||||
//method to play the game
|
||||
public void playGame() {
|
||||
int currentPlayer = 1;
|
||||
boolean win = false;
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
while (!win && !isBoardFull()) {
|
||||
boardDisplay();
|
||||
// Determine which player's turn it is
|
||||
if (currentPlayer == 1) {
|
||||
player1.makeMove(board);
|
||||
} else {
|
||||
player2.makeMove(board);
|
||||
}
|
||||
// Check for a win
|
||||
win = isWin();
|
||||
|
||||
// Switch to the other player if no win
|
||||
if (!win) {
|
||||
currentPlayer = (currentPlayer == 1) ? 2 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Display the final board
|
||||
boardDisplay();
|
||||
|
||||
// Display the result of the game
|
||||
if (win) {
|
||||
System.out.println("Spieler " + currentPlayer + " gewinnt!");
|
||||
} else {
|
||||
System.out.println("Unentschieden!");
|
||||
}
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
// Display the current state of the board
|
||||
public void boardDisplay() {
|
||||
char[][] currentBoard = board.getBoard();
|
||||
for (int i = 0; i < currentBoard.length; i++) {
|
||||
for (int j = 0; j < currentBoard[i].length; j++) {
|
||||
System.out.print(" [" + currentBoard[i][j] + "] ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
// Check if there is a win on the current board
|
||||
public boolean isWin() {
|
||||
char[][] currentBoard = board.getBoard();
|
||||
|
||||
// Check rows and columns for a win
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (currentBoard[i][0] == currentBoard[i][1] && currentBoard[i][1] == currentBoard[i][2] && currentBoard[i][0] != ' ') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (currentBoard[0][i] == currentBoard[1][i] && currentBoard[1][i] == currentBoard[2][i] && currentBoard[0][i] != ' ') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Check diagonals for a win
|
||||
if ((currentBoard[0][0] == currentBoard[1][1] && currentBoard[1][1] == currentBoard[2][2] && currentBoard[0][0] != ' ') ||
|
||||
(currentBoard[0][2] == currentBoard[1][1] && currentBoard[1][1] == currentBoard[2][0] && currentBoard[0][2] != ' ')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Check if the board is full
|
||||
public boolean isBoardFull() {
|
||||
char[][] currentBoard = board.getBoard();
|
||||
|
||||
for (int i = 0; i < currentBoard.length; i++) {
|
||||
for (int j = 0; j < currentBoard[i].length; j++) {
|
||||
if (currentBoard[i][j] == ' ') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package Uebung_04;
|
||||
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class HumanPlayer extends Player {
|
||||
private Scanner scanner;
|
||||
|
||||
// Constructor to set the symbol and initialize a scanner for input
|
||||
public HumanPlayer(char symbol, Scanner scanner) {
|
||||
super(symbol);
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeMove(Board board) {
|
||||
System.out.println("Player, enter your position (1-9): ");
|
||||
int input = scanner.nextInt();
|
||||
|
||||
// Convert input to row and column
|
||||
int row = (input - 1) / 3;
|
||||
int col = (input - 1) % 3;
|
||||
|
||||
// Check whether the position is valid and the field is not occupied
|
||||
if (input >= 1 && input <= 9 && board.getBoard()[row][col] == ' ') {
|
||||
board.getBoard()[row][col] = symbol;
|
||||
} else {
|
||||
System.out.println("Invalid position. Please try again.");
|
||||
makeMove(board); // Recursive call for new input
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package Uebung_04;
|
||||
|
||||
/**
|
||||
* Tic Tac Toe
|
||||
* @author Hanin Aljalab
|
||||
*/
|
||||
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
|
||||
//create a scanner
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
// Create players
|
||||
HumanPlayer humanPlayer = new HumanPlayer('O', scanner);
|
||||
ComputerPlayer computerPlayer = new ComputerPlayer('X');
|
||||
|
||||
// Create game
|
||||
Game game = new Game(humanPlayer, computerPlayer);
|
||||
|
||||
// Play the game
|
||||
game.playGame();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package Uebung_04;
|
||||
|
||||
|
||||
public class Player {
|
||||
protected char symbol;
|
||||
protected int winCount;
|
||||
protected int loseCount;
|
||||
|
||||
// Constructor to set the player's symbol
|
||||
public Player(char symbol) {
|
||||
this.symbol = symbol;
|
||||
this.winCount = 0;
|
||||
this.loseCount = 0;
|
||||
}
|
||||
|
||||
// Getter for symbol
|
||||
public char getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
// Increment win count
|
||||
public void incrementWinCount() {
|
||||
winCount++;
|
||||
}
|
||||
|
||||
// Increment lose count
|
||||
public void incrementLoseCount() {
|
||||
loseCount++;
|
||||
}
|
||||
|
||||
// Abstract method for making a move
|
||||
public void makeMove(Board board) {
|
||||
// To be implemented by subclasses
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue