| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 603 人关注过本帖
标题:写了个解迷小程序,c prime plus上的练习题太乏味了。
取消只看楼主 加入收藏
lleon
Rank: 2
等 级:论坛游民
帖 子:25
专家分:43
注 册:2013-10-28
结帖率:66.67%
收藏
 问题点数:0 回复次数:1 
写了个解迷小程序,c prime plus上的练习题太乏味了。
#include <stdio.h>
int a[7] = {1, 2, 3, 0, 4, 5, 6};
void print_it(void);

int main(void)
{
    char str[1000];
    int ch;
    int i;
    printf(
        "Turn  >> >> >>    << << <<\n"
        "  to  << << <<    >> >> >>    1-6: move  r: restart  q: quit\n\n");
    print_it();
    while (gets(str) != NULL)
    {
        ch = *str;
        if (ch == 'q')
            break;
        else if (ch == 'r')
        {
            a[0] = 1;
            a[1] = 2;
            a[2] = 3;
            a[3] = 0;
            a[4] = 4;
            a[5] = 5;
            a[6] = 6;
            printf("\n");
        }
        else if (ch >= '1' && ch <= '6')
        {
            ch -= '0';
            for (i = 0; i < 7; i++)
                if (a[i] == ch)
                    break;
            if (a[i] < 4 && i < 6 && a[i+1] == 0)
                {a[i+1] = a[i]; a[i] = 0;}
            else if (a[i] < 4 && i < 5 && a[i+2] == 0)
                {a[i+2] = a[i]; a[i] = 0;}
            else if (a[i] > 3 && i > 0 && a[i-1] == 0)
                {a[i-1] = a[i]; a[i] = 0;}
            else if (a[i] > 3 && i > 1 && a[i-2] == 0)
                {a[i-2] = a[i]; a[i] = 0;}
            else
                printf("\a");
        }
        else
            printf("\a");
        print_it();
        if (a[0] + a[1] + a[2] == 15 && a[3] == 0)
        {
            printf("\b\b\bYOU WIN!\n");
            break;
        }
    }
    printf("\nBye.\n");
    return 0;
}

void print_it(void)
{
    int i;
    printf("                   ");
    for (i = 0; i < 7; i++)
        if (a[i] == 0)
            printf("     ");
        else if (a[i] < 4)
            printf("%d>>  ", a[i]);
        else
            printf("<<%d  ", a[i]);
    printf("   ");
}
搜索更多相关主题的帖子: include 练习题 
2013-11-19 16:53
lleon
Rank: 2
等 级:论坛游民
帖 子:25
专家分:43
注 册:2013-10-28
收藏
得分:0 
呵呵,sorry。
这个小程序我又改了下,界面看起来简洁多了:

程序代码:
#include <stdio.h>
int a[7] = {1, 2, 3, 0, 4, 5, 6};
void print_it(int flag);

int main(void)
{
    char str[1000];
    int ch;
    int i;
    printf(
        "Turn  > > >   < < <\n"
        "  to  < < <   > > >    1-7: move  r: restart  q: quit\n\n");
    print_it(1);
    while (gets(str) != NULL)
    {
        ch = *str;
        if (ch == 'q')
            break;
        else if (ch == 'r')
        {
            a[0] = 1;
            a[1] = 2;
            a[2] = 3;
            a[3] = 0;
            a[4] = 4;
            a[5] = 5;
            a[6] = 6;
            printf("\n");
        }
        else if (ch >= '1' && ch <= '7')
        {
            i = ch - '0' -1;
            if (a[i] < 4 && i < 6 && a[i+1] == 0)
                {a[i+1] = a[i]; a[i] = 0;}
            else if (a[i] < 4 && i < 5 && a[i+2] == 0)
                {a[i+2] = a[i]; a[i] = 0;}
            else if (a[i] > 3 && i > 0 && a[i-1] == 0)
                {a[i-1] = a[i]; a[i] = 0;}
            else if (a[i] > 3 && i > 1 && a[i-2] == 0)
                {a[i-2] = a[i]; a[i] = 0;}
            else
                printf("\a");
        }
        else
            printf("\a");
        ch == 'r' ? print_it(1) : print_it(0);
        if (a[0] + a[1] + a[2] == 15 && a[3] == 0)
        {
            printf("\b\b\bYOU WIN!\n");
            break;
        }
    }
    printf("\nBye.\n");
    return 0;
}

void print_it(int flag)
{
    int i;
    if (flag == 1)
            printf("                   1 2 3 4 5 6 7\n");
    printf("                   ");
    for (i = 0; i < 7; i++)
        if (a[i] == 0)
            printf("  ");
        else if (a[i] < 4)
            printf("> ");
        else
            printf("< ");
    printf("   ");
}
2013-11-20 15:21
快速回复:写了个解迷小程序,c prime plus上的练习题太乏味了。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055750 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved