论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

井字棋游戏.不够完善

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:46:48

#include"stdio.h"
#include"conio.h"
#include"time.h"
#include"stdlib.h"

int a[3][3]={{0},{0},{0}};              /*存放所下棋,0表示未走过的,1表示游戏者走过的,2表示电脑走的*/

/*画出棋盘*/
void picture()
{
   int i;

/*画出左边的棋盘*/
   for(i=5;i<=20;i++)
   {
      gotoxy(i,4);
      cprintf("%c",196);
      gotoxy(i,10);
      cprintf("%c",196);
   }
   for(i=2;i<=14;i++)
   {
      gotoxy(9,i);
      cprintf("%c",179);
      gotoxy(16,i);
      cprintf("%c",179);
   }

/*画出右边的棋盘*/
   for(i=45;i<=60;i++)
   {
      gotoxy(i,4);
      cprintf("%c",196);
      gotoxy(i,10);
      cprintf("%c",196);
   }
   for(i=2;i<=14;i++)
   {
      gotoxy(49,i);
      cprintf("%c",179);
      gotoxy(56,i);
      cprintf("%c",179);
   }

/*写下右边棋盘中的数字*/
   gotoxy(47,3);
   printf("1");
   gotoxy(53,3);
   printf("2");
   gotoxy(58,3);
   printf("3");

   gotoxy(47,7);
   printf("4");
   gotoxy(53,7);
   printf("5");
   gotoxy(58,7);
   printf("6");

   gotoxy(47,12);
   printf("7");
   gotoxy(53,12);
   printf("8");
   gotoxy(58,12);
   printf("9");

   gotoxy(65,5);
   printf("YOU:%c",2);
   gotoxy(65,10);
   printf("COMPUTER:%c",42);
}

/*检查是否还有没有未走之处*/
void exam()
{
   int i,j;
   for(i=0;i<3;i++)
   for(j=0;j<3;j++)
   {
       if(a[i][j]==0)
          return;
   }
   exit(0);
}

/*游戏者走一步*/
void user()
{
   int n,i,j;

   usergo:

   gotoxy(5,15);
   printf("enter the num(1-9) you choice:\n");
   scanf("%d",&n);
   sleep(1);
   gotoxy(5,16);
   switch(n)
   {
      case 1:if(a[0][0]!=1&&a[0][0]!=2)              /*假如所选的那一步未走过,则,赋值1*/
             {
                 a[0][0]=1;
                 gotoxy(7,2);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
      case 2:if(a[0][1]!=1&&a[0][1]!=2)
             {
                 a[0][1]=1;
                 gotoxy(13,2);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
      case 3:if(a[0][2]!=1&&a[0][2]!=2)
             {
                 a[0][2]=1;
                 gotoxy(18,2);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
      case 4:if(a[1][0]!=1&&a[1][0]!=2)
             {
                 a[1][0]=1;
                 gotoxy(7,7);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
      case 5:if(a[1][1]!=1&&a[1][1]!=2)
             {
                 a[1][1]=1;
                 gotoxy(13,7);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;

             }
      case 6:if(a[1][2]!=1&&a[1][2]!=2)
             {
                 a[1][2]=1;
                 gotoxy(18,7);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
      case 7:if(a[2][0]!=1&&a[2][0]!=2)
             {
                 a[2][0]=1;
                 gotoxy(7,12);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
      case 8:if(a[2][1]!=1&&a[2][1]!=2)
             {
                 a[2][1]=1;
                 gotoxy(13,12);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
      case 9:if(a[2][2]!=1&&a[2][2]!=2)
             {
                 a[2][2]=1;
                 gotoxy(18,12);
                 printf("%c",2);
                 break;
             }
             else
             {
                 goto usergo;
             }
     default:goto usergo;
   }
   sleep(1);

}


/*电脑随机走一步*/
void computer()
{
   int n;

   computergo:

   randomize();
   n=rand()%9;
   switch(n)
   {
      case 0:if(a[0][0]!=1&&a[0][0]!=2)                  /*假如所选的那一步未走过,则,赋值2*/
             {
                 a[0][0]=2;
                 gotoxy(7,2);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 1:if(a[0][1]!=1&&a[0][1]!=2)
             {
                 a[0][1]=2;
                 gotoxy(13,2);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 2:if(a[0][2]!=1&&a[0][2]!=2)
             {
                 a[0][2]=2;
                 gotoxy(18,2);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 3:if(a[1][0]!=1&&a[1][0]!=2)
             {
                 a[1][0]=2;
                 gotoxy(7,7);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 4:if(a[1][1]!=1&&a[1][1]!=2)
             {
                 a[1][1]=2;
                 gotoxy(13,7);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 5:if(a[1][2]!=1&&a[1][2]!=2)
             {
                 a[1][2]=2;
                 gotoxy(18,7);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 6:if(a[2][0]!=1&&a[2][0]!=2)
             {
                 a[2][0]=2;
                 gotoxy(7,12);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 7:if(a[2][1]!=1&&a[2][1]!=2)
             {
                 a[2][1]=2;
                 gotoxy(13,12);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
      case 8:if(a[2][2]!=1&&a[2][2]!=2)
             {
                 a[2][2]=2;
                 gotoxy(18,12);
                 printf("%c",42);
                 break;
             }
             else
             {
                 goto computergo;
             }
     default:goto computergo;
   }
   sleep(1);
}


/*测试游戏状态,0,1,2*/
int exam_user()
{
   int i,key=0;
   for(i=0;i<3;i++)
   {
       if((a[i][0]==a[i][1]&&a[i][0]==a[i][2]&&a[i][1]==a[i][2]&&a[i][0]==1)||(a[0][i]==a[1][i]&&a[0][i]==a[2][i]&&a[1][i]==a[2][i]&&a[0][i]==1)||(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[1][1]==a[2][2]&&a[2][2]==1)||(a[2][0]==a[1][1]&&a[2][0]==a[0][2]&&a[1][1]==a[0][2]&&a[0][2]==1))
       key=1;
       break;
   }
   return key;
}

int exam_computer()
{
   int i,key=0;
   for(i=0;i<3;i++)
   {
        if((a[i][0]==a[i][1]&&a[i][0]==a[i][2]&&a[i][1]==a[i][2]&&a[i][0]==2)||(a[0][i]==a[1][i]&&a[0][i]==a[2][i]&&a[1][i]==a[2][i]&&a[0][i]==2)||(a[0][0]==a[1][1]&&a[0][0]==a[2][2]&&a[1][1]==a[2][2]&&a[2][2]==2)||(a[2][0]==a[1][1]&&a[2][0]==a[0][2]&&a[1][1]==a[0][2]&&a[0][2]==2))
        key=1;
        break;
   }
   return key;
}


/*游戏者和电脑各走完一步,做一下检查*/
void end()
{
   int key1,key2;
   key1=exam_user();
   key2=exam_computer();
   if((key1==1)&&(key2==0))
   {
         gotoxy(35,20);
         printf("YOU WIN");
         sleep(2);
         exit(0);
   }
   else if((key1==0)&&(key2==1))
   {
         gotoxy(35,20);
         printf("YOU LOST");
         sleep(2);
         exit(0);
   }
   else if((key1==key2)&&(key1==0))
   {
         exam();
         return;
   }
   else if((key1==key2)&&(key1==1))
   {
         gotoxy(35,20);
         printf("PING JU");
         sleep(2);
         exit(0);
   }
   else exit(0);
}


main()
{
   picture();
   while(1)
   {
      user();
      exam();
      computer();
      end();
   }
}

上一篇:{应用}乒乓球游戏 人气:6432
下一篇:{应用}贪吃蛇,还在完善中 人气:4572
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058