我目前正在尝试编写一个程序,该程序是一个零重力的四连棋游戏,这意味着您可以从棋盘的任何一侧放置棋子,我目前正在选择一列并从顶部放置棋子。我的程序还询问用户他们想在棋盘上放置多少块,使游戏更难。例如,当我键入 C8 时,它会通过列表中的第 8 行查看每个列表中的第 8 个元素。有什么想法吗?
我编辑的代码如下:
#this imports random for my blocks
import random
#this makes my board
table = [[ " ","C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"],
[ " R1|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R2|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R3|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R4|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R5|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R6|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R7|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R8|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R9|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ "R10|" ," |", " |", " |", " |", " |", " |", " |", " |", " |", " |"]]
#my two different player pieces
player1="#|"
player2="@|"
block="B|"
row=11
columns=11
#this is a function i can call to print the table
def printTable(table):
for row in table:
for value in row:
print (value,end=' ')
print ('\n')
#this ask the user how many blocks they want on their board then places it on there
def block(table):
blockQuestion=(input("how many blocks would you like to put on the table? \n (please enter a number between 1 and 25)"))
number=blockQuestion[:2]
number=int(number)
number=number-1
print("this is number ",number)
count = 0
number=int(number)
while number >= count:
x=random.randint(1,10)
y=random.randint(1,10)
print("x and y ", x,y)
table[x][y]="B|"
count +=1
printTable(table)
move()
#this is my function to ask the user for their move.
def move():
move=input("Please choose where you would like to drop your marker \n (For instance C1, R5)")
move.split()
rorc=move[0:1]
ans=move[1:3]
answer=int(ans)
print(rorc," ",answer)
if "R" in move:
row(answer)
if "C" in move:
col(answer)
#this is my function if the user wants to go by row
def row(answer):
side=input("would you like to insert it from the right or left \n Please type R for right or L for left ")
if "R" in side:
try:
table[answer].reverse()
blockCheck=table[answer].index("B|")
if blockCheck == 0:
print ("you can not place a peice there because of the block")
tryAgain()
except:
try:
p1Check=table[answer].index("#|")
if p1Check is 0:
print ("you can not place a peice there due to the opponents marker")
tryAgain()
except:
try:
p2Check=table[answer].index("@|")
if p2check is 0:
print ("you can not place a peice there due to the opponents marker")
tryAgain()
except:
print('hi')
try:
tits=max(blockCheck,p1Check,p2Check)
print("All three checks",tits)
except:
try:
tits=max(blockCheck,p1Check)
print("this is bc and p1c",tits)
except:
tits=(blockCheck)
print("this is block check",tits)
table[answer].reverse()
table[answer][-tits]= (player1)
printTable(table)
#this is my function if the user wants to go by columns
def col(answer):
side=input("would you like to insert it from the top or bottom \nPlease type T for top or B for bottom")
answer=int(answer)
if "T" in side:
try:
blockCheck=table[:][answer].index("B|")
print("blockCheck ", blockCheck)
if blockCheck == 1:
print ("you can not place a peice there because of the block")
tryAgain()
except:
try:
p1Check=table[answer].index("#|")
if p1Check is 1:
print ("you can not place a peice there due to the opponents marker")
tryAgain()
except:
try:
p2Check=table[answer].index("@|")
if p2check is 1:
print ("you can not place a peice there due to the opponents marker")
tryAgain()
except:
print("whaa")
try:
tits=min(blockCheck,p1Check,p2Check)
print("All three checks",tits)
except:
try:
tits=min(blockCheck,p1Check)
print("this is bc and p1c",tits)
except:
try:
tits=(blockCheck)
print("this is block check",tits)
except:
tits=11
table[tits-1][answer]= (player2)
printTable(table)
#function to restart the program to the move function
def tryagain():
tryAgain=input('try again \nPlease type y or n ')
if tryAgain == 'y':
move()
elif tryAgain =='n':
bash
#calls the function to start the program
block(table)
提前致谢!