关于四子棋 电脑智慧函数问题
这是我编写的 四子棋 其实 与五子棋类似 但是 在电脑函数那一片 电脑 没有什么智慧 请帮我解决一下代码如下:
其中diannao 是电脑的 函数
/*
功能:四子棋
作者:卢山
日期:2013-5-5
*/
//The headers
#include "SDL.h"
#include "SDL_image.h"
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 640;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *screen = NULL;
SDL_Surface *bg = NULL;
SDL_Surface *bai = NULL;
SDL_Surface *hei = NULL;
SDL_Surface *daixia = NULL;
SDL_Surface *win_bg = NULL;
SDL_Surface *lose_bg = NULL;
//The event structure
SDL_Event event;
int qipan[15][15] = {0};
int who = 1;//判断谁下子
int winner = 0;
int temp[2] = {7, 7};//初始一开始的棋子位置
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Get the number of ticks since the timer started
//or gets the number of ticks when the timer was paused
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was in error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Frame Rate Test", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the background image
bg = load_image( "bg.jpg" );
bai = load_image( "bai.jpg" );
hei = load_image( "hei.jpg" );
daixia = load_image( "daixia.jpg" );
win_bg = load_image( "win_bg.jpg" );
lose_bg = load_image( "lose_bg.jpg" );
//If there was a problem in loading the background
if( bg == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( bg );
SDL_FreeSurface( bai );
SDL_FreeSurface( hei );
SDL_FreeSurface( daixia );
SDL_FreeSurface( win_bg );
SDL_FreeSurface( lose_bg );
//Quit SDL
SDL_Quit();
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
//画棋盘 函数
void hua(){
int i;
int j;
for(i = 0; i < 15; i++){
for( j = 0; j < 15; j++){
if(qipan[i][j] == 1){
apply_surface( 21 + i * 51, 13 + j * 41, bai, screen );//将白棋子 渲染到界面上
}else if(qipan[i][j] == 2){
apply_surface( 21 + i * 51, 13 + j * 41, hei, screen );//将黑棋子 渲染到界面上
}
}
}
apply_surface( 21 + temp[0] * 51, 13 + temp[1] * 41, daixia, screen );//你将要下棋子的位置 的提示框
}
int xiaqi(){
//键盘按下
if( event.type == SDL_KEYDOWN )
{
//判断按下的是哪一个
switch( event.key.keysym.sym )
{
case SDLK_UP:
if(temp[1] > 0) temp[1]--;
break;
case SDLK_DOWN:
if(temp[1] < 14)temp[1]++;
break;
case SDLK_LEFT:
if(temp[0] > 0) temp[0]--;
break;
case SDLK_RIGHT:
if(temp[0] < 14)temp[0]++;
break;
case SDLK_RETURN:
if(qipan[temp[0]][temp[1]] == 0){
qipan[temp[0]][temp[1]] = 1;
who = 2;
}
}
}
return 0;
}
void diannao(){
if(who == 1) return;
//电脑判断位置
int i,j;
int n=0,m=0;
int a = 0;//循环结束
for(i = 0; i < 15; i++){
for(j = 0; j < 15; j++){
if((qipan[i][j]==1&&qipan[i-1][j]!=2&&qipan[i-1][j]!=1)||(qipan[i][j]==1&&qipan[i+1][j]!=2&&qipan[i+1][j]!=2)||(qipan[i][j]==1&&qipan[i][j+1]!=2&&qipan[i][j+1]!=2)||(qipan[i][j]==1&&qipan[i][j-1]!=2&&qipan[i][j-1]!=2))
{
if((qipan[i][j]==1&&qipan[i+1][j+1]!=2&&qipan[i+1][j+1]!=2)||(qipan[i][j]==1&&qipan[i-1][j-1]!=2&&qipan[i-1][j-1]!=2)||(qipan[i][j]==1&&qipan[i+1][j-1]!=2&&qipan[i+1][j-1]!=2)||(qipan[i][j]==1&&qipan[i-1][j+1]!=2&&qipan[i-1][j+1]!=2))
{
if((qipan[i][j]==1&&qipan[i+1][j]==1)||(qipan[i][j]==1&&qipan[i][j+1]==1))
{
qipan[i-1][j]=2;
a=1;
}
if((qipan[i][j]==1&&qipan[i+1][j-1]==1)||(qipan[i][j]==1&&qipan[i-1][j+1]==1))
{
qipan[i+1][j+1]=2;
a=1;
}
if(qipan[i][j]==2&&qipan[i-1][j]==2&&qipan[i-1][j]!=1)
{
qipan[i-1][j]=2;
a=1;
}
else
{
qipan[i][j+1]=2;
a=1;
}
}
/*if((qipan[i][j]==1&&qipan[i+1][j]==1)||(qipan[i][j]==1&&qipan[i][j+1]==1))
{
qipan[i-1][j]=2;
a=1;
}
if((qipan[i][j]==1&&qipan[i+1][j-1]==1)||(qipan[i][j]==1&&qipan[i-1][j+1]==1))
{
qipan[i+1][j+1]=2;
a=1;
}
else
{
qipan[i][j+1]=2;
a=1;
}*/
/*a=1;
break;*/
}
/* if(qipan[i][j] == 1){
qipan[i-1][j] = 2;
a = 1;
break;
}*/
}
if(a == 1) break;
}
who = 1;
}
/*、
函数名字:win
函数功能:判断输赢
函数参数:无
函数返回值:1||2
*/
int win(){
int i,j;
for(i=0;i<14;i++)
{
for(j=0;j<14;j++)
{
if((qipan[i][j]==1&&qipan[i+1][j]==1&&qipan[i+2][j]==1&&qipan[i+3][j])==1||(qipan[i][j]==1&&qipan[i][j+1]==1&&qipan[i][j+2]==1&&qipan[i][j+3]==1))
{
winner = 1;
return 1;
}
if((qipan[i][j]==2&&qipan[i+1][j]==2&&qipan[i+2][j]==2&&qipan[i+3][j])==2||(qipan[i][j]==2&&qipan[i][j+1]==2&&qipan[i][j+2]==2&&qipan[i][j+3]==2))
{
winner = 2;
return 2;
}
if((qipan[i][j]==1&&qipan[i+1][j+1]==1&&qipan[i+2][j+2]==1&&qipan[i+3][j+3])==1||(qipan[i][j]==1&&qipan[i-1][j-1]==1&&qipan[i-2][j-2]==1&&qipan[i-3][j-3]==1))
{
winner = 1;
return 1;
}
if((qipan[i][j]==2&&qipan[i-1][j-1]==2&&qipan[i-2][j-2]==2&&qipan[i-3][j-3])==2||(qipan[i][j]==2&&qipan[i+1][j+1]==2&&qipan[i+2][j+2]==2&&qipan[i+3][j+3]==2))
{
winner = 2;
return 2;
}
if((qipan[i][j]==1&&qipan[i+1][j-1]==1&&qipan[i+2][j-2]==1&&qipan[i+3][j-3])==1||(qipan[i][j]==1&&qipan[i-1][j+1]==1&&qipan[i-2][j+2]==1&&qipan[i-3][j+3]==1))
{
winner = 1;
return 1;
}
}
}
/* if(qipan[3][3] == 1){
winner = 1;
return 1;
}*/
return 0;
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Timer used to calculate the frames per second
Timer fps;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Start the frame timer
//While the user hasn't quit
while( quit == false )
{
fps.start();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Apply the background
xiaqi();
diannao();
apply_surface( 0, 0, bg, screen );
hua();
quit = win() || quit;
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//定义屏幕刷新时间
//15为每秒刷新15次
while( fps.get_ticks() < 1000 / 15 )
{
//wait
}
}
if(winner == 1){
apply_surface( 0, 0, win_bg, screen );
hua();
SDL_Flip( screen );
SDL_Delay( 3000 );
}else if(winner == 2){
apply_surface( 0, 0, lose_bg, screen );
hua();
SDL_Flip( screen );
SDL_Delay( 3000 );
}
//Clean up
clean_up();
return 0;
}