小游戏、大家看看、代码居多。
代码太多、求优化、求告知如何把这个游戏编译出来到其他电脑上使用。
以下代码:
#include <stdio.h>
#include <stdlib.h>
char sum[9] = {49,50,51,52,53,54,55,56,57};
void biao (char *psum)
{
printf(" 当前棋谱 : \n\n");
printf(" %c | %c | %c\n",psum[0],psum[1],psum[2]);
printf(" ---+---+---\n");
printf(" %c | %c | %c\n",psum[3],psum[4],psum[5]);
printf(" ---+---+---\n");
printf(" %c | %c | %c\n\n",psum[6],psum[7],psum[8]);
}
void error()
{
biao(sum);
printf("此处已经被占领,请重试!\n");
}
char panduan(char *psum)
{
if(psum[0]=='O'&&psum[1]=='O'&&psum[2]=='O'
|| psum[3]=='O'&&psum[4]=='O'&&psum[5]=='O'
|| psum[6]=='O'&&psum[7]=='O'&&psum[8]=='O'
|| psum[0]=='O'&&psum[4]=='O'&&psum[8]=='O'
|| psum[2]=='O'&&psum[4]=='O'&&psum[6]=='O'
|| psum[0]=='O'&&psum[3]=='O'&&psum[6]=='O'
|| psum[1]=='O'&&psum[4]=='O'&&psum[7]=='O'
|| psum[2]=='O'&&psum[5]=='O'&&psum[8]=='O')
return 1;
if(psum[0]=='X'&&psum[1]=='X'&&psum[2]=='X'
|| psum[3]=='X'&&psum[4]=='X'&&psum[5]=='X'
|| psum[6]=='X'&&psum[7]=='X'&&psum[8]=='X'
|| psum[0]=='X'&&psum[4]=='X'&&psum[8]=='X'
|| psum[2]=='X'&&psum[4]=='X'&&psum[6]=='X'
|| psum[0]=='X'&&psum[3]=='X'&&psum[6]=='X'
|| psum[1]=='X'&&psum[4]=='X'&&psum[7]=='X'
|| psum[2]=='X'&&psum[5]=='X'&&psum[8]=='X')
return 2;
}
int main()
{
kaishi :
{
char shuru;
char *psum;
char pd;
int aa;
psum = sum;
printf("游戏规则,玩家输入相应序号即可.\n");
biao(sum);
char player1[30];
char player2[30];
printf("请输入玩家1的名字:");
scanf("%s",player1);
printf("请输入玩家2的名字:");
scanf("%s",player2);
int s = 1;
int qi = 0;
int win = 0;
do{
do{
printf("轮到%s输入:",player1);
scanf("%d",&qi);
system("cls");
switch(qi)
{
case 1 : if (sum[0] == 'O' || sum[0]=='X')
{error();continue;}
psum[0] = 'O'; break;
case 2 : if (sum[1] == 'O' || sum[1]=='X')
{error();continue;}
psum[1] = 'O'; break;
case 3 : if (sum[2] == 'O' || sum[2]=='X')
{error();continue;}
psum[2] = 'O'; break;
case 4 : if (sum[3] == 'O' || sum[3]=='X')
{error();continue;}
psum[3] = 'O'; break;
case 5 : if (sum[4] == 'O' || sum[4]=='X')
{error();continue;}
psum[4] = 'O'; break;
case 6 : if (sum[5] == 'O' || sum[5]=='X')
{error();continue;}
psum[5] = 'O'; break;
case 7 : if (sum[6] == 'O' || sum[6]=='X')
{error();continue;}
psum[6] = 'O'; break;
case 8 : if (sum[7] == 'O' || sum[7]=='X')
{error();continue;}
psum[7] = 'O'; break;
case 9 : if (sum[8] == 'O' || sum[8]=='X')
{error();continue;}
psum[8] = 'O'; break;
default: printf("输入错误会导致程序崩溃!只能输入数字喔!\n");break;
}
biao(sum);
s++;
}while(s%2!=0);
win = panduan(sum);
if(win == 1) {printf(" 恭喜! %s WIN!\n\n",player1); break;}
do
{
printf("轮到%s输入:",player2);
scanf("%d",&qi);
system("cls");
switch(qi)
{
case 1 :if (sum[0] == 'O' || sum[0]=='X')
{error();continue;}
psum[0] = 'X'; break;
case 2 :if (sum[1] == 'O' || sum[1]=='X')
{error();continue;}
psum[1] = 'X'; break;
case 3 :if (sum[2] == 'O' || sum[2]=='X')
{error();continue;}
psum[2] = 'X'; break;
case 4 :if (sum[3] == 'O' || sum[3]=='X')
{error();continue;}
psum[3] = 'X'; break;
case 5 :if (sum[4] == 'O' || sum[4]=='X')
{error();continue;}
psum[4] = 'X'; break;
case 6 :if (sum[5] == 'O' || sum[5]=='X')
{error();continue;}
psum[5] = 'X'; break;
case 7 :if (sum[6] == 'O' || sum[6]=='X')
{error();continue;}
psum[6] = 'X'; break;
case 8 :if (sum[7] == 'O' || sum[7]=='X')
{error();continue;}
psum[7] = 'X'; break;
case 9 :if (sum[8] == 'O' || sum[8]=='X')
{error();continue;}
psum[8] = 'X'; break;
default :printf("输入错误! 会导致程序崩溃!只能输入数字喔!\n");break;
}
biao(sum);
s++;
}while(s%2==0);
win = panduan(sum);
if(win == 2) {printf(" 恭喜! %s WIN!\n\n",player2); break;}
}while(s<9);
if(win!=1&&win!=2)
printf("平局咯!请重新开始!\n\n");
printf("是否继续 Y/N \n");
scanf("%s",&shuru);
if(shuru == 'Y' || shuru == 'y')
goto kaishi;
printf("游戏结束!\n");
system("pause");
return 0;
}}