命令行时代的井字棋python_井字棋.py · _Shadow〆扯淡/python – Gitee.com

– coding: cp936 –

import pygame,sys,random

from random import randint

import tkMessageBox

from pygame.locals import * #模块引入

pygame.init()

width = 480

height = 480 #对窗口大小进行设置

screen=pygame.display.set_mode((width, height))

black, red, blue, white = (0,0,0), (255,0,0), (0,0,255), (255,255,255)

empty = 0

xxx = -1

ooo = 1

computer = xxx

def draw_game():

pygame.draw.rect(screen, white, (0, 0, width, height))

pygame.draw.line(screen, black, (160, 0), (160, 480), 5)

pygame.draw.line(screen, black, (320, 0), (320, 480), 5)

pygame.draw.line(screen, black, (0, 160), (480, 160), 5)

pygame.draw.line(screen, black, (0, 320), (480, 320), 5)

for row, line in enumerate(state): #row, line => 0,[0,0,0] 1,[0,0,0] 2,[0,0,0]

for col, val in enumerate(line): #col, val => 0,0 1,0 2,0

if val == xxx:

画一个×

upper_left = (col * 160 + 5, row * 160 + 5)

lower_right = (col * 160 + 155, row * 160 + 155)

pygame.draw.line(screen, red, upper_left, lower_right, 5)

upper_right = (col * 160 + 155, row * 160 + 5)

lower_left = (col * 160 + 5, row * 160 + 155)

pygame.draw.line(screen, red, upper_right, lower_left, 5)

elif val == ooo:

画圈

rect = (col * 160 + 5, row * 160 + 5, 150, 150)

pygame.draw.ellipse(screen, blue, rect, 5)

else:

assert val == empty

continue

pygame.display.flip()

def dian(player):

if all(val == empty for line in state for val in line):

row = random.randrange(3)

col = random.randrange(3)

result = (row, col)

else:

result = best_move(player)

if state[row][col] == 0:

state[row][col] = 1

break

return result

draw_game()

pygame.display.flip()

def best_move(player):

best = None

for row, line in enumerate(state):

for col, val in enumerate(line):

if val == empty:#判断是不是空位

state[row][col] = player

if panduan() == player: #从这

state[row][col] = empty

return (ooo, (row, col))

counter = best_move(player * -1)

if best is None or best[0]

Original: https://blog.csdn.net/weixin_29195217/article/details/113711748
Author: 达布斯
Title: 命令行时代的井字棋python_井字棋.py · _Shadow〆扯淡/python – Gitee.com

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783021/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球