全红婵妹妹爱看的乌龟对对碰!!!

看到网络上很火的乌龟对对碰感觉他们好欧气,闲着没事写一个代码自己跑跑,比较匆忙,欢迎各位大佬指点优化!!!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>


//init the 3*3 frame 
//use the 1-9 to represent the color of the tortoise
int tortoise[9];
unsigned int tortoise_num_init;
unsigned int tortoise_num_leave;
unsigned int tort_full_flag = 0;
unsigned int tort_total_num = 0;
unsigned int lucky_color ;
char choice;

void sleep_ms(int milliseconds) {
    usleep(milliseconds * 1000);
}

void add_tortoise( int* tort)
{
    int i;
    srand(time(0)); 
    for ( i = 0; i < 9 && tortoise_num_leave > 0; i++)
    {
        /* code */
        if ( tortoise[i] == 0){
            // add the random number from 1 to 10
            int randomNumber = rand() % 10 + 1; // create a random number between 1 and 10
            printf("The random number is: %d\n", randomNumber);
            if(randomNumber == lucky_color){
                printf("The tortoise is lucky\n");
                tortoise_num_leave++;
            }
            sleep_ms(1000); 
            tortoise[i] = randomNumber;
            tortoise_num_leave--;
        }
    }
    printf("The tortoise color is: %d %d %d %d %d %d %d %d %d \n", tort[0], tort[1], tort[2], 
                                                                   tort[3], tort[4], tort[5], 
                                                                   tort[6], tort[7], tort[8]);
}

void remove_tortoise( int* tort){
    int head = 0;
    int tail = 0;

    // Three same color remove
    for(head = 0; head < 7;head = head +3){
        if(head == 0 && tort[head] != 0){
            if(tort[head] == tort[head + 1] && tort[head] == tort[head + 2]){
                tort[head] = 0;
                tort[head + 1] = 0;
                tort[head + 2] = 0;
                tortoise_num_leave = tortoise_num_leave + 2;
                tort_total_num = tort_total_num + 2;
            }
            if(tort[head] == tort[head + 3] && tort[head] == tort[head + 6] && tort[head] != 0){
                tort[head] = 0;
                tort[head + 3] = 0;
                tort[head + 6] = 0;
                tortoise_num_leave = tortoise_num_leave + 2;
                tort_total_num = tort_total_num + 2;
            }
            if(tort[head] == tort[head + 4] && tort[head] == tort[head + 8] && tort[head] != 0){
                tort[head] = 0;
                tort[head + 4] = 0;
                tort[head + 8] = 0;
                tortoise_num_leave = tortoise_num_leave + 2;
                tort_total_num = tort_total_num + 2;
            }
        }
        else if(head == 3 && tort[head] != 0){
            if(tort[head] == tort[head + 1] && tort[head] == tort[head + 2] && tort[head] != 0){
                tort[head] = 0;
                tort[head + 1] = 0;
                tort[head + 2] = 0;
                tortoise_num_leave = tortoise_num_leave + 2;
                tort_total_num = tort_total_num + 2;
            }
        }
        else if(head == 6 && tort[head] != 0){
            if(tort[head] == tort[head + 1] && tort[head] == tort[head + 2] && tort[head] != 0){
                tort[head] = 0;
                tort[head + 1] = 0;
                tort[head + 2] = 0;
                tortoise_num_leave = tortoise_num_leave + 2;
                tort_total_num = tort_total_num + 2;
            }
            if(tort[head] == tort[head - 2] && tort[head] == tort[head - 4] && tort[head] != 0){
                tort[head] = 0;
                tort[head + 4] = 0;
                tort[head + 8] = 0;
                tortoise_num_leave = tortoise_num_leave + 2;
                tort_total_num = tort_total_num + 2;
            }
        }
    }

    //TWO the same color remove
    for(head = 0; head < 9; head++){
        if(tort[head] != 0){
            for(tail = head + 1; tail < 9; tail++){
                if(tort[head] == tort[tail]){
                    tort[head] = 0;
                    tort[tail] = 0;
                    tortoise_num_leave = tortoise_num_leave + 1;
                    tort_total_num = tort_total_num + 1;
                    break;
                }
            }
        }
    }

    tort_full_flag = 0;
    //All the color remove
    for(head = 0; head < 9; head++){
        tort_full_flag = tort_full_flag + tort[head];
    }

    if(tort_full_flag == 0){
        tortoise_num_leave = tortoise_num_leave + 8;
    }
}

void play_game(){
    printf("Please input the initial number of tortoise: ");
    scanf("%d", &tortoise_num_init);
    printf("Please input the lucky color: ");
    scanf("%d", &lucky_color);
    tortoise_num_leave = tortoise_num_init;
    tort_total_num = tortoise_num_leave;
    while(tortoise_num_leave){
        add_tortoise(tortoise);
        remove_tortoise(tortoise);
    }
    printf("The total number of tortoise is %d\n", tort_total_num);
}

void main()
{
    do{
        play_game();
        printf("Do you want to play again? (Y/N): ");
        scanf(" %c", &choice);
    }while(choice == 'Y' || choice == 'y');
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值