【发布时间】:2022-01-06 09:21:35
【问题描述】:
我写了一个井字游戏代码,它使用了三个函数。但即使条件得到满足,它也不会停止。我尝试了多种编译器,例如在线 gdb,以及其他在线编译器以及 VS Code。
我创建了一个 for 循环来运行程序 8 次,因为如果有任何获胜者出现,将在此之前宣布获胜者。否则它将宣布平局。但是抽签条件不正常。
//tic tac toe multiplayer mode
#include<stdio.h>
void player1(char user1, char arr[][3]);
void player2(char user2, char arr[][3]);
int checkwinner( char arr[][3], char user1, char user2);
int main(){
char user1='x',user2='0';
char arr[][3]={ {'1','2','3',},
{'4','5','6'},
{'7','8','9'}
};
printf("user1 is x and user2 is 0.\ndecide who wants to be user1.\nx plays first\n");
int i=0,y=0;
int a=0,b=0;
for(a=0;a<3;a++){
for(b=0;b<3;b++){
printf("%c ",arr[a][b]);
}printf("\n");
}
printf("\n");
for(i=0;i<8;i++){
player1(user1,arr); //goto line 64
for(a=0;a<3;a++){ //print array
for(b=0;b<3;b++){
printf(" %c ",arr[a][b]);
}printf("\n");
}
printf("\n");
y=checkwinner(arr, user1, user2); // check if winner is found @line 130
if(y==1){
printf("user1 wins\n");
return 0;
}
if(y==2){
printf("user2 wins\n");
return 0;
}
player2(user2,arr);
for(a=0;a<3;a++){
for(b=0;b<3;b++){
printf( "%c ",arr[a][b]);
}printf("\n");
}
printf("\n");
y=checkwinner(arr, user1, user2); //check for winner @line 130
if(y==1){
printf("user1 wins\n");
return 0;
}
if(y==2){
printf("user2 wins\n");
return 0;
}
}
if(y==0){
printf("draw\n");
}
return 0;
}
void player1(char user1, char arr[][3]){
printf("user1 enter location. please donot select already used location\n");
int location;
scanf("%d",&location);
if((arr[0][0]!='x' && arr[0][0]!='0' ) &&(location==1)){
arr[0][0]='x';
}
if((arr[0][1]!='x' && arr[0][1]!='0' ) &&(location==2)){
arr[0][1]='x';
}
if((arr[0][2]!='x' && arr[0][2]!='0' ) &&(location==3)){
arr[0][2]='x';
}
if((arr[1][0]!='x' && arr[1][0]!='0' ) &&(location==4)){
arr[1][0]='x';
}
if((arr[1][1]!='x' && arr[1][1]!='0' ) &&(location==5)){
arr[1][1]='x';
}
if((arr[1][2]!='x' && arr[1][2]!='0' ) &&(location==6)){
arr[1][2]='x';
}
if((arr[2][0]!='x' && arr[2][0]!='0' ) &&(location==7)){
arr[2][0]='x';
}
if((arr[2][1]!='x' && arr[2][1]!='0' ) &&(location==8)){
arr[2][1]='x';
}
if((arr[2][2]!='x' && arr[2][2]!='0' ) &&(location==9)){
arr[2][2]='x';
}
}
void player2(char user2, char arr[][3]){
printf("user2 enter location.please donot select already used location\n");
int location;
scanf("%d",&location);
if((arr[0][0]!='x' && arr[0][0]!='0' ) &&(location==1)){
arr[0][0]='0';
}
if((arr[0][1]!='x' && arr[0][1]!='0' ) &&(location==2)){
arr[0][1]='0';
}
if((arr[0][2]!='x' && arr[0][2]!='0' ) &&(location==3)){
arr[0][2]='0';
}
if((arr[1][0]!='x' && arr[1][0]!='0' ) &&(location==4)){
arr[1][0]='0';
}
if((arr[1][1]!='x' && arr[1][1]!='0' ) &&(location==5)){
arr[1][1]='0';
}
if((arr[1][2]!='x' && arr[1][2]!='0' ) &&(location==6)){
arr[1][2]='0';
}
if((arr[2][0]!='x' && arr[2][0]!='0' ) &&(location==7)){
arr[2][0]='0';
}
if((arr[2][1]!='x' && arr[2][1]!='0' ) &&(location==8)){
arr[2][1]='0';
}
if((arr[2][2]!='x' && arr[2][2]!='0' ) &&(location==9)){
arr[2][2]='0';
}
}
int checkwinner( char arr[][3], char user1, char user2){
int y=0;
if( (arr[0][0] ==arr[0][1])&& (arr[0][0]==arr[0][2]) ){
if(arr[0][0]==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if((arr[1][0] ==arr[1][1]) && ( arr[1][0] ==arr[1][2]) ){
if(arr[1][0] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( (arr[2][0] ==arr[2][1])&& ( arr[2][0]==arr[2][2])){
if(arr[2][0] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if((arr[0][0] ==arr[1][0] )&& ( arr[0][0]==arr[2][0]) ){
if(arr[0][0] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( (arr[0][1] ==arr[1][1])&& ( arr[0][1]==arr[2][1])){
if(arr[0][1] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if((arr[0][2] ==arr[1][2])&& ( arr[0][2]==arr[2][2]) ){
if(arr[0][2] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( (arr[0][0] ==arr[1][1]) && ( arr[0][0]==arr[2][2]) ){
if(arr[0][0] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if((arr[0][2] ==arr[1][1])&& ( arr[0][2]==arr[2][0])){
if(arr[0][2] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
y=0;
return y;
}
【问题讨论】:
-
为什么要在
return y语句之前初始化y=0? -
去掉所有重复的代码来打印板和读取玩家输入。这就是子程序的用途。