TIC TAC TOE ohne Monte Carlo Algorithmus
parent
132e142e72
commit
3c3695d0a8
|
|
@ -0,0 +1,81 @@
|
|||
package Uebung_04_Version_02;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Represents the Tic Tac Toe game board.
|
||||
*/
|
||||
public class Board {
|
||||
private char[][] board;
|
||||
|
||||
/**
|
||||
* Initializes an empty game board.
|
||||
*/
|
||||
public Board() {
|
||||
board = new char[][] {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the current state of the game board.
|
||||
*/
|
||||
public void printBoard() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the board is full
|
||||
* @return true if the board is full, false otherwise.
|
||||
*/
|
||||
public boolean isFull() {
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
if (board[i][j] == ' ') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Places a marks on the board.
|
||||
* @param row the row index.
|
||||
* @param col the coloum index
|
||||
* @param mark the mark to place ('X' or 'O')
|
||||
* @return true if the mark was placed successully, flase otherwise
|
||||
*/
|
||||
public boolean placeMark(int row, int col, char mark) {
|
||||
if (board[row][col] == ' ') {
|
||||
board[row][col] = mark;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there a winning condition on the board.
|
||||
* @return true if a player has a won, false otherwise.
|
||||
*/
|
||||
public boolean checkWin() {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {
|
||||
return true;
|
||||
}
|
||||
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package Uebung_04_Version_02;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Represents a computer-controlled player in the TIC TAC TOE game.
|
||||
*/
|
||||
public class ComputerPlayer extends Player {
|
||||
private Random random;
|
||||
|
||||
/**
|
||||
* Constructor for computer player
|
||||
* @param mark the player's mark ('X')
|
||||
*/
|
||||
public ComputerPlayer(char mark) {
|
||||
super(mark);
|
||||
this.random = new Random();
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a move by randomly selecting available position.
|
||||
* @param board the game board.
|
||||
*/
|
||||
@Override
|
||||
public void makeMove(Board board) {
|
||||
int row, col;
|
||||
do {
|
||||
row = random.nextInt(3);
|
||||
col = random.nextInt(3);
|
||||
} while (!board.placeMark(row, col, mark));
|
||||
System.out.println("Computer setzt auf Position: " + (row * 3 + col + 1));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package Uebung_04_Version_02;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Represents the TIC TAC TOE game.
|
||||
*/
|
||||
public class Game {
|
||||
private Board board;
|
||||
private Player player1;
|
||||
private Player player2;
|
||||
|
||||
/***
|
||||
* Initializes the game with a human player and computer player.
|
||||
*/
|
||||
public Game() {
|
||||
board = new Board();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
player1 = new HumanPlayer('O', scanner);
|
||||
player2 = new ComputerPlayer('X');
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts and controls the game.
|
||||
*/
|
||||
public void play() {
|
||||
Player currentPlayer = player1;
|
||||
while (!board.isFull() && !board.checkWin()) {
|
||||
board.printBoard();
|
||||
currentPlayer.makeMove(board);
|
||||
if (board.checkWin()) {
|
||||
board.printBoard();
|
||||
System.out.println("Spieler " + currentPlayer.getMark() + " gewinnt!");
|
||||
return;
|
||||
}
|
||||
currentPlayer = (currentPlayer == player1) ? player2 : player1;
|
||||
}
|
||||
board.printBoard();
|
||||
System.out.println("Unentschieden!");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package Uebung_04_Version_02;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Represent a human player in the TIC TAC TOE game.
|
||||
*/
|
||||
public class HumanPlayer extends Player {
|
||||
private Scanner scanner;
|
||||
|
||||
/**
|
||||
* Constructor for a human player.
|
||||
* @param mark the player's mark ('X' or 'O')
|
||||
* @param scanner the scanner for user input.
|
||||
*/
|
||||
public HumanPlayer(char mark, Scanner scanner) {
|
||||
super(mark);
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes a move by reading user input.
|
||||
* @param board the game board.
|
||||
*/
|
||||
@Override
|
||||
public void makeMove(Board board) {
|
||||
int input;
|
||||
do {
|
||||
System.out.println("Spieler " + mark + ", geben Sie Ihre Position ein (1-9): ");
|
||||
input = scanner.nextInt();
|
||||
int row = (input - 1) / 3;
|
||||
int col = (input - 1) % 3;
|
||||
if (board.placeMark(row, col, mark)) {
|
||||
return;
|
||||
} else {
|
||||
System.out.println("Ungültige Position. Bitte erneut versuchen.");
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package Uebung_04_Version_02;
|
||||
|
||||
public class Main {
|
||||
/**
|
||||
* Main method to start the game.
|
||||
* @param args command-line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Game game = new Game();
|
||||
game.play();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue