1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
bool PC_algo_win_it(int array[4][4], bool used){
srand(time(NULL));
//!1.Zeile
if((array[0][0]==1)&&(array[0][1]==1)&&(array[0][2] == 0)){
array[0][2] = 1; used = true;
}
//!1.Zeile
else if((array[0][0]==1)&&(array[0][2]==1)&&(array[0][1] == 0)){
array[0][1] = 1; used = true;
}
//!1.Zeile
else if((array[0][1]==1)&&(array[0][2]==1)&&(array[0][0] == 0)){
array[0][0] = 1; used = true;
}
//!2.Zeile
else if((array[1][0]==1)&&(array[1][1]==1)&&(array[1][2] == 0)){
array[1][2] = 1; used = true;
}
//!2.Zeile
else if((array[1][0]==1)&&(array[1][2]==1)&&(array[1][1] == 0)){
array[1][1] = 1; used = true;
}
//!2.Zeile
else if((array[1][1]==1)&&(array[1][2]==1)&&(array[1][0] == 0)){
array[1][0] = 1; used = true;
}
//!3.Zeile
else if((array[2][0]==1)&&(array[2][1]==1)&&(array[2][2] == 0)){
array[2][2] = 1; used = true;
}
//!3.Zeile
else if((array[2][0]==1)&&(array[2][2]==1)&&(array[2][1] == 0)){
array[2][1] = 1; used = true;
}
//!3.Zeile
else if((array[2][1]==1)&&(array[2][2]==1)&&(array[2][0] == 0)){
array[2][0] = 1; used = true;
}
//!-----------------------------------------
//!1.Spalte
else if((array[0][0]==1)&&(array[1][0]==1)&&(array[2][0] == 0)){
array[2][0] = 1; used = true;
}
//!1.Spalte
else if((array[0][0]==1)&&(array[2][0]==1)&&(array[1][0] == 0)){
array[1][0] = 1; used = true;
}
//!1.Spalte
else if((array[0][1]==1)&&(array[0][2]==1)&&(array[0][0] == 0)){
array[0][0] = 1; used = true;
}
//!2.Spalte
else if((array[0][1]==1)&&(array[1][1]==1)&&(array[2][1] == 0)){
array[2][1] = 1; used = true;
}
//!2.Spalte
else if((array[0][1]==1)&&(array[2][1]==1)&&(array[1][1] == 0)){
array[1][1] = 1; used = true;
}
//!2.Spalte
else if((array[1][1]==1)&&(array[2][1]==1)&&(array[0][1] == 0)){
array[0][1] = 1; used = true;
}
//!3.Spalte
else if((array[0][2]==1)&&(array[2][2]==1)&&(array[1][2] == 0)){
array[1][2] = 1; used = true;
}
//!3.Spalte
else if((array[0][2]==1)&&(array[1][2]==1)&&(array[2][2] == 0)){
array[2][2] = 1; used = true;
}
//!3.Spalte
else if((array[1][2]==1)&&(array[2][2]==1)&&(array[0][2] == 0)){
array[0][2] = 1; used = true;
}
//!Diagonale LO->RU
else if((array[0][0]==1)&&(array[2][2]==1)&&(array[1][1] == 0)){
array[1][1] = 1; used = true;
}
//!Diagonale LO->RU
else if((array[0][0]==1)&&(array[1][1]==1)&&(array[2][2] == 0)){
array[2][2] = 1; used = true;
}
//!Diagonale LO->RU
else if((array[1][1]==1)&&(array[2][2]==1)&&(array[0][0] == 0)){
array[0][0] = 1; used = true;
}
//!Diagonale LU->RO
else if((array[2][0]==1)&&(array[1][1]==1)&&(array[0][2] == 0)){
array[0][2] = 1; used = true;
}
//!Diagonale LU->RO
else if((array[2][0]==1)&&(array[0][2]==1)&&(array[1][1] == 0)){
array[1][1] = 1; used = true;
}
//!Diagonale LU->RO
else if((array[1][1]==1)&&(array[0][2]==1)&&(array[2][0] == 0)){
array[2][0] = 1; used = true;
}
return used;
}
void player_selection(short user_bit, int array[4][4]){
short benutzer_position;
bool used = false;
srand(time(NULL));
//Computer ist dran
if(user_bit == 0){
used = PC_algo_win_it(array,used);
PC_algo(array,used);
}
//Benutzer ist dran
if(user_bit == 1){
srand(time(NULL));
do{
benutzer_position = eingabe();
}while(!((benutzer_position>0 && benutzer_position<10) && (array[(benutzer_position-1)/3][(benutzer_position-1)%3] == 0)));
array[(benutzer_position-1)/3][(benutzer_position-1)%3] = 2;
}
}
short search_gewinner(int array[4][4]){
short i, gewinner = 0;
for(i = 1; i<3; i++){
if( ((array[0][0]==i)&&(array[0][1]==i)&&(array[0][2]==i))||//1.Zeile
((array[1][0]==i)&&(array[1][1]==i)&&(array[1][2]==i))||//2.Zeile
((array[2][0]==i)&&(array[2][1]==i)&&(array[2][2]==i))||//3.Zeile
((array[0][0]==i)&&(array[1][0]==i)&&(array[2][0]==i))||//1.Spalte
((array[0][1]==i)&&(array[1][1]==i)&&(array[2][1]==i))||//2.Spalte
((array[0][2]==i)&&(array[1][2]==i)&&(array[2][2]==i))||//3.Spalte
((array[0][0]==i)&&(array[1][1]==i)&&(array[2][2]==i))||//LO->RU Diagonale
((array[0][2]==i)&&(array[1][1]==i)&&(array[2][0]==i))) //RO->LU Diagonale
{
gewinner = i;
}
}
return gewinner;
}
int main(){
bool GAME_IS_RUNNING = false, GAME_NO_RESULT = false;
char again[2];
int array[4][4];
short user_bit, gewinner, Rundenzaehler = 0, Durchgaenge = 0, statistik_PC = 0, statistik_USER = 0, statistik_unentschieden = 0; //User_bit = 1 -> Benutzer ist an der Reihe; = 0 -> PC
float Prozent;
srand(time(NULL));
user_bit = rand() %2;
GAME_IS_RUNNING = true;
while(GAME_IS_RUNNING){
system("COLOR F2");
Durchgaenge++;
init_raster(array);
raster(array);
GAME_NO_RESULT = true;
while(GAME_NO_RESULT){
Rundenzaehler++;
player_selection(user_bit, array);
if(user_bit == 1){user_bit = 0;}
else if(user_bit == 0){user_bit = 1;}
raster(array);
gewinner = search_gewinner(array);
if(gewinner > 0){
GAME_NO_RESULT =false;
continue;
}
else if(Rundenzaehler == 9){
gewinner = 0;
Rundenzaehler = 0;
GAME_NO_RESULT =false;
}
}
Rundenzaehler = 0;
if(gewinner == 0){ statistik_unentschieden++; printf("\nUnentschieden!\nWillst du noch einmal spielen? (y/n)< >\b\b"); scanf("%c",&again); fflush(stdin);}
else if(gewinner == 1){ statistik_PC++; system("COLOR 4F"); printf("\nDer PC hat gewonnen!\nWillst du noch einmal spielen? (y/n)< >\b\b"); scanf("%c",&again); fflush(stdin);}
else if(gewinner == 2){ statistik_USER++; system("COLOR 2F"); printf("\nDer User hat gewonnen!\nWillst du noch einmal spielen? (y/n)< >\b\b"); scanf("%c",&again); fflush(stdin);}
if(again[0] == 'n'){GAME_IS_RUNNING = false;}
else{GAME_IS_RUNNING = true;}
}
printf("\n\nSTATISTIK:\n");
printf("PC : %i\n", statistik_PC);
printf("USER: %i\n", statistik_USER);
printf("REMI: %i\n", statistik_unentschieden);
Prozent = (float)statistik_unentschieden/Durchgaenge*100;
printf("\nREMI/DURCHGANG in %c: %.2f %c\n",0x25, Prozent, 0x25);
return 0;
}
| |