很久之前用SDL在MAC下 写的五子棋,左右互博程序

 

/*
 *  game_wuziqi.cpp
 *  SDL_Game_wuziqi
 *
 *  Created by mac on 11-4-26.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "game_wuziqi.h"
//#include <vector>
//#include <map>

int main(int argc, char* argv[])
{
	//map<Left_Up_Point,int>   button_color; 
	bool win=false;
//	char c;
    Init();
	SDL_Surface *whitewin;
	SDL_Surface *blackwin;
	whitewin=loadBMP("/Users/mac/Desktop/image/whitewin.bmp");
	blackwin=loadBMP("/Users/mac/Desktop/image/blackwin.bmp");
	setoffset();
	background=loadBMP("/Users/mac/Desktop/image/mapbgrd.bmp");
	line=loadBMP_blackcolor("/Users/mac/Desktop/image/tianya.bmp");
    image=loadBMP_blackcolor("/Users/mac/Desktop/image/bwbr.bmp");
	SDL_BlitSurface(background,0,screen,0);
	for (int i=0;i<=480;i+=20)
	{
	    apply_surface(rcl_offset,i, 0, line, screen);
		if (i==480) 
			apply_surface(rcl_offset, i-1, 0, line, screen);
		
		
	}
	for (int i=0; i<=480; i+=20)
	{
		apply_surface(lcl_offset, 0, i,line, screen);
		if (i==480) 
		    apply_surface(lcl_offset, 0, i-1, line, screen);
	}
	bool gameOver=false;
	int black=0;
	while (gameOver==false)
	{
		SDL_Flip(screen);
		SDL_Event gameEvent;
		while (SDL_PollEvent(&gameEvent)!=0&&!win)
		{
			if (gameEvent.type==SDL_QUIT)
			{
				gameOver=true;
			}
			if (gameEvent.type==SDL_MOUSEBUTTONDOWN) 
			{
				if(gameEvent.button.button==SDL_BUTTON_RIGHT)
				{
					if(black==0) 
					{ 
						Left_Up_Point point(gameEvent.button.x,gameEvent.button.y);
					    point.count_left_up_point();
						if (qipan[point.returnPoint_x()+10][point.returnPoint_y()+10]==2) 
						{
							Button_qizi  currentqizi(black,image,point.returnPoint_x(),point.returnPoint_y());
							currentqizi.show();
							qipan[point.returnPoint_x()+10][point.returnPoint_y()+10]=black;
							//ToEqual_1 where_currentqizi(point.returnPoint_x(),point.returnPoint_y());
							if (du_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black)||rl_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black)
								||rdlu_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black)||ruld_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black))
							{
								cout << "Black win! :-)\n";
								cout << "Please Press ESCAP to quit\n";
								SDL_BlitSurface(blackwin,0, screen,0);
								SDL_Flip(screen);
								SDL_Delay(2000);
								win=true;
							}
							else
								black+=1;
						}
						else
						{
							cout << "Because there is a  white qizi int this place "<<" Please change the place where you are cliping\n";
							break;
						}
					}
					else 
					{
						cout << "You should clip left button\n"	;
						break;
					}

				}
				else if(gameEvent.button.button==SDL_BUTTON_LEFT)
				{
					if (black==1) 
					{ 
						Left_Up_Point point(gameEvent.button.x,gameEvent.button.y);
						point.count_left_up_point();
						if (qipan[point.returnPoint_x()+10][point.returnPoint_y()+10]==2) 
						{
							Button_qizi  currentqizi(black,image,point.returnPoint_x(),point.returnPoint_y());
							qipan[point.returnPoint_x()+10][point.returnPoint_y()+10]=black;
							//ToEqual_1 where_currentqizi(point.returnPoint_x(),point.returnPoint_y());
							currentqizi.show();
							if (du_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black)||rl_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black)
								||rdlu_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black)||ruld_is_5(point.returnPoint_x(),point.returnPoint_y(),qipan,black))
							{
								cout << "white win! :-)\n";
								cout << "Please Press ESCAP to quit\n";
								win=true;
								SDL_BlitSurface(whitewin,0,screen,0);
								SDL_Flip(screen);
								SDL_Delay(2000);
							}
							else
							    black-=1;			
						}
						else {
							cout << "Because there is a black qizi int this place "<<" Please change the place where you are cliping\n";
							break;
						}						
					}
					else 
					{
						cout << "You should clip the right button\n";
						break;
					}

				}//button_color.insert(make_pair(point, currentqizi.getcolor_int()));
            }
		}
		while (gameEvent.type==SDL_KEYDOWN&&gameOver==false)
		{
			if (gameEvent.key.keysym.sym==SDLK_ESCAPE)
				gameOver=true;
			else 
			{
				gameOver=false;
			}

		}
	}
	//gameOver=false;
	//SDL_Delay(2000);
	clear_qipan();
	SDL_Quit();
	return 0;
}



/*
 *  game_wuziqi.h
 *  SDL_Game_wuziqi
 *
 *  Created by mac on 11-4-26.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */
#ifndef GAME01_WUZIQI_MAP
#define GAME01_WUZIQI_MAP
#include "SDL.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
using namespace std;
vector<vector<int> >  qipan(501,vector<int>(661,2));
SDL_Surface* screen=NULL;
SDL_Surface* line=NULL;
SDL_Surface* background=NULL;
SDL_Surface* image=NULL;
SDL_Rect rcl_offset,lcl_offset;
void setoffset()
{
	rcl_offset.x=0;
    rcl_offset.y=0;
	rcl_offset.w=1;
	rcl_offset.h=480;
	lcl_offset.x=0;
	lcl_offset.y=0;
	lcl_offset.w=480;
	lcl_offset.h=1;
}
bool Init()
{
	if (SDL_Init(SDL_INIT_VIDEO)<0)
	{
		cerr<<"init failed\n"<<endl;
		return false;
	}
	screen=SDL_SetVideoMode(480, 480, 32, SDL_SWSURFACE);
	if (screen==NULL)
	    return false;
	SDL_WM_SetCaption("Five heros' story!", NULL);
	return true;
}
SDL_Surface* loadBMP(string filename)
{
	SDL_Surface* loadImage=NULL;
    loadImage=SDL_LoadBMP(filename.c_str());
	return loadImage;
}

SDL_Surface *loadBMP_blackcolor( string filename )
{
	//The image that's loaded
	SDL_Surface* loadedImage = NULL;
	
	//The optimized image that will be used
	SDL_Surface* optimizedImage = NULL;
	
	//Load the image
	loadedImage = SDL_LoadBMP( filename.c_str() );
	
	//If the image loaded
	if( loadedImage != NULL )
	{
		//Create an optimized image
		optimizedImage = SDL_DisplayFormat( loadedImage );
		
		//Free the old image
		SDL_FreeSurface( loadedImage );
		
		//If the image was optimized just fine
		if( optimizedImage != NULL )
		{
			//Map the color key
			Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0x00, 0x00 );
			
			//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
			SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
		}
	}
	
	//Return the optimized image
	return optimizedImage;
}

void apply_surface(SDL_Rect temp,int des_x,int des_y,SDL_Surface* source,SDL_Surface *des)
{
	SDL_Rect des_offset;
    des_offset.x=des_x;
	des_offset.y=des_y;
    SDL_BlitSurface(source,&temp,des,&des_offset);
}
/*class ToEqual_1
{
private:
	int s;
	int at_x;
	int at_y;
	
public:
	ToEqual_1(int x,int y);
	void to1();
	bool is_1();
};
ToEqual_1::ToEqual_1(int x,int y)
{ 
	s=0;
	at_x=x;
	at_y=y;
}
void ToEqual_1::to1()
{
	s=1;
}
bool ToEqual_1::is_1()
{
	if (s==1) 
		return true;
	else 
		return false;
	
}
*/
class Button_qizi 
{
private:
	int i;
	SDL_Rect keyup;
	SDL_Surface* Buttonqizi;
	int des_x;
	int des_y;
public:
	Button_qizi(int one_or_zero,SDL_Surface* image,int x,int y);
	//bool current(ToEqual_1& current_blit);
	int getcolor_int()const;
	void show();
};
Button_qizi::Button_qizi(int one_or_zero,SDL_Surface* image,int x,int y)
{
	i=one_or_zero;
	//0表示黑色
	if (i==0)
	{
	    keyup.x=0;
	    keyup.y=0;
	    keyup.w=20;
		keyup.h=20;
		//color="black_color";
	}
	//1代表白色
	else
	{
	    keyup.x=20;
	    keyup.y=0;
	    keyup.w=20;
	    keyup.h=20;
		//color="white_color";
	}
	Buttonqizi=SDL_DisplayFormat(image);
	des_x=x;
	des_y=y;
	
}
/*bool Button_qizi::current(ToEqual_1& current_blit)
{
    return	current_blit.is_1();	
}*/
void Button_qizi::show()
{
	apply_surface((*this).keyup,(*this).des_x,(*this).des_y,(*this).Buttonqizi,screen);
	SDL_Flip(screen); 
}
int Button_qizi::getcolor_int()const
{
	return i;
}
class Left_Up_Point
{
private: 
	int x;
	int y;
public:
	Left_Up_Point(int clip_x,int clip_y):x(clip_x),y(clip_y)
	{}
	void count_left_up_point();
	int returnPoint_x()const;
	int returnPoint_y()const;
	//bool operator<(Left_Up_Point& point);
	
};
void Left_Up_Point::count_left_up_point()
{
	int left_up_x=0,left_up_y=0;
	if(x%20!=0&&y%20!=0)
	{
	    left_up_x=x-x%20;
		left_up_y=y-y%20;
	}
	if (x%20!=0&&y%20==0)
	{
		left_up_x=x-x%20;
		left_up_y=y;
	}
	if (x%20==0&&y%20!=0)
	{
		left_up_x=x;
		left_up_y=y-y%20;
	}
	if (x%20==0&&y%20==0)
	{
		left_up_x=x;
		left_up_y=y;
	}
	left_up_x+=10;
	left_up_y+=10;
	if(x<left_up_x&&y<left_up_y)
	{
		x=left_up_x-20;
		y=left_up_y-20;
	}
	else if (x<left_up_x&&y>left_up_y)
	{
		x=left_up_x-20;
		y=left_up_y;
	}
	else if (x>left_up_x&&y<left_up_y)
	{
		x=left_up_x;
		y=left_up_y-20;
	}
	else if (x>left_up_x&&y>left_up_y)
	{
		x=left_up_x;
		y=left_up_y;
	}
	else if (x==left_up_x&&y<left_up_x)
	{
		x=left_up_x-20;
		y=left_up_y-20;
	}
	else if (x==left_up_x&&y>left_up_y)
	{
		x=left_up_x;
		y=left_up_y;
	}
	else if (x<left_up_x&&y==left_up_y)
	{
		x=left_up_x-20;
		y=left_up_y-20;
	}
	else if (x>=left_up_x&&y==left_up_y) 
	{
		x=left_up_x;
		y=left_up_y;
	}
	
}
int Left_Up_Point::returnPoint_x()const
{
	return x;
}
int Left_Up_Point::returnPoint_y()const
{
	return y;
}
bool du_is_5(int x,int y,vector<vector<int> >& qiziButton,int player)
{
	int count_qizi=0;
	x=x+10;
	y=y+10;
	while (x<=500)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			x=x+20;
		}
		else if (count_qizi==5) 
		    return true;
		else 
		    break;

	}
	x=x-20*count_qizi;
	count_qizi=count_qizi-1;
	while (x>=0)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			x=x-20;
		}
		else if(count_qizi>=5)
			return true;
		else
		    return false;
	}
	if (count_qizi>=5)
		return true;
	else 
	    return false;
}

bool rl_is_5(int x,int y,vector<vector<int> >& qiziButton,int player)
{
	int count_qizi=0;
	x=x+10;
	y=y+10;
	while (y<=500)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			y=y+20;
		}
		else if (count_qizi==5) 
		    return true;
		else 
		    break;
		
	}
	y=y-20*count_qizi;
	count_qizi=count_qizi-1;
	while (y>=0)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			y=y-20;
		}
		else if(count_qizi>=5)
			return true;
		else
		    return false;
	}
	if (count_qizi>=5)
		return true;
	else 
	    return false;
}
bool rdlu_is_5(int x,int y,vector<vector<int> >& qiziButton,int player)
{
	int count_qizi=0;
	x=x+10;
	y=y+10;
	while (x<=500&&y<=500)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			x=x+20;
			y=y+20;
		}
		else if (count_qizi==5) 
		    return true;
		else 
		    break;
		
	}
	x=x-20*count_qizi;
	y=y-20*count_qizi;
	count_qizi=count_qizi-1;
	while (x>=0&&y>=0)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			x=x-20;
			y=y-20;
		}
		else if(count_qizi>=5)
			return true;
		else
		    return false;
	}
	if (count_qizi>=5)
		return true;
	else 
	    return false;
	
}
bool ruld_is_5(int x,int y,vector<vector<int> >& qiziButton,int player)
{
	int count_qizi=0;
	x=x+10;
	y=y+10;
	while (x>=0&&y<=500)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			y=y+20;
			x=x-20;
		}
		else if (count_qizi==5) 
		    return true;
		else 
		    break;
		
	}
	y=y-20*count_qizi;
	x=x+20*count_qizi;
	count_qizi=count_qizi-1;
	while (x<=500&&y>=0)
	{
		if (qiziButton[x][y]==player)
		{
			++count_qizi;
			y=y-20;
			x=x+20;
		}
		else if(count_qizi>=5)
			return true;
		else
		    return false;
	}
	if (count_qizi>=5)
		return true;
	else 
	    return false;
}
void clear_qipan()
{
	for (vector<vector<int> >::iterator iter=qipan.begin(); iter!=qipan.end(); ++iter)
		(*iter).clear();
}

/*bool Left_Up_Point::operator<(Left_Up_Point& point)
{
	point.count_left_up_point();
	if ((*this).x<point.x)
	    return true;
	else if((*this).x==point.x)
	{
	    if ((*this).y<point.y)
			return true;
		else 
		    return false;
	}
	else 
	    return false;
}*/
#endif

XO_OX 0.0 这些是“XO_OX"的注解。它们会让你全面了解这个游戏,并会说明如何安装它。 什么是“XO_OX"? “XO_OX"又名“五子棋”,五子棋则咸信是流传于古中国的传统棋种之一,至今仍在民间广泛流传,规则相当简单。或许因没有形成一套独立完整的棋种理论及文化内涵,更无制定公平完善的规则来解决黑白平衡问题,一直没有得到发展,所以没有像六博、格五、弹棋等传统棋类流传广泛,导致缺少可考古的棋具或文献,直到流传到外国才规则改革。 不管是哪种五子棋,棋手在先后手的观念、空间的思维及对棋形的理解都十分重要。 游戏规则: * 行棋:一人流轮一著下于棋盘空点处,下后不得移动。 * 胜负:先把五枚或以上己棋相连成任何横纵斜方向为胜。 * 和棋: o 行棋中一方提出和棋,另一方同意则判和棋。 o 棋子落满整张棋盘仍未分出胜负为和棋。 o 一方PASS后另一方下一手也PASS为和棋。 技术规格说明: 1、用C语言调用SDL实现; 2、基于LGPL协议。 3、程序中用到了SDL_image扩展包 如何安装: 1、在终端中运行make 2、在终端中运行make install 如何卸载: make uninstall 历史: 一、2011年8月15日 项目开始,谢红负责图形模块,赵梓辰负责游戏逻辑,吕玉飞负责事件响应,范人豪负责整体架构。 二、2011年8月17日 为了增加游戏的可玩性,项目由圈叉棋升级为五子棋
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zanglengyu

敲碗要饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值