1. Си / Говнокод #17581

    +143

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    unsigned int board[4][4];
    void firstblood(); void drawboard(); int turn(); void changeup(); void changedown(); void changeleft(); void changeright(); int numbofpos(); void gameover(); 
    int pow2 ( int n ) { return 1<<n; }
    
    int main ()
    {	do{   firstblood();   drawboard();    while ( turn() );	puts("New game? (y,n)");	fflush(stdin);	char c=getchar();	if (c!='y') return 0;	for (int i=0;i<4;i++) for (int j=0;j<4;j++) board[i][j]=0;	} while (1);
    }
    
    void firstblood()
    { srand((unsigned int)time); board[rand()%4][rand()%4]=pow2(rand()%3+1);    board[rand()%4][rand()%4]=pow2(rand()%4+1); }
    
    void drawboard ()
    {   system("CLS");    puts("        2048        ");  int i,j;  for (i=0;i<4;i++, printf("\n\n") )        for (j=0;j<4;j++)            printf("%5u",board[i][j]);    puts("\nw,a,s,d and r to move and new game");
    }
    
    int numbofpos()
    {   int i,j,n=0;  for (i=1;i<4;i++      for (j=0;j<4;j++){if (board[i][j] && board[i][j]==board[i-1][j]) n++;	if (board[j][i] && board[j][i]==board[j][i-1]) n++;	}for (i=0;i<4;i++) for (j=0;j<4;j++) if (!board[i][j]) n++;
        return n ;
    }
    
    void gameover()
    {
        system("CLS");    puts("        2048        ");   int i,j;   for (i=0;i<4;i++, printf("\n\n") )     for (j=0;j<4;j++)     printf("%5u",board[i][j]);    puts("\n     Game over      ");
    }
    
    int turn()
    {
        char c=getchar(); int i,j,n=0, nulls[16][2];
        switch(c)
        {   case 'w' : changeup(); break;
            case 'a' : changeleft(); break;
            case 's' : changedown(); break;
            case 'd' : changeright(); break;
    		case 'r' : return 0;
    	}
        for (i=0;i<4;i++)
            for (j=0;j<4;j++)
                if ( !board[i][j] ) { nulls[n][0]=i; nulls[n++][1]=j; }
    	if (n)
    	{
    		int t=rand()%n;
    		board[ nulls[t][0] ][ nulls[t][1] ] = pow2(rand()%2+1);
    	}
        if ( !numbofpos() )
            { gameover(); return 0; }
        drawboard();
    	return 1;
    }
    void changeleft()
    {
        int i,j,k;
        for (i=0;i<4;i++)
        {
            int f= ( !board[i][3] )?0:1 ;
            for (j=2;j>=0;j--)
              { if (board[i][j] && !f) f=1;
    			if (!board[i][j] && f==1) { for (k=j+1;k<4;board[i][k-1]=board[i][k],k++); board[i][3]=0; }
    		  }
    		for (j=0;j<3;j++)
    			if ( board[i][j] && board[i][j]==board[i][j+1])  
    			{
    				board[i][j] *= 2;
    				for (k=j+1;k<3; board[i][k]=board[i][k+1], k++ );
    				board[i][3]=0;
    			}
    	}
    }
    void changeright()
    {
    	int i,j,k;
        for (i=0;i<4;i++)
        {
            int f= ( !board[i][0] )?0:1 ;
            for (j=1;j<4;j++)
              { if (board[i][j] && !f) f=1;
    			if (!board[i][j] && f==1) { for (k=j;k>0;board[i][k]=board[i][k-1],k--); board[i][0]=0; }
    		  }
    		for (j=3;j>0;j--)
    			if ( board[i][j] && board[i][j]==board[i][j-1])  
    			{
    				board[i][j] *= 2;
    				for (k=j-1;k>0; board[i][k]=board[i][k-1], k-- );
    				board[i][0]=0;
    			}
    	}
    }
    
    void changeup()
    {	int i,j,k;    for (i=0;i<4;i++)    {        int f= ( !board[3][i] )?0:1 ;        for (j=2;j>=0;j--)          { if (board[j][i] && !f) f=1;			if (!board[j][i] && f==1) { for (k=j+1;k<4;board[k-1][i]=board[k][i],k++); board[3][i]=0; }		  }
    		for (j=0;j<3;j++)			if ( board[j][i] && board[j][i]==board[j+1][i])  {board[j][i] *= 2;			for (k=j+1;k<3; board[k][i]=board[k+1][i], k++ );	board[3][i]=0;	}}}
    
    void changedown()
    {int i,j,k;    for (i=0;i<4;i++) {       int f= ( !board[0][i] )?0:1 ;       for (j=1;j<4;j++)          { if (board[j][i] && !f) f=1;			if (!board[j][i] && f==1) { for (k=j;k>0;board[k][i]=board[k-1][i],k--); board[0][i]=0; }		  }		for (j=3;j>0;j--)			if ( board[j][i] && board[j][i]==board[j-1][i])  				board[j][i] *= 2;
    				for (k=j-1;k>0; board[k][i]=board[k-1][i], k-- );			board[0][i]=0;
    }	}}

    2048 только с библиотеками stdio.h, stdlib.h (srand,rand) и time.h (тоже для рандома)

    Пожалуйста, уберите ограничение в 100 строк

    Запостил: post_skript, 05 Февраля 2015

    Комментарии (9) RSS

    Добавить комментарий