Python连接4圈
警告:我几乎完全不熟悉python,因此这里的错误可能很明显-我将提前道歉。另外,请尽量让白痴的答案简单易懂!谢谢:) 我正在尝试用python创建一个Connect 4游戏,我几乎所有的东西都在工作,除了我不能让它在零和十字架之间交换回合。我的主要代码如下:Python连接4圈,python,Python,警告:我几乎完全不熟悉python,因此这里的错误可能很明显-我将提前道歉。另外,请尽量让白痴的答案简单易懂!谢谢:) 我正在尝试用python创建一个Connect 4游戏,我几乎所有的东西都在工作,除了我不能让它在零和十字架之间交换回合。我的主要代码如下: def main(): global Xor0 winCheck = True while winCheck == True: humanTile = enterHumanTile()
def main():
global Xor0
winCheck = True
while winCheck == True:
humanTile = enterHumanTile()
print("The", Xor0, "player shall go first")
mainBoard= getNewBoard()
if Xor0 == "Crosses":
drawBoard(mainBoard)
move = getHumanMove(mainBoard)
makeMove(mainBoard, humanTile, move)
Xor0 = "Noughts"
if isWinner(mainBoard, tile):
drawBoard(mainBoard)
print("Crosses win")
winCheck = False
Xor0 = "Noughts"
else:
drawBoard(mainBoard)
move = getHumanMove(mainBoard)
makeMove(mainBoard, humanTile, move)
if isWinner(mainBoard, tile):
drawBoard(mainBoard)
print("Noughts win")
winCheck=False
Xor0 ="Crosses"
Xor0本质上是turn,但我在前面使用:
firstHTurn = random.randint(1,2)
if firstHTurn == 1:
Xor0 = 'Crosses'
tile = 'X'
else:
Xor0 = 'Noughts'
tile = '0'
这很好,但我不明白为什么在main()中的每个“if”语句的末尾不交替
完整代码:
import random
import sys
import copy
import time
def s():
print("")
print(" _________ __ ___________ ")
print(" \_ ___ \ ____ ____ ____ ____ _____/ |_ \_ _____/___ __ _________ ")
print(" / \ \/ / _ \ / \ / \_/ __ \_/ ___\ __\ | __)/ _ \| | \_ __ \ ")
print(" \ \___( <_> ) | \ | \ ___/\ \___| | | \( <_> ) | /| | \/")
print(" \______ /\____/|___| /___| /\___ >\___ >__| \___ / \____/|____/ |__| ")
print(" \/ \/ \/ \/ \/ \/ ")
print("")
print("")
playerMethod = input("Would you like to player a computer or a friend (C for Computer, F for Friends)")
if playerMethod== "F":
s()
method = "pvp"
print("Choose who will be Noughts and who will be Crosses")
s()
#time.sleep(3)
#firstHTurn = random.randint(1,2)
firstHTurn = 2
if firstHTurn == 1:
Xor0 = 'Crosses'
tile = 'X'
else:
Xor0 = 'Noughts'
tile = '0'
print("The", Xor0, "will play first")
s()
print("-----------------------------")
s()
elif playerMethod == "C":
method = "pve"
firstHTurn = random.randint(1,2)
if firstHTurn == 1:
print("The computer shall go first")
else:
print("You shall go first")
print("-----------------------------")
a=0
board = []
while a==0:
boardwidth = input("How wide should the board be?")
try:
float(boardwidth)
break
except ValueError:
pass
print("")
print("======================")
print("Please enter a number")
print("======================")
print("")
print("=== Board width set to", boardwidth, "===")
b=0
while b==0:
boardheight = input("How tall should the board be?")
try:
float(boardheight)
break
except ValueError:
pass
print("")
print("======================")
print("Please enter a number")
print("======================")
print("")
print("=== Board height set to", boardheight, "===")
s()
if method == "pvp":
print("=======================================")
print(" ==The", Xor0, "player shall go first== ")
print("=======================================")
if method == "pve" and firstHTurn == 1:
print("=================================")
print("== The Computer shall go first ==")
print("=================================")
if method == "pve" and firstHTurn == 2:
print("==============================")
print("===== You Shall go first =====")
print("==============================")
BHeight = int(boardheight)
BWidth = int(boardwidth)
def getNewBoard():
board = []
for x in range(BWidth):
board.append([' '] * BHeight)
return board
def drawBoard(board):
print()
print(' ', end='')
for x in range(1, BWidth + 1):
print(' %s ' % x, end='')
print()
print('+---+' + ('---+' * (BWidth - 1)))
for y in range(BHeight):
print('| |' + (' |' * (BWidth - 1)))
print('|', end='')
for x in range(BWidth):
print(' %s |' % board[x][y], end='')
print()
print('| |' + (' |' * (BWidth - 1)))
print('+---+' + ('---+' * (BWidth - 1)))
def enterHumanTile():
if tile == 'X':
return ['X', '0']
else:
return ['0', 'X']
def isValidMove(board, move):
if move < 0 or move >= (BWidth):
return False
if board[move][0] != ' ':
return False
return True
def getHumanMove(board):
while True:
print("Which column do you want to move on? (1-%s, or 'quit' to quit the game)" % (BWidth))
move = input()
if move.lower().startswith('q'):
sys.exit()
if not move.isdigit():
continue
move = int(move) - 1
if isValidMove (board, move):
return move
def makeMove(board, player, column):
for y in range(BHeight-1, -1, -1):
if board[column][y] ==' ':
board[column][y] =tile
return
def playAgain():
# This function returns True if the player wants to play again, otherwise it returns False.
print('Do you want to play again? (yes or no)')
return input().lower().startswith('y')
def isWinner(board, tile):
# check horizontal spaces
for y in range(BHeight):
for x in range(BWidth - 3):
if board[x][y] == tile and board[x+1][y] == tile and board[x+2][y] == tile and board[x+3][y] == tile:
return True
# check vertical spaces
for x in range(BWidth):
for y in range(BHeight - 3):
if board[x][y] == tile and board[x][y+1] == tile and board[x][y+2] == tile and board[x][y+3] == tile:
return True
# check / diagonal spaces
for x in range(BWidth - 3):
for y in range(3, BHeight):
if board[x][y] == tile and board[x+1][y-1] == tile and board[x+2][y-2] == tile and board[x+3][y-3] == tile:
return True
# check \ diagonal spaces
for x in range(BWidth - 3):
for y in range(BHeight - 3):
if board[x][y] == tile and board[x+1][y+1] == tile and board[x+2][y+2] == tile and board[x+3][y+3] == tile:
return True
return False
def main():
global Xor0
winCheck = True
while winCheck == True:
humanTile = enterHumanTile()
print("The", Xor0, "player shall go first")
mainBoard= getNewBoard()
while winCheck == True:
if Xor0 == "Crosses":
drawBoard(mainBoard)
move = getHumanMove(mainBoard)
makeMove(mainBoard, humanTile, move)
Xor0 = "Noughts"
if isWinner(mainBoard, tile):
drawBoard(mainBoard)
print("YOU WIN")
winCheck = False
Xor0 = "Noughts"
else:
drawBoard(mainBoard)
move = getHumanMove(mainBoard)
makeMove(mainBoard, humanTile, move)
if isWinner(mainBoard, tile):
drawBoard(mainBoard)
print("Noughts win")
winCheck=False
Xor0 ="Crosses"
main()
随机导入
导入系统
导入副本
导入时间
def s():
打印(“”)
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU”)
打印(“/\/\/\/\\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
打印(“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\()\\\\\\\\/”)
打印(“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
打印(“\/\/\/\/\/”)
打印(“”)
打印(“”)
playerMethod=输入(“您想玩电脑还是朋友(C代表电脑,F代表朋友)”)
如果playerMethod==“F”:
s()
方法=“pvp”
打印(“选择谁是零,谁是十字架”)
s()
#时间。睡眠(3)
#firstHTurn=random.randint(1,2)
firstHTurn=2
如果firstHTurn==1:
Xor0=‘交叉’
平铺='X'
其他:
Xor0=‘零’
磁贴='0'
打印(“Xor0将首先播放”)
s()
打印(“-------------------------------”)
s()
elif playerMethod==“C”:
method=“pve”
firstHTurn=random.randint(1,2)
如果firstHTurn==1:
打印(“计算机应先行”)
其他:
打印(“你应该先走”)
打印(“-------------------------------”)
a=0
董事会=[]
当a==0时:
boardwidth=输入(“电路板应该有多宽?”)
尝试:
浮动(板宽)
打破
除值错误外:
通过
打印(“”)
打印(“==================================”)
打印(“请输入一个数字”)
打印(“==================================”)
打印(“”)
打印(“==线路板宽度设置为“,”线路板宽度“==”)
b=0
当b==0时:
boardheight=输入(“板应该有多高?”)
尝试:
浮动(板高)
打破
除值错误外:
通过
打印(“”)
打印(“==================================”)
打印(“请输入一个数字”)
打印(“==================================”)
打印(“”)
打印(“==线路板高度设置为“,”线路板高度“==”)
s()
如果方法==“pvp”:
打印(“==========================================================”)
打印(“==”,Xor0,“玩家应先走==”)
打印(“==========================================================”)
如果method==“pve”和firstHTurn==1:
打印(“====================================================”)
打印(“==计算机应先走==”)
打印(“====================================================”)
如果method==“pve”和firstHTurn==2:
打印(“==============================================”)
打印(“=======您应先走=====”)
打印(“==============================================”)
BHHEIGHT=int(板高)
BWidth=int(板宽)
def getNewBoard():
董事会=[]
对于范围内的x(宽):
董事会。追加(['']*BHeight)
返回板
def牵引板(板):
打印()
打印(“”,结束=“”)
对于范围(1,宽度+1)内的x:
打印(“%s”%x,结束=“”)
打印()
打印('+--+'+('--+'*(BWidth-1)))
对于范围内的y(BHHEIGHT):
打印(“||”+(“|”*(BWidth-1)))
打印(“|”,结束=”)
对于范围内的x(宽):
打印(“%s”|“%board[x][y],end=”)
打印()
打印(“||”+(“|”*(BWidth-1)))
打印('+--+'+('--+'*(BWidth-1)))
def enterHumanTile():
如果平铺='X':
返回['X','0']
其他:
返回['0','X']
def isValidMove(板,移动):
如果移动<0或移动>=(b宽度):
返回错误
如果板[move][0]!='':
返回错误
返回真值
def getHumanMove(板):
尽管如此:
打印(“您希望移动到哪一列?(1-%s,或“退出”退出游戏)%”(b宽度)
移动=输入()
如果move.lower().startswith('q'):
sys.exit()
如果不移动。isdigit():
持续
move=int(move)-1
如果isValidMove(板,移动):
回击
def makeMove(板、播放器、列):
对于范围内的y(BHeight-1,-1,-1):
如果电路板[列][y]='':
板[列][y]=瓷砖
返回
def playreach():
#如果玩家想再次玩,此函数返回True,否则返回False。
打印('您想再次播放吗?(是或否)')
返回输入()
def isWinner(板、磁贴):
#检查水平空间
对于范围内的y(BHHEIGHT):
对于范围内的x(宽度-3):
如果板[x][y]==平铺和板[x+1][y]==平铺和板[x+2][y]==平铺和板[x+3][y]==平铺:
返回真值
#检查垂直空间
对于范围内的x(宽):
对于范围内的y(BHeight-3):
如果线路板[x][y]==平铺和线路板[x][y+1]==平铺和线路板[x][y+2]==平铺和线路板[x][y+3]==平铺:
返回真值
#检查/对角空格
对于范围内的x(宽度-3):
对于范围内的y(3,bHweight):
如果电路板[x][y]==磁砖和电路板[x+1][y-1]==磁砖和电路板[x+2][y-2]==磁砖和电路板[x+3][y-3]==磁砖:
返回真值
#检查\对角空格
对于范围内的x(宽度-3):
对于范围内的y(BHeight-3):
如果板[x][y]==平铺和板[x+1][y+1]==平铺和板[x+2][y+2]==平铺和板[x+3][y+3]==平铺:
返回真值
返回