Diesmal sollte es Tic Tac Toe sein. Die Zeit hatten wir diesmal vorher auf 1 Stunde beschränkt.
Wieder gibt es eine Java und eine C# - XNA Lösung.
Programmierzeit Java: 1 h 15 min
Programmierzeit C#: 57 min
Heute möchten wir etwas mehr auf den dabei entstandenen Code eingehen.
Anhand des Java-Beispiels möchten wir euch die TicTacToe-KI vorstellen. Man beachte bitte die begrenzte Zeit von einer Stunde und unsere absolute Priorität liegt bei der schnellen Programmierung.
private void play() {
for (int i = 0; i < field.length; i++) {
int cpu = 0;
int player = 0;
int none = 0;
for (int j = 0; j < field[i].length; j++) {
switch (field[i][j]) {
case PLAYER:
player++;
break;
case CPU:
cpu++;
break;
case NONE:
none++;
break;
}
}
if (cpu == 2 || player == 2) {
for (int j = 0; j < field[i].length; j++) {
if (field[i][j] == NONE) {
set(i, j);
return;
}
}
}
}
for (int j = 0; j < field.length; j++) {
int cpu = 0;
int player = 0;
int none = 0;
for (int i = 0; i < field[j].length; i++) {
switch (field[i][j]) {
case PLAYER:
player++;
break;
case CPU:
cpu++;
break;
case NONE:
none++;
break;
}
}
if (cpu == 2 || player == 2) {
for (int i = 0; i < field[j].length; i++) {
if (field[i][j] == NONE) {
set(i, j);
return;
}
}
}
}
int cpu = 0;
int player = 0;
int none = 0;
for (int i = 0; i < field.length; i++) {
switch (field[i][i]) {
case PLAYER:
player++;
break;
case CPU:
cpu++;
break;
case NONE:
none++;
break;
}
}
if (cpu == 2 || player == 2) {
for (int i = 0; i < field.length; i++) {
if (field[i][i] == NONE) {
set(i, i);
return;
}
}
}
cpu = 0;
player = 0;
none = 0;
for (int i = 0; i < field.length; i++) {
switch (field[i][2 - i]) {
case PLAYER:
player++;
break;
case CPU:
cpu++;
break;
case NONE:
none++;
break;
}
}
if (cpu == 2 || player == 2) {
for (int i = 0; i < field.length; i++) {
if (field[i][2 - i] == NONE) {
set(i, 2 - i);
return;
}
}
}
boolean set = false;
while (!set) {
int x = (int) (3 * Math.random());
int y = (int) (3 * Math.random());
if (field[x][y] == NONE) {
set(x, y);
set = true;
}
}
checkWon(CPU);
}
Die beiden Versionen im Video:
C#:
Java:
Kann man das auch runterladen?
AntwortenLöschenBrauche die XNA-Version