|
五子棋是一种源自中国的传统棋类游戏,起源可以追溯到古代。它是一种两人对弈的游戏,使用棋盘和棋子进行。棋盘通常是一个15×15的网格,棋子分为黑白两色,双方轮流在棋盘上落子。游戏的目标是通过在棋盘上落子,使自己的五个棋子在横向、纵向或斜向形成连续的线路,从而获胜。五子棋被认为是一种智力游戏,它要求玩家在竞技中思考对手的走法并制定自己的策略。由于规则简单、易于上手,五子棋在中国以及世界各地都很受欢迎,并且有许多不同的变种和玩法。笔者选择了一个最原始最简易的一个简易五子棋的程序,采用文本界面操作。源码获取:主要源代码展示`#检查水平方向count=0foriinrange(max(0,col-4),min(15,col+5)):ifboard[row][i]==player:count+=1ifcount==5:returnTrueelse:count=0#检查垂直方向count=0foriinrange(max(0,row-4),min(15,row+5)):ifboard[i][col]==player:count+=1ifcount==5:returnTrueelse:count=0#检查斜向(\)方向count=0foriinrange(-4,5):r=row+ic=col+iifr=15orc=15:continueifboard[r][c]==player:count+=1ifcount==5:returnTrueelse:count=0#检查斜向(/)方向count=0foriinrange(-4,5):r=row+ic=col-iifr=15orc=15:continueifboard[r][c]==player:count+=1ifcount==5:returnTrueelse:count=0returnFalseboard=[["."for_inrange(15)]for_inrange(15)]players=["X","O"]current_player=0print_board(board)whileTrue:print(f"Player{players[current_player]}'sturn:")try:row=int(input("Enterrow(0-14):"))col=int(input("Entercol(0-14):"))ifrow=15orcol=15orboard[row][col]!=".":raiseValueErrorexceptValueError:print("Invalidinput.Tryagain.")continueboard[row][col]=players[current_player]print_board(board)ifcheck_win(board,row,col,players[current_player]):print(f"Player{players[current_player]}wins!")breakcurrent_player=(current_player+1)%2123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475通过改进,新增了可使用鼠标交互的效果,相比较于原始的代码,大大提高了游戏体验性,并且使游戏界面更加易于操作,在游戏体验上大大增加也玩性。原始代码`importpygameimportsys游戏设置CELL_SIZE=40BOARD_SIZE=15WINDOW_WIDTH=CELL_SIZE*BOARD_SIZEWINDOW_HEIGHT=CELL_SIZE*BOARD_SIZELINE_COLOR=(0,0,0)BG_COLOR=(139,69,19)#棕色背景初始化游戏pygame.init()screen=pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))pygame.display.set_caption(“五子棋”)clock=pygame.time.Clock()board=[[’’for_inrange(BOARD_SIZE)]for_inrange(BOARD_SIZE)]current_player=‘X’game_over=Falsedefdraw_board():screen.fill(BG_COLOR)foriinrange(BOARD_SIZE):pygame.draw.line(screen,LINE_COLOR,(CELL_SIZE//2,CELL_SIZE//2+i*CELL_SIZE),(WINDOW_WIDTH-CELL_SIZE//2,CELL_SIZE//2+i*CELL_SIZE))pygame.draw.line(screen,LINE_COLOR,(CELL_SIZE//2+i*CELL_SIZE,CELL_SIZE//2),(CELL_SIZE//2+i*CELL_SIZE,WINDOW_HEIGHT-CELL_SIZE//2))defdraw_piece(row,col,player):x=col*CELL_SIZE+CELL_SIZE//2y=row*CELL_SIZE+CELL_SIZE//2ifplayer==‘X’:pygame.draw.circle(screen,(0,0,0),(x,y),CELL_SIZE//3)else:pygame.draw.circle(screen,(255,255,255),(x,y),CELL_SIZE//3)defcheck_win(row,col):directions=[(1,0),(0,1),(1,1),(1,-1)]fordindirections:count=1foriinrange(1,5):if0=5:returnTruereturnFalsedefdisplay_winner(player):font=pygame.font.Font(None,36)ifplayer==‘X’:text=font.render(“Blackwins!”,True,(255,0,0))else:text=font.render(“Whitewins!”,True,(255,0,0))text_rect=text.get_rect(center=(WINDOW_WIDTH//2,WINDOW_HEIGHT//2))screen.blit(text,text_rect)游戏循环whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()elifevent.type==pygame.MOUSEBUTTONDOWNandnotgame_over:x,y=event.poscol=x//CELL_SIZErow=y//CELL_SIZEif0=5:returnTruereturnFalsedefdisplay_winner(player):font=pygame.font.Font(None,36)ifplayer==‘X’:text=font.render(“Blackwins!”,True,(255,0,0))else:text=font.render(“Whitewins!”,True,(255,0,0))text_rect=text.get_rect(center=(WINDOW_WIDTH//2,WINDOW_HEIGHT//2))screen.blit(text,text_rect)游戏循环whileTrue:foreventinpygame.event.get():ifevent.type==pygame.QUIT:pygame.quit()sys.exit()elifevent.type==pygame.MOUSEBUTTONDOWNandnotgame_over:x,y=event.poscol=x//CELL_SIZErow=y//CELL_SIZEif0
|
|