到目前为止,我有一个程序,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()
将数据和数据表示形式分开。就是这样。现在你只是在画东西,而不是你应该生成一些竞争环境的表示(例如,框及其状态的列表,如 p1 选中、p2 选中或未选中),然后在需要时使用它来绘制。优势应该是显而易见的 - 如果你知道游戏的状态,确定是否有赢家(以及它是谁)是微不足道的。
在 3 回合(获胜的最小回合数)后,检查您的 2D 数组旁边是否有通过添加/减去一个播放的标记旁边是否有标记,如果发现,则对数组索引重复相同的操作,否则会爆发。
如果达到第二个控制结构,则打破并宣布获胜者。
在游戏中的每一次移动中,都应使用 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
或者,您可以将 movement 用作函数,并让它递归调用自身,直到输入有效:
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 次检查。