Guten Tag alle zusammen...
Ich habe das Spiel 4 Gewinnt in der Programmiersprache C geschrieben! Das Problem hierbei ist das das Spiel einfach abbrechen tuet bei erfolgreicher Kette (4'er Kette)... Kann mir jemand helfen ? Würde mich sehr freuen! Danke !
p.S.: Beim compilieren erhalte ich keine Fehler ich benutze den Compiler von Dev C++!
_____________________________________________________________
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WIDTH 6
#define HEIGHT 5
const char *player_str[2] = { "Computer", "Mensch" };
const int frame_char[2] = { '+', '-' };
const int feld_char[3] = { ' ', 'O', '#' };
int xy_zeahler = 0;
enum feld {
LEER = 0,
PL1, // player 1 set
PL2 // player 2 set
};
enum feld xy[WIDTH][HEIGHT];
enum ergebnis {
FP, // Falsche pos.
BENUTZT,
GEWONNEN,
NE,
VOLL
};
enum ergebnis spiel_neue_pos();
enum ergebnis computer_neu_pos();
enum ergebnis mensch_neu_pos();
enum ergebnis place();
//------------------------ok
void auswahl(int human[2])
{
int wahl, c;
while (1) {
printf("\nSpiel Modus weahlen:\n 1 - Player1 vs. Player2\n 2 - computer vs. Player1\n "
"3 - Player1 vs. computer\n 4 - computer vs. computer\n Wahl (1-4): " );
wahl = (c = getchar()) - '0';
while (c != '\n')
c = getchar();
if (wahl >= 1 && wahl <= 4)
break;
else
printf("Falsche Eingabe bitte nochmals weahlen \n");
}
human[0] = wahl == 1 || wahl == 3;
human[1] = wahl == 1 || wahl == 2;
printf( "player #1: %s\n", player_str[human[0]] );
printf( "player #2: %s\n", player_str[human[1]] );
}
//-----------------------------ok
//-----------------------------ok
void init_feld()
{
int ix, iy;
for (ix = 0; ix < WIDTH; ix++)
for (iy = 0; iy < HEIGHT; iy++)
xy[ix][iy] = 0;
xy_zeahler = 0;
}
//-----------------------------ok
//-----------------------------OK
void ausgabeFeld()
{
int ix, iy;
putchar('\n');
for (ix = 0; ix < WIDTH; ix++)
printf(" %d", ix+1);
putchar('\n');
for (ix = 0; ix <= WIDTH * 2; ix++)
putchar(frame_char[ix%2]);
putchar('\n');
for (iy = 0; iy < HEIGHT; iy++)
{
putchar('|');
for (ix = 0; ix < WIDTH; ix++)
{
putchar( feld_char[ xy[ix][HEIGHT-iy-1] ] );
putchar('|');
}
putchar('\n');
}
for (ix = 0; ix <= WIDTH * 2; ix++)
putchar(frame_char[ix%2]);
putchar('\n');
for (ix = 0; ix < WIDTH; ix++)
printf(" %d", ix+1);
putchar('\n');
putchar('\n');
}
//----------------------------------------ok
int next_empty_y(int ix)
{
int iy;
for (iy = 0; iy < HEIGHT; iy++)
if (xy[ix][iy] == LEER) // y position empty?
return iy;
return -1; // all y positions set by some player
}
int xy_pl(int player, int ix, int iy)
{
return ix >= 0 && ix < WIDTH &&
iy >= 0 && iy < HEIGHT &&
xy[ix][iy] == player;
}
enum ergebnis spiel_neue_pos(int player, int human)
{
if (xy_zeahler == WIDTH*HEIGHT)
return VOLL;
return human ?
mensch_neu_pos(player) :
computer_neu_pos(player);
}
enum ergebnis mensch_neu_pos(int player)
{
int new_x, c;
printf( "player #%d (%c), new pos (1-%d): ",
player, feld_char[player], WIDTH );
new_x = (c = getchar()) - '1';
while (c != '\n')
c = getchar();
if ( new_x < 0 || new_x >= WIDTH )
return FP;
return place(player, new_x);
}
enum ergebnis computer_neu_pos(int player)
{
static int randomize = 1;
if (randomize)
{
srand(time(NULL));
randomize = 0;
}
return place(player, (double)rand() / RAND_MAX * WIDTH);
}
enum ergebnis place(int player, int ix)
{
int tlbr_cnt = 0; // top-left -> bottom-right
int tmbm_cnt = 0; // top-middle -> bottom-middle
int mlmr_cnt = 0; // middle-left -> middle-right
int trbl_cnt = 0; // top-right -> bottom-left
int i, iy;
// check if parameters are valid
if ( ( player != PL1 && player != PL2 ) ||
ix < 0 || ix >= WIDTH )
return FP;
// check if there is an empty position in that column
if ((iy = next_empty_y(ix)) == -1)
return BENUTZT;
// set position
xy[ix][iy] = player;
xy_zeahler++;
#define CHECK4(player, ix, iy, counter) \
for (i = 1; i <= 3; i++) \
if ( !xy_pl(player,ix,iy) ) \
break; \
counter += i - 1; \
CHECK4(player, ix-i, iy-i, tlbr_cnt);
CHECK4(player, ix+i, iy+i, tlbr_cnt);
CHECK4(player, ix, iy-i, tmbm_cnt);
CHECK4(player, ix, iy+i, tmbm_cnt);
CHECK4(player, ix-i, iy, mlmr_cnt);
CHECK4(player, ix+i, iy, mlmr_cnt);
CHECK4(player, ix+i, iy-i, trbl_cnt);
CHECK4(player, ix-i, iy+i, trbl_cnt);
if ( tlbr_cnt > 2 ||
tmbm_cnt > 2 ||
trbl_cnt > 2 ||
mlmr_cnt > 2 )
return GEWONNEN;
else
return NE;
}
void play()
{
enum ergebnis ergebnis;
int human[2];
auswahl(human);
init_feld();
while (1)
{
ausgabeFeld();
do
ergebnis = spiel_neue_pos(PL1, human[0]);
while ( ergebnis == FP || ergebnis == BENUTZT );
if (ergebnis == GEWONNEN)
{
ausgabeFeld();
printf( "Player 1 (%s) gewinnt !\n\n", player_str[human[0]] );
break;
}
else if (ergebnis == VOLL)
break;
ausgabeFeld();
do
ergebnis = spiel_neue_pos(PL2, human[1]);
while ( ergebnis == FP || ergebnis == BENUTZT );
if (ergebnis == GEWONNEN)
{
ausgabeFeld();
printf( "Player 2 (%s) gewinnt !\n\n", player_str[human[1]] );
break;
}
else if (ergebnis == VOLL)
break;
}
if (ergebnis == VOLL)
{
ausgabeFeld();
printf("\nUnentschieden!");
}
}
int main() {
play();
return 1;
}
kingbozz Gast |
