我正在进行记忆匹配的基本游戏。想法是通过用户输入可以用卡创建板。这些牌都在棋盘上的某个地方有比赛。基本上输入是电路板的尺寸,然后协调以识别可能的匹配。但是每次我输入输入即使有效,代码似乎也会推迟无效输入。但是,当我输入两个相同的坐标时,我的代码部分将针对该情况执行。我不确定这里发生了什么。
import random
class Card():
"""Card object """
def __init__(self, val):
self.val = val
self.face = False
def isFaceUp(self):
"""check to see if the cards face up"""
return self.face
def getVal(self):
return self.val
def makeFaceUp(self):
self.face = True
def __str__(self):
return ", ".join(("Value: ", str(self.val), "Face: ", str(self.face)))
class Deck():
def __init__(self, pairs):
self._pairs = pairs
self._cards = []
for cards in range(self._pairs):
card1 = Card(cards + 1)
self._cards.append(card1)
card2 = Card(cards+1)
self._cards.append(card2)
def deal(self):
if len(self) ==0:
return None
else:
return self._cards.pop(0)
def shuffle(self):
random.shuffle(self._cards)
def __len__(self):
return len(self._cards)
class Game():
def __init__(self, rows, columns):
self._deck = Deck((rows*columns)//2)
self._rows = rows
self._columns = columns
self._board = []
for row in range(self._rows):
self._board.append([0] * self._columns)
self.populateBoard()
def populateBoard(self):
"""Puts all cards in the board random"""
self._deck.shuffle()
for columns in range(self._columns):
for rows in range(self._rows):
self._board[rows][columns] = self._deck.deal()
def revealBoard(self):
"""checks the values on the board making sure theres pairs"""
for rows in range(self._rows):
for columns in range(self._columns):
print(str(self._board[rows][columns].getVal()) + \
" ", end="")
print("")
def displayGame(self):
"""Displays the game in a 2d list"""
for rows in range(self._rows):
for columns in range(self._columns):
if self._board[rows][columns].isFaceUp() == False:
print("*", end = "")
else:
print(str(self._board[rows][columns].getVal() + \
" ", end = ""))
print("")
def play(self):
"""Allows the game to play setting the cards into a double list """
while not self.isGameOver():
self.displayGame()
c1 = input("Enter coordinates, (row, column) of card: ")
c2 = input("Enter the coordinates of match: ")
newC1 = list(map(int, c1.split()))
newCard1 = self._board[(newC1[0])-1][(newC1[1])-1] #Get value here???
newC2 = list(map(int, c2.split()))
newCard2 = self._board[(newC2[0])-1][(newC2[1])-1]
try:
if newCard1 != newCard2:
print("Not a pair", "Found: ",newCard1, "at", "(" + newC1, ", ", newC2, ")")
elif newC1 == newC2:
print("Identical Coordinate Entery")
elif newCard1.getVal() == newCard2.getVal():
self._board[newC1[0]-1][newC1[1]-1].makeFaceUp()
self._board[newC2[0]-1][newC2[1]-1].makeFaceUp()
print("pair found")
except:
print("invalid input")
print("Game Over")
self.displayGame
def isGameOver(self):
"""Test to determine if all the cards are facing up and revelied the game will be over"""
for rows in range(self._rows):
if not all(card.isFaceUp() for card in self._board[rows]):
return False
return True
def main():
while True:
# Force user to enter valid value for number of rows
while True:
rows = input("Enter number of rows ")
if rows.isdigit() and ( 1 <= int(rows) <= 9):
rows = int(rows)
break
else:
print (" ***Number of rows must be between 1 and 9! Try again.***")
# Adding *** and indenting error message makes it easier for the user to see
# Force user to enter valid value for number of columns
while True:
columns = input("Enter number of columns ")
if columns.isdigit() and ( 1 <= int(columns) <= 9):
columns = int(columns)
break
else:
print (" ***Number of columns must be between 1 and 9! Try again.***")
if rows * columns % 2 == 0:
break
else:
print (" ***The value of rows X columns must be even. Try again.***")
game = Game(rows, columns)
game.play()
if __name__ == "__main__":
main()
任何帮助都表示赞赏,如果有人需要查看我的代码的其他部分,只要问我是否愿意将它们放在那里,如果它有助于发现此错误。
编辑:继续前进并添加整个代码进行测试。
答案 0 :(得分:0)
好的,从我可以推断的内容来看,错误是Game.play()
中的非常广泛的错误处理。它会检查任何错误,但是当您删除该语句时,您可能会发现您正在尝试将字符串添加到字符串中。要解决此问题,请添加:
newC1 = "(" + ", ".join (list (map (str, newC1))) + ")"
为newC2做同样的事情。这只是将列表更改为看起来像坐标的字符串。