fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
Success #stdin #stdout 0.09s 54708KB
stdin
Ein Spiel in Java!

Lass uns ein einfaches Spiel programmieren. Hier ist ein Beispiel für ein Tic-Tac-Toe-Spiel:
```
import java.util.Scanner;

public class TicTacToe {
    public static void main(String[] args) {
        char[][] board = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
        char currentPlayer = 'X';
        Scanner scanner = new Scanner(System.in);

        while (true) {
            printBoard(board);
            System.out.println("Spieler " + currentPlayer + ", wähle ein Feld (1-9):");
            int move = scanner.nextInt();

            if (move < 1 || move > 9) {
                System.out.println("Ungültiger Zug!");
                continue;
            }

            int row = (move - 1) / 3;
            int col = (move - 1) % 3;

            if (board[row][col] == 'X' || board[row][col] == 'O') {
                System.out.println("Feld bereits besetzt!");
                continue;
            }

            board[row][col] = currentPlayer;

            if (checkWin(board, currentPlayer)) {
                printBoard(board);
                System.out.println("Spieler " + currentPlayer + " hat gewonnen!");
                break;
            }

            currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
        }
    }

    public static void printBoard(char[][] board) {
        System.out.println(board[0][0] + " | " + board[0][1] + " | " + board[0][2]);
        System.out.println("---------");
        System.out.println(board[1][0] + " | " + board[1][1] + " | " + board[1][2]);
        System.out.println("---------");
        System.out.println(board[2][0] + " | " + board[2][1] + " | " + board[2][2]);
    }

    public static boolean checkWin(char[][] board, char player) {
        // Zeilen und Spalten überprüfen
        for (int i = 0; i < 3; i++) {
            if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
                return true;
            }
            if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
                return true;
            }
        }

        // Diagonalen überprüfen
        if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
                (board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
            return true;
        }

        return false;
    }
}
```
Dieses Spiel kann von zwei Spielern gespielt werden. Du kannst es kopieren und ausführen, um es selbst auszuprobieren!

Möchtest du ein anderes Spiel programmieren?
stdout
Standard output is empty