diff --git a/代码/.DS_Store b/代码/.DS_Store new file mode 100644 index 0000000..a3bba44 Binary files /dev/null and b/代码/.DS_Store differ diff --git a/代码/.vscode/launch.json b/代码/.vscode/launch.json new file mode 100644 index 0000000..aa52e5b --- /dev/null +++ b/代码/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + "version": "0.2.0", + "configurations": [ + { + "name": "Python 调试程序: 当前文件", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/代码/data/.DS_Store b/代码/data/.DS_Store new file mode 100644 index 0000000..8f33625 Binary files /dev/null and b/代码/data/.DS_Store differ diff --git a/代码/data/bg.png b/代码/data/bg.png new file mode 100644 index 0000000..c47b082 Binary files /dev/null and b/代码/data/bg.png differ diff --git a/代码/data/storn_black.png b/代码/data/storn_black.png new file mode 100644 index 0000000..42076b1 Binary files /dev/null and b/代码/data/storn_black.png differ diff --git a/代码/data/storn_white.png b/代码/data/storn_white.png new file mode 100644 index 0000000..d1e03e8 Binary files /dev/null and b/代码/data/storn_white.png differ diff --git a/代码/font/12345.TTF b/代码/font/12345.TTF new file mode 100644 index 0000000..53439a2 Binary files /dev/null and b/代码/font/12345.TTF differ diff --git a/代码/frame_begin.py b/代码/frame_begin.py new file mode 100644 index 0000000..5891f62 --- /dev/null +++ b/代码/frame_begin.py @@ -0,0 +1,68 @@ +# 五子棋棋盘:一个15*15的棋盘 +board = [[' ']*15 for line in range(15)] +for line in board: + print(line) + +# 落子:黑棋白棋轮流落子,只能在把棋子落在两条线的交点上,已落子的地方不能再重复落子; +def set_chess(x,y,color): + # 有棋子 + if board[x][y] != ' ': + print('该位置已有棋子') + return False + # 位置为空落子 + else: + board[x][y] = color + for i in board: + print(i) + return True + +# 用X代表黑棋,O代表白棋 +flag = 0 +going = True +while going: + if flag == 0: + print('黑棋回合') + x = int(input('请输入棋子横坐标0-14:')) + y = int(input('请输入棋子纵坐标0-14:')) + set_chess(x,y,'X') + flag = 1 + else: + print('白棋回合') + x = int(input('请输入棋子横坐标0-14:')) + y = int(input('请输入棋子纵坐标0-14:')) + set_chess(x,y,'O') + flag = 0 + + +# 胜利条件:横、竖、正斜、反斜四个方向相同颜色五子连珠的一方,获胜; +def check_win(board): + for line in board: + if ''.join(line).find('O'*5) != -1: + print('白棋获胜') + return 0 + elif ''.join(line).find('X'*5) != -1: + print('黑棋获胜') + return 1 + else: + return -1 + +board_b = [list(l) for l in zip(*board)] +board_c = [[] for line in range(29)] +for x in range(15): + for y in range(15): + board_c[x-y].append(board[x][y]) +board_d = [[] for line in range(29)] +for x in range(15): + for y in range(15): + board_d[x+y].append(board[x][y]) + +def check_win_all(board): + board_c = [[] for line in range(29)] + for x in range(15): + for y in range(15): + board_c[x-y].append(board[x][y]) + board_d = [[] for line in range(29)] + for x in range(15): + for y in range(15): + board_d[x+y].append(board[x][y]) + return [check_win(board), check_win([list(l) for l in zip(*board)]), check_win(board_c), check_win(board_d)] \ No newline at end of file diff --git a/代码/gui_new.py b/代码/gui_new.py new file mode 100644 index 0000000..31c9753 --- /dev/null +++ b/代码/gui_new.py @@ -0,0 +1,29 @@ +import pygame as pg +if __name__ == '__main__': + pg.init() + WIDTH = 615 + HEIGHT = 615 + clock = pg.time.Clock() + screen = pg.display.set_mode((WIDTH, HEIGHT)) + black = pg.image.load("data/storn_black.png").convert_alpha() + white = pg.image.load("data/storn_white.png").convert_alpha() + background = pg.image.load("data/bg.png").convert_alpha() + objects = [] + pg.display.set_caption("五子棋") + going = True + while going: + screen.blit(background, (0, 0)) + for event in pg.event.get(): + if event.type == pg.QUIT: + going = False + elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE: + going = False + elif event.type == pg.MOUSEBUTTONDOWN: + pos = pg.mouse.get_pos() + print(pos) + objects.append([black,(pos[0]-18,pos[1]-18)]) + for o in objects: + screen.blit(o[0], o[1]) + clock.tick(60) + pg.display.update() + pg.quit() \ No newline at end of file diff --git a/代码/main_new.py b/代码/main_new.py new file mode 100644 index 0000000..e2fa9e9 --- /dev/null +++ b/代码/main_new.py @@ -0,0 +1,79 @@ +import pygame as pg + +#设置棋盘上的棋子 +def set_chess(x, y, color): + if board[x][y] != ' ': + print('该位置已有棋子') #有了 + return False + else:#没有棋子 + board[x][y] = color + return True + +#检查是否获胜 +def check_win(board): + for list_str in board: #遍历棋盘的每一行 + if ''.join(list_str).find('O'*5) != -1: #如果找到连续的五个'O',表示白棋获胜,并返回一个0表示白棋获胜 + print('白棋获胜') + return 0 + elif ''.join(list_str).find('X'*5) != -1: + print('黑棋获胜') + return 1 + else: + return -1 #都没有获胜 + +#检查所有方向的获胜情况 +def check_win_all(board): + board_c = [[] for line in range(29)] #水平方向的棋盘副本 + for x in range(15): + for y in range(15): + board_c[x-y].append(board[x][y]) #根据斜线填充棋盘副本 + board_d = [[] for line in range(29)] #反对角线方向的棋盘副本 + for x in range(15): + for y in range(15): + board_d[x+y].append(board[x][y]) #根据反对角线填充棋盘副本 + #返回四个方向的检查结果 + return [check_win(board), check_win([list(l) for l in zip(*board)]), check_win(board_c), check_win(board_d)] + + +def main(): + pg.init() + clock = pg.time.Clock() + screen = pg.display.set_mode((WIDTH, HEIGHT)) #设置显示模式 + + black = pg.image.load("data/storn_black.png").convert_alpha() + white = pg.image.load("data/storn_white.png").convert_alpha() + background = pg.image.load("data/bg.png").convert_alpha() + + objects = [] #棋子列表 + pg.display.set_caption("五子棋") #标题 + flag = 0 #当前下棋的标记 + going = True #游戏是否继续的标志 + chess_list = [black, white] #黑棋和白棋的列表 + letter_list = ['X', 'O'] #黑棋和白棋的标记列表 + while going: + screen.blit(background, (0, 0)) + for event in pg.event.get(): + if event.type == pg.QUIT: #点击关闭窗口 + going = False + elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE: #如果按下Esc键 + going = False + elif event.type == pg.MOUSEBUTTONDOWN: #鼠标点击 + pos = pg.mouse.get_pos() #获取位置 + a, b = round((pos[0] - 27)/40), round((pos[1] - 27)/40) #计算棋盘坐标 + x, y = max(0, a) if a < 0 else min(a, 14), max(0, b) if b < 0 else min(b, 14) #限制棋子棋盘内 + if set_chess(x, y, letter_list[flag]): #成功放置棋子 + objects.append([chess_list[flag], (9+x*40, 9+y*40)]) #将棋子添加到棋子列表 + flag = [1, 0][flag] #转换下棋方 + if 0 in check_win_all(board) or 1 in check_win_all(board): #检查是否有人获胜 + going = False #有人获胜,结束游戏 + for o in objects: + screen.blit(o[0], o[1]) + clock.tick(60) + pg.display.update() + +if __name__ == '__main__': + WIDTH = 615 + HEIGHT = 615 + board = [[' ']*15 for line in range(15)] + main() + pg.quit() \ No newline at end of file