/*
* 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