Jan-21-2022, 11:58 AM
THIS IS NOT PART OF THE CODE ONLY SOME GUIDING TOWARDS WHERE MY PROBLEMS ARE
The function that checks for diagonal lines wont detect them.
------------------------------------------------------------------------------------------------------------------------
code starts here
The function that checks for diagonal lines wont detect them.
------------------------------------------------------------------------------------------------------------------------
code starts here
BLANK = "*" # sets the character for the BLANK positions ################################################################## # DECLARE the variables used in the program as required by CIE ### ################################################################## # Board(0:6, 0:7) : str (but ignore row 0 and column 0) # ThisPlayer : str[1] # GameFinished, WinnerFound : bool # ColumnNumber : int # ValidColumn, ValidRow : int def InitialiseBoard(): global Board Board = [[BLANK] * 7 for x in range(7)] # Creates the array of blank values def SetUpGame(): # Set up the parameters for the beginning of the game global ThisPlayer global GameFinished ThisPlayer = "O" # Player O always starts GameFinished = False def OutputBoard(): print("The board as it currently stands...\n") print("Choose your column") print("1 2 3 4 5 6 7\n") for Row in range(6, 0, -1): for Column in range(7): print(Board[Row][Column], end=" ") print() print() def ColumnNumberValid(): # returns whether or not the column number is valid global ColumnNumber Valid = False if (ColumnNumber >= 0) and (ColumnNumber <= 6): if Board[6][ColumnNumber] == BLANK: # at least 1 empty space in column Valid = True return Valid def ThisPlayerChoosesColumn(): # returns a valid column number global ColumnNumber print("Player ", ThisPlayer, "'s turn.") ColumnNumber = int(input("Enter a valid column number: ")) - 1 while ColumnNumberValid() == False: # check whether the column number is valid ColumnNumber = int(input("Enter a valid column number: ")) - 1 return ColumnNumber def FindNextFreePositionInColumn(): # returns the next free position ThisRow = 1 while Board[ThisRow][ValidColumn] != BLANK: # find first empty cell ThisRow += 1 return ThisRow def ThisPlayerMakesMove(): global Board global ValidColumn global ValidRow ValidColumn = ( ThisPlayerChoosesColumn() ) # use a module to return valid column number ValidRow = FindNextFreePositionInColumn() # use a module to return row number Board[ValidRow][ValidColumn] = ThisPlayer def CheckHorizontalLineInValidRow(): global WinnerFound for i in range(5): if i < 5: if ( (Board[ValidRow][i - 1] == ThisPlayer) and (Board[ValidRow][i] == ThisPlayer) and (Board[ValidRow][i + 1] == ThisPlayer) and (Board[ValidRow][i + 2] == ThisPlayer) ): WinnerFound = True def CheckVerticalLineInValidColumn(): global WinnerFound if (ValidRow == 4) or (ValidRow == 5) or (ValidRow == 6): if ( (Board[ValidRow][ValidColumn] == ThisPlayer) and (Board[ValidRow - 1][ValidColumn] == ThisPlayer) and (Board[ValidRow - 2][ValidColumn] == ThisPlayer) and (Board[ValidRow - 3][ValidColumn] == ThisPlayer) ): WinnerFound = True def CheckDiagonalRight(): global WinnerFound for i in range(5): if ( (Board[ValidRow][i] == ThisPlayer) and (Board[ValidRow + 1][i + 1] == ThisPlayer) and (Board[ValidRow + 2][i + 2] == ThisPlayer) and (Board[ValidRow + 3][i + 3] == ThisPlayer) ): WinnerFound = True def CheckDiagonalLeft(): global WinnerFound for i in range(7): if i > 4: if ( (Board[ValidRow][i] == ThisPlayer) and (Board[ValidRow + 1][i - 1] == ThisPlayer) and (Board[ValidRow + 2][i - 2] == ThisPlayer) and (Board[ValidRow + 3][ValidColumn - 3] == ThisPlayer) ): WinnerFound = True def CheckDiagonalLine(): global WinnerFound if ValidRow < 4: CheckDiagonalRight() elif ValidRow == 4: CheckDiagonalLeft() CheckDiagonalRight() elif ValidRow > 4: CheckDiagonalLeft() def CheckForFullBoard(): global GameFinished BlankFound = False ThisRow = 0 while (ThisRow != 6) and (BlankFound == False): ThisColumn = 0 ThisRow += 1 while (ThisColumn != 7) and (BlankFound == False): ThisColumn += 1 if Board[ThisRow][ThisColumn] == BLANK: BlankFound = True if BlankFound == False: print("It is a draw") GameFinished = True def CheckIfThisPlayerHasWon(): global WinnerFound global GameFinished WinnerFound = False CheckHorizontalLineInValidRow() if WinnerFound == False: CheckVerticalLineInValidColumn() if WinnerFound == False: CheckDiagonalLine() if WinnerFound == True: GameFinished = True print(ThisPlayer, " is the winner") else: CheckForFullBoard() def SwapThisPlayer(): global ThisPlayer if ThisPlayer == "O": ThisPlayer = "X" else: ThisPlayer = "O" # ******************* This is the main program ****************** def main(): InitialiseBoard() # Call board set up routines SetUpGame() OutputBoard() # Below is the loop for players to make moves while GameFinished == False: ThisPlayerMakesMove() OutputBoard() CheckIfThisPlayerHasWon() if GameFinished == False: SwapThisPlayer() # next player takes their turn main() # Call the main program