Beispiel #1
0
int main()
{
	srand(time(NULL));
    char ch;
	int t,flag=0;
    generate(0);
    print();
	while(1)
    {
        flag=0;
        if(gameover())
        break;
        ch=getch();
        t=isfull();
        switch(ch)
        {
            case 'w' : flag=shiftu();break;
            case 'a' : flag=shiftl();break;
            case 's' : flag=shiftd();break;
            case 'd' : flag=shiftr();
        }
        system("cls");
        if(!t && flag)
        generate(1);
        print();
    }
    printf("Game over  !! :)\n");
	return 0;
}
Beispiel #2
0
void convert(double x, char mantissa_exponent[]) {
    int i;
    char mantissa[23] = {(char)0};
    char exponent[8] = {(char)0};

    // Calculate sign bit first
    if (x < 0) {
        mantissa_exponent[0] = 1;
        x *= -1;
    } else
        mantissa_exponent[0] = 0;
    
    // Seperate the decimal and fractional parts
    int decPart = (int) x;
    double fracPart = x - decPart;

    // Conver the decimal part to binary, put it at the end of the array
    for (i = 0; decPart >= 1; i++) {
        mantissa[22-i] = (char) (decPart % 2);
        decPart = decPart / 2;
    }
    i--;
    int exp = i;

    // Shift mantissa to left 22-exp
    shiftl(mantissa, 22-exp);

    // Put exponent in mantissa_exponent array
    dec2bin(exp, exponent);

    // Deal with fractional part
    if (fracPart != 0.0) {
        i = exp+1; // Current position in mantissa
        for (; i < 32; i++) {
            fracPart = fracPart * 2;
            mantissa[i] = (char) ((int)fracPart);
            fracPart -= (int)fracPart;
        }
    }

    // Gather result
    gather(mantissa, exponent, mantissa_exponent);
}
Beispiel #3
0
void runTests(void) {
    /* Test bin2dec {{{ */
    int x;
    char z[8] = {0,0,0,0,0,1,1,0};
    x = bin2dec(z);
    if (x != 6)
        printf("\nbin2dec Test Failure: %d != 8\n", x);
    else
        printf("|");
    char y[8] = {1,1,1,1,1,1,1,1};
    x = bin2dec(y);
    if (x != 255)
        printf("bin2dec Test Failure: %d != 255\n", x);
    else
        printf(".");
    char v[8] = {1,0,1,0,1,0,1,0};
    x = bin2dec(v);
    if (x != 170)
        printf("bin2dec Test Failure: %d != 255\n", x);
    else
        printf(".");
    // }}}

    /* Test dec2bin {{{ */
    int i;
    int fail = 0;
    char testResult[8] = {0};
    dec2bin(255, testResult);
    for (i = 0; i < 8; i++)
        if ((int)testResult[i] == 0)
            fail = 1;
    if (fail) {
        printf("dec2bin Test Failure: ");
        for (i = 0; i < 8; i++)
            printf("%d", (int)testResult[i]);
        printf(" != 11111111\n");
    }
    else
        printf("|");
    fail = 0;
    dec2bin(0, testResult);
    for (i = 0; i < 8; i++)
        if ((int)testResult[i] == 1)
            fail = 1;
    if (fail) {
        printf("dec2bin Test Failure: ");
        for (i = 0; i < 8; i++)
            printf("%d", (int)testResult[i]);
        printf(" != 00000000\n");
    }
    else
        printf(".");
    fail = 0;
    dec2bin(85, testResult);
    for (i = 0; i < 8; i++)
        if ((int)testResult[i] != i%2)
            fail = 1;
    if (fail) {
        printf("dec2bin Test Failure: ");
        for (i = 0; i < 8; i++)
            printf("%d", (int)testResult[i]);
        printf(" != 01010101\n");
    }
    else
        printf(".");
    fail = 0;
    dec2bin(170, testResult);
    for (i = 0; i < 8; i++)
        if ((int)testResult[i] == i%2)
            fail = 1;
    if (fail) {
        printf("dec2bin Test Failure: ");
        for (i = 0; i < 8; i++)
            printf("%d", (int)testResult[i]);
        printf(" != 10101010\n");
    }
    else
        printf(".");
    // }}}

    /* Test gather {{{ */
    char a[32] = {0};
    char exponent[8] = {1,0,1,0,1,0,1,0};
    char mantissa[23] = {1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0};
    char gathered[32] = {0,1,0,1,0,1,0,1,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0};
    gather(mantissa, exponent, a);
    fail = 0;
    for (i = 0; i < 32; i++)
        if (a[i] != gathered[i])
            fail = 1;
    if (fail) {
        printf("gather Test Failure: ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) a[i]);
        printf(" != ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) gathered[i]);
        printf("\n");
    } else
        printf("|");
    // }}}

    /* Test split {{{ */
    char expRes[8] = {0};
    char manRes[23] = {0};
    split(gathered, manRes, expRes);
    fail = 0;
    for (i = 0; i < 8; i++)
        if (expRes[i] != exponent[i])
            fail = 1;
    for (i = 0; i < 23; i++)
        if (manRes[i] != mantissa[i])
            fail = 1;
    if (fail)
        printf("split Test Failure\n");
    else
        printf("|");
    // }}}
    
    /* Test shiftl {{{ */
    char unshifted[23] = {1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0};
    char shifted[23] = {0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0};
    shiftl(unshifted, 4);
    fail = 0;
    for (i = 0; i < 23; i++)
        if (unshifted[i] != shifted[i])
            fail = 1;
    if (fail) {
        printf("shiftl Test Failure: ");
        for (i = 0; i < 23; i++)
            printf("%d", (char) unshifted[i]);
        printf(" != ");
        for (i = 0; i < 23; i++)
            printf("%d", (char) shifted[i]);
        printf("\n");
    } else
        printf("|");
    // }}}

    /* Test shift {{{ */
    char unshifted1[23] = {1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0};
    char shifted1[23] = {0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1};
    shift(unshifted1, 4);
    fail = 0;
    for (i = 0; i < 23; i++)
        if (unshifted1[i] != shifted1[i])
            fail = 1;
    if (fail) {
        printf("shift Test Failure: ");
        for (i = 0; i < 23; i++)
            printf("%d", (char) unshifted1[i]);
        printf(" != ");
        for (i = 0; i < 23; i++)
            printf("%d", (char) shifted1[i]);
        printf("\n");
    } else
        printf("|");
    // }}}

    /* Test convert {{{*/
    char pi[32] = {0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0,1,0,0,0,0,1,1,1,1,1,1,0,1,1,0,1};
    char oneHundred[32] = {0,0,0,0,0,0,1,1,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    char one[32] = {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    char negOne[32] = {1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    char negTwo[32] = {1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    fail = 0;
    convert(3.141592654, a);
    for (i = 0; i < 32; i++)
        if (a[i] != pi[i])
            fail = 1;
    if (fail) {
        printf("convert Test Failure: ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) a[i]);
        printf(" != ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) pi[i]);
        printf("\n");
    } else
        printf("|");
    fail = 0;
    convert(100.0, a);
    for (i = 0; i < 32; i++)
        if (a[i] != oneHundred[i])
            fail = 1;
    if (fail) {
        printf("convert Test Failure: ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) a[i]);
        printf(" != ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) oneHundred[i]);
        printf("\n");
    } else
        printf(".");
    fail = 0;
    convert(1.0, a);
    for (i = 0; i < 32; i++)
        if (a[i] != one[i])
            fail = 1;
    if (fail) {
        printf("convert Test Failure: ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) a[i]);
        printf(" != ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) one[i]);
        printf("\n");
    } else
        printf(".");
    fail = 0;
    convert(-1.0, a);
    for (i = 0; i < 32; i++)
        if (a[i] != negOne[i])
            fail = 1;
    if (fail) {
        printf("convert Test Failure: ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) a[i]);
        printf(" != ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) negOne[i]);
        printf("\n");
    } else
        printf(".");
    fail = 0;
    convert(-2.0, a);
    for (i = 0; i < 32; i++)
        if (a[i] != negTwo[i])
            fail = 1;
    if (fail) {
        printf("convert Test Failure: ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) a[i]);
        printf(" != ");
        for (i = 0; i < 32; i++)
            printf("%d", (char) negTwo[i]);
        printf("\n");
    } else
        printf(".");
    // }}}

    /* Test convert_back {{{ */
    fail = 0;
    double oot = convert_back(oneHundred);
    if (oot != 100.0)
        printf("convert_back Test Failure: %g != 100.0\n", oot);
    else
        printf("|");
    oot = convert_back(one);
    if (oot != 1.0)
        printf("convert_back Test Failure: %g != 1.0\n", oot);
    else
        printf(".");
    oot = convert_back(negOne);
    if (oot != -1.0)
        printf("convert_back Test Failure: %g != -1.0\n", oot);
    else
        printf(".");
    oot = convert_back(negTwo);
    if (oot != -2.0)
        printf("convert_back Test Failure: %g != -1.0\n", oot);
    else
        printf(".");
    oot = convert_back(pi);
    if (oot <= 3.141592654*0.9 || oot >= 3.141592654*1.1)
        printf("convert_back Test Failure: %1.100g != 3.141592654\n", oot);
    else
        printf(".");
    // }}}
    
    /* Test align {{{ */
    int exp1 = 3;
    int exp2 = 1;
    char man1[23] = {1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0};
    char man2[23] = {0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1};
    char res1[23] = {1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0,0, 1,1,1,1, 0,0,0};
    char res2[23] = {0,0,0,0, 0,0,1,1, 1,1,0,0, 0,0,1,1, 1,1,0,0, 0,0,1};
    fail = 0;
    align(exp1, man1, exp2, man2);
    for (i = 0; i < 23; i++)
        if (man1[i] != res1[i] || man2[i] != res2[i])
            fail = 1;
    if (fail) {
        printf("align Test Failure: ");
        for (i = 0; i < 23; i++)
            printf("%d", (char) man2[i]);
        printf(" != ");
        for (i = 0; i < 23; i++)
            printf("%d", (char) res2[i]);
        printf("\n");
    } else
        printf("|");
    // }}}

    /* Clean up */
    printf("\n");
}