为什么计算机会生成这么多随机位置?
Why does the computer generates so many random places?
我正在制作一个井字游戏,其中玩家与计算机对战。问题是,我编写了一个 rand() 定义,它仅在没有组合时调用,例如。玩家在 1x1 和 1x2 上没有 'X' 或者玩家在 1x3 和 1x2 上没有 'X'。然后计算机生成一个随机框,必须用 'O' 填充,但计算机会填充除 'X' 框之外的所有剩余 space。
我需要计算机只填充一个随机 space,然后将 player.turn 变为“播放器”。
请记住,我还没有完成 computer.defend().
这是图片:
这是我的代码:
import pygame,os,random #import
os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()
pygame.init() #initilization
###########creating screen#############
info = pygame.display.Info()
screen_width,screen_height = info.current_w,info.current_h
window_width,window_height = screen_width,screen_height-65
canvas = pygame.display.set_mode((window_width,window_height)) #setting the screen
##########loading images##############
bckgrnd = pygame.image.load("images/background 2.jfif") # background image
bckgrnd = pygame.transform.smoothscale(bckgrnd, (window_width,window_height)) #resizing background
xImage = pygame.image.load("images/x image.png") # loading x Image
oImage = pygame.image.load("images/o image.png")
#########others###################
places = {"1x1":" ","1x2":" ","1x3":" ","2x1":" ","2x2":" ","2x3":" ","3x1":" ","3x2":" ","3x3":" "}
turn = "player"
############# Fucntions ############
def arena():
####drawing lines####
pygame.draw.line(canvas, (0,0,0),(650,100),(650,750),10)
pygame.draw.line(canvas, (0,0,0),(900,100),(900,750),10)
pygame.draw.line(canvas, (0,0,0),(450,300),(1100,300),10)
pygame.draw.line(canvas, (0,0,0),(450,550),(1100,550),10)
####curving the edges of lines####
pygame.draw.circle(canvas, (0,0,0), (1103,301), 5)
pygame.draw.circle(canvas, (0,0,0), (450,301), 5)
pygame.draw.circle(canvas, (0,0,0), (450,551), 5)
pygame.draw.circle(canvas, (0,0,0), (1100,551), 5)
pygame.draw.circle(canvas, (0,0,0), (901,100), 5)
pygame.draw.circle(canvas, (0,0,0), (651,100), 5)
pygame.draw.circle(canvas, (0,0,0), (651,750), 5)
pygame.draw.circle(canvas, (0,0,0), (901,750), 5)
def checkX(): #drawing x
if places["1x1"] == "x":
canvas.blit(xImage, (450,100))
if places["1x2"] == "x":
canvas.blit(xImage, (690,100))
if places["1x3"] == "x":
canvas.blit(xImage, (920,100))
if places["2x1"] == "x":
canvas.blit(xImage, (450,340))
if places["2x2"] == "x":
canvas.blit(xImage,(690,340))
if places["2x3"] == "x":
canvas.blit(xImage, (920,340))
if places["3x1"] == "x":
canvas.blit(xImage, (450,570))
if places["3x2"] == "x":
canvas.blit(xImage, (690,570))
if places["3x3"] == "x":
canvas.blit(xImage,(920,570))
def checkO():
if places["1x1"] == "o":
canvas.blit(oImage, (450,100))
if places["1x2"] == "o":
canvas.blit(oImage, (690,100))
if places["1x3"] == "o":
canvas.blit(oImage, (920,100))
if places["2x1"] == "o":
canvas.blit(oImage, (450,340))
if places["2x2"] == "o":
canvas.blit(oImage,(690,340))
if places["2x3"] == "o":
canvas.blit(oImage, (920,340))
if places["3x1"] == "o":
canvas.blit(oImage, (450,570))
if places["3x2"] == "o":
canvas.blit(oImage, (690,570))
if places["3x3"] == "o":
canvas.blit(oImage,(920,570))
########classes###############
class players: # player class
def __init__(self,canvas,places,turn): # initialzing variables
self.canvas = canvas
self.places = places
self.turn = turn
def mouse(self,event): # mouse detection
self.event = event
#######mouse position#########
self.mouseX = pygame.mouse.get_pos()[0]
self.mouseY = pygame.mouse.get_pos()[1]
if self.event.type == pygame.MOUSEBUTTONDOWN:
######1st line detection#######
if self.mouseX < 650 and self.mouseX > 450:
if self.mouseY < 300 and self.mouseY > 100:
self.places["1x1"] = "x"
self.turn = "computer"
if self.mouseX < 900 and self.mouseX > 650:
if self.mouseY < 300 and self.mouseY > 100:
self.places["1x2"] = "x"
self.turn = "computer"
if self.mouseX < 1100 and self.mouseX > 900:
if self.mouseY < 300 and self.mouseY > 100:
self.places["1x3"] = "x"
self.turn = "computer"
#######2nd line detection########
if self.mouseX < 650 and self.mouseX > 450:
if self.mouseY < 550 and self.mouseY > 300:
self.places["2x1"] = "x"
self.turn = "computer"
if self.mouseX < 900 and self.mouseX > 650:
if self.mouseY < 550 and self.mouseY > 300:
self.places["2x2"] = "x"
self.turn = "computer"
if self.mouseX < 1100 and self.mouseX > 900:
if self.mouseY < 550 and self.mouseY > 300:
self.places["2x3"] = "x"
self.turn = "computer"
###########3rd line detection###########
if self.mouseX < 650 and self.mouseX > 450:
if self.mouseY < 900 and self.mouseY > 550:
self.places["3x1"] = "x"
self.turn = "computer"
if self.mouseX < 900 and self.mouseX > 650:
if self.mouseY < 900 and self.mouseY > 550:
self.places["3x2"] = "x"
self.turn = "computer"
if self.mouseX < 1100 and self.mouseX > 900:
if self.mouseY < 900 and self.mouseY > 550:
self.places["3x3"] = "x"
self.turn = "computer"
class comp:
def __init__(self,canvas,places):
self.canvas = canvas
self.places = places
def rand(self):
self.ran = random.choice(list(self.places.keys()))
if self.ran == "1x1":
player.turn = "player"
self.places["1x1"] = "o"
if self.ran == "1x2":
player.turn = "player"
self.places["1x2"] = "o"
if self.ran == "1x3":
player.turn = "player"
self.places["1x3"] = "o"
if self.ran == "2x1":
player.turn = "player"
self.places["2x1"] = "o"
if self.ran == "2x2":
player.turn = "player"
self.places["2x2"] = "o"
if self.ran == "2x3":
player.turn = "player"
self.places["2x3"] = "o"
if self.ran == "3x1":
player.turn = "player"
self.places["3x1"] = "o"
if self.ran == "3x2":
player.turn = "player"
self.places["3x2"] = "o"
if self.ran == "3x3":
player.turn = "player"
self.places["3x3"] = "o"
def defend(self):
if self.places["1x1"] == "x" and self.places["1x2"] == "x":
self.places["1x3"] = "o"
player.turn = "player"
if self.places["1x1"] == "x" and self.places["2x1"] == "x":
self.places["3x1"] = "o"
player.turn = "player"
if self.places["1x2"] == "x" and self.places["1x3"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
if self.places["3x1"] == "x" and self.places["2x1"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
if self.places["3x1"] == "x" and self.places["1x1"] == "x":
self.places["2x1"] = "o"
player.turn = "player"
if self.places["1x1"] == "x" and self.places["2x2"] == "x":
self.places["3x3"] = "o"
player.turn = "player"
if self.places["1x2"] == "x" and self.places["2x2"] == "x":
self.places["3x2"] = "o"
player.turn = "player"
else:
self.rand()
player.turn = "player"
player = players(canvas,places,turn)
computer = comp(canvas,places)
while True: #main loop
for event in pygame.event.get(): # event handler
if event.type == pygame.QUIT:
pygame.quit()
from sys import exit
exit()
canvas.blit(bckgrnd, (0,0)) # drawing background
arena()
if player.turn == "player":
player.mouse(event)
checkX() #draw X
if player.turn == "computer":
computer.defend()
checkO() #draw O
pygame.display.update()
求助,这件事困扰了一个月。
谢谢
您的代码中有两个问题:
- 您在每次执行循环时都重新发送鼠标事件,而不是在它实际发生时重新发送。
为了解决这个问题,我将鼠标事件移到了事件循环中:
while True: #main loop
for event in pygame.event.get(): # event handler
if event.type == pygame.QUIT:
pygame.quit()
from sys import exit
exit()
if player.turn == "player":
player.mouse(event)
canvas.blit(bckgrnd, (0,0)) # drawing background
arena()
checkX() #draw X
if player.turn == "computer":
player.turn = computer.defend()
checkO() #draw O
pygame.display.update()
- 您正在引用 class comp 内部的局部变量 player.turn,而不是引用玩家的变量 class。
为了解决这个问题,我 return 计算机对象的 turn 变量并将其分配给玩家对象的 turn 变量:
def defend(self):
if self.places["1x1"] == "x" and self.places["1x2"] == "x":
self.places["1x3"] = "o"
player.turn = "player"
elif self.places["1x1"] == "x" and self.places["2x1"] == "x":
self.places["3x1"] = "o"
player.turn = "player"
elif self.places["1x2"] == "x" and self.places["1x3"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
elif self.places["3x1"] == "x" and self.places["2x1"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
elif self.places["3x1"] == "x" and self.places["1x1"] == "x":
self.places["2x1"] = "o"
player.turn = "player"
elif self.places["1x1"] == "x" and self.places["2x2"] == "x":
self.places["3x3"] = "o"
player.turn = "player"
elif self.places["1x2"] == "x" and self.places["2x2"] == "x":
self.places["3x2"] = "o"
player.turn = "player"
else:
player.turn = "player"
self.rand()
return player.turn
# inside the main loop:
if player.turn == "computer":
player.turn = computer.defend()
我正在制作一个井字游戏,其中玩家与计算机对战。问题是,我编写了一个 rand() 定义,它仅在没有组合时调用,例如。玩家在 1x1 和 1x2 上没有 'X' 或者玩家在 1x3 和 1x2 上没有 'X'。然后计算机生成一个随机框,必须用 'O' 填充,但计算机会填充除 'X' 框之外的所有剩余 space。 我需要计算机只填充一个随机 space,然后将 player.turn 变为“播放器”。 请记住,我还没有完成 computer.defend().
这是图片:
import pygame,os,random #import
os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()
pygame.init() #initilization
###########creating screen#############
info = pygame.display.Info()
screen_width,screen_height = info.current_w,info.current_h
window_width,window_height = screen_width,screen_height-65
canvas = pygame.display.set_mode((window_width,window_height)) #setting the screen
##########loading images##############
bckgrnd = pygame.image.load("images/background 2.jfif") # background image
bckgrnd = pygame.transform.smoothscale(bckgrnd, (window_width,window_height)) #resizing background
xImage = pygame.image.load("images/x image.png") # loading x Image
oImage = pygame.image.load("images/o image.png")
#########others###################
places = {"1x1":" ","1x2":" ","1x3":" ","2x1":" ","2x2":" ","2x3":" ","3x1":" ","3x2":" ","3x3":" "}
turn = "player"
############# Fucntions ############
def arena():
####drawing lines####
pygame.draw.line(canvas, (0,0,0),(650,100),(650,750),10)
pygame.draw.line(canvas, (0,0,0),(900,100),(900,750),10)
pygame.draw.line(canvas, (0,0,0),(450,300),(1100,300),10)
pygame.draw.line(canvas, (0,0,0),(450,550),(1100,550),10)
####curving the edges of lines####
pygame.draw.circle(canvas, (0,0,0), (1103,301), 5)
pygame.draw.circle(canvas, (0,0,0), (450,301), 5)
pygame.draw.circle(canvas, (0,0,0), (450,551), 5)
pygame.draw.circle(canvas, (0,0,0), (1100,551), 5)
pygame.draw.circle(canvas, (0,0,0), (901,100), 5)
pygame.draw.circle(canvas, (0,0,0), (651,100), 5)
pygame.draw.circle(canvas, (0,0,0), (651,750), 5)
pygame.draw.circle(canvas, (0,0,0), (901,750), 5)
def checkX(): #drawing x
if places["1x1"] == "x":
canvas.blit(xImage, (450,100))
if places["1x2"] == "x":
canvas.blit(xImage, (690,100))
if places["1x3"] == "x":
canvas.blit(xImage, (920,100))
if places["2x1"] == "x":
canvas.blit(xImage, (450,340))
if places["2x2"] == "x":
canvas.blit(xImage,(690,340))
if places["2x3"] == "x":
canvas.blit(xImage, (920,340))
if places["3x1"] == "x":
canvas.blit(xImage, (450,570))
if places["3x2"] == "x":
canvas.blit(xImage, (690,570))
if places["3x3"] == "x":
canvas.blit(xImage,(920,570))
def checkO():
if places["1x1"] == "o":
canvas.blit(oImage, (450,100))
if places["1x2"] == "o":
canvas.blit(oImage, (690,100))
if places["1x3"] == "o":
canvas.blit(oImage, (920,100))
if places["2x1"] == "o":
canvas.blit(oImage, (450,340))
if places["2x2"] == "o":
canvas.blit(oImage,(690,340))
if places["2x3"] == "o":
canvas.blit(oImage, (920,340))
if places["3x1"] == "o":
canvas.blit(oImage, (450,570))
if places["3x2"] == "o":
canvas.blit(oImage, (690,570))
if places["3x3"] == "o":
canvas.blit(oImage,(920,570))
########classes###############
class players: # player class
def __init__(self,canvas,places,turn): # initialzing variables
self.canvas = canvas
self.places = places
self.turn = turn
def mouse(self,event): # mouse detection
self.event = event
#######mouse position#########
self.mouseX = pygame.mouse.get_pos()[0]
self.mouseY = pygame.mouse.get_pos()[1]
if self.event.type == pygame.MOUSEBUTTONDOWN:
######1st line detection#######
if self.mouseX < 650 and self.mouseX > 450:
if self.mouseY < 300 and self.mouseY > 100:
self.places["1x1"] = "x"
self.turn = "computer"
if self.mouseX < 900 and self.mouseX > 650:
if self.mouseY < 300 and self.mouseY > 100:
self.places["1x2"] = "x"
self.turn = "computer"
if self.mouseX < 1100 and self.mouseX > 900:
if self.mouseY < 300 and self.mouseY > 100:
self.places["1x3"] = "x"
self.turn = "computer"
#######2nd line detection########
if self.mouseX < 650 and self.mouseX > 450:
if self.mouseY < 550 and self.mouseY > 300:
self.places["2x1"] = "x"
self.turn = "computer"
if self.mouseX < 900 and self.mouseX > 650:
if self.mouseY < 550 and self.mouseY > 300:
self.places["2x2"] = "x"
self.turn = "computer"
if self.mouseX < 1100 and self.mouseX > 900:
if self.mouseY < 550 and self.mouseY > 300:
self.places["2x3"] = "x"
self.turn = "computer"
###########3rd line detection###########
if self.mouseX < 650 and self.mouseX > 450:
if self.mouseY < 900 and self.mouseY > 550:
self.places["3x1"] = "x"
self.turn = "computer"
if self.mouseX < 900 and self.mouseX > 650:
if self.mouseY < 900 and self.mouseY > 550:
self.places["3x2"] = "x"
self.turn = "computer"
if self.mouseX < 1100 and self.mouseX > 900:
if self.mouseY < 900 and self.mouseY > 550:
self.places["3x3"] = "x"
self.turn = "computer"
class comp:
def __init__(self,canvas,places):
self.canvas = canvas
self.places = places
def rand(self):
self.ran = random.choice(list(self.places.keys()))
if self.ran == "1x1":
player.turn = "player"
self.places["1x1"] = "o"
if self.ran == "1x2":
player.turn = "player"
self.places["1x2"] = "o"
if self.ran == "1x3":
player.turn = "player"
self.places["1x3"] = "o"
if self.ran == "2x1":
player.turn = "player"
self.places["2x1"] = "o"
if self.ran == "2x2":
player.turn = "player"
self.places["2x2"] = "o"
if self.ran == "2x3":
player.turn = "player"
self.places["2x3"] = "o"
if self.ran == "3x1":
player.turn = "player"
self.places["3x1"] = "o"
if self.ran == "3x2":
player.turn = "player"
self.places["3x2"] = "o"
if self.ran == "3x3":
player.turn = "player"
self.places["3x3"] = "o"
def defend(self):
if self.places["1x1"] == "x" and self.places["1x2"] == "x":
self.places["1x3"] = "o"
player.turn = "player"
if self.places["1x1"] == "x" and self.places["2x1"] == "x":
self.places["3x1"] = "o"
player.turn = "player"
if self.places["1x2"] == "x" and self.places["1x3"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
if self.places["3x1"] == "x" and self.places["2x1"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
if self.places["3x1"] == "x" and self.places["1x1"] == "x":
self.places["2x1"] = "o"
player.turn = "player"
if self.places["1x1"] == "x" and self.places["2x2"] == "x":
self.places["3x3"] = "o"
player.turn = "player"
if self.places["1x2"] == "x" and self.places["2x2"] == "x":
self.places["3x2"] = "o"
player.turn = "player"
else:
self.rand()
player.turn = "player"
player = players(canvas,places,turn)
computer = comp(canvas,places)
while True: #main loop
for event in pygame.event.get(): # event handler
if event.type == pygame.QUIT:
pygame.quit()
from sys import exit
exit()
canvas.blit(bckgrnd, (0,0)) # drawing background
arena()
if player.turn == "player":
player.mouse(event)
checkX() #draw X
if player.turn == "computer":
computer.defend()
checkO() #draw O
pygame.display.update()
求助,这件事困扰了一个月。 谢谢
您的代码中有两个问题:
- 您在每次执行循环时都重新发送鼠标事件,而不是在它实际发生时重新发送。
为了解决这个问题,我将鼠标事件移到了事件循环中:
while True: #main loop
for event in pygame.event.get(): # event handler
if event.type == pygame.QUIT:
pygame.quit()
from sys import exit
exit()
if player.turn == "player":
player.mouse(event)
canvas.blit(bckgrnd, (0,0)) # drawing background
arena()
checkX() #draw X
if player.turn == "computer":
player.turn = computer.defend()
checkO() #draw O
pygame.display.update()
- 您正在引用 class comp 内部的局部变量 player.turn,而不是引用玩家的变量 class。
为了解决这个问题,我 return 计算机对象的 turn 变量并将其分配给玩家对象的 turn 变量:
def defend(self):
if self.places["1x1"] == "x" and self.places["1x2"] == "x":
self.places["1x3"] = "o"
player.turn = "player"
elif self.places["1x1"] == "x" and self.places["2x1"] == "x":
self.places["3x1"] = "o"
player.turn = "player"
elif self.places["1x2"] == "x" and self.places["1x3"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
elif self.places["3x1"] == "x" and self.places["2x1"] == "x":
self.places["1x1"] = "o"
player.turn = "player"
elif self.places["3x1"] == "x" and self.places["1x1"] == "x":
self.places["2x1"] = "o"
player.turn = "player"
elif self.places["1x1"] == "x" and self.places["2x2"] == "x":
self.places["3x3"] = "o"
player.turn = "player"
elif self.places["1x2"] == "x" and self.places["2x2"] == "x":
self.places["3x2"] = "o"
player.turn = "player"
else:
player.turn = "player"
self.rand()
return player.turn
# inside the main loop:
if player.turn == "computer":
player.turn = computer.defend()