Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tic-Tac-Toe
- row0 = ["", "", ""]
- row1 = ["", "", ""]
- row2 = ["", "", ""]
- currentPlayer = "X"
- done = False
- def printBoard():
- global currentPlayer
- global done
- # print current player turn and game board
- print("Current Player: " + currentPlayer)
- print(" -0-1-2-")
- print("0: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
- print(" -------")
- print("1: |" + row1[0] + "|" + row1[1] + "|" + row1[2] + "|")
- print(" -------")
- print("2: |" + row2[0] + "|" + row2[1] + "|" + row2[2] + "|")
- print(" -------")
- print("")
- # get row and column for current player
- inputRow = int(input("Enter player " + currentPlayer + " row: "))
- inputCol = int(input("Enter player " + currentPlayer + " col: "))
- # place mark in appropriate cell
- if (inputRow == 0):
- row0[inputCol] = currentPlayer
- elif (inputRow == 1):
- row1[inputCol] = currentPlayer
- elif (inputRow == 2):
- row2[inputCol] = currentPlayer
- else:
- done == True
- # escape from loop on invalid row
- # Create an “if†block that will change the player.
- if currentPlayer == "X":
- currentPlayer = "o"
- else:
- currentPlayer = "X"
- return
- # Check to see if there is 3-in-a-row in any direction.
- # There are 8 possibilities, so write code that will manually check each one for the samemarks in a row.
- # If 3 in a row is found, print a winning message and set a variable to “True†to escape loop.
- #The first row and column has been done for you. Complete the remaining rows and columns.
- #Check the diagonals for 3 in a diagonal too. The first one has been done for you.
- def CheckRows():
- global done
- # row 0
- if ((row0[0] == "X") and (row0[1] == "X") and (row0[2] == "X")):
- print("Player X wins in row 0!")
- done = True
- if ((row0[0] == "o") and (row0[1] == "o") and (row0[2] == "o")):
- print("Player o wins in row 0!")
- done = True
- # row 1
- if ((row1[0] == "X") and (row1[1] == "X") and (row1[2] == "X")):
- print("Player X wins in row 1!")
- done = True
- if ((row1[0] == "o") and (row1[1] == "o") and (row1[2] == "o")):
- print("Player o wins in row 1!")
- done = True
- # row 2
- if ((row2[0] == "X") and (row2[1] == "X") and (row2[2] == "X")):
- print("Player X wins in row 2!")
- done = True
- if ((row2[0] == "o") and (row2[1] == "o") and (row2[2] == "o")):
- print("Player o wins in row 2!")
- done = True
- return
- def CheckCols():
- global done
- # column 0
- if ((row0[0] == "X") and (row1[0] == "X") and (row2[0] == "X")):
- print("Player X wins in column 0!")
- done = True
- if ((row0[0] == "o") and (row1[0] == "o") and (row2[0] == "o")):
- print("Player o wins in column 0!")
- done = True
- # column 1
- if ((row0[1] == "X") and (row1[1] == "X") and (row2[1] == "X")):
- print("Player X wins in row 0!")
- done = True
- if ((row0[1] == "o") and (row1[1] == "o") and (row2[1] == "o")):
- print("Player o wins in row 0!")
- done = True
- # column 2
- if ((row0[2] == "X") and (row1[2] == "X") and (row2[2] == "X")):
- print("Player X wins in row 0!")
- done = True
- if ((row0[2] == "o") and (row1[2] == "o") and (row2[2] == "o")):
- print("Player o wins in row 0!")
- done = True
- return
- def CheckDiagonal():
- global done
- # first diagonal
- if ((row0[0] == "X") and (row1[1] == "X") and (row2[2] == "X")):
- print("Player X wins in first diagonal!")
- done = True
- if ((row0[0] == "o") and (row1[1] == "o") and (row2[2] == "o")):
- print("Player o wins in first diagonal!")
- done = True
- # second diagonal
- if ((row0[2] == "X") and (row1[1] == "X") and (row2[0] == "X")):
- print("Player X wins in first diagonal!")
- done = True
- if ((row0[2] == "o") and (row1[1] == "o") and (row2[0] == "o")):
- print("Player o wins in first diagonal!")
- done = True
- return
- #create game board
- # game board is made up of 3 lists containing 3 elements each
- # every element in the list starts out as a blank space, and may gain an "X" or "O" as the user plays the game.
- # Initialize a current player variable
- # Create a “counter or variable†that should become “True†when the game is over, butremains “False†until it is over
- # Create a loop that runs until the game is over using a “while loopâ€.
- while True:
- if not done:
- printBoard()
- CheckRows()
- CheckCols()
- CheckDiagonal()
- print(" -0-1-2-")
- print("0: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
- print("1: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
- print("2: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
- print(" -------")
- print("")
- print("GAME OVER")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement