本人是c++新手一枚,望大家见谅。
//main.cpp
#include "fiveChess.h"
#include<iostream>
using namespace std;
int main()
{
fiveChess f;
f.runGame();
return 0;
}
下面是fiveChess(五子棋)类的定义
//fiveChess.h
#ifndef A_H
#define A_H
#include<iostream>
using namespace std;
#define maxm 25 //定义棋盘大小
class fiveChess
{
private:
int cx; //光标的x坐标
int cy;
int cols; //棋盘缓冲器当前列数
int rows; //棋盘缓冲器当前行数
int now; //下棋方
int round; //回合数
int ch[maxm][maxm]; //储存棋盘上的棋子
char buff[maxm*2+1][maxm*4+3]; //棋盘缓冲器
// static int maxm; //棋盘大小
void gotoxy(int x, int y);
void init(); //初始化对局
void printChessBoard(); //输出棋盘
void write(char *c1, char const * c2); //向棋盘c1写入字符c2
int check();
char const *getCurse(int i, int j); //获取光标
char const *getTable(int i, int j); //获取当前位置对应的制表符
public:
void runGame();
};
#endif
下面是类的实现
//fiveChess.cpp
#include "fiveChess.h"
#include<windows.h>
//#include<stdio.h>
#include<conio.h>
//#include<stdlib.h>
#include<iostream>
#include<string.h>
#define BLACK 1
#define WHITE 2
//#define
//#define maxm 25 //定义棋盘大小
using namespace std;
//fiveChess::fiveChess(){
// maxm = 15;
//}
//int fiveChess::maxm = 10;
void fiveChess::gotoxy(int x, int y){
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void fiveChess::init(){
cx = cy = maxm/2;
round = 0;
now = BLACK; //黑方先开局
memset(ch,0,sizeof(ch));
memset(buff,"\0",sizeof(buff));
}
void fiveChess::write(char *c1, char const * c2){
cols+=strlen(c2);
while(*c2 != "\0")
*c1++ = *c2++;
}
//scdn不支持特殊字符,此处删掉
char const* fiveChess::getCurse(int i,int j){
}
//scdn不支持特殊字符,此处删掉
char const* fiveChess::getTable(int i,int j){
}
int fiveChess::check(){ //胜负检查,即判断当前走子位置有没有造成五连珠的情况
int w=1,x=1,y=1,z=1,i;//累计横竖正斜反邪四个方向的连续相同棋子数目
for(i=1;i<5;i++)
if(cy+i<maxm&&ch[cx][cy+i]==now) //向下检查
w++;
else break;
for(i=1;i<5;i++)
if(cy-i>0&&ch[cx][cy-i]==now) //向上检查
w++;
else break;
if(w>=5)
return now; //若果达到5个则判断当前走子玩家为赢家
for(i=1;i<5;i++)
if(cx+i<maxm&&ch[cx+i][cy]==now) //向右检查
x++;
else break;
for(i=1;i<5;i++)
if(cx-i>0&&ch[cx-i][cy]==now) //向左检查
x++;
else break;
if(x>=5)
return now;
for(i=1;i<5;i++)
if(cx+i<maxm&&cy+i<maxm&&ch[cx+i][cy+i]==now) //向右下检查
y++;
else break;
for(i=1;i<5;i++)
if(cx-i>0&&cy-i>0&&ch[cx-i][cy-i]==now) //向左上检查
y++;
else break;
if(y>=5)
return now;
for(i=1;i<5;i++)
if(cx+i<maxm&&cy-i>0&&ch[cx+i][cy-i]==now) //向右上检查
z++;
else break;
for(i=1;i<5;i++)
if(cx-i>0&&cy+i<maxm&&ch[cx-i][cy+i]==now) //向左下检查
z++;
else break;
if(z>=5)
return now;
return 0;
}
void fiveChess::runGame(){
int op;
int winner;
int cnt=0;
char const* showText;
init();
// exit;
while(1){
printChessBoard();
op = getch(); //非标准函数,按下键盘无需回车自动读取
if(op == 0x20){
if(ch[cx][cy] == 0){
ch[cx][cy] = now;
// round++;
cnt++;
}
if(winner = check()){ //检查胜负
if(winner == BLACK)
showText = "黑方获得了胜利!";
else if(winner == WHITE)
showText = "白方获得了胜利!";
gotoxy(0, 2*(2*maxm+1)+2);
cout<<showText<<endl;
}
now = now==BLACK ? WHITE : BLACK;
}
else if(cnt == maxm*maxm){
// showText = "白方获得了胜利!";
gotoxy(0, 2*(2*maxm+1)+2);
cout<<"平局!"<<endl;
}
else if(op == 0xE0){ //特殊标志
op == getch();
switch(op){
case 0x4B:{cx--;break;}
case 0x48:{cy--;break;}
case 0x4D:{cx++;break;}
case 0x50:{cy++;break;}
}
if(cx<0)cx=maxm-1;//假如光标位置越界则移动到对侧
if(cy<0)cy=maxm-1;
if(cx>maxm-1)cx=0;
if(cy>maxm-1)cy=0;
}
}
}
//scdn不支持特殊字符,此处删掉
void fiveChess::printChessBoard(){
}
直接编译运行main.cpp报错
undefined reference to `fiveChess::runGame()”
[Error] ld returned 1 exit status
问一下要怎样才能让程序跑起来呢。
解决方案
3
包含关系上没发现有什么问题
10
题主用vs吗?是的话请学一下怎么建立“工程”
用gcc之类的,请学一下makefile怎么写
用gcc之类的,请学一下makefile怎么写
10
IDE 内,直接添加到工程中
也可以手动写 makefile
或
。
命令行下
分别编译两个.cc,cxx,cpp
然后链接
也可以手动写 makefile
或
。
命令行下
分别编译两个.cc,cxx,cpp
然后链接
2
这算不上新手了吧,谦虚了
10
代码好像没啥问题啊
题主用的IDE还是本人手动编译链接的
题主用的IDE还是本人手动编译链接的