kokusz19

ConnectFour.localhost.c

Dec 14th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define COLS 7
  4. #define ROWS 6
  5.  
  6. int win = 0;
  7.  
  8. /*************************************************/
  9. /*          Tomb feltoltese, kiiratasa           */
  10. /*************************************************/
  11. void feltolt(char feltolt[COLS][ROWS]){
  12.     int i, j;
  13.     for(i = 0; i < ROWS; i++){
  14.         for(j = 0; j < COLS; j++){
  15.             feltolt[j][i] = '-';
  16.         }  
  17.     }
  18. }
  19. void kiir(char kiirat[COLS][ROWS]){
  20.     int i, j;
  21.     printf("\n\n");
  22.     printf("1 2 3 4 5 6 7\n");
  23.     printf("*************\n");
  24.  
  25.     for(i = 0; i < ROWS; i++){
  26.         for(j = 0; j < COLS; j++)
  27.             printf("%c ", kiirat[j][i]);
  28.         printf("\n");
  29.         }
  30.     printf("*************\n");
  31.     printf("*************\n");
  32. }
  33.  
  34. /*************************************************/
  35. /*              Player1 valasztasa               */
  36. /*************************************************/
  37. int bele1(char tomb[COLS][ROWS]){
  38.     char string[255];
  39.  
  40.     int i, place, bad = 0;
  41.     printf("\nP1: Chose a column: ");
  42.  
  43.     do{
  44.         if(bad == 0){
  45.             scanf("%s", &string);
  46.             if(!strcmp(string, "feladom")){
  47.                 return -1;
  48.             }
  49.             place = atoi(string);
  50.             bad++;
  51.         }
  52.         else{
  53.             printf("\nInvalid move: Chose another column (1-7)");
  54.             scanf("%s", &string);
  55.             place = atoi(string);
  56.         }
  57.         bad++;
  58.     }while(checkifplacable(tomb, place) != 1);
  59.        
  60.     for(i = ROWS-1; i >= 0; i--){                                       //Placement
  61.         if(tomb[place-1][i] == '-'){
  62.             tomb[place-1][i] = 'X';
  63.             checkforwin1(tomb);
  64.             break;
  65.         }
  66.     }
  67.    
  68.     kiir(tomb);
  69.     return 1;
  70. }
  71.  
  72. /*************************************************/
  73. /*              Player2 valasztasa               */
  74. /*************************************************/
  75. int bele2(char tomb[COLS][ROWS]){
  76.     char string[255];
  77.    
  78.     int i, place, bad = 0;
  79.     printf("\nP2: Chose a column: ");
  80.  
  81.     do{
  82.         if(bad == 0){
  83.             scanf("%s", &string);
  84.             if(!strcmp(string, "feladom")){
  85.                 return -1;
  86.             }
  87.             place = atoi(string);
  88.             bad++;
  89.         }
  90.         else{
  91.             printf("\nInvalid move: Chose another column (1-7)");
  92.             scanf("%d", &place);
  93.         }
  94.         bad++;
  95.     }while(checkifplacable(tomb, place) != 1);
  96.        
  97.     for(i = ROWS-1; i >= 0; i--){                                       //Placement
  98.         if(tomb[place-1][i] == '-'){
  99.             tomb[place-1][i] = 'O';
  100.             checkforwin2(tomb);
  101.             break;
  102.         }
  103.     }
  104.    
  105.     kiir(tomb);
  106.     return 1;
  107. }
  108.  
  109. /*************************************************/
  110. /*                    Checks                     */
  111. /*************************************************/
  112. int checkifplacable(char tomb[COLS][ROWS], int place){
  113.     if(place < 1 || place > 7 || tomb[place-1][0] != '-')   return -1;
  114.     else return 1;
  115. }
  116. int checkforwin1(char tomb[COLS][ROWS]){
  117.     int i, j, oszlopban = 0;
  118.    
  119.     for(i = 0; i < COLS; i++){              // OSZLOPBAN
  120.         for(j = 0; j < ROWS-3; j++){
  121.             if(tomb[i][j] == 'X' && tomb[i][j+1] == 'X' && tomb[i][j+2] == 'X' && tomb[i][j+3] == 'X')
  122.                 oszlopban = 1;
  123.         }
  124.     }
  125.    
  126.     for(i = 0; i < COLS-3; i++){            // SORBAN
  127.         for(j=0; j<ROWS; j++){
  128.             if(tomb[i][j] == 'X' && tomb[i+1][j] == 'X' && tomb[i+2][j] == 'X' && tomb[i+3][j] == 'X')
  129.                 oszlopban = 1;
  130.         }
  131.     }
  132.    
  133.     for(i = 0; i < COLS-3; i++){            // ATLOBAN LEFELE
  134.         for(j = 0; j < ROWS-3; j++){
  135.             if(tomb[i][j] == 'X' && tomb[i+1][j+1] == 'X' && tomb[i+2][j+2] == 'X' && tomb[i+3][j+3] == 'X')
  136.                 oszlopban = 1;
  137.         }
  138.     }
  139.    
  140.     for(i = 0; i < COLS-3; i++){            // ATLOBAN FELFELE
  141.         for(j = ROWS; j > 3; j--){
  142.             if(tomb[i][j] == 'X' && tomb[i+1][j-1] == 'X' && tomb[i+2][j-2] == 'X' && tomb[i+3][j-3] == 'X')
  143.                 oszlopban = 1;
  144.         }
  145.     }
  146.     win = oszlopban;
  147.     //return oszlopban;
  148. }
  149. int checkforwin2(char tomb[COLS][ROWS]){
  150.     int i, j, oszlopban = 0;
  151.  
  152.     for(i = 0; i < COLS; i++){              // OSZLOPBAN
  153.         for(j = 0; j < ROWS-3; j++){
  154.             if(tomb[i][j] == 'O' && tomb[i][j+1] == 'O' && tomb[i][j+2] == 'O' && tomb[i][j+3] == 'O') 
  155.                 oszlopban = 1;
  156.         }
  157.     }
  158.    
  159.     for(i = 0; i < COLS-3; i++){            // SORBAN
  160.         for(j=0; j<ROWS; j++){
  161.             if(tomb[i][j] == 'O' && tomb[i+1][j] == 'O' && tomb[i+2][j] == 'O' && tomb[i+3][j] == 'O')
  162.                 oszlopban = 1;
  163.         }
  164.     }
  165.    
  166.     for(i = 0; i < COLS-3; i++){            // ATLOBAN LEFELE
  167.         for(j = 0; j < ROWS-3; j++){
  168.             if(tomb[i][j] == 'O' && tomb[i+1][j+1] == 'O' && tomb[i+2][j+2] == 'O' && tomb[i+3][j+3] == 'O')
  169.                 oszlopban = 1;
  170.         }
  171.     }
  172.    
  173.     for(i = 0; i < COLS-3; i++){            // ATLOBAN FELFELE
  174.         for(j = ROWS; j >= 3; j--){
  175.             if(tomb[i][j] == 'O' && tomb[i+1][j-1] == 'O' && tomb[i+2][j-2] == 'O' && tomb[i+3][j-3] == 'O')
  176.                 oszlopban = 1;
  177.         }
  178.     }
  179.    
  180.     win = oszlopban;
  181.     //return oszlopban;
  182. }
  183. int istheremorespace(char tomb[COLS][ROWS]){
  184.     if(tomb[1][0] != '-' && tomb[2][0] != '-' && tomb[3][0] != '-' && tomb[4][0] != '-' && tomb[5][0] != '-' && tomb[6][0] != '-' && tomb[7][0] != '-'){
  185.         printf("\nTIE\nMatch is over!\tThere is no more free space!");
  186.         return -1;
  187.     }
  188.     return 1;
  189. }
  190.  
  191. /*************************************************/
  192. /*                     HUB                       */
  193. /*************************************************/
  194. int hub(){
  195.     win = 0;
  196.     int i, j, oszlop;
  197.     char table[COLS][ROWS];
  198.    
  199.     feltolt(table);
  200.     kiir(table);
  201.    
  202.     while(win == 0){
  203.         if((istheremorespace(table)) == -1)
  204.             break;
  205.        
  206.         if(bele1(table) == 1){
  207.             if(win == 1){
  208.                 printf("Player 1 wins the match");
  209.                 break;
  210.             }
  211.         }
  212.         else {
  213.             printf("Player 2 wins the match, as Player 1 surrendered!");
  214.             return 0;
  215.         }
  216.            
  217.         if(bele2(table) == 1){
  218.             if(win == 1){
  219.                 printf("Player 2 wins the match");
  220.                 break;
  221.             }
  222.         }
  223.         else {
  224.             printf("Player 1 wins the match, as Player 2 surrendered!");
  225.             return 0;
  226.         }
  227.     }  
  228.    
  229.     return 0;
  230. }
  231.  
  232.  
  233. /*************************************************/
  234. /*                     MAIN                      */
  235. /*************************************************/
  236. int main(){
  237.     hub();
  238.     printf("\nDo you want a rematch? (ujra/vege)\t");
  239.    
  240.     char valasztas1[255];
  241.     char valasztas2[255];
  242.     printf("\nP1: ");
  243.     scanf("%s", valasztas1);
  244.     printf("P2: ");
  245.     scanf("%s", valasztas2);
  246.     if(!strcmp(valasztas1, "vege"))
  247.         printf("\nPlayer1 abandoned the match.\nThe session will close soon.");
  248.     if(!strcmp(valasztas2, "vege"))
  249.         printf("\nPlayer2 abandoned the match.\nThe session will close soon.");
  250.     if(!strcmp(valasztas1, "ujra") && !strcmp(valasztas2, "ujra")){
  251.         printf("\nThe rematch will start soon!");
  252.         hub();
  253.     }
  254.     return 0;
  255. }
Add Comment
Please, Sign In to add comment