我用Python制作了Connect 4的工作版本。我对Python非常陌生,所以它非常原始。请看一看,让我知道你的想法!
import random
board = [["." for x in range(7)] for x in range(6)]
symbol = ""
winCondition = ""
fullCols = []
def boardLogic(pick, user):
if user == "user":
symbol = "0"
else:
symbol = "X"
if board[0][pick] != ".":
if board[1][pick] != ".":
if board[2][pick] != ".":
if board[3][pick] != ".":
if board[4][pick] != ".":
if board[5][pick] != ".":
print("That column is full.")
if pick not in fullCols:
fullCols.append(pick)
fullCols.sort()
print fullCols
else:
board[5][pick] = symbol
else:
board[4][pick] = symbol
else:
board[3][pick] = symbol
else:
board[2][pick] = symbol
else:
board[1][pick] = symbol
else:
board[0][pick] = symbol
def printBoard():
for row in board:
for val in row:
print '{:4}'.format(val),
print
def does_square_contain_win(i, j):
#right_diag: [[i,j], [i-1,j+1], [i-2,j+2], [i-3,j+3]]
if i-3 in range(0, 6) and j+3 in range(0,5):
if board[i][j] == "0" and board[i-1][j+1] == "0" and board[i-2][j+2] == "0" and board[i-3][j+3] == "0":
print("******************************\nPlayer Wins\n******************************")
quit()
if i-3 in range(0, 6) and j+3 in range(0,5):
if board[i][j] == "X" and board[i-1][j+1] == "X" and board[i-2][j+2] == "X" and board[i-3][j+3] == "X":
print("******************************\nComputer Wins\n******************************")
quit()
#left_diag: [[i,j], [i-1,j-1], [i-2,j-2], [i-3,j-3]]
if i-3 in range(0, 6) and j-3 in range(0,5):
if board[i][j] == "0" and board[i-1][j-1] == "0" and board[i-2][j-2] == "0" and board[i-3][j-3] == "0":
print("******************************\nPlayer Wins\n******************************")
quit()
if i-3 in range(0, 6) and j-3 in range(0,5):
if board[i][j] == "X" and board[i-1][j-1] == "X" and board[i-2][j-2] == "X" and board[i-3][j-3] == "X":
print("******************************\nComputer Wins\n******************************")
quit()
#right: [[i,j], [i,j+1], [i,j+2], [i,j+3]]
if j+3 in range(0, 5):
if board[i][j] == "0" and board[i][j+1] == "0" and board[i][j+2] == "0" and board[i][j+3] == "0":
print("******************************\nPlayer Wins\n******************************")
quit()
if j+3 in range(0, 5):
if board[i][j] == "X" and board[i][j+1] == "X" and board[i][j+2] == "X" and board[i][j+3] == "X":
print("******************************\nComputer Wins\n******************************")
quit()
#down: [[i,j], [i-1,j], [i-2,j], [i-3,j]]
if i-3 in range(0, 6):
if board[i][j] == "0" and board[i-1][j] == "0" and board[i-2][j] == "0" and board[i-3][j] == "0":
print("******************************\nPlayer Wins\n******************************")
quit()
if i-3 in range(0, 6):
if board[i][j] == "X" and board[i-1][j] == "X" and board[i-2][j] == "X" and board[i-3][j] == "X":
print("******************************\nComputer Wins\n******************************")
quit()
while True:
printBoard()
#pick a column
while True:
try:
yourPick = int(raw_input('Pick a column 0 - 6: '))
except ValueError:
print 'That\'s not a number!'
else:
if 0 <= yourPick < 6 and yourPick not in fullCols:
break
else:
print 'Out of range or column full. Try again.'
print(yourPick)
#column logic
boardLogic(yourPick, "user")
#opponent randomly picks a column
while True:
try:
theirPick = random.randint(0, 6)
except ValueError:
print 'That\'s not a number!'
else:
if theirPick not in fullCols:
break
else:
print 'Column full!'
print(theirPick)
#column logic
boardLogic(theirPick, "notUser")
#check win conditions
for i in range(0, 6):
for j in range(0, 5):
does_square_contain_win(i, j)发布于 2016-02-19 19:32:18
if board[0][pick] != ".":
if board[1][pick] != ".":
if board[2][pick] != ".":
if board[3][pick] != ".":
if board[4][pick] != ".":
if board[5][pick] != ".":
print("That column is full.")
if pick not in fullCols:
fullCols.append(pick)
fullCols.sort()
print fullCols
else:
board[5][pick] = symbol
else:
board[4][pick] = symbol
else:
board[3][pick] = symbol
else:
board[2][pick] = symbol
else:
board[1][pick] = symbol
else:
board[0][pick] = symbol因此,如果第一个是空的,则分配第一个,如果第二个是空的,则分配第二个.
换句话说,您分配第一个空,让我写一些伪代码:
def boardLogic(pick, user):
# ... Logic before (this should maybe be moved elsewhere)
try:
first_empty = first(lambda i: board[i][pick] == EMPTY, range(5+1))
board[first_empty][pick] = symbol
except StopIteration:
print("That column is full.")
# ... No empty space哪里
EMPTY = '.'在高级语言中,您应该尝试编写类似于问题的高级描述的代码,并避免使用许多链式条件。
https://codereview.stackexchange.com/questions/120561
复制相似问题