Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python TicTacToe win conditions

ThomasMarx

New Coder
playerx = input("Insert player 1 username: ")
playery = input("Insert player 2 username: ")
board = [["1,1", "1,2", "1,3"], ["2,1", "2,2", "2,3"], ["3,1", "3,2", "3,3"]]
def printBoard(board):
print(board[0][0] + '|' + board[0][1] + '|' + board[0][2])
print(' - + - + -')
print(board[1][0] + '|' + board[1][1] + '|' + board[1][2])
print(' - + - + -')
print(board[2][0] + '|' + board[2][1] + '|' + board[2][2])
gamenum = 1
while gamenum < 10:
printBoard(board)
playerx1 = int(input("what row? "))
playerx2 = int(input("Which space in that row? "))
if gamenum == 1 or 3 or 5 or 7 or 9:
if board[playerx1 - 1][playerx2 - 1] == "X" or board[playerx1 - 1][playerx2 - 1] == "O":
print("There is already something here. You just lost your turn, " + playerx)
continue
else:
board[playerx1 - 1][playerx2 - 1] = "X"
gamenum = gamenum + 1
printBoard(board)
playery1 = int(input("what row? "))
playery2 = int(input("What space in that row? "))
if gamenum == 2 or 4 or 6 or 8:
if board[playery1 - 1][playery2 - 1] == "X" or board[playery1 - 1] == "O":
print("There is already something here. You just lost your turn, " + playery)
continue
else:
board[playery1 - 1][playery2 - 1] = "O"
gamenum = gamenum + 1

if board[0][0] and board[0][1] and board[0][2] == "X":
gamenum = 100
print(playerx + " Wins!")
if board[0][0] and board [0][1] and board [0][2] == "O":
gamenum = 100
print(playery + " Wins!")

i have it so where my code will check the top line and it seemingly works, however, i feel like doing this for every win option would be tedious and unoptimized, is there a better way to do this?
 
Back
Top Bottom