2017-09-24 24 views
1

警告:我幾乎全新的python,所以這裏的錯誤可能是顯而易見的 - 我會提前道歉。此外,請嘗試保持答案容易理解爲白癡!謝謝:)Python連接4轉

我想在python中創建一個連接4遊戲,我幾乎所有的工作,除了我不能讓它交換之間的輪流和交叉之間的轉換。我的主要代碼如下:

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基本上是轉的,但我給它分配使用前面:

firstHTurn = random.randint(1,2) 
if firstHTurn == 1: 
    Xor0 = 'Crosses' 
    tile = 'X' 
else: 
    Xor0 = 'Noughts' 
    tile = '0' 

,工作正常,但我不明白爲什麼轉不交在main()的

的完整代碼中的每個 '如果' 語句的結尾:

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() 

回答

0

我不能評論,所以我的Wi而是發佈這個答案,然後將其刪除一次, 請把所有的代碼都放進去,這樣我們就可以測試它了,至少我很難測試一些東西,而沒有其餘的東西來使它工作

但是,從我所能看到的情況來看,您需要反覆調用Main,每次去一次,這是因爲事後再也不會重新檢查它的十字轉向了,如果它不轉向未轉向後

+0

完成後,首先應該做到這一點。 – DetectiveSh4rk

+0

由於某種原因,你的代碼爲我加載了大量的錯誤,所以我放棄了,我的python副本有時可能會很奇怪,所以它可能是這樣,無論如何,好看 – Kieron

+0

@ DetectiveSh4rk我只能要求你嘗試改變else主要到elif – Kieron