CyborgBeta
Banned
- Registriert
- Jan. 2021
- Beiträge
- 3.405
Hi Leute,
ich bin vorhin auf ein interessantes Problem gestoßen, und zwar soll mit nur einer äußeren Schleife überprüft werden, ob jemand und wer beim Spiel Tic-Tac-Toe gewonnen hat:
Die Ausgabe stimmt so weit - bis auf Nummer 11 (Zeile 51):
diese müsste eigentlich wie folgt sein:
11
_ _ a
_ b _
_ _ _
das Problem dabei ist, wenn man
Hätte vielleicht jemand eine Idee?
Danke, und kommt alle gut und wohlbehalten ins neue Jahr. 
ich bin vorhin auf ein interessantes Problem gestoßen, und zwar soll mit nur einer äußeren Schleife überprüft werden, ob jemand und wer beim Spiel Tic-Tac-Toe gewonnen hat:
Java:
package org.example;
public class Dodelschach {
private static int s_i = 1;
public static void print_helper(int a, int b) {
System.out.println(s_i++);
for (int i = 0; i < 9; ) {
boolean printed = false;
if (i == a) {
printed = true;
System.out.print("a");
}
if (i == b) {
printed = true;
System.out.print("b");
}
if (printed) {
System.out.print(" ");
} else {
System.out.print("_ ");
}
i++;
if (i % 3 == 0) {
System.out.println();
}
}
System.out.println();
}
public static int getResult(int[] c) {
for (int i = 0; i < 3; i++) {
boolean wins1 = true;
boolean wins2 = true;
// Reihe:
for (int j = 0; j < 2; j++) {
int idx1 = 3 * i + j;
int idx2 = 3 * i + j + 1;
print_helper(idx1, idx2);
// if (c[idx1] == 0 || c[idx2] == 0) {
// wins1 = false;
// break;
// }
// if (c[idx1] != c[idx2]) {
// wins2 = false;
// break;
// }
}
// if (wins1 && wins2) {
// return c[3 * i];
// }
wins1 = true;
wins2 = true;
// Spalte:
for (int j = 0; j < 2; j++) {
int idx1 = j * 3 + i;
int idx2 = (j + 1) * 3 + i;
print_helper(idx1, idx2);
// if (c[idx1] == 0 || c[idx2] == 0) {
// wins1 = false;
// break;
// }
// if (c[idx1] != c[idx2]) {
// wins2 = false;
// break;
// }
}
// if (wins1 && wins2) {
// return c[i];
// }
wins1 = true;
wins2 = true;
// Haupt- und Nebendiagonale:
for (int j = 0; j < 2; j++) {
if (i == 2) {
wins1 = false;
break;
}
int idx1 = j * 3 + j; // <- Was stimmt hier nicht?
int idx2 = (j + 1) * 3 + j + 1 - i * j * 2;
print_helper(idx1, idx2);
// if (c[idx1] == 0 || c[idx2] == 0) {
// wins1 = false;
// break;
// }
// if (c[idx1] != c[idx2]) {
// wins2 = false;
// break;
// }
}
// if (wins1 && wins2) {
// return c[i * 3];
// }
}
return 0;
}
public static void main(String[] args) {
getResult(new int[9]);
}
}
Die Ausgabe stimmt so weit - bis auf Nummer 11 (Zeile 51):
Code:
1
a b _
_ _ _
_ _ _
2
_ a b
_ _ _
_ _ _
3
a _ _
b _ _
_ _ _
4
_ _ _
a _ _
b _ _
5
a _ _
_ b _
_ _ _
6
_ _ _
_ a _
_ _ b
7
_ _ _
a b _
_ _ _
8
_ _ _
_ a b
_ _ _
9
_ a _
_ b _
_ _ _
10
_ _ _
_ a _
_ b _
11
a _ _
_ b _
_ _ _
12
_ _ _
_ a _
b _ _
13
_ _ _
_ _ _
a b _
14
_ _ _
_ _ _
_ a b
15
_ _ a
_ _ b
_ _ _
16
_ _ _
_ _ a
_ _ b
diese müsste eigentlich wie folgt sein:
11
_ _ a
_ b _
_ _ _
das Problem dabei ist, wenn man
idx1
(Zeile 81) ändert, beeinflusst das auch die Ausgabe von 5, 6 und 12...Hätte vielleicht jemand eine Idee?

