Skip to content

邓堂瑞 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

add_subdirectory(level1)
add_subdirectory(level2)
add_subdirectory(gobang)
18 changes: 18 additions & 0 deletions gobang/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
project(gobang)

set(CMAKE_PREFIX_PATH "F:\\develop\\c\\.vcpkg-clion\\vcpkg\\packages\\raylib_x64-windows\\")
find_package(raylib CONFIG REQUIRED)

#set(raylib_DIR "F:\\develop\\c\\.vcpkg-clion\\vcpkg\\packages\\raylib_x64-windows\\share\\raylib")

add_executable(gobang
main.c
src/gui.c
src/gobang.c
src/alpha_beta_pruning.c
src/stack.c
src/alpha_beta_pruning.c
)

target_link_libraries(gobang PRIVATE raylib)

File renamed without changes.
35 changes: 35 additions & 0 deletions gobang/headers/alpha_beta_pruning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Created by Cz_13 on 2023/10/31.
//

#ifndef C2023_CHALLENGE_ALPHA_BETA_PRUNING_H
#define C2023_CHALLENGE_ALPHA_BETA_PRUNING_H

#include "gobang.h"
#include "string.h"

#define PATTERN_SIZE 11
#define PATTERN_NUM 18 // 模式
#define MAX_DEPTH 4 // 迭代层数, 只允许是偶数
#define INFINITY 2147283648 // 2 ^ 31

const int statements[PATTERN_NUM];
PIECE ai;
int best_pos[2];
const char patterns[PATTERN_NUM][PATTERN_SIZE];
int nodes[MAX_DEPTH + 1][BOARD_SIZE * BOARD_SIZE][3]; // 启发式搜索使用, 在get_score时对单个子进行打分, 之后再排序
int piece_score[BOARD_SIZE][BOARD_SIZE]; // 储存单个子的得分
int pred_chessboard[MAX_DEPTH + 1][BOARD_SIZE][BOARD_SIZE]; // 每层待探查棋子的粗略得分
int pos_num[MAX_DEPTH + 1];
int heu_locations[6][BOARD_SIZE * BOARD_SIZE][2];
int best_steps[MAX_DEPTH][2];

extern int search(Gobang *gobang, int depth, int alpha, int beta, PIECE cur_player);
extern int evaluate(Gobang *gobang);
extern void node_sort(Gobang *gobang);
extern void init_score(Gobang *gobang);
extern int first_search(Gobang *gobang, int depth, int alpha, int beta, PIECE cur_player);
extern void update_score(Gobang *gobang);
extern int init();

#endif //C2023_CHALLENGE_ALPHA_BETA_PRUNING_H
35 changes: 35 additions & 0 deletions gobang/headers/gobang.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef C2023_CHALLENGE_GOBANG_H
#define C2023_CHALLENGE_GOBANG_H

#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "stack.h"

#define DIR_NUM 4 // 有几个检验方向
#define BOARD_SIZE 15
#define PRED_COUNT 2 // 考虑附近的落点个数

enum PIECE{PLAYER_BLACK = -1, PLAYER_WHITE = 1}; // -1 为黑棋, 1 为白棋

const int direction[DIR_NUM][2]; // 遍历方向时使用的方向常量

typedef enum PIECE PIECE; // -1 为黑棋, 1 为白棋
typedef struct Gobang{
int chessboard[15][15]; // 棋盘 15 x 15, 0表示空, -1表示黑, 1表示白
PIECE now_player; // 当前应该下棋的玩家
Stack *steps;
int pos_steps[15][15]; // 可能的落子点, 采用一个棋盘表示, 0: 不建议搜索, 1: 建议搜索
int pos_steps_num;
} Gobang;

extern void print_pos_steps_in_stdin(Gobang *gobang);
extern Gobang *create_gobang();
extern int fall(Gobang *gobang, int x, int y);
extern int is_win(Gobang *gobang);
extern void print_chessboard_in_stdin(Gobang *gobang);
extern int remove_piece(Gobang *gobang, int x, int y);
extern void print_debug_msg(Gobang *gobang);
extern void pred_init(Gobang *gobang);
extern int take_back(Gobang *gobang);
#endif //C2023_CHALLENGE_GOBANG_H
23 changes: 23 additions & 0 deletions gobang/headers/gui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by Cz_13 on 2023/10/31.
//

#ifndef C2023_CHALLENGE_GUI_H
#define C2023_CHALLENGE_GUI_H

#include "raylib.h"
#include "time.h"
#include "gobang.h"

#define WINDOW_WIDTH 1600
#define WINDOW_HEIGHT 1200
#define CHESSBOARD_START_X 32
#define CHESSBOARD_START_Y 32
#define MARGIN 24 // 页边空格 24, 上下左右均为24
#define PIECE_SIZE 45
#define BOUNDARY_LENGTH 3
#define GAP_LENGTH 54

extern void
gobang_gui(Gobang *gobang, int (*player_move)(Gobang *gobang, int x, int y), void (*ai_move)(Gobang *gobang), int *ai_piece);
#endif //C2023_CHALLENGE_GUI_H
8 changes: 8 additions & 0 deletions gobang/headers/mcts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Created by Cz_13 on 2023/10/31.
//

#ifndef C2023_CHALLENGE_MCTS_H
#define C2023_CHALLENGE_MCTS_H

#endif //C2023_CHALLENGE_MCTS_H
22 changes: 22 additions & 0 deletions gobang/headers/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef C2023_CHALLENGE_STACK_H
#define C2023_CHALLENGE_STACK_H

#include "stdlib.h"
#include "stdio.h"
#include <assert.h>



typedef struct Stack{
int sp; // 下一个将要填入数据的位置
int **stack;
int size;
int (*is_empty)(struct Stack *stack);
int (*is_full)(struct Stack *stack);
int* (*pop)(struct Stack *stack);
void (*append)(struct Stack *stack, int player, int x, int y);
int* (*get_last_element)(struct Stack *stack); // 返回栈中第一个元素
} Stack;

extern Stack *create_stack(int size);
#endif //C2023_CHALLENGE_STACK_H
40 changes: 40 additions & 0 deletions gobang/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "headers/gui.h"
#include "headers/gobang.h"
#include "headers/alpha_beta_pruning.h"




void ai_move(Gobang *gobang){
long start = clock();
if(gobang->steps->is_empty(gobang->steps)){
best_pos[0] = 7;
best_pos[1] = 7;
} else {
init_score(gobang);
first_search(gobang, MAX_DEPTH, -INFINITY, INFINITY - 1, ai);
}
long end = clock();
printf("\ntime: %ld ms\n", end - start);
if(best_pos[0] == -1){
printf("[ERROR] FAILED TO SEARCH THE ANSWER!!!\n");
best_pos[0] = 14;
best_pos[1] = 14;
} else {
fall(gobang, best_pos[0], best_pos[1]);
}
best_pos[0] = -1;
best_pos[1] = -1;
}

int player_move(Gobang *gobang, int x, int y){
return fall(gobang, x, y);
}

int main() {
Gobang *gobang = create_gobang();
init();
ai = PLAYER_WHITE;
gobang_gui(gobang, player_move, ai_move, &ai);

}
Binary file added gobang/ppt.pptx
Binary file not shown.
Binary file added gobang/res/AIFirstHand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gobang/res/AISecondHand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gobang/res/black_piece.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gobang/res/black_win.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gobang/res/chessboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gobang/res/gobang.xlsx
Binary file not shown.
Loading