西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
西西首頁(yè) 電腦軟件 安卓軟件 電腦游戲 安卓游戲 排行榜 專題合集

人機(jī)對(duì)戰(zhàn)井字棋游戲

Python版
  • 人機(jī)對(duì)戰(zhàn)井字棋游戲Python版
  • 軟件大小:1KB
  • 更新時(shí)間:2020-12-31 16:51
  • 軟件語(yǔ)言:中文
  • 軟件廠商:
  • 軟件類(lèi)別:國(guó)產(chǎn)軟件 / 免費(fèi)軟件 / 源碼相關(guān)
  • 軟件等級(jí):3級(jí)
  • 應(yīng)用平臺(tái):WinXP, Win7, win8
  • 官方網(wǎng)站:暫無(wú)
  • 應(yīng)用備案:
好評(píng):50%
壞評(píng):50%

本類(lèi)精品

軟件介紹

人機(jī)對(duì)戰(zhàn)井字棋游戲,這是由Python語(yǔ)言編寫(xiě)的一款井字棋游戲,下面給出了詳細(xì)的代碼說(shuō)明,感興趣的朋友們可以下載參考。人機(jī)對(duì)戰(zhàn)井字棋游戲直接在Python語(yǔ)言編輯器生成即可獲得游戲本體,可以嘗試與AI進(jìn)行互動(dòng)。

人機(jī)對(duì)戰(zhàn)井字棋游戲

人機(jī)對(duì)戰(zhàn)井字棋游戲代碼說(shuō)明

用pygame實(shí)現(xiàn)交互,程序比較簡(jiǎn)陋,有不足之處歡迎大家批評(píng)指正

AI的移動(dòng)思想

逐個(gè)遍歷每個(gè)空的格子,

如果某個(gè)格子落子后AI能贏就下,

如果對(duì)方能贏AI就堵住。

人機(jī)對(duì)戰(zhàn)井字棋游戲代碼一覽

__author__ = 'lthero'

import pygame

from pygame import *

import random as ra

pygame.init()

white = (255, 255, 255)

black = (0, 0, 0)

size = width, height = 600, 600

screen = pygame.display.set_mode(size)

points = [[0, 0, 0],

          [0, 0, 0],

          [0, 0, 0]]

x = 0

y = 0

flag = 1

lst = []

lst_mine = []

lst_android = []

count = 0

text = pygame.font.SysFont('宋體', 50)

Play_score = 0

AI_score = 0

def draw_restart():

    steps = [(400, 450), (400, 500), (550, 500), (550, 450)]

    pygame.draw.polygon(screen, black, steps, 1)

    text_x = text.render("AGAIN?", 1, black)

    screen.blit(text_x, (410, 460))

def draw_img(player, x, y):

    # 玩家

    if player == 1:

        pygame.draw.circle(screen, black, (x, y), 40, 1)

    # 機(jī)器

    else:

        pygame.draw.rect(screen, black, ((x - 20, y - 20), (50, 50)), 1)

def draw_score():

    text_1 = pygame.font.SysFont('宋體', 30)

    text_player_score = text_1.render('PLAYER SCORE ' + str(Play_score), 1, black)

    text_AI_score = text_1.render('AI SCORE     ' + str(AI_score), 1, black)

    screen.blit(text_player_score, (410, 10))

    screen.blit(text_AI_score, (410, 40))

def draw_back():

    screen.fill(white)

    steps = [(100, 100), (100, 400), (400, 400), (400, 100)]

    pygame.draw.polygon(screen, black, steps, 1)

    pygame.draw.lines(screen, black, False, [(100, 200), (400, 200)])

    pygame.draw.lines(screen, black, False, [(100, 300), (400, 300)])

    pygame.draw.lines(screen, black, False, [(200, 100), (200, 400)])

    pygame.draw.lines(screen, black, False, [(300, 100), (300, 400)])

def check_win(tab):

    return ((points[0][0] == tab and points[0][1] == tab and points[0][2] == tab) or

            (points[1][0] == tab and points[1][1] == tab and points[1][2] == tab) or

            (points[2][0] == tab and points[2][1] == tab and points[2][2] == tab) or

            (points[0][0] == tab and points[1][0] == tab and points[2][0] == tab) or

            (points[0][1] == tab and points[1][1] == tab and points[2][1] == tab) or

            (points[0][2] == tab and points[1][2] == tab and points[2][2] == tab) or

            (points[0][0] == tab and points[1][1] == tab and points[2][2] == tab) or

            (points[0][2] == tab and points[1][1] == tab and points[2][0] == tab)

            )

def winner():

    # AI

    if check_win(100):

        return 100

    elif check_win(1):

        return -100

def is_full():

    fl = 0

    for i in range(3):

        for j in range(3):

            if points[i][j] != 0:

                fl += 1

    return fl

def AI_move():

    # 一步能贏

    for i in range(3):

        for j in range(3):

            if points[i][j] == 0:

                points[i][j] = 100

                if check_win(100):

                    return (i, j)

                else:

                    points[i][j] = 0

    # 堵上

    for i in range(3):

        for j in range(3):

            if points[i][j] == 0:

                points[i][j] = 1

                if check_win(1):

                    return (i, j)

                else:

                    points[i][j] = 0

    # 占中間

    if points[1][1] == 0:

        return (1, 1)

    # 占四角

    temp = []

    for i in (0, 2):

        for j in (0, 2):

            if points[i][j] == 0:

                temp.append((i, j))

    if len(temp) != 0:

        return ra.choice(temp)

    # 占四邊

    for i in ((0, 1), (1, 0), (1, 2), (2, 1)):

        if points[i[0]][i[1]] == 0:

            temp.append((i[0], i[1]))

    if len(temp) != 0:

        return ra.choice(temp)

def draw_all():

    draw_back()

    draw_score()

    for i in lst:

        draw_img(i[0], i[1], i[2])

    if flag == 100:

        text_conent = text.render("AI win", 1, black)

        screen.blit(text_conent, (220, 50))

    elif flag == -100:

        text_conent = text.render("You win", 1, black)

        screen.blit(text_conent, (220, 50))

    elif flag == 123:

        text_conent = text.render("TIE", 1, black)

        screen.blit(text_conent, (220, 50))

    if flag == 123 or flag == 100 or flag == -100:

        draw_restart()

def play():

    global flag, AI_score, Play_score

    while True:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                exit()

            if event.type == MOUSEBUTTONDOWN:

                x, y = pygame.mouse.get_pos()

                if 400 < x < 550 and 450 < y < 500:

                    lst.clear()

                    for i in range(3):

                        for j in range(3):

                            points[i][j] = 0

                    flag = 1

                if 100 <= x <= 400 and 100 <= y <= 400:

                    x = (x - 100) // 100

                    y = (y - 100) // 100

                    l_x = x * 100 + 150

                    l_y = y * 100 + 150

                    # player

                    if flag == 1:

                        if is_full() != 9:

                            if points[x][y] == 0:

                                points[x][y] = 1

                                lst.append((1, l_x, l_y))

                                if winner() == -100:

                                    flag = -100

                                    Play_score += 1

                                    print('player win')

                                else:

                                    flag = -1

                        else:

                            flag = 123

            if flag == -1:

                if is_full() != 9:

                    # 人機(jī)動(dòng)

                    xx, yy = AI_move()

                    l_x = xx * 100 + 150

                    l_y = yy * 100 + 150

                    points[xx][yy] = 100

                    lst.append((2, l_x, l_y))

                    if winner() == 100:

                        flag = 100

                        AI_score += 1

                        print('AI win')

                    else:

                        flag = 1

                else:

                    flag = 123

        draw_all()

        pygame.display.flip()

if __name__ == '__main__':

    play()

其他版本下載

發(fā)表評(píng)論

昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
查看所有(0)條評(píng)論 > 字?jǐn)?shù): 0/500

TOP
軟件下載