用C语言,编程,求助大神
发布网友
发布时间:2022-08-01 11:38
我来回答
共3个回答
热心网友
时间:2024-11-25 09:34
时间仓促,做得比较简陋。
#include<stdio.h>
#include<windows.h>
#include<conio.h>
void menu();
void game();
int main()
{
int n=0;
menu();
while(scanf("%d", &n))
{
system("cls");
menu();
switch(n)
{
case 1: game(); break;
case 2: return 0;
default:printf("请重新输入:");
}
}
}
void menu()
{
printf("* * * * * * * * * *\n");
printf("* 井字棋小游戏 *\n");
printf("* 按1开始游戏 *\n");
printf("* 按2退出游戏 *\n");
printf("* * * * * * * * * *\n");
}
void game()
{
printf("请在小键盘输入1-9");
int step_number=1;
int board[3][3]={0};
while(1)
{
char num;
num = getch();
system("cls");
if (num=='1') board[2][0] += step_number++;
if (num=='2') board[2][1] += step_number++;
if (num=='3') board[2][2] += step_number++;
if (num=='4') board[1][0] += step_number++;
if (num=='5') board[1][1] += step_number++;
if (num=='6') board[1][2] += step_number++;
if (num=='7') board[0][0] += step_number++;
if (num=='8') board[0][1] += step_number++;
if (num=='9') board[0][2] += step_number++;
for(int i=0; i<3; i++) //打印棋盘
{
printf("\n");
for(int j=0; j<3; j++)
{
if (board[i][j] == 0) printf("_ ");
else if (board[i][j]%2 == 1) printf("X ");
else if (board[i][j]%2 == 0) printf("O ");
}
}
for(int i=0; i<3; i++)
{
if(board[i][0]!=0 &&board[i][1]!=0 &&board[i][2]!=0)
{
if(board[i][0]%2==1)
{
if(board[i][0]%2==board[i][1]%2 && board[i][0]%2==board[i][2]%2)
printf("\nX方胜!\n");
}
if(board[i][0]%2==0)
{
if(board[i][0]%2==board[i][1]%2 && board[i][0]%2==board[i][2]%2)
printf("\nO方胜!\n");
}
}
}
for(int j=0; j<3; j++)
{
if(board[0][j]!=0 && board[1][j]!=0 && board[2][j]!=0)
{
if(board[0][j]%2==1)
{
if(board[0][j]%2==board[1][j]%2 && board[0][j]%2==board[2][j]%2)
printf("\nX方胜!\n");
}
if(board[0][j]%2==0)
{
if(board[0][j]%2==board[1][j]%2 && board[0][j]%2==board[2][j]%2)
printf("\nO方胜!\n");
}
}
}
if(board[0][0]!=0 && board[1][1]!=0 && board[2][2]!=0)
{
if(board[0][0]%2==1)
{
if(board[0][0]%2==board[1][1]%2 && board[0][0]%2==board[2][2]%2)
printf("\nX方胜!\n");
}
if(board[0][0]%2==0)
{
if(board[0][0]%2==board[1][1]%2 && board[0][0]%2==board[2][2]%2)
printf("\nO方胜!\n");
}
}
if(board[0][2]!=0 && board[1][1]!=0 && board[2][0]!=0)
{
if(board[0][2]%2==1)
{
if(board[0][2]%2==board[1][1]%2 && board[0][2]%2==board[2][0]%2)
printf("\nX方胜!\n");
}
if(board[0][2]%2==0)
{
if(board[0][2]%2==board[1][1]%2 && board[0][2]%2==board[2][0]%2)
printf("\nO方胜!\n");
}
}
if (step_number > 10)
{
printf("平局\n");
printf("按任意键回主菜单");
if(getchar()) break;
}
}
}
热心网友
时间:2024-11-25 09:34
#include "stdio.h"
int main()
{
char a;
double num;
printf("character to number:\nPlease input a character:");
scanf("%c",&a);
num=a-'0';
printf("The character's double value:%d\n",num);
printf("--------------------------------------------\n");
printf("number to char:\nPlease input a number:");
scanf("%d",&num);
a=num+'0';
printf("The character is:%c\n",a);
printf("--------------------------------------------\n");
scanf("%c",&a);
if(a=='.'||a=='+'||='-')
{
exit(1);
}
}
热心网友
时间:2024-11-25 09:34
要求什么时候交 帮写私信