本文主要是介绍C井字棋,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <stdio.h>
#include <stdbool.h>int main()
{char qipan[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};int step = 0;int player = 0;int pos = 0;bool win = false;int i , j;//绘制棋盘 printf("|-------------|\n");printf("| %c || %c || %c |\n", qipan[0][0],qipan[0][1],qipan[0][2]); printf("|-------------|\n");printf("| %c || %c || %c |\n", qipan[1][0],qipan[1][1],qipan[1][2]);printf("|-------------|\n"); printf("| %c || %c || %c |\n", qipan[2][0],qipan[2][1],qipan[2][2]);printf("|-------------|");while(step<9){printf("\n请选手%d落子:",step%2+1);scanf("%d",&pos);pos = pos - 1;//判断落子是否合理if(qipan[pos/3][pos%3]==' '){qipan[pos/3][pos%3] = step%2 ? 'O' : 'X';printf("|-------------|\n");printf("| %c || %c || %c |\n", qipan[0][0],qipan[0][1],qipan[0][2]); printf("|-------------|\n");printf("| %c || %c || %c |\n", qipan[1][0],qipan[1][1],qipan[1][2]);printf("|-------------|\n"); printf("| %c || %c || %c |\n", qipan[2][0],qipan[2][1],qipan[2][2]);printf("|-------------|"); }else{printf("落子位置错误!");continue; }//判断是否胜利for(i=0;i<3;i++){if(qipan[i][0]==qipan[i][1]&&qipan[i][1]==qipan[i][2]&&qipan[i][0]!= ' ')win = true;if(qipan[0][i]==qipan[1][i]&&qipan[1][i]==qipan[2][i]&&qipan[0][i]!= ' ')win = true;}if(qipan[0][0]==qipan[1][1]&&qipan[1][1]==qipan[2][2]&&qipan[0][0]!= ' ')win = true;if(qipan[0][2]==qipan[1][1]&&qipan[1][1]==qipan[2][0]&&qipan[0][2]!= ' ')win = true;if (win){printf("\n选手%d胜利\n",step%2+1);system("pause");return 0;}elsestep++;}printf("\n平局\n");system("pause");return 0;
}
这篇关于C井字棋的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!