无法在船上标记球员

时间:2018-02-15 03:43:08

标签: python

我正在尝试制作一款游戏,但是通过将玩家的当前得分作为创建棋盘游戏的列表中的索引,让玩家的标记出现在棋盘上有问题。

class board():
    def __init__(self):
        self.board = [36,35,34,33,32,31,
                      25,26,27,28,29,30,
                      24,23,22,21,20,19,
                      13,14,15,16,17,18,
                      12,11,10,9,8,7,
                      1,2,3,4,5,6]

        self.playerscore = 0

    def make_grid(self):
             print('-------------------------------------------------------------------')
    print("|  %3s     |  %3s      |  %3s    |  %3s      |  %3s      |  %3s   |" % (self.board[35],self.board[34],self.board[33],self.board[32],self.board[31], self.board[30]))
    print("|          |           |         |   C3      |   L3      |        |")       
    print('-------------------------------------------------------------------')
    print("|  %3s     |  %3s      |  %3s    |  %3s      |  %3s      |  %3s   |" % (self.board[24],self.board[25],self.board[26],self.board[27],self.board[28], self.board[29]))
    print("|          |   L4      |         |   C2      |           |        |")
    print('-------------------------------------------------------------------')
    print("|  %3s     |  %3s      |  %3s    |  %3s      |  %3s      |  %3s   |" % (self.board[23],self.board[22],self.board[21],self.board[20],self.board[19], self.board[18],))
    print("|   L4     |   C1      |   C3    |   L2      |   C2      |        |")
    print('-------------------------------------------------------------------')
    print("|  %3s     |  %3s      |  %3s    |  %3s      |  %3s      |  %3s   |" % (self.board[12],self.board[13],self.board[14],self.board[15],self.board[16], self.board[17],))
    print("|          |           |   L1    |           |           |        |")
    print('-------------------------------------------------------------------')
    print("|  %3s     |  %3s      |  %3s    |  %3s      |  %3s      |  %3s   |" % (self.board[11],self.board[10],self.board[9],self.board[8],self.board[7], self.board[6],))
    print("|          |   C1      |         |           |    L2     |        |")
    print('-------------------------------------------------------------------')
    print("|  %3s     |  %3s      |  %3s    |  %3s      |  %3s      |  %3s   |" % (self.board[0],self.board[1],self.board[2],self.board[3],self.board[4], self.board[5],))
    print("|          |    L1     |         |           |           |        |")
    print('-------------------------------------------------------------------')

    def playermover(self):
        while self.playerscore < 36

            dice = input("Enter 1 to roll:")
            if dice == 1:
                self.playerscore = self.playerscore + randint(1,6)
                self.board[self.playerscore] = 'P'
            self.make_grid()

当我打电话给课堂时,我会看到游戏板和输入,但每次按下1来掷骰子时,我都会得到同一个棋盘,但没有标记玩家的位置。

2 个答案:

答案 0 :(得分:0)

input返回一个字符串,而不是整数。所以你有两个选择。

比较字符串:

dice = input("Enter 1 to roll:")
if dice == '1':

或转换为int:

dice = int(input("Enter 1 to roll:"))
if dice == 1:

答案 1 :(得分:0)

你没有(或者,至少显示你的)from random import randint

正如其他人所说,要么将输入转换为int,要么与字符串进行比较。

您永远不会实例化该类,也不会调用这些方法。

在设置电路板时,你的工作太复杂了。

当玩家离开该区域时,您还需要将棋盘格设置回正确的数字。

from random import randint

FINALSCORE = 36
class board():
    def __init__(self):
        self.board = [i+1 for i in range(36)]

        self.playerscore = 0
        self.gameover = False

    def make_grid(self):
        print('-------------------------------------------------------------------')
        print("|  %3s     |  %3s      |  %3s    |  %3s/C3   |  %3s/L3   |  %3s   |" % (self.board[35],self.board[34],self.board[33],self.board[32],self.board[31], self.board[30]))
        print('-------------------------------------------------------------------')
        print("|  %3s     |  %3s/L4   |  %3s    |  %3s/C2   |  %3s      |  %3s   |" % (self.board[24],self.board[25],self.board[26],self.board[27],self.board[28], self.board[29]))
        print('-------------------------------------------------------------------')
        print("|  %3s/L4  |  %3s/C1   |  %3s/C3 |  %3s/L2   |  %3s/C2   |  %3s   |" % (self.board[23],self.board[22],self.board[21],self.board[20],self.board[19], self.board[18],))
        print('-------------------------------------------------------------------')
        print("|  %3s     |  %3s      |  %3s/L1 |  %3s      |  %3s      |  %3s   |" % (self.board[12],self.board[13],self.board[14],self.board[15],self.board[16], self.board[17],))
        print('-------------------------------------------------------------------')
        print("|  %3s     |  %3s/C1   |  %3s    |  %3s      |  %3s/L2   |  %3s   |" % (self.board[11],self.board[10],self.board[9],self.board[8],self.board[7], self.board[6],))
        print('-------------------------------------------------------------------')
        print("|  %3s     |  %3s/L1   |  %3s    |  %3s      |  %3s      |  %3s   |" % (self.board[0],self.board[1],self.board[2],self.board[3],self.board[4], self.board[5],))
        print('-------------------------------------------------------------------')

    def playermover(self):
        dice = input("Enter 1 to roll:")
        if dice == '1':
            roll = randint(1,6)
            print(roll)
            if self.playerscore:
                self.board[self.playerscore-1] = self.playerscore 
            self.playerscore += roll
            if self.playerscore > FINALSCORE: self.playerscore = FINALSCORE
            if self.playerscore == FINALSCORE: self.gameover = True
            self.board[self.playerscore - 1] = 'P'
            print(self.playerscore)
        self.make_grid()


game = board()
game.make_grid()
while not game.gameover:
    game.playermover()