我使用Python 3创建了一个Noughts和Crosses的多人游戏。 除了检查循环之外,我已经完成了所有代码。
每次输入新符号时,如果是获胜的移动或者电路板已满,则检查循环将被输入。 目前,我的代码在一个玩家连续输入3个符号或者电路板变满后才能完成。
到目前为止,这是我的代码:
import random
def start_player():
startplayer = random.randint(1,2)
if startplayer == 1:
turn = 'X'
print("Player One (X) will start the game.")
else:
startplayer == 2
turn = 'O'
print("Player Two (O) will start the game.")
return turn
def getmove():
row = int(input("Please enter a number between 0 and 2: "))
column = int(input("Please enter a number between 0 and 2: "))
while grid[row][column] != "":
print("Invalid move.")
row = int(input("Please enter a number between 0 and 2: "))
column = int(input("Please enter a number between 0 and 2: "))
return row, column
def mainturn(row, column):
global countmove
countmove = countmove + 1
global symbol
grid[row][column] = symbol
for y in range(0,(len(grid))):
print(grid[y])
if symbol == 'X':
symbol = 'O'
elif symbol == 'O':
symbol = 'X'
return countmove
def check_win(row, column, symbol):
if (grid[0][0] and grid[0][1] and grid[0][2] == symbol) or (grid[1][0] and grid[1][1] and grid[1][2] == symbol) or (grid[2][0] and grid[2][1] and grid[2][2] == symbol) or (grid[0][0] and grid[1][0] and grid[2][0] == symbol) or (grid[0][1] and grid[1][1] and grid[2][1] == symbol)or (grid[0][2] and grid[1][2] and grid[2][2] == symbol)or (grid[0][0] and grid[1][1] and grid[2][2] == symbol) or (grid[2][0] and grid[1][1] and grid[0][2] == symbol):
print("Well done!",symbol," won the game.")
return true
elif countmove == 9:
print("Board Full. Game over.")
#main program
grid = [["","",""],["","",""],["","",""]]
countmove = 0
win = 'false'
for y in range(0,(len(grid))):
print(grid[y])
symbol = start_player()
while countmove != 9 or win == 'false':
countmove = 0
row, column = getmove()
mainturn(row,column)
win = check_win(row,column, symbol)
答案 0 :(得分:0)
这是因为两件事。首先,您要在循环中重置AttributeError: module 'pkg_resources' has no attribute 'declare_namespace'
值,因此它始终为零并进入无限循环。其次,在检查是否为胜利之前,您正在更改count_move
,symbol
例程正在检查下一个符号而不是播放器的当前符号。这是工作代码:
check_win
子程序change_symbol在这里:
while countmove != 9 or win == 'false':
row, column = getmove()
mainturn(row,column)
win = check_win(row,column, symbol)
symbol = change_symbol(symbol)
作为良好编码实践的一部分,避免在子程序中使用全局变量,这只会加剧混乱。
答案 1 :(得分:0)
我尽力评论为什么我做了事以及为什么要删除东西。我希望这个例子可以帮助你了解为什么在使用python时使用类不能更好。我添加了一些其他功能,我认为可以帮助您继续学习编程
import random
# Lets put all these functions into a class
class Game:
# Lets set up the game
def __init__(self, player_one="X", player_two="O"):
self.player_one = player_one
self.player_two = player_two
# Where the game board is stored
# Using periods instead of spaces so the players can see the possible moves
self.game_board = [
[".", ".", "."],
[".", ".", "."],
[".", ".", "."]
]
# This value is to check if the game is still going
self.running = True
# Whos turn is it?
self.active_player = ""
# The tasks we HAVE to do to make the game work
self.start_player()
self.run_game()
# The function is part of the Game class so we have to pass self into it.
def start_player(self):
# Randomly Choose a starting player
startplayer = random.randint(1,2)
if startplayer == 1:
# We declared the string values in the __init__ function
player = self.player_one
print("Player One ({}) will start the game.".format(player))
else:
startplayer == 2
player = self.player_two
print("Player Two ({}) will start the game.".format(player))
# Set the initial player
self.active_player = player
def get_move(self):
# Seems silly to have them enter the rows and columns one by one
#row = int(input("Please enter a number between 0 and 2: "))
#column = int(input("Please enter a number between 0 and 2: "))
# Show the player whos turn it is
input_data = input("Player ({}) please choose a Column and a Row: ".format(self.active_player))
# Default values that aren't in the game, if they arent changed they will be caught
row = -1
column = -1
# Users entry all types of funky data, lets make sure its right
try:
r, c = input_data.split(" ")
r = int(r)
c = int(c)
if r >= 0 and r <= 3:
row = int(r)
if c >= 0 and c <= 3:
column = int(c)
except:
print("Enter only two numbers (0, 1, or 2) seperated by a space")
return row, column
# This check for the grid should be its own function
#while grid[row][column] != "":
# print("Invalid move.")
# row = int(input("Please enter a number between 0 and 2: "))
# column = int(input("Please enter a number between 0 and 2: "))
def check_move(self, row, column):
if row == -1 or column == -1:
return False
# If the space is blank return True
if self.game_board[row][column] == ".":
return True
print("{} {} is an invalid move, try again".format(row, column))
return False
# Add another function to print out the board for us
def show_board(self):
for row in self.game_board:
row_string = ""
for cell in row:
row_string = "{} {} ".format(row_string, cell)
print(row_string)
#def mainturn(row, column):
# Try to avoid using globals. We'll store these in our class
#global countmove
#countmove = countmove + 1
#global symbol
#grid[row][column] = symbol
#for y in range(0,(len(grid))):
# print(grid[y])
#if symbol == 'X':
# symbol = 'O'
#elif symbol == 'O':
# symbol = 'X'
#return countmove
# This is one heck of an if statement. Lets turn it into a function
# if (grid[0][0] and grid[0][1] and grid[0][2] == symbol) or (grid[1][0] and grid[1][1] and grid[1][2] == symbol) or (grid[2][0] and grid[2][1] and grid[2][2] == symbol) or (grid[0][0] and grid[1][0] and grid[2][0] == symbol) or (grid[0][1] and grid[1][1] and grid[2][1] == symbol)or (grid[0][2] and grid[1][2] and grid[2][2] == symbol)or (grid[0][0] and grid[1][1] and grid[2][2] == symbol) or (grid[2][0] and grid[1][1] and grid[0][2] == symbol):
def check_win(self, symbol):
combinations = [
# horizontal
[(0,0), (1,0), (2,0)],
[(0,1), (1,1), (2,1)],
[(0,2), (1,2), (2,2)],
# vertical
[(0,0), (0,1), (0,2)],
[(1,0), (1,1), (1,2)],
[(2,0), (2,1), (2,2)],
# crossed
[(0,0), (1,1), (2,2)],
[(2,0), (1,1), (0,2)]
]
for coordinates in combinations:
letters = [self.game_board[x][y] for x, y in coordinates]
# If all the letters match
if "." not in letters:
if len(set(letters)) <= 1:
# returns corresponding letter for winner (X/O)
print("Well done {}! You won the game!".format(symbol))
self.running = False
return True
return False
# Lets try another method of checking if the board is full
#elif countmove == 9:
# print("Board Full. Game over.")
#main program
def board_full(self):
for row in self.game_board:
if "." in row:
return False
print("The game is a draw :( ")
# Stop the game
self.running = False
return True
def run_game(self):
# While the game is not over
while self.running != False:
# Show the player the board
self.show_board()
row, column = self.get_move()
# Is the move valid?
if self.check_move(row, column):
self.game_board[row][column] = self.active_player
# Did they win?
self.check_win(self.active_player)
# Change Players
if self.active_player == self.player_one:
self.active_player = self.player_two
else:
self.active_player = self.player_one
# Print the winning game board
self.show_board()
g = Game("X", "O")
# Handled this in the class
#grid = [["","",""],["","",""],["","",""]]
#countmove = 0
#win = 'false'
# Turned this code into the show_board function
#for y in range(0,(len(grid))):
#print(grid[y])
#symbol = start_player()
#while countmove != 9 or win == 'false':
# Shouldnt reset the countmove inside of the loop thats checking the countmove
#countmove = 0
#row, column = getmove()
#mainturn(row,column)
#win = check_win(row,column, symbol)
示例输出:
Player One (X) will start the game.
. . .
. . .
. . .
Player (X) please choose a Column and a Row: 0 1
. X .
. . .
. . .
Player (O) please choose a Column and a Row: 2 2
. X .
. . .
. . O
Player (X) please choose a Column and a Row: 0 0
X X .
. . .
. . O
Player (O) please choose a Column and a Row: 2 1
X X .
. . .
. O O
Player (X) please choose a Column and a Row: 0 2
Well done X! You won the game!
X X X
. . .
. O O