这个项目适合大一上新生在完成新课以后尝试,大概时间是在12月份以后。
其中没有任何涉及的数据结构的用法,更多的是设计结构的考察。
本文的代码是使用的MVC设计模式(当然是极其简化的,作者水平不高也是大一新生)。
而对于这个项目本身,边界处理和特殊情况的考虑是比较重要的,具体细节在下面的代码里面有所体现。
肯定会有更好的胜利条件,即本文judge函数,这就需要读者自己思考了。(比如回朔法?)
这串代码在我的电脑上能够流畅运行。
#include#define lone 1 #define two 0 void jingplacedraw(int a[]) { int hang,lie,flag = lone,i = 1; for(hang = 0;hang < 7;hang++) { for(lie = 0;lie < 5;lie++) { if( flag == lone) { printf("__"); } else if(lie % 2 == 0) { if(a[i] >= 0) printf(" %d|",a[i]); else printf(" |"); i++; } } printf("n"); (flag == lone) ? (flag = two):(flag = lone); } } int judge(int a[]) { int tag = -1; int place; for(place = 1;place < 10;place+=3) { if(a[place] == a[place + 1] && a[place] == a[place + 2]) { if( a[place] == 0) tag = 0; else tag = 1; } } for(place = 1;place < 4;place+=1) { if(a[place] == a[place + 3] && a[place] == a[place + 6]) { if( a[place] == 0) tag = 0; else tag = 1; } } place = 1; if(a[place] == a[place + 4] && a[place] == a[place + 8] ) { if( a[place] == 0) tag = 0; else tag = 1; } place = 1; if(a[place + 2] == a[place + 4] && a[place + 2] == a[place + 6]) { if( a[place + 2] == 0) tag = 0; else tag = 1; } return tag; } void competation(int a[]) { int time = -1; int tern = 0; int timenow = 0; int qi; while(time == -1 && timenow < 9) { jingplacedraw(a); printf("按数字1~9则可在对应位点下棋(你不会不懂对应位子是什么意思吧?)n"); printf("现在轮到%d选手下棋n",tern); scanf("%d",&qi); if(qi > 0 && qi < 10) { if(a[qi] < 0) { a[qi] = tern; (tern == 0)?(tern = 1):(tern = 0); time = judge(a); timenow++; } else printf("你下的位置已经有东西了,重新下一次n"); } else printf("你输入的数字不在棋盘里面,重新下一次n"); } jingplacedraw(a); time = judge(a); if(time >= 0) printf("获胜的是%d号选手n",time); else printf("好遗憾哟,这局是平局n"); } int main() { int a[10] = {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10}; int name; printf("来一把井字棋?n先手是0号选手,后手是1号选手n"); competation(a); }



