【问题标题】:Card Matching Game on PythonPython上的卡片匹配游戏
【发布时间】:2013-01-31 22:29:29
【问题描述】:

我目前正在用 python 构建一个简单的纸牌匹配游戏,有一个 5x4(行*列)的网格,其中两个玩家尝试匹配一副 20 张牌(2,10 只花色红心)*2。

我遇到的问题是遍历卡片组,以网格方式打印卡片,所以它看起来像这样:

-----     -----    -----     -----
-   -     -   -    -   -     -   -
 4-H       6-H      7-H       8-H
-   -     -   -    -   -     -   - 
-----     -----    -----     -----

我目前拥有的代码如下:

#needed import for shuffle function
from random import shuffle

#class for my deck
class Deck:
    #constructor starts off with no cards
    def __init__( self ):
        self._deck = []

    #populate the deck with every combination of suits and values    
    def Populate( self ):
        #Heart, Diamond, Spades, Clubs
        for suit in 'HDSC':
            #Jack = 11, Queen = 12, King = 13, Ace = 14
            for value in range(2, 15):
                if value == 11:
                    value = 'J'
                elif value == 12:
                    value = 'Q'
                elif value == 13:
                    value = 'K'
                elif value == 14:
                    value = 'A'
                #add to deck list
                self._deck.append(str(value) + '-' + suit)

    #populate the deck with only hears hearts and all cards except face cards and aces (2, 3, 4, 5, 6, 7, 8, 9, 10) twice
    def gamePop( self ):
        suit = 'H'
        for x in range(2):
            for value in range(2, 11):
                self._deck.append(str(value) + '-' + suit)

    #shuffle the deck with the random import             
    def Shuffle( self ):
        shuffle( self._deck )

    #length of the deck
    def len( self ):
        return len( self._deck )

    def stringIt( self ): 
        #Returns the string representation of a deck
        result = ''
        for c in self._deck:
            result = result + str(c) + '\n'
        return result

#class for a single card
class Card:
    #constructor for what type of card it is
    def __init__( self, value, suit ):
        self._value = value
        self._suit = suit
        self._card = self._value + self._suit

    #print the type of card    
    def Description( self ):
        return ( self._card )

    #overloaded ==
    def __eq__( self, another ):
        if ( self._card == another.Description() ):
            return True
        else:
            return False
#main function which plays the game
def main():

    #sets player counters to zero,
    pOneCount = 0
    pTwoCount = 0

    #creates the deck to be put on the board
    gameDeck = Deck()
    gameDeck.gamePop()
    gameDeck.Shuffle()

    print(gameDeck._deck)
    currentCard = 0
    for row in range(5):
        for card in range(0,4+i):
            mystring = 
        print ('-------  ' * 4)
        print ('|     |  ' * 4)
        for x in range(4):
            print ('|  ' +gameDeck._deck[currentCard]+'|'),
            currentCard += 1
        print ('|     |  ' * 4)
        print ('-------  ' * 4)

编辑:我清除了我尝试过的代码。

当前的输出是这样的:

-------  -------  -------  -------  
|     |  |     |  |     |  |     |  
|  7-H|
|  5-H|
|  7-H|
|  9-H|
|     |  |     |  |     |  |     |  
-------  -------  -------  -------  

【问题讨论】:

  • 他尝试的是他发布的代码。他缺少的是对问题的一个很好的解释。
  • 抱歉,我已对其进行了编辑以包含我尝试过的代码。
  • 你能举例说明你当前的输出是什么吗?
  • 呈现的代码根本不会产生任何输出。 main 函数有一个悬空的mystring =,这是一个明显的语法错误。此外,还有一个对 i 的引用,它没有在任何地方定义。

标签: python list python-3.x


【解决方案1】:

问题出在 def main() 中:

def main():

    print ('-------  ' * 4)
    print ('|     |  ' * 4)
    for x in range(4):
        print ('|  ' +gameDeck._deck[currentCard]+'|'),
        currentCard += 1
    print ('|     |  ' * 4)
    print ('-------  ' * 4)

* 4 的意思是这样的:

print ('-------  ' * 4)

会变成这样:

print ('-------  ' + '-------  ' + '-------  ' + '-------  ' )

也可以这样写:

print ('-------  -------  -------  -------  ' )

所以。你的问题在这里:

    for x in range(4):
        print ('|  ' +gameDeck._deck[currentCard]+'|'),
        currentCard += 1

这将打印为:

|  7-H|
|  5-H|
|  7-H|
|  9-H|

你需要把它写成这样:

        print ('|  ' +gameDeck._deck[currentCard]+'|'+'|  ' +gameDeck._deck[currentCard+1]+'|'+'|  ' +gameDeck._deck[currentCard+2]+'|'+'|  ' +gameDeck._deck[currentCard+3]+'|')

所以它会像你想要的那样打印在一行中:

    |  7-H|  |  5-H|  |  7-H|  |  9-H|

这是我稍微清理的代码。如果它可以正常工作,它应该可以工作:

def main():

    #sets player counters to zero,
    pOneCount = 0
    pTwoCount = 0

    #creates the deck to be put on the board
    gameDeck = Deck()
    gameDeck.gamePop()
    gameDeck.Shuffle()

    print(gameDeck._deck)
    currentCard = 0
    for row in range(5):
        for card in range(0,4+i): 
        print (' ------- ' * 4)
        print (' |     | ' * 4)
        print (' | ' +gameDeck._deck[currentCard]+' | '+' | ' +gameDeck._deck[currentCard+1]+' | '+' | ' +gameDeck._deck[currentCard+2]+' | '+' | ' +gameDeck._deck[currentCard+3]+' | ')
        print (' |     | ' * 4)
        print (' ------- ' * 4)

哦,就像 John Y 说的(复制和粘贴):

主函数有一个悬空的mystring =,这是一个明显的语法错误

这是我用来测试的,因为整个代码对我不起作用,我只是测试了打印部分:

print (' ------- ' * 4)
print (' |     | ' * 4)
print (' | ' +"1-H"+' | '+' | ' +"2-H"+' | '+' | ' +"3-H"+' | '+' | ' +"4-H"+' |  ')
print (' |     | ' * 4)
print (' ------- ' * 4)

这让我:

 -------  -------  -------  ------- 
 |     |  |     |  |     |  |     | 
 | 1-H |  | 2-H |  | 3-H |  | 4-H |  
 |     |  |     |  |     |  |     | 
 -------  -------  -------  ------- 
>>> 

【讨论】:

  • 看起来 OP 可能一直在尝试使用print 上的 Python 2 尾随逗号来抑制换行符,但实际上是在使用 Python 3,但这种方式不起作用。 (即使他取消了换行符,他也必须发出另一个 print 才能实际打印换行符。)
  • 好吧,我的打印作品。刚刚测试了打印部分,因为整个 codez 对我不起作用,请参阅答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
相关资源
最近更新 更多