C++控制台界面库_实例4: 五子棋(二人对战)

二人对战,一个人用鼠标,一个人用键盘,代码如下:

// Sample1.cpp : 定义控制台应用程序的入口点。
//

#include “stdafx.h”
#include “console.h”
#pragma comment(lib,“console.lib”)

extern COORD mouse;

#define WHITE 1
#define BLACK 2

int const left=3,top=3;
#define WIDE 15
#define HEIGH 15

void jiemian0(); // 0

int win(int data[WIDE][HEIGH],int currentpoint_y,int currentpoint_x);
void show(int currentpoint_y,int currentpoint_x);
void clear(int currentpoint_y,int currentpoint_x);
void putdown(int currentpoint_y,int currentpoint_x,BOOL black); //下子

int _tmain(int argc, _TCHAR* argv[])
{
Screen screen;
screen.SetTitle(“五子棋”);
screen.ShowOrHideCursor(0);

Event event;
event.AddMouseMode();
	
jiemian0();
	     
event.RestoreMode();

screen.MoveCursorTo(4,34);
screen.SetTextColorAndTextBackgroundColor(1,11);
screen.Pause();

screen.Close_handle();
return 0;

}

void jiemian0()
{
Screen screen;
screen.SetSize(80,35);
screen.Paint_screen(11);

Button EXIT_button=Button(66,30,"退出");
EXIT_button.Attribute(4,11);
EXIT_button.Display_button();

Event event;

Window window0(left-1,top-1,WIDE*4,(HEIGH-1)*2+3);
window0.Paint_window(8);

Form form;
form.Attribute(7,8);	
form.DrawForm(left,top,WIDE-1,HEIGH-1,4,2);

int data[WIDE][HEIGH]={0};
int currentpoint_x=7,currentpoint_y=7;
BOOL black=TRUE;	

screen.text_Bgcolor_num=8;
screen.DisplayText(4,32,"提示信息:");
screen.text_color_num=0;
screen.text_Bgcolor_num=11;
screen.DisplayText(66,24,"●");	

Textbox textbox=Textbox(14,32,40);
textbox.textbox_textcolor_num=3;
textbox.textbox_textBgcolor_num=8;
textbox.textbox_Bgcolor_num=8;

Window window=Window(65,4,14,15);
window.window_textcolor_num=3;
window.window_textBgcolor_num=11;
window.DisplayFileEx_SinglePageWindow("shuoming.txt");

while(1)
{		
	event.ReadEvent(); 
	if(event.Mouse_Event())
	{
		event.Get_mouse_position();
		if(mouse.X>=left+4&&mouse.X=top+2&&mouse.Y1)currentpoint_x--;
			show(currentpoint_y,currentpoint_x);
			break;

		case VK_RIGHT:
			clear(currentpoint_y,currentpoint_x);
			if(currentpoint_x1)currentpoint_y--;
			show(currentpoint_y,currentpoint_x);
			break;

		case VK_DOWN:
			clear(currentpoint_y,currentpoint_x);
			if(currentpoint_y

}

int win(int data[WIDE][HEIGH],int currentpoint_y,int currentpoint_x)
{
int x,y,sum=0;
int temp=data[currentpoint_y][currentpoint_x];
if(temp==0)return 0;

//  方向:"-"
sum=0;
for(x=currentpoint_x,y=currentpoint_y;x>0;x--)if(temp==data[y][x])sum++;else break;	
for(x=currentpoint_x+1;x=5)return 1; 

//  方向:"|"
sum=0;
for(x=currentpoint_x,y=currentpoint_y;y>0;y--)if(temp==data[y][x])sum++;else break;
for(y=currentpoint_y+1;y=5)return 1;

//  方向: "\"
sum=0;
for(x=currentpoint_x,y=currentpoint_y;x>0 && y>0;x--,y--)if(temp==data[y][x])sum++;else break;	
for(x=currentpoint_x+1,y=currentpoint_y+1;x=5)return 1;

//  方向: "/"
sum=0;
for(x=currentpoint_x,y=currentpoint_y;x0;x++,y--)if(temp==data[y][x])sum++;else break;	
for(x=currentpoint_x-1,y=currentpoint_y+1;x>0 && y=5)return 1;

return 0;   //  满足条件返回1,不满足返回0;	

}

void show(int currentpoint_y,int currentpoint_x)
{
COORD point;
point.X=left+4currentpoint_x;
point.Y=top+2
currentpoint_y;

Screen screen;
screen.TextAttribute(6,8);
screen.DisplayText(point.X-2,point.Y-1,"┎");
screen.DisplayText(point.X+2,point.Y-1,"┒");
screen.DisplayText(point.X-2,point.Y+1,"┗");
screen.DisplayText(point.X+2,point.Y+1,"┛");

}

void clear(int currentpoint_y,int currentpoint_x)
{
COORD point;
point.X=left+4currentpoint_x;
point.Y=top+2
currentpoint_y;

Screen screen;
screen.TextAttribute(6,8);
screen.DisplayText(point.X-2,point.Y-1," ");
screen.DisplayText(point.X+2,point.Y-1," ");
screen.DisplayText(point.X-2,point.Y+1," ");
screen.DisplayText(point.X+2,point.Y+1," ");

}

void putdown(int currentpoint_y,int currentpoint_x,BOOL black) //下子
{
COORD point;
point.X=left+4currentpoint_x;
point.Y=top+2
currentpoint_y;

Screen screen;
screen.text_Bgcolor_num=8;

if(black)
{
	screen.text_color_num=0;
	screen.DisplayText(point.X,point.Y,"●");
}
else
{
	screen.text_color_num=1;
	screen.DisplayText(point.X,point.Y,"●");
}

}

运行效果:

C++控制台界面库_实例4: 五子棋(二人对战)_第1张图片

你可能感兴趣的:(C++控制台界面库_实例4: 五子棋(二人对战))