到目前为止,我有一个程序,其中2个玩家可以点击以轮流放置X和O.我不确定如何让该计划认可赢家/平局。如果你们能帮我制作一个能以任何方式在屏幕上显示胜利/平局的功能,我会永远爱你。感谢。
from graphics import *
import sys
def player_o(win, center):
'''
Parameters:
- win: the window
'''
outline_width = 5
circle = Circle(center, boxsize/2)
circle.setOutline('red')
circle.setWidth(outline_width)
circle.draw(win)
def player_x(win, p1x, p1y):
'''
Parameters:
- win: the window
'''
for i in range(2):
deltaX = (-1) ** i * (boxsize / 2)
deltaY = (boxsize / 2)
line = Line(Point(p1x - deltaX, p1y - deltaY),
Point(p1x + deltaX, p1y + deltaY))
line.setFill('red')
line.setWidth(5)
line.draw(win)
def game():
global win
global boxsize
try:
winsize = int(input("How large would you like the window? (Between 100 and 3000): "))
if winsize < 100 or winsize > 3000:
print("Invalid window size")
quit()
squares = int(input("How many squares per row? (Between 3 and 10):"))
boxsize = winsize/ squares
if squares < 3 or squares > winsize / 10:
print("Invalid number")
quit()
except ValueError:
sys.exit("Not a valid number")
win = GraphWin("Tic Tac Toe", winsize, winsize)
for i in range(squares - 1):
hline = Line(Point(0, (winsize/squares) * (i + 1)), Point(winsize, (winsize/squares) * (i + 1)))
hline.draw(win)
vline = Line(Point((winsize/squares) * (i + 1), 0), Point((winsize/squares) * (i + 1), winsize))
vline.draw(win)
for i in range((squares ** 2) // 2):
print("X, click a square.")
p1mouse = win.getMouse()
p1x = p1mouse.getX()
p1y = p1mouse.getY()
player_x(win, p1x, p1y)
print("O, click a square.")
p2mouse = win.getMouse()
p2x = p2mouse.getX()
p2y = p2mouse.getY()
player_o(win, Point(p2x, p2y))
if squares % 2 == 1:
print("X, click a square.")
p1mouse = win.getMouse()
p1x = p1mouse.getX()
ply = p1mouse.getY()
player_x(win, p1x, p1y)
game()
答案 0 :(得分:6)
保持数据和数据表示的分离。这就是如何。现在你只是绘制东西,而不是你应该生成一些游戏场的表示(例如,盒子及其状态的列表,如p1检查,p2检查,或未选中),然后在需要时使用它来绘制。优势应该是显而易见的 - 如果你知道游戏的状态,确定是否有赢家(以及它是谁)是微不足道的。
答案 1 :(得分:0)
在3转(最小转弯到赢)之后检查你的2d阵列,如果在最后一次播放旁边有一个令牌,加上/减去一个,如果发现重复相同的操作到数组索引,则爆发。
如果达到第二控制结构,则宣布获胜者。
答案 2 :(得分:0)
随着游戏中的每次移动,应使用2D数组或字典(值为列表)。然后,您可以检查每种获胜方式。通过这种方式,您还可以检查移动是否有效---是否采用了板上的点。
我还建议使用数字或坐标系来指示运动。
董事会看起来像这样:
1 2 3
4 5 6
7 8 9
这些数字是主板上的相应位置。
例如:
在初始化中:
moves = 0
positions = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9':0}
# '1' - '9' are the spots on the board.
# 0 means the spot is empty, 'X' means the spot is taken by 'X', 'O' means the spot is taken by 'O'. You can use any other naming system, but this is simple.
在移动代码中:
while 1 == 1: # just a loop until the input is valid. See the 'break' statement below
new_move = input("X, enter what space to move to: ")
if positions[new_move] == 0: # if that board spot is empty
moves += 1 #moves = moves + 1
positions[new_move] == 'X' # board spot is now occupied by 'X'
# code to show the piece on the board
if moves >= 5: # least possible moves to win is 5
win_check(positions)
break
或者,您可以将移动用作函数,并以递归方式调用自身,直到输入有效:
def move_X():
new_move = input("X, enter what space to move to: ")
if positions[new_move] == 0: # if that board spot is empty
moves += 1 #moves = moves + 1
positions[new_move] == 'X' # board spot is now occupied by 'X'
# code to show the piece on the board
if moves >= 5: # least possible moves to win is 5
win_check(positions)
move_O() # this should be defined similarly to 'move_X' except that it would correlate to 'O'.
else:
move_X()
胜利检查方法:
def win_check(positions):
if positions['1'] == 'X' and positions['2'] == 'X' and positions['3'] == 'X':
return "Winner: X"
elif # similar things, checking all of the other ways to win.
你需要1 if
个陈述(在开头)和15个elif
陈述,因为有8种方法可以为每个玩家赢得胜利,因此必须进行16次检查。