当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 2 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 写了个井字游戏,大家可以给些意见。
帖子发表于 : 2016-11-23 21:47 

注册: 2015-06-19 21:16
帖子: 41
送出感谢: 26
接收感谢: 0 次
代码:
#include<stdio.h>

int main()
{
   char chess[3][3]={'1','2','3','4','5','6','7','8','9',};
   int player1=0,player2=0,winner=0,column,line,turn=0;
   int check(char chess[3][3]);
   
   printf("Game start!\nplayer1 *\nplayer2 O\n\n");
   
   printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
   while(1)
   {
      
      
      printf("player1's turn:");
      scanf("%d",&player1);
      if(!(player1>=1)&&(player1<=9))
      {
         printf("input error\n");
         return 0;
      }
      turn++;
      column=(int)(player1-1)/3;
      line=(int)(player1-1)%3;
      chess[column][line]='*';
      printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
      if(check(chess)==1)
      {
         printf("player1 is the winner\n");
         return 1;
      }
      if(turn==9)
      {
         printf("onbody win\n");
         return 1;
      }
         printf("player2's turn:");
      scanf("%d",&player2);
      if(!(player2>=1)&&(player2<=9))
      {
         printf("input error\n");
         return 0;
      }
      column=(int)(player2-1)/3;
      line=(int)(player2-1)%3;
      chess[column][line]='O';
      turn++;
      printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]);
      printf("---+---+---\n");
      printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]);
      if(check(chess)==1)
      {
         printf("player2 is the winner\n");
         return 1;
      }
      
      
      
   }
}

int check(char chess[3][3])
{
   for(int i=0;i<=2;i++)
      if(chess[i][0]==chess[i][1]&&chess[i][1]==chess[i][2])
         return 1;
      else if(chess[0][i]==chess[1][i]&&chess[1][i]==chess[2][i])
         return 1;
    if(chess[0][0]==chess[1][1]&&chess[1][1]==chess[2][2])
         return 1;
      else if(chess[0][2]==chess[1][1]&&chess[1][1]==chess[2][0])
         return 1;
      else
         return 0;   
}










页首
 用户资料  
 
2 楼 
 文章标题 : Re: 写了个井字游戏,大家可以给些意见。
帖子发表于 : 2016-11-24 0:23 
头像

注册: 2008-09-18 13:11
帖子: 2781
送出感谢: 1
接收感谢: 453
1、上次我跟你说的同一位置重复落子的问题你还是没改
2、处理玩家输入错误应该是让其重新输入,而不是直接退出程序
3、既然你会用函数,那么就不要把同一段代码到处复制粘贴
4、
引用:
printf("onbody win\n");
onbody是谁?


如果这是个大学计算机课程作业的话,你这个代码100分里能得50分吧


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 2 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译