4 Gewinnt

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Leute ich brauche dringent hilfe ich muss e projekt abgeben und zwar

    Schreiben Sie ein Spiel „4Gewinnt“. Falls sie das Spiel nicht kennen sollten finden sie eine Version des Spiels unter dem folgenden Link:
    http://www.funnygames.ch/4gewinnt/4gewinnt.htm

    Folgende Eigenschaften muss das Spiel mindestens haben:


    • Spieler gegen Spieler
    • Spieler gegen Computer
    • Erkennung der Gewinnbedingung für beide Spieler
    • Intelligenz des Computers muss mindestens „zufällig“ sein


    Bitte Bitte ich brauch Hilfe von euch

    Programier Sprache C :)
  • kürzere lösungen gibts auf jeden fall..
    aber gerade die vergehensweise finde ich persönlich ziemlich verständlich

    angefangen wird in der main()
    hier werden die spielernamen eingeben
    der aktuelle spieler wird in der globalen SPIELER definiert und wechselt in jedem durchlauf ab (genau wie die symbole)

    dann wird das spielzeug gezeigt showSpielfeld()

    bei korrekter eingabe gehts dann weiter mit setzeFeld()
    dieser schaut bei angegebener spalte ob die zeilen schon besetzt sind
    findet er ein freies feld, dann wird das zeichen dort gesetzt

    die gesetzt position wird dann weitergegeben an IsGameOver()
    welche wiederum in alle richtungen die felder des spielers zählt
  • Bin noch nicht sehr weit.
    Die 'Spieler' muss ich noch in Funktionen und Schleifen schreiben!!

    kann da jemand helfen?

    Quellcode

    1. #include <stdio.h>
    2. #include <conio.h>
    3. #include <stdlib.h>
    4. #include "ConsoleFunctions.h"
    5. //Hauptprogrammm
    6. int main (void)
    7. {
    8. //Variablendeclaration
    9. int matrix[7][7];
    10. int i=0,j=0;
    11. int zug;
    12. int sp=14; //Spalte(Standart=14)
    13. char sp1z=X; //Spieler 1 Zeichen
    14. printf(" ****************\n");
    15. printf(" **Vier Gewinnt**\n");
    16. printf(" ****************\n\n\n\n");
    17. printf("0123456\n");
    18. printf("_______\n");
    19. for(i=0;i<7;i++){
    20. for(j=0;j<7;j++){
    21. matrix[i][j]='.';
    22. printf("%c",matrix[i][j]);
    23. }
    24. printf("\n");
    25. }
    26. //Spieler 1
    27. printf("\n\n\n\n");
    28. printf("Wo wollen sie ihren Stein platzieren?(1,2,3,4,5,6,7)\n");
    29. scanf("%d",&zug);
    30. gotoxy(zug,14);
    31. printf("%c",sp1z);
    32. //Spieler 2
    33. printf("\n\n");
    34. printf("Wo wollen sie ihren Stein platzieren?(1,2,3,4,5,6,7)\n");
    35. scanf("%d",&zug);
    36. gotoxy(zug,sp);
    37. for(i=0;i<7;i++){
    38. if((i==0)||(i==sp1z)){
    39. sp--;
    40. //Sollte überprüfen ob schon ein Stein hier liegt
    41. }
    42. else{
    43. printf("0");
    44. }
    45. }
    46. return 0;
    47. }
    Alles anzeigen
  • so kann man 4 Gewinnt Lösen...


    Quellcode

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <time.h>
    4. #define WIDTH 6
    5. #define HEIGHT 6
    6. // one position in field can be:
    7. enum field {
    8. EMPTY = 0,
    9. PL1, // player 1 set
    10. PL2 // player 2 set
    11. };
    12. // functions may return:
    13. enum result {
    14. INV, // invalid position
    15. USED, // position already set
    16. WON, // found 4 chain
    17. NE, // not enough (4 chain not found)
    18. FULL // all positions on field already set
    19. };
    20. const int field_char[3] = { ' ', 'O', '#' };
    21. const int frame_char[2] = { '+', '-' };
    22. const char *player_str[2] = { "computer", "human" };
    23. enum field xy[WIDTH][HEIGHT]; // game-field (width x height)
    24. int xy_count = 0; // count of positions in field xy[][] set
    25. // description:
    26. // print xy[][] field on text-screen,
    27. // ix / width - vertical, iy / height - horizontal,
    28. // x numbers on top and bottom
    29. void print_field(void)
    30. {
    31. int ix, iy;
    32. putchar('\n');
    33. // top line with x numbers
    34. for (ix = 0; ix < WIDTH; ix++)
    35. printf(" %d", ix+1);
    36. putchar('\n');
    37. // frame top line
    38. for (ix = 0; ix <= WIDTH * 2; ix++)
    39. putchar(frame_char[ix%2]);
    40. putchar('\n');
    41. for (iy = 0; iy < HEIGHT; iy++)
    42. {
    43. // an iy line
    44. putchar('|');
    45. for (ix = 0; ix < WIDTH; ix++)
    46. {
    47. putchar( field_char[ xy[ix][HEIGHT-iy-1] ] );
    48. putchar('|');
    49. }
    50. putchar('\n');
    51. }
    52. // frame bottom line
    53. for (ix = 0; ix <= WIDTH * 2; ix++)
    54. putchar(frame_char[ix%2]);
    55. putchar('\n');
    56. // bottom line with x numbers
    57. for (ix = 0; ix < WIDTH; ix++)
    58. printf(" %d", ix+1);
    59. putchar('\n');
    60. putchar('\n');
    61. }
    62. // description:
    63. // search for next empty (not set by a player)
    64. // position of a given column, i.e. top of pile
    65. // parameters:
    66. // int ix - x column, 0 <= x < WIDTH
    67. // return:
    68. // next empty y position
    69. // -1 if ix-column full
    70. int next_empty_y(int ix)
    71. {
    72. int iy;
    73. for (iy = 0; iy < HEIGHT; iy++)
    74. if (xy[ix][iy] == EMPTY) // y position empty?
    75. return iy;
    76. return -1; // all y positions set by some player
    77. }
    78. // description:
    79. // check if 0 <= ix < WIDTH and
    80. // check if 0 <= iy < HEIGHT and
    81. // check if xy position is set by player one (PL1) or player two (PL2)
    82. // parameters:
    83. // int player - player one (PL1) or player two (PL2)
    84. // int ix - x column
    85. // int iy - y position in x column
    86. int xy_pl(int player, int ix, int iy)
    87. {
    88. return ix >= 0 && ix < WIDTH &&
    89. iy >= 0 && iy < HEIGHT &&
    90. xy[ix][iy] == player;
    91. }
    92. // description:
    93. // set given x position, if still free
    94. // parameters:
    95. // int player - player one (PL1) or player two (PL2)
    96. // int ix - x column
    97. // returns: a enum result value
    98. // INV - invalid position
    99. // USED - position already set
    100. // WON - found 4 chain
    101. // NE - not enough (4 chain not found)
    102. enum result place(int player, int ix)
    103. {
    104. int tlbr_cnt = 0; // top-left -> bottom-right
    105. int tmbm_cnt = 0; // top-middle -> bottom-middle
    106. int mlmr_cnt = 0; // middle-left -> middle-right
    107. int trbl_cnt = 0; // top-right -> bottom-left
    108. int i, iy;
    109. // check if parameters are valid
    110. if ( ( player != PL1 && player != PL2 ) ||
    111. ix < 0 || ix >= WIDTH )
    112. return INV;
    113. // check if there is an empty position in that column
    114. if ((iy=next_empty_y(ix)) == -1)
    115. return USED;
    116. // set position
    117. xy[ix][iy] = player;
    118. xy_count++;
    119. #define CHECK4(player, ix, iy, counter) \
    120. for (i = 1; i <= 3; i++) \
    121. if ( !xy_pl(player,ix,iy) ) \
    122. break; \
    123. counter += i - 1;
    124. // check for 4 in a row
    125. CHECK4(player, ix-i, iy-i, tlbr_cnt);
    126. CHECK4(player, ix+i, iy+i, tlbr_cnt);
    127. CHECK4(player, ix, iy-i, tmbm_cnt);
    128. CHECK4(player, ix, iy+i, tmbm_cnt);
    129. CHECK4(player, ix-i, iy, mlmr_cnt);
    130. CHECK4(player, ix+i, iy, mlmr_cnt);
    131. CHECK4(player, ix+i, iy-i, trbl_cnt);
    132. CHECK4(player, ix-i, iy+i, trbl_cnt);
    133. // check if there is a 4 chain
    134. if ( tlbr_cnt > 2 ||
    135. tmbm_cnt > 2 ||
    136. trbl_cnt > 2 ||
    137. mlmr_cnt > 2 )
    138. return WON;
    139. return NE;
    140. }
    141. // description:
    142. // human player plays one turn
    143. // get column number and place
    144. // parameters:
    145. // int player - player one (PL1) or player two (PL2)
    146. enum result human_new_pos(int player)
    147. {
    148. int new_x, c;
    149. // get column number
    150. printf( "player #%d (%c), new pos (1-%d): ",
    151. player, field_char[player], WIDTH );
    152. new_x = (c = getchar()) - '1';
    153. while (c != '\n')
    154. c = getchar();
    155. // check if column valid
    156. if ( new_x < 0 || new_x >= WIDTH )
    157. return INV;
    158. // place
    159. return place(player, new_x);
    160. }
    161. // description:
    162. // computer player plays one turn
    163. // select random column number and place
    164. // parameters:
    165. // int player - player one (PL1) or player two (PL2)
    166. enum result computer_new_pos(int player)
    167. {
    168. static int randomize = 1;
    169. // check if random generator initialised
    170. if (randomize)
    171. {
    172. // intialise random generator
    173. srand(time(NULL));
    174. randomize = 0;
    175. }
    176. // place to a random column
    177. return place(player, (double)rand() / RAND_MAX * WIDTH);
    178. }
    179. // description:
    180. // a player plays one turn
    181. // call apropriate funktion for human or computer player
    182. // parameters:
    183. // int player - player one (PL1) or player two (PL2)
    184. // int human - 1 it is human, 0 it is computer
    185. enum result play_new_pos(int player, int human)
    186. {
    187. // check if all positions already set
    188. if (xy_count == WIDTH*HEIGHT)
    189. return FULL;
    190. // call apropriate function for the turn
    191. return human ?
    192. human_new_pos(player) :
    193. computer_new_pos(player);
    194. }
    195. // description:
    196. // initialise field xy
    197. void init_field(void)
    198. {
    199. int ix, iy;
    200. for (ix = 0; ix < WIDTH; ix++)
    201. for (iy = 0; iy < HEIGHT; iy++)
    202. xy[ix][iy] = EMPTY;
    203. xy_count = 0;
    204. }
    205. // description:
    206. // select play modus, i.e. who plays against whom
    207. // parameters:
    208. // int human[2] - human[0] and human[1] is set to 0 or 1
    209. // according to play modus
    210. void select_modus(int human[2])
    211. {
    212. int chosen, c;
    213. // get play modus
    214. while (1) {
    215. printf("\n\n"
    216. "select play modus:\n"
    217. " 1 - human vs. human\n"
    218. " 2 - computer vs. human\n"
    219. " 3 - human vs. computer\n"
    220. " 4 - computer vs. computer\n"
    221. "choose (1-4): " );
    222. // save play modus
    223. chosen = (c = getchar()) - '0';
    224. // flush input
    225. while (c != '\n')
    226. c = getchar();
    227. // check if valid play modus chosen
    228. if (chosen >= 1 && chosen <= 4)
    229. break; // and break out of loop
    230. else
    231. printf("invalid modus! select again...\n");
    232. }
    233. // set human[0] and human[1] to 0 or 1 depending on
    234. // whether the player is human or computer according to play modus
    235. human[0] = chosen == 1 || chosen == 3;
    236. human[1] = chosen == 1 || chosen == 2;
    237. printf( "player #1: %s\n", player_str[human[0]] );
    238. printf( "player #2: %s\n", player_str[human[1]] );
    239. }
    240. // description:
    241. // play 4 wins
    242. void play(void)
    243. {
    244. enum result result;
    245. int human[2];
    246. // get play modus
    247. select_modus(human);
    248. // initialise field
    249. init_field();
    250. // play until one player wins or till remis
    251. while (1)
    252. {
    253. //if (human[0])
    254. print_field();
    255. // player 1 turn
    256. do
    257. result = play_new_pos(PL1, human[0]);
    258. while ( result == INV || result == USED );
    259. // chek if player 1 won
    260. if (result == WON)
    261. {
    262. print_field();
    263. printf( "player 1 (%s) won!\n\n", player_str[human[0]] );
    264. break;
    265. }
    266. else if (result == FULL)
    267. break;
    268. //if (human[1])
    269. print_field();
    270. // player 2 turn
    271. do
    272. result = play_new_pos(PL2, human[1]);
    273. while ( result == INV || result == USED );
    274. // check if player 2 won
    275. if (result == WON)
    276. {
    277. print_field();
    278. printf( "player 2 (%s) won!\n\n", player_str[human[1]] );
    279. break;
    280. }
    281. else if (result == FULL)
    282. break;
    283. }
    284. // check for remis
    285. if (result == FULL)
    286. {
    287. print_field();
    288. printf("draw! no one won..");
    289. }
    290. }
    291. int main(int argc, char *argv[])
    292. {
    293. while (1)
    294. {
    295. play();
    296. printf("press <enter> to continue\n"
    297. "press <ctrl> + <c> to quit");
    298. while (getchar() != '\n')
    299. ;
    300. }
    301. exit(0);
    302. }
    Alles anzeigen



    Lg