问题遇到的现象和发生背景
用devc++写的三子棋项目,编译时有几个问题
问题相关代码,请勿粘贴截图
1.
#ifndef GAME__H
#define GAME__H
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
void Isboard(char board[ROW][COL],int row,int col);
//void meun(int ord);
void playermove(char board[ROW][COL],int row,int col);
void computermove(char board[ROW][COL],int row,int col);
int Isfull(char board[ROW][COL],int row,int col);
void Initboard(char board[ROW][COL],int row,int col);
char board[3][3];
#endif
2.
#define _CRT_SRCURE_NO_WARNING 1
#include <stdio.h>
#include"game.h"
void Initboard(char board[ROW][COL],int row,int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
board[i][j]=0;
}
}
}
void Isboard(char board[ROW][COL],int row,int col)
{
int i=0,j=0;
for(i=0;i<row;i++)
{
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if(i<row-1)
{
printf("---|---|---\n");
}
}
}
void playermove((char board[ROW][COL],int row,int col)
{
int x=0,y=0;
while(1)
{
printf("请选择要下的坐标:");
scanf("%d%d",&x,&y);
if(x>=1&&x<=row&&y>=1&&y<=col)
{
if((board[x-1][y-1])==" ")
{
(board[x-1][y-1])="#";
break;
}
else
{
printf("坐标已被占用,请重新选择\n");
}
}
else
{
printf("坐标非法,请重新选择\n");
}
}
}
void computermove(char board[ROW][COL],int row,int col)
{
int x=0,y=0;
while(1)
{
x=rand()%row;
y=rand()%col;
if(board[x][y]=' ')
{
board[x][y]='*';
printf("电脑走:\n");
break;
}
}
}
int Isfull(char board[ROW][COL],int row,int col)
{
int i;
int j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(board[i][j]==' ')
{
return 0;
}
else
{
return 1;
}
}
}
}
char Iswin(char board[ROW][COL],int row,int col)
{
int c=0;
for(c=0;c<row;c++)
{
if(board[c][0]==board[c][1]==board[c][2]&&board[c][0]!=' ')
{
return board[c][0];
}
}
for(c=0;c<row;c++)
{
if(board[0][c]==board[1][c]==board[2][c]&&board[0][c]!=' ')
{
return board[c][0];
}
}
if(board[0][0]==board[1][1]==board[2][2]&&board[0][0]!=' ')
{
return board[0][0];
}
if(board[0][2]==board[1][1]==board[2][0]&&board[0][2]!=' ')
{
return board[0][2];
}
if(1==Isfull(board,row,col))
{
return 'Q';
}
return 'W';
}
3.
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void game()
{
//棋盘
char ret;
char board[3][3];
Initboard(board,ROW,COL);
Isboard(board,ROW,COL);
while(1)
{
//玩家下棋
playermove(board,ROW,COL);
Isboard(board,ROW,COL);
ret=Iswin();//判断输赢
if(ret!='W')
{
break;
}
//电脑下棋
computermove(board,ROW,COL);
Isboard(board,ROW,COL);
ret=Iswin();//判断输赢
if(ret!='W')
{
break;
}
}
if(ret=='#')
{
printf("玩家赢\n");
}
if(ret=='*')
{
printf("电脑赢\n");
}
if(ret=='Q')
{
printf("平局\n");
}
}
int main()
{
srand((unsigned int)time(NULL));
int ORD=0;
do{
printf("***************\n");
printf("***************\n");
printf("*1.PLAY 0.EXIT*\n");
printf("请输入指令:1/0\n");
scanf("%d",&ORD);
switch(ORD)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("指令错误,请重新输入\n");
break;
}
}while(ORD);
return 0;
}
运行结果及报错内容
第一遍能编译成功,但卡在玩家下棋的函数的循环中出不去,第二遍就报错[Error] ld returned 1 exit status
还在项目中添了个makefile.win,望先生指点