各位前辈,这是五子棋代码,请问结束后如何把“游戏结束”变成“白棋胜利”或“黑棋胜利”?

 

#include <graphics.h>      
#include <conio.h>
#include<windows.h>

int num=-1;
int piece[15][15];
IMAGE bgp;

void put_background()//背景图
{
 putimage(0,0,240,240,&bgp,69,71);
 putimage(240,0,240,240,&bgp,69,71);
 putimage(0,240,240,240,&bgp,69,71);
 putimage(240,240,240,240,&bgp,69,71);

}

void draw_line()//画线
{
 setlinecolor(RED);
 for(int x=15;x<480;x+=30)
  line(x,15,x,465);
 for(int y=15;y<480;y+=30)
  line(15,y,465,y);
}

void draw_point()//画中间的四个点
{
 setfillcolor(BLACK);
 fillcircle(4*30-15,4*30-15,3);
 fillcircle(4*30-15,12*30-15,3);
 fillcircle(8*30-15,8*30-15,3);
 fillcircle(12*30-15,4*30-15,3);
 fillcircle(12*30-15,12*30-15,3);
}

void initpiece()
{
 for(int i=0;i<15;i++)
  for(int j=0;j<15;j++)
   piece[i][j]=0;
}

int change_piece(int x,int y)
{
 if(piece[x][y]!=0)
  return 0;
 else
  piece[x][y]=num;
 return 1;
}
  
void draw_piece(int m,int n)//设置num=-1落子为白棋
{
 if (num==-1)
	setfillcolor(WHITE);
 else
    setfillcolor(BLACK);
 int x,y;
 x=m/30;
 y=n/30;
 if(change_piece(x,y)==0)
  return;
 fillcircle(m-(m%30)+15,n-(n%30)+15,13);
  num*=-1;
}
int check_five_piece(int x,int y)//判断是否五子连线
{if(x<2 || y<2 || x>12 || y>12)
	return 0;
if(piece[x][y] == piece[x-1][y] && piece[x][y] == piece[x-2][y] && piece[x][y] == piece[x+1][y] && piece [x][y] == piece[x+2][y])
	return 1;
if(piece[x][y] == piece[x][y-1] && piece[x][y] == piece[x][y-2] && piece[x][y] == piece[x][y+1] && piece [x][y] == piece[x][y+2])
	return 1;
if(piece[x][y] == piece[x-1][y-1] && piece[x][y] == piece[x-2][y-2] && piece[x][y] == piece[x+1][y+1] && piece [x][y] == piece[x+2][y+2])
	return 1;
if(piece[x][y] == piece[x-1][y+1] && piece[x][y] == piece[x-2][y+2] && piece[x][y] == piece[x+1][y-1] && piece [x][y] == piece[x+2][y-2])
	return 1;
return 0;
}
int check_over()//判断是否结束
{
		for(int i = 0; i < 15; i++)
		{
			if(piece[i][j] = 0)
				continue;
			if(check_five_piece(i, j) == 1)
				return 1;
		}
	return 0;

}
  
int main()
{
    initgraph(480, 480); 
    loadimage(&bgp, _T("background.jpg"));
	settextcolor(WHITE);
	settextstyle(42, 20, _T("楷书"));
	setbkmode(TRANSPARENT);
    put_background();
    draw_line();
    draw_point();
	MOUSEMSG m;
	while(1)
	{
		m=GetMouseMsg();
		if(m.uMsg==WM_LBUTTONDOWN)
		{
			draw_piece(m.x,m.y);
		}
		if (check_over() == 1)//结束时弹出
		{
			outtextxy(180, 220, _T("游戏结束"));
			system("pause");
			return 0;
		}
	}
	return 0;       
}

倒数第七行

用cleardevice清屏,然后用outtextxy方法输出内容。

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include<windows.h>
#include <time.h>
#include <string>
#pragma warning(disable:4996)
enum ChessPieceColor {
	white = 1,
	blue = 2,
	/* any your want color here */
};
int x = 25, y = 25;  //初始坐标
int board[21][21] = { 0 };		//棋盘占位数组
void InitGame()  //棋盘初始化
{
	initgraph(600, 500);
	setlinecolor(RED);
	for (int i = 0; i < 20; i++)
	{
		line(0, i * 25, 500, i * 25);
		line(i * 25, 0, i * 25, 500);
	}
	setlinestyle(PS_SOLID, 2);  //画实线 宽度2
	line(500, 0, 500, 500);
	outtextxy(512, 60, _T("玩家1:白棋"));
	outtextxy(512, 180, _T("W,A,S,D移动"));
	outtextxy(512, 210, _T("G键下棋子"));
	outtextxy(512, 120, _T("玩家2:蓝棋"));
	setfillcolor(WHITE);  //棋盘白字初始位置
	// 前朝余孽
	board[1][1] = 1;
	solidcircle(x, y, 12);
	//???????????????????????????????????????????????
	//	为什么要 getchar()
	//getchar();


}



int flag = 1;   //标记下棋状态
char key;		//检测键盘按键


void InitMove()     //棋子移动初步判断部分
{
	switch (key) {
	case 'w': //上 
		y -= 25;
		break;
	case 's': //下 
		y += 25;
		break;
	case 'a'://左 
		x += 25;
		break;

	case 'd': //右 
		x -= 25;
		break;
	default:
		break;
	}
	if (x < 0)x = 500;
	if (y > 500)y = 0;
	if (x > 500)x = 0;
	if (y < 0)y = 500;


}

//判断获胜条件
int judge(int a, int b)
{
	int i, j;
	int t = 2 - flag % 2;
	for ((i = a - 4) && (j = b); i <= a; i++)  //横向
	{
		if (i >= 1 && i < 16 && t == board[i][j] && t == board[i + 1][j] && t == board[i + 2][j] && t == board[i + 3][j] && t == board[i + 4][j])
			return 1;
	}
	for ((i = a) && (j = b - 4); j <= a; j++)   //竖向
	{
		if (j >= 1 && j < 16 && t == board[i][j] && t == board[i][j + 1] && t == board[i][j + 2] && t == board[i][j + 3] && t == board[i][j + 4])
			return 1;
	}
	for ((i = a - 4) && (j = b - 4); ((i <= a) && (j >= b)); i++, j++)   //斜下
	{
		if (i >= 1 && i < 16 && j >= 1 && j < 16 && t == board[i][j] && t == board[i + 1][j + 1] && t == board[i + 2][j + 2] && t == board[i + 3][j + 3] && t == board[i + 4][j + 4])
			return 1;
	}
	for ((i = a - 4) && (j = b + 4); ((i <= a) && (j >= b)); i++, j--)   //斜上
	{
		if (i >= 1 && i < 16 && j >= 1 && j < 16 && t == board[i][j] && t == board[i + 1][j - 1] && t == board[i + 2][j - 2] && t == board[i + 3][j - 3] && t == board[i + 4][j - 4])
			return 1;
	}
}
// 棋子的英文怎么拼 baby
void DrawChessPieces(short x, short y, ChessPieceColor color) {
	switch (color) {
	case ChessPieceColor::blue: {
		setfillcolor(BLUE);
		break;
	}
	case ChessPieceColor::white: {
		setfillcolor(WHITE);
		break;
	}

	}
	// 好可怕你女朋友
	solidcircle(x, y, 12);
}


//void Move()   //棋子移动,以及该位置是否有棋子检测,并按G下棋
//{
//	setfillcolor(BLACK);		//消除上一步光标
//	solidcircle(x, y, 12);
//	key = getchar();
//	InitMove();
//	while (board[x / 25][y / 25] = 1)InitMove();//如果已经有棋子则跳转到下一个位置
//	if (flag % 2 == 1) { setfillcolor(WHITE); };//根据flag数判断棋子颜色
//	if (flag % 2 == 0) { setfillcolor(BLUE); };
//	solidcircle(x, y, 12);
//	if (getchar() == 'g' || 'G')
//	{	
//		board[x / 25][y / 25] = 1;
//		if (judge(x / 25, y / 25))         //每下完一次之后进行胜负判断
//		{
//			if (1 == flag % 2)
//			{
//				outtextxy(512, 400, _T("玩家2胜利"));
//		
//				return; 
//			}
//			else
//			{
//				outtextxy(512, 60, _T("玩家胜利"));
//				return;
//			}
//		}
//		//如果没有结束,跳转到下一个棋子的位置
//		InitMove();
//		while (board[x / 25][y / 25] = 1)InitMove();
//	}
//
//	
//	
//
//	flag++;
//	if (flag % 2 == 1) { setfillcolor(WHITE); };//根据flag数判断棋子颜色
//	if (flag % 2 == 0) { setfillcolor(BLUE); };
//	solidcircle(x, y, 12);
//
//}
MOUSEMSG m; int k;
bool judgeEdge(int x, int y) {
	if (x >= 1 && x < 19 && y >= 1 && y < 19) {
		return true;
	}
	else {
		return false;
	}
}

bool judgeWin(int x,int y, ChessPieceColor color) {
	int judgeX = x;
	int judgeY = y;
	int ChessPieces = 1;
	bool flag = false;
	 {
		 {
			 int judgeX = x;
			 int judgeY = y;
			 // 横向 向左
			 judgeX--;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeX--;
				 }
				 // 不满足直接退出
				 else {
					 break;
				 }
			 }
			 // 横向 向右
			 judgeX = x + 1;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeX++;
				 }
				 // 不满足直接退出
				 else {
					 break;
				 }
			 }
			 if (ChessPieces >= 5) {
				 flag = true;
			 }
			 else {
				 ChessPieces = 1;
			 }
			
		 }
		 {
			 // 横向 向左
			 judgeY--;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeY--;
				 }
				 // 不满足直接退出
				 else {
					 break;
				 }
			 }
			 // 横向 向右
			 judgeY = y + 1;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeY++;
				 }
				 // 不满足直接退出
				 else {
					 ChessPieces = 1;
				 }
			 }
			 if (ChessPieces >= 5) {
				 flag = true;
			 }
			 else {
				 ChessPieces = 1;
			 }
			
		 }
		 {
			 int judgeX = x;
			 int judgeY = y;
			 // 横向 向左
			 judgeY--;
			 judgeX--;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeY--;
					 judgeX--;
				 }
				 // 不满足直接退出
				 else {
					 break;
				 }
			 }
			 // 横向 向右
			 judgeY = y + 1;
			 judgeX = x + 1;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeY++;
					 judgeX++;
				 }
				 // 不满足直接退出
				 else {
					 ChessPieces = 1;
				 }
			 }
			 if (ChessPieces >= 5) {
				 flag = true;
			 }
			 else {
				 ChessPieces = 1;
			 }
		 }
		 {
			 int judgeX = x;
			 int judgeY = y;
			 // 横向 向左
			 judgeY++;
			 judgeX--;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeY++;
					 judgeX--;
				 }
				 // 不满足直接退出
				 else {
					 break;
				 }
			 }
			 // 横向 向右
			 judgeY = y - 1;
			 judgeX = x + 1;
			 while (board[judgeX][judgeY] == color) {
				 ChessPieces++;
				 // 如果满足边界
				 if (judgeEdge(judgeX, judgeY)) {
					 judgeY--;
					 judgeX++;
				 }
				 // 不满足直接退出
				 else {
					 ChessPieces = 1;
				 }
			 }
			 if (ChessPieces >= 5) {
				 flag = true;
			 }
			 else {
				 ChessPieces = 1;
			 }
		 }
		
		
	}
	return flag;
	
		
	
}

void Move()   //鼠标点击下棋
{

	int x, y;
	m = GetMouseMsg();
	if (m.mkLButton == 1)	//点击下棋
	{
		char msg[200];
		sprintf(msg, "x=%d,y=%d", m.x, m.y);
		//	MessageBoxA(NULL, msg, "鼠标在哪儿", NULL);
			/*MessageBoxA(NULL, msg, "鼠标在哪儿", NULL);*/
		/*	被你气死得 for什么for*/
		
		// x=0,y=0             x=25,y=25

			/*int x = m.x % 25 >12;
			int y = m.y % 25;*/
			int x = m.x / 25;
			int y = m.y / 25;

			int offsetX = m.x % 25 <= 12 ? 0 : 25;
			int offsetY = m.y % 25 <= 12 ? 0 : 25;

			x *= 25;
			y *= 25;
			x += offsetX;
			y += offsetY;

			
			

			if (x <= 500 && y <= 500 && x >= 0 && y >= 0) {
				int boardX = x / 25;
				int boardY = y / 25;
				if (!board[boardX][boardY]) {
					if (flag) {
						DrawChessPieces(x, y, ChessPieceColor::blue);
						board[boardX][boardY] = ChessPieceColor::blue;
						bool winFlag = judgeWin(boardX, boardY, ChessPieceColor::blue);
						if (winFlag) {
							MessageBoxA(NULL, "WIN", "", NULL);
						}
						flag = false;
					}
					else {
						DrawChessPieces(x, y, ChessPieceColor::white);
						board[boardX][boardY] = ChessPieceColor::white;
						bool winFlag = judgeWin(boardX, boardY, ChessPieceColor::white);
						if (winFlag) {
							MessageBoxA(NULL, "WIN", "", NULL);
						}
						flag = true;
					}
				}
			}
		/*if (m.x >= x && m.x <= (x + 25) && m.y >= y && y <= (y + 25))*/
			
		//for (x = 12; x < 600; x += 25)
		//	for (y = 12; y < 500; y += 25)
		//	{
		//		if (m.x >= x && m.x <= (x + 25) && m.y >= y && y <= (y + 25))
		//		{
		//			if (board[(x + 13) / 25][(y + 13) / 25] = 0)     //判断该位置是否有棋子
		//			{
		//				if (flag % 2 == 1) { setfillcolor(WHITE); };//根据flag数判断棋子颜色
		//				if (flag % 2 == 0) { setfillcolor(BLUE); };
		//				solidcircle(x + 13, y + 13, 12);
		//				board[(x + 13) / 25][(y + 13) / 25] = 1;
		//				flag++;
		//			}
		//		}
		//	}
	}
	FlushMouseMsgBuffer();

	//if (judge(x / 25, y / 25))         //每下完一次之后进行胜负判断
	//{
	//	if (1 == flag % 2)
	//	{
	//		k = 1;
	//		outtextxy(512, 400, _T("玩家2胜利"));

	//		return;
	//	}
	//	else
	//	{
	//		k = 1;
	//		outtextxy(512, 60, _T("玩家胜利"));
	//		return;
	//	}
	//}


}




void main()
{
	InitGame();
	while (1)
		Move();





}

 

 这是一套graphic开发的点击的五子棋小程序

可以啊,你可以圈定一个范围 就像画网格线一样

然后将字符串打印在那个界面上 就行了

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632