私は現在、無重力のコネクト4ゲームであるプログラムを作成しようとしています。つまり、ボードのどこからでもピースを配置できます。現在、列を選択し、ピースを上から配置する作業を行っています。また、私のプログラムは、ゲームを難しくするために、ボード上にいくつのブロックを配置したいかをユーザーに尋ねます。たとえば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)
前もって感謝します!