标题:文字版--国际象棋代码(已写完,运行ok,求优化!)
只看楼主
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
结帖率:71.43%
 问题点数:0 回复次数:8 
文字版--国际象棋代码(已写完,运行ok,求优化!)
]要求如下:
Write a program for playing chess. Program stores information about each chess piece in an array of
pieces. There are total of 32 pieces in initial setup.

Description                    Type and/or size
name of pice                   string
colour                         string
coordinates                    integer (1 - 8)
coordinates                    char or integer (a - h)
The program must have (at least) following commands:
1. start a new game (initialize board to start setting)
2. save game state to disk
3. read game state from disk
4. move chess piece (ask which piece to move and where to move)
5. print game state (list pieces that are still on the board: colour, name, coordinates)
本人第一个三百行以上的代码(烂归烂哈)
时间关系这学期结束了,没时间了,所以代码里面不包括走起的规则棋子可以满天飞=。=
但是可以做到基本的1.吃
                  2.走到空白格子
                  3.判断是不是己方棋子,等。
老师说如果能用到动态数组可以加分,所以上来问问
哪里可以用怎么用,或者代码里什么地方需要改进的
虚心请教!谢谢
data.txt内容如下:
white   king   5   1   
white   queen  4   1   
white   rook   1   1   
white   rook   8   1   
white   knight 2   1   
white   knight 7   1   
white   bishop 3   1   
white   bishop 6   1   
white   pawn   1   2   
white   pawn   2   2   
white   pawn   3   2   
white   pawn   4   2   
white   pawn   5   2   
white   pawn   6   2   
white   pawn   7   2   
white   pawn   8   2   
black   king   5   8   
black   queen  4   8   
black   rook   1   8   
black   rook   8   8   
black   knight 2   8   
black   knight 7   8   
black   bishop 3   8   
black   bishop 6   8   
black   pawn   8   7   
black   pawn   7   7   
black   pawn   6   7   
black   pawn   5   7   
black   pawn   4   7   
black   pawn   3   7   
black   pawn   2   7   
black   pawn   1   7   



代码如下:(codeblocks可以运行)

程序代码:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <conio.h>
#define MY_FILENAME "chess.txt"
#define INITIAL_FILE "data.txt"
struct chess
{
    char name[10];
    char colour[10];
    int x;
    int y;
}pieces[32];
void output(struct chess pieces[32]);
void fileprint(struct chess pieces[32]);
void fileread(struct chess pieces[32]);
void initialread(struct chess pieces[32]);
void menu(void);
void data(struct chess pieces[32]);

int main()
{
    struct chess pieces[32];
    int i,j;
    int xaxis=0;
    int yaxis=0;
    int xchess=0;
    int ychess=0;
    char colourold[10];
    int check=0;
    int checkblank=0;
    int choice=0;
    data(pieces);
    while (1)
    {
        menu();
        do
        {
            printf("Please enter number from 1-6.\n");
            scanf("%d",&choice);
            fflush(stdin);
        }
        while (choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6);

        if (choice==1)
        {

            system("cls");
            initialread(pieces);
            printf("Initializing completed...\n");
        }
        else if (choice==2)
        {
            fileprint(pieces);
            system("cls");
            printf("Save successfully!\n");
        }
        else if (choice==3)
        {
            fileread(pieces);
            system("cls");
            printf("Load successfully!\n");
        }
        else if (choice==4)
        {
            system("cls");
            printf("Please enter the coordinate of the piece you want to move!\n\n");
            do
            {
                printf("what is the x?(input a number between 1-8)\n");
                scanf("%d",&xchess);
                fflush(stdin);
            }
            while (xchess>8||xchess<1);
            do
            {
                printf("what is the y?(input a number between 1-8)\n");
                scanf("%d",&ychess);
                fflush(stdin);
            }
            while (ychess>8||ychess<1);

            for (i=0;i<32;i++)/*check if the piece exists.*/
            {
                if ((pieces[i].x==xchess) && (pieces[i].y==ychess))
                {
                    printf("the piece you want is %6s  %6s \n",pieces[i].colour,pieces[i].name);
                    strcpy(colourold,pieces[i].colour);
                    check=1;/*piece exists*/
                }

            }

            if (check==1)
            {

                printf("To which coordinate you want it to move?\n\n");
                do
                {
                    printf("what is the x?(input a number between 1-8)\n");
                    scanf("%d",&xaxis);
                    fflush(stdin);
                }
                while (xaxis>8||xaxis<1);
                do
                {
                    printf("what is the y?(input a number between 1-8)\n");
                    scanf("%d",&yaxis);
                    fflush(stdin);
                }
                while (yaxis>8||yaxis<1);

                for (j=0;j<32;j++)
                {
                    if ((pieces[j].x==xchess) && (pieces[j].y==ychess))
                    {
                        for (i=0;i<32;i++)
                        {
                            if ((pieces[i].x==xaxis) && (pieces[i].y==yaxis))/*if move to piece coordinate*/
                            {
                                if (strcmp(pieces[i].colour,colourold)!=0)
                                {
                                    memset(pieces[i].colour,0,sizeof(pieces[i].colour));
                                    memset(pieces[i].name,0,sizeof(pieces[i].name));
                                    printf("%d %d\n",pieces[i].x,pieces[i].y);
                                    pieces[i].x=0;/*if removed,shows '0' !*/
                                    pieces[i].y=0   ;/*if removed,shows '0' !*/
                                    pieces[j].x=xaxis;
                                    pieces[j].y=yaxis;
                                    printf("Eat successfully!\n\n");
                                    break;
                                }
                                else
                                {
                                    printf("Cant eat yourself!\n\n");
                                    break;
                                }
                            }
                            else if ((pieces[i].x!=xaxis) || (pieces[i].y!=yaxis))
                            {
                                checkblank=2;
                            }
                        }
                    }


                }
                if (checkblank==2)
                {
                    for (i=0;i<32;i++)
                    {
                        if ((pieces[i].x==xchess) && (pieces[i].y==ychess))
                        {
                            pieces[i].x=xaxis;
                            pieces[i].y=yaxis;
                            printf("Move successfully to %d %d\n",pieces[i].x,pieces[i].y);
                        }
                    }
                }
            }
            else
            {
                printf("The piece doestn't exit!!\n\n");
            }
        }
        else if (choice==5)
        {
            output(pieces);

        }
        else if (choice==6)
        {
            exit(0);
        }

        check=0;
    }
    return 0;
}
void data(struct chess pieces[32])
{
    int i;

    for (i=0;i<16;i++)
    {
        strcpy(pieces[i].colour,"white");
    }
    for (i=16;i<32;i++)
    {
        strcpy(pieces[i].colour,"black");
    }
    strcpy(pieces[0].name,"king");
    strcpy(pieces[1].name,"queen");
    strcpy(pieces[2].name,"rook");
    strcpy(pieces[3].name,"rook");
    strcpy(pieces[4].name,"knight");
    strcpy(pieces[5].name,"knight");
    strcpy(pieces[6].name,"bishop");
    strcpy(pieces[7].name,"bishop");
    strcpy(pieces[8].name,"pawn");
    strcpy(pieces[9].name,"pawn");
    strcpy(pieces[10].name,"pawn");
    strcpy(pieces[11].name,"pawn");
    strcpy(pieces[12].name,"pawn");
    strcpy(pieces[13].name,"pawn");
    strcpy(pieces[14].name,"pawn");
    strcpy(pieces[15].name,"pawn");
    strcpy(pieces[16].name,"king");
    strcpy(pieces[17].name,"queen");
    strcpy(pieces[18].name,"rook");
    strcpy(pieces[19].name,"rook");
    strcpy(pieces[20].name,"knight");
    strcpy(pieces[21].name,"knight");
    strcpy(pieces[22].name,"bishop");
    strcpy(pieces[23].name,"bishop");
    strcpy(pieces[24].name,"pawn");
    strcpy(pieces[25].name,"pawn");
    strcpy(pieces[26].name,"pawn");
    strcpy(pieces[27].name,"pawn");
    strcpy(pieces[28].name,"pawn");
    strcpy(pieces[29].name,"pawn");
    strcpy(pieces[30].name,"pawn");
    strcpy(pieces[31].name,"pawn");


    pieces[0].x=5;
    pieces[1].x=4;
    pieces[2].x=1;
    pieces[3].x=8;
    pieces[4].x=2;
    pieces[5].x=7;
    pieces[6].x=3;
    pieces[7].x=6;
    pieces[8].x=1;
    pieces[9].x=2;
    pieces[10].x=3;
    pieces[11].x=4;
    pieces[12].x=5;
    pieces[13].x=6;
    pieces[14].x=7;
    pieces[15].x=8;
    pieces[16].x=5;
    pieces[17].x=4;
    pieces[18].x=1;
    pieces[19].x=8;
    pieces[20].x=2;
    pieces[21].x=7;
    pieces[22].x=3;
    pieces[23].x=6;
    pieces[24].x=8;
    pieces[25].x=7;
    pieces[26].x=6;
    pieces[27].x=5;
    pieces[28].x=4;
    pieces[29].x=3;
    pieces[30].x=2;
    pieces[31].x=1;

    for (i=0;i<8 ;i++ )
    {
        pieces[i].y=1;
    }
    for (i=8;i<16 ;i++ )
    {
        pieces[i].y=2;
    }
    for (i=16;i<24 ;i++ )
    {
        pieces[i].y=8;
    }
    for (i=24;i<32 ;i++ )
    {
        pieces[i].y=7;
    }
}
void initialread(struct chess pieces[32])
{
    FILE *my_file;
    my_file=fopen(INITIAL_FILE,"r");
    int i;
    for (i=0;i<32;i++)
    {
        fscanf(my_file,"%6s  %6s %2d  %2d   ",pieces[i].colour,pieces[i].name,&pieces[i].x,&pieces[i].y);
    }
    fclose(my_file);
}

void fileread(struct chess pieces[32])
{
    FILE *my_file;
    my_file=fopen(MY_FILENAME,"r");
    int i;
    for (i=0;i<32;i++)
    {
        fscanf(my_file,"%6s  %6s %2d  %2d   ",pieces[i].colour,pieces[i].name,&pieces[i].x,&pieces[i].y);
    }
    fclose(my_file);
}

void fileprint(struct chess pieces[32])
{
    FILE *my_file;
    my_file=fopen(MY_FILENAME,"w");
    int i;
    for (i=0;i<32;i++)
    {
        fprintf(my_file,"%-6s  %-6s %-2d  %-2d   \n",pieces[i].colour,pieces[i].name,pieces[i].x,pieces[i].y);
    }
    fclose(my_file);
}



void output(struct chess pieces[32])
{
    int i;
    printf("colour  name   x   y  \n\n");
    for (i=0;i<32 ;i++ )
    {
        printf("%-6s  %-6s %-2d  %-2d  ",pieces[i].colour,pieces[i].name,pieces[i].x,pieces[i].y);
        fflush(stdin);
        printf("\n");
    }
}

void menu(void)
{
    printf("GAME MENU\n1.Start a new game.\n");
    printf("2.Save game state to disk.\n");
    printf("3.Read game stae from disk.\n");
    printf("4.Move chess piece\n");
    printf("5.Print game state.\n");
    printf("6.Exit\n");
}


[ 本帖最后由 Timber 于 2010-5-27 02:46 编辑 ]
搜索更多相关主题的帖子: 运行 代码 文字 国际象棋 
2010-05-27 02:41
冥卫
Rank: 8Rank: 8
来 自:深山老林
等 级:蝙蝠侠
帖 子:280
专家分:772
注 册:2010-4-20
得分:0 
顶顶
2010-05-27 08:29
winyzy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-27
得分:0 
怎么联系到您,我有代码讨论一下
2010-05-27 14:29
winyzy
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-27
得分:0 
我的QQ41152974
2010-05-27 14:30
xiaoyi2010
该用户已被删除
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2010-05-27 14:38
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
得分:0 
回复 3楼 winyzy
334539921

But here I am
2010-05-27 20:18
zqlswt566664
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-5-27
得分:0 
为什么我试了一下  有错误啊
2010-05-27 22:17
Timber
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2010-1-31
得分:0 
回复 7楼 zqlswt566664
可能是你没有创建txt文件,文件内容帖子已经有了

[ 本帖最后由 Timber 于 2010-5-28 08:31 编辑 ]

But here I am
2010-05-27 23:22
阳光之旅
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-5-24
得分:0 
拿走了,非常谢谢
2010-05-29 21:45



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-308316-1-1.html




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

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