Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlefisher619 authored Apr 5, 2019
1 parent 78205fc commit e6068ab
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 23 deletions.
211 changes: 211 additions & 0 deletions ai.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#include<windows.h>
#include "gobang.h"
extern void launchAIMode();
extern COORD waitAI();

int score(int x,int y);
int score_board[SIZE][SIZE];
void launchAIMode(){
gobangGameMode=GOBANG_AI_MODE;
clear();
initListener();
resetBoard();
SetConsoleTitle(TITLE_GOBANG);
hideCursor();
renderBotton();
pos(25,0);
puts(BANNER_GOBANG);
srand(time(0));
isBlackIdentity=random(1);
pos(25,19);
puts(isBlackIdentity?MESSAGE_IDENTITY_BLACK:MESSAGE_IDENTITY_WHITE);
while (1) {
pos(25,1);
COORD coord;
if(isBlackIdentity^isBlackMove==0){
puts(MESSAGE_WAIT_YOU);
coord=waitForMouseClick();
}else{
puts(MESSAGE_WAIT_AI);
coord=waitAI();

}
int ret=Trigger(coord.X,coord.Y);
if(ret==GAMEOVER_SIGNAL) break;
}
MessageBox(NULL, TEXT( (isBlackTheWinner&&isBlackIdentity) ? MESSAGE_YOU_WIN: MESSAGE_YOU_LOSE ), TEXT(TITLE_GAMEOVER), MB_OK | MB_ICONINFORMATION);
}
COORD waitAI(){
COORD coord;

int max_score=0; //place for AI to play
int i,j;
for (i=0;i<SIZE;i++)
for (j=0;j<SIZE;j++)
{
if(board[i][j]!=-1) continue;
if ( (score_board[i][j]=score(i,j)) >=max_score)
{
// printf("%d %d %d\n",i,j,score_board[i][j]);
coord.X=i,coord.Y=j,max_score=score_board[i][j];
max_score=score_board[i][j];
}
}

int temp=coord.X;
coord.X=coord.Y;
coord.Y=temp;
coord.X++;
coord.Y++;
return coord;

}


int score(int x,int y)
{
int flag=isBlackMove;
int i1=x+1,j1=y,i2=x-1,j2=y,ans=0,i3,j3,i4,j4;
while(board[i1][y]==(1-flag) && i1<SIZE)
{
i1++;
}
while( (board[i2][y]==(1-flag)) && i2>=0) //敌方棋子
i2--;
//printf("%d %d %d %d\n",x,y,i1,i2);
i1--; //该方向最远敌方棋子位置
i2++;
int pl=i1-i2;

if(pl>=4) ans+=1000000;
else if(pl==3) ans+=100000;
else if(pl==2) ans+=1500;
else if(pl==1) ans+=500;
i1=x,j1=y+1,j2=y-1;
while(board[x][j1]==(1-flag)&&j1<SIZE)
j1++;
while(board[x][j2]==(1-flag)&&j2>=0) //敌方棋子
j2--;
j1--;
j2++;
pl=j1-j2;
if(pl>=4) ans+=1000000;
else if(pl==3) ans+=100000;
else if(pl==2) ans+=1500;
else if(pl==1) ans+=500;
i1=x-1,j1=y-1,i2=x+1,j2=y+1;
while(board[i1][j1]==(1-flag)&& i1>=0 &&j1>=0)
i1--,j1--;
while(board[i2][j2]==(1-flag)&& i2<SIZE &&j2<SIZE)
i2++,j2++;
i1++,i2--;
pl=i2-i1;
if(pl>=4) ans+=1000000;
else if(pl==3) ans+=100000;
else if(pl==2) ans+=1500;
else if(pl==1) ans+=500;
i1=x-1,j1=y+1,i2=x+1,j2=y-1;
while(board[i1][j1]==(1-flag)&& i1>=0 &&j1<SIZE)
i1--,j1++;
while(board[i2][j2]==(1-flag)&& i2<SIZE &&j2>=0)
i2++,j2--;
i1++,i2--;
pl=i2-i1;
if(pl>=4) ans+=1000000;
else if(pl==3) ans+=100000;
else if(pl==2) ans+=1500;
else if(pl==1) ans+=500;

//following are the computer attack player position
i1=x-1,j1=y,i2=x+1,j2=y;
while(board[i1][y]==flag&&i1>=0)
i1--;
i1++;
while(board[i2][y]==flag&&i2<SIZE)
i2++;
i2--;
pl=i2-i1;
//judging whether this direction can win
i3=x-1,i4=x+1;
while(board[i3][y]!=(1-flag)&&x-i3<=6)
i3--;
i3++;
while(board[i4][y]!=(1-flag)&&i4-x<=6)
i4++;
i4--;
if(i4-i3>=5)
{
if(pl==4) ans+=10000000; //我方冲5
else if(pl==3) ans+=10000; //我方冲4
else if(pl==2) ans+=5000; //我方冲3
else if(pl==1) ans+=501;
}

j1=y-1,j2=y+1;
while(board[x][j1]==flag&&j1>=0)
j1--;
j1++;
while(board[x][j2]==flag&&j2<SIZE)
j2++;
j2--;
j3=y-1,j4=y+1;
while(board[x][j3]!=(1-flag)&&j3>=0&&y-j3<=6)
j3--;
j3++;
while(board[x][j4]!=(1-flag)&&j4<SIZE&&j4-y<=6)
j4++;
j4--;
pl=j2-j1;
if(j4-j3>=5)
{
if(pl==4) ans+=10000000;
else if(pl==3) ans+=10000;
else if(pl==2) ans+=5000;
else if(pl==1) ans+=501;
}

i1=x-1,j1=y-1,i2=x+1,j2=y+1;
while(board[i1][j1]==flag&&i1>=0&&j1>=0)
i1--,j1--;
while(board[i2][j2]==flag&&i2<SIZE&&j2<SIZE)
i2++,j2++;
i1++,i2--;
pl=i2-i1;
i3=x-1,j3=y-1,i4=x+1,j4=y+1;
while(board[i3][j3]!=(1-flag) &&i3>=0&&j3>=0&&x-i3<=6)
i3--,j3--;
i3++;
while(board[i4][j4]!=(1-flag) &&i4<SIZE&&j4<SIZE&&i4-x<=6)
i4++,j4++;
i4--;
if(i4-i3>=5)
{
if(pl==4) ans+=10000000;
else if(pl==3) ans+=10000;
else if(pl==2) ans+=5000;
else if(pl==1) ans+=501;
}

i1=x-1,j1=y+1,i2=x+1,j2=y-1;
while(board[i1][j1]==flag&&i1>=0&&j1<SIZE)
i1--,j1++;
while(board[i2][j2]==flag&&i2<SIZE&&j2>=0)
i2++,j2--;
i1++,i2--;
pl=i2-i1;
i3=x-1,j3=y+1,i4=x+1,j4=y-1;
while(board[i3][j3]!=(1-flag) &&i3>=0&&j3<SIZE)
i3--,j3++;
i3++;
while(board[i4][j4]!=(1-flag) &&i4<SIZE&&j4>=0)
i4++,j4--;
i4--;
if(i4-i3>=5)
{
if(pl==4) ans+=10000000;
else if(pl==3) ans+=100000;
else if(pl==2) ans+=5000;
else if(pl==1) ans+=501;
}
return ans;
}
9 changes: 6 additions & 3 deletions gobang.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
#pragma once
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include "message.h"
#define SIZE 20
Expand All @@ -9,6 +10,7 @@ int history[400][3]={0},num=0;//record whlch step who go while one
int isBlackMove=1;//if it is the time for black one to play, flag=0, or flag=1
int isBlackTheWinner=0;
int isBlackIdentity=0;
int gobangGameMode;
extern void draw(); //绘制棋盘
extern int pending_win (int x ,int y); //judge whether someone can win
extern int pending_legal(int x,int y); //judging whether put the chess here is legal
Expand Down Expand Up @@ -74,7 +76,7 @@ int play(int y,int x)

record(x,y,num++);//record the step now
insert(x,y); //put the chess into the board
flipIsBlackMove();

draw();
if (pending_win(x,y))
{
Expand All @@ -84,6 +86,7 @@ int play(int y,int x)
puts(isBlackTheWinner?MESSAGE_BLACK_WIN:MESSAGE_WHITE_WIN);
return GAMEOVER_SIGNAL;
}
flipIsBlackMove();
return CONTINUE_SIGNAL;
}

Expand Down
4 changes: 1 addition & 3 deletions internet.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "ui.h"
#include "message.h"
#include "gobang.h"
Expand All @@ -13,7 +12,6 @@ void clear();
void initListener();
void renderBotton();
void internetGameInit();
int internetMode;//-1 LocalMode 0 ServerMode 1 ClientMode
void internetGameInit(){
//启动游戏
setIsBlackMove(1);
Expand Down Expand Up @@ -263,7 +261,7 @@ int internetUserOperationHandler(SOCKET s_client){
MessageBox(NULL, TEXT((isBlackTheWinner&&isBlackIdentity) ? MESSAGE_YOU_WIN: MESSAGE_YOU_LOSE ), TEXT(TITLE_GAMEOVER), MB_OK | MB_ICONINFORMATION);
return GAMEOVER_SIGNAL;
}
SetConsoleTitle(internetMode==GOBANG_SERVER_MODE?MESSAGE_WAIT_CLIENT_MOVE:MESSAGE_WAIT_SERVER_MOVE);
SetConsoleTitle(gobangGameMode==GOBANG_SERVER_MODE?MESSAGE_WAIT_CLIENT_MOVE:MESSAGE_WAIT_SERVER_MOVE);
}
return CONTINUE_SIGNAL;
}
Expand Down
34 changes: 19 additions & 15 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

#include<conio.h>

#include<unistd.h>
#include<string.h>
#include<winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include "internet.h"

#include "message.h"
#include "ai.h"
#include "gobang.h"
void launchLocalMode();
void launchAIMode();
void internetModeMenu();
void initListener();
void renderBotton();
Expand All @@ -18,14 +18,17 @@ int onUndoClick(int x,int y);
int onSurrenderClick(int x,int y);
int mainMenu();
void clear();

COORD waitAI();
COORD waitForMouseClick();
int onUndoClick(int x,int y){
if(internetMode==GOBANG_LOCAL_MODE) return onLocalUndoClick(x, y);
if(gobangGameMode==GOBANG_LOCAL_MODE ||
gobangGameMode==GOBANG_AI_MODE) return onLocalUndoClick(x, y);
return UNDO_CLICK_SIGNAL;

}
int onSurrenderClick(int x,int y){
if(internetMode==GOBANG_LOCAL_MODE) return onLocalSurrenderClick(x, y);
if(gobangGameMode==GOBANG_LOCAL_MODE ||
gobangGameMode==GOBANG_AI_MODE) return onLocalSurrenderClick(x, y);
return SURRENDER_CLICK_SIGNAL;
}
void initListener(){//注册游戏按钮监听器
Expand All @@ -52,7 +55,7 @@ void internetModeMenu(){
scanf("%d",&op);
getchar();
if(op==1){
internetMode=GOBANG_CLIENT_MODE;
gobangGameMode=GOBANG_CLIENT_MODE;
clear();
pos(0,0);
puts(MESSAGE_ENTER_IPADDR);
Expand All @@ -62,7 +65,7 @@ void internetModeMenu(){
internetClientMode(ip);

}else if(op==2){
internetMode=GOBANG_SERVER_MODE;
gobangGameMode=GOBANG_SERVER_MODE;
clear();
pos(0,0);
internetServerMode();
Expand All @@ -75,12 +78,15 @@ void internetModeMenu(){
int mainMenu(){
clear();
pos(0,0);
puts("Welcome to GOBANG!\n[1] Local Multiple Player\n[2] Internet Multiple Player\n[3] Exit\n");
puts("Welcome to GOBANG!\n[0] Local Single Game\n[1] Local Multiple Player\n[2] Internet Multiple Player\n[3] Exit\n");
showCursor();
int op;
scanf("%d",&op);
pos(0,4);
pos(0,5);
switch(op){
case 0:
launchAIMode();
break;
case 1:
launchLocalMode();
break;
Expand All @@ -95,11 +101,8 @@ int mainMenu(){

}
}



void launchLocalMode(){
internetMode=GOBANG_LOCAL_MODE;
gobangGameMode=GOBANG_LOCAL_MODE;
//本地游戏
clear();
//注册本地游戏按钮监听器
Expand All @@ -123,6 +126,7 @@ void launchLocalMode(){
}
MessageBox(NULL, TEXT(TITLE_GAMEOVER), TEXT(TITLE_GAMEOVER), MB_OK | MB_ICONINFORMATION);
}

int main(){
while(1){
//主菜单
Expand Down
3 changes: 2 additions & 1 deletion message.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const char* MESSAGE_IDENTITY_WHITE="You're the WHITE(O)";
const char* MESSAGE_WAIT_BLACK="Wait BLACK(*) make the move...";
const char* MESSAGE_WAIT_WHITE="Wait WHITE(O) make the move...";
const char* MESSAGE_WAIT_YOU ="Wait your move... ";
const char* MESSAGE_WAIT_AI ="Wait AI move... ";
const char* MESSAGE_WAIT_SERVER_MOVE="Gobang - Wait server's move...";
const char* MESSAGE_WAIT_CLIENT_MOVE="Gobang - Wait client's move...";
const char* MESSAGE_BLACK_WIN="BLACK(*) WIN! ";
Expand Down Expand Up @@ -48,4 +49,4 @@ const int SURRENDER_CLICK_SIGNAL=0xAA;
const int GOBANG_CLIENT_MODE=1;
const int GOBANG_SERVER_MODE=0;
const int GOBANG_LOCAL_MODE=-1;
//const int GOBANG_AI_MODE=0xFF;
const int GOBANG_AI_MODE=0xFF;
1 change: 0 additions & 1 deletion ui.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include<windows.h>
#include<stdio.h>
#include "message.h"
Expand Down

0 comments on commit e6068ab

Please sign in to comment.