急求 !各位java大神帮我解一下这道题吧,谢谢!
2个回答
展开全部
在jdk安装目录下有一个类似的参考例子,也是TicTacToe,人机交互的,你可以运行看看,jdk1.6.0_31\demo\applets\TicTacToe\example1.html,这个目录是我机子的,你安装JDK之后,进入到对应的目录,直接双击example1.html就可以在浏览器玩了。推荐安装JDK6,里面有源码的。如果这个答案你还看不懂的,建议回去再学一下Java最基础的知识。最近看到这种问题太多了,不免多说了一句,Sorry.
展开全部
#include <stdio.h>
char num[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
void print()
{
int iflag=0;
printf("-------\n");
while(iflag<3)
{
printf("|%c|%c|%c|\n",num[iflag][0],num[iflag][1],num[iflag][2]);
printf("-------\n");
iflag++;
}
}
bool can_play=true;
bool X_play=true;
void play()
{
char c;
if(X_play)
{
c='X';
}
else
{
c='O';
}
printf("Player %c\n",c);
int r;
int col;
step:printf("Enter a row for player %c:",c);
scanf("%d",&r);
printf("Enter a column for player %c:",c);
scanf("%d",&col);
if(num[r][col] != ' ')
{
printf("This cell is already occupied.Try a different cell\n");
goto step;
}
num[r][col]=c;
X_play=!X_play;
}
void judge()
{
if(num[0][0] != ' ')
{
if(num[0][0] == num[0][1] && num[0][1] == num[0][2])
{
can_play = false;
printf("Player %c won\n",num[0][0]);
return;
}
if(num[0][0] == num[1][1] && num[1][1] == num[2][2])
{
can_play = false;
printf("Player %c won\n",num[0][0]);
return;
}
if(num[0][0] == num[1][0] && num[1][0] == num[2][0])
{
can_play = false;
printf("Player %c won\n",num[0][0]);
return;
}
}
if(num[0][1] != ' ' && num[0][1] == num[1][1] && num[1][1] == num[2][1])
{
can_play = false;
printf("Player %c won\n",num[0][1]);
return;
}
if(num[1][0] != ' ' && num[1][0] == num[1][1] && num[1][1] == num[1][2])
{
can_play = false;
printf("Player %c won\n",num[1][0]);
return;
}
if(num[0][2] != ' ' && num[0][2] == num[1][2] && num[1][2] == num[2][2])
{
can_play = false;
printf("Player %c won\n",num[0][2]);
return;
}
if(num[2][0] != ' ' )
{
if(num[2][0] == num[2][1] && num[2][1] == num[2][2])
{
can_play = false;
printf("Player %c won\n",num[2][0]);
return;
}
if(num[2][0] == num[1][1] && num[1][1] == num[0][2])
{
can_play = false;
printf("Player %c won\n",num[2][0]);
return;
}
}
for(int i =0;i <3;i++)
{
for(int j = 0;j < 3;j++)
{
if(num[i][j]==' ') return;
}
}
can_play = false;
}
int main(){
printf("Play the tic-tac-toe game by two users.\n");
print();
while(can_play)
{
play();
judge();
print();
}
}
char num[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
void print()
{
int iflag=0;
printf("-------\n");
while(iflag<3)
{
printf("|%c|%c|%c|\n",num[iflag][0],num[iflag][1],num[iflag][2]);
printf("-------\n");
iflag++;
}
}
bool can_play=true;
bool X_play=true;
void play()
{
char c;
if(X_play)
{
c='X';
}
else
{
c='O';
}
printf("Player %c\n",c);
int r;
int col;
step:printf("Enter a row for player %c:",c);
scanf("%d",&r);
printf("Enter a column for player %c:",c);
scanf("%d",&col);
if(num[r][col] != ' ')
{
printf("This cell is already occupied.Try a different cell\n");
goto step;
}
num[r][col]=c;
X_play=!X_play;
}
void judge()
{
if(num[0][0] != ' ')
{
if(num[0][0] == num[0][1] && num[0][1] == num[0][2])
{
can_play = false;
printf("Player %c won\n",num[0][0]);
return;
}
if(num[0][0] == num[1][1] && num[1][1] == num[2][2])
{
can_play = false;
printf("Player %c won\n",num[0][0]);
return;
}
if(num[0][0] == num[1][0] && num[1][0] == num[2][0])
{
can_play = false;
printf("Player %c won\n",num[0][0]);
return;
}
}
if(num[0][1] != ' ' && num[0][1] == num[1][1] && num[1][1] == num[2][1])
{
can_play = false;
printf("Player %c won\n",num[0][1]);
return;
}
if(num[1][0] != ' ' && num[1][0] == num[1][1] && num[1][1] == num[1][2])
{
can_play = false;
printf("Player %c won\n",num[1][0]);
return;
}
if(num[0][2] != ' ' && num[0][2] == num[1][2] && num[1][2] == num[2][2])
{
can_play = false;
printf("Player %c won\n",num[0][2]);
return;
}
if(num[2][0] != ' ' )
{
if(num[2][0] == num[2][1] && num[2][1] == num[2][2])
{
can_play = false;
printf("Player %c won\n",num[2][0]);
return;
}
if(num[2][0] == num[1][1] && num[1][1] == num[0][2])
{
can_play = false;
printf("Player %c won\n",num[2][0]);
return;
}
}
for(int i =0;i <3;i++)
{
for(int j = 0;j < 3;j++)
{
if(num[i][j]==' ') return;
}
}
can_play = false;
}
int main(){
printf("Play the tic-tac-toe game by two users.\n");
print();
while(can_play)
{
play();
judge();
print();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询