Help me with my tic tac toe game pls i need it before monday

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
the computer doesn’t take its second turn

<What do you expect to happen instead?>
it should print out all the X’s

```python

import random
winning = False
print(“tic tac toe. you will be noughts (O). you will also go first”)
board = [[" “,”|"," “,”|"," " ],
["-","+","-","+","-"],
[" “,”|"," “,”|"," “],
[”-","+","-","+","-"],
[" “,”|"," “,”|"," "]]

def places(place):
place = place - 1
place = place * 2
return place

def print_board(board):
print(board[0][0], board[0][1], board[0][2], board[0][3], board[0][4])
print(board[1][0], board[1][1], board[1][2], board[1][3], board[1][4])
print(board[2][0], board[2][1], board[2][2], board[2][3], board[2][4])
print(board[3][0], board[3][1], board[3][2], board[3][3], board[3][4])
print(board[4][0], board[4][1], board[4][2], board[4][3], board[4][4])

def if_player_won(board1, board2, board3, a):
if board1 == a and board2 == a and board3 == a:
return True
else:
return False

def player_winning(board1, board2, board3, a, b):
if board1 == a and board2 == a and board3 == a:
print(b)
return True
else:
return False

def if_computer_won(board1, board2, board3, a):
if board1 == a and board2 == a and board3 == a:
return True
else:
return False

def computer_won(board1, board2, board3, a, b):
if board1 == a and board2 == a and board3 == a:
print(b)
return True
else:
return False

def if_computer_can_win(board1, board2, board3, a, b):
winning = False
if board1 == a and board2 == a and board3 == b:
return True
elif board1 == a and board3 == a and board2 == b:
return True
elif board2 == a and board3 == a and board1 == b:
return True
else:
return False

def computer_win(board1, board2, board3, a, b, c):
if board1 == a and board2 == a and board3 == b:
board3 = a
return True

``````elif board1 == a and board3 == a and board2 == b:
board2 = a
return True
print(c)

elif board2 == a and board3 == a and board1 == b:
board1 = a
return True
print(c)
else:
return False
``````

def if_computer_needs_to_block(board1, board2, board3, a, b):
if board1 == a and board2 == a and board3 == b:
return True
elif board1 == a and board3 == a and board2 == b:
return True
elif board2 == a and board3 == a and board1 == b:
return True
else:
return False

def computer_block(board1, board2, board3, a, b, c):
if board1 == a and board2 == a and board3 == b:
board3 = c
return True

``````elif board1 == a and board3 == a and board2 == b:
board2 = c
return True

elif board2 == a and board3 == a and board1 == b:
board1 = c
return True
else:
return False
``````

def computer_move(board, a, b):
move = False
while move == False:
x = random.randint(0, 4)
y = random.randint(0, 4)
if board[y] == a:
board[y] = b
move = True
return board
else:
move = False

count = 0

winning_message = “well done you have won”

winner = False

lost = “you have lost”

computer_turn = False

while winning == False and count <= 5:
print_board(board)
y = int(input(“which row would you like? (rows start at the top)”))
x = int(input(“which column would you like?”))
a = places(y)
b = places(x)

``````if board[a][b] == " ":
board[a][b] = "O"
count += 1
computer_turn = True

else:
print("retry")
computer_turn == True
if computer_turn == True:
if if_player_won(board[0][0], board[0][2], board[0][4], "O") == True:
print(player_winning(board[0][0], board[0][2], board[0][4], "O"))
winner = True

elif if_player_won(board[2][0], board[2][2], board[2][4], "O") == True:
print(player_winning(board[2][0], board[2][2], board[2][4], "O", winning_message))
winning = True

elif if_player_won(board[4][0], board[4][2], board[4][4], "O") == True:
print(player_winning(board[4][0], board[4][2], board[4][4], "O", winning_message))
winning = True

elif if_player_won(board[0][0], board[2][0], board[4][0], "O") == True:
print(player_winning(board[0][0], board[2][0], board[4][0], "O", winning_message))
winning = True

elif if_player_won(board[0][2], board[2][2], board[4][2], "O") == True:
print(player_winning(board[0][2], board[2][2], board[4][2], "O", winning_message))
winning = True

elif if_player_won(board[0][4], board[2][4], board[4][4], "O") == True:
print(player_winning(board[0][4], board[2][4], board[4][4], "O", winning_message))
winning = True

elif if_player_won(board[0][4], board[2][2], board[4][0], "O") == True:
print(player_winning(board[0][4], board[2][2], board[4][0], "O", winning_message))
winning = True

elif if_player_won(board[0][0], board[2][2], board[4][4], "O") == True:
print(player_winning(board[0][0], board[2][2], board[4][4], "O", winning_message))
winning = True

elif if_computer_won(board[0][0], board[0][2], board[0][4], "X") == True:
print(computer_won(board[0][0], board[0][2], board[0][4], "X", lost))
winning = True

elif if_computer_won(board[0][0], board[0][2], board[0][4], "X") == True:
print(computer_won(board[0][0], board[0][2], board[0][4], "X", lost))
winning = True

elif if_computer_won(board[2][0], board[2][2], board[2][4], "X") == True:
print(computer_won(board[2][0], board[2][2], board[2][4], "X", lost))
winning = True

elif if_computer_won(board[4][0], board[4][2], board[4][4], "X") == True:
print(computer_won(board[4][0], board[4][2], board[4][4], "X", lost))
winning = True

elif if_computer_won(board[0][0], board[2][0], board[4][0], "X") == True:
print(computer_won(board[0][0], board[2][0], board[4][0], "X", lost))
winning = True

elif if_computer_won(board[0][2], board[2][2], board[4][2], "X") == True:
print(computer_won(board[0][0], board[2][2], board[4][2], "X", lost))
winning = True

elif if_computer_won(board[4][4], board[4][4], board[4][4], "X") == True:
print(computer_won(board[4][4], board[4][4], board[4][4], "X", lost))
winning = True

elif if_computer_won(board[0][0], board[2][2], board[4][4], "X") == True:
print(computer_won(board[0][0], board[2][2], board[4][4], "X", lost))
winning = True

elif if_computer_won(board[0][4], board[2][2], board[4][0], "X") == True:
print(computer_won(board[0][4], board[2][2], board[4][0], "X", lost))
winning = True

elif if_computer_can_win(board[0][0], board[0][2], board[0][4], "X", " ") == True:
print(computer_win(board[0][0], board[0][2], board[0][4], "X", " ", lost))
winning = True

elif if_computer_can_win(board[2][0], board[2][2], board[2][4], "X", " ") == True:
print(computer_win(board[2][0], board[2][2], board[2][4], "X", " ", lost))
winning = True

elif if_computer_can_win(board[4][0], board[4][2], board[4][4], "X", " ") == True:
print(computer_win(board[4][0], board[4][2], board[4][4], "X", " ", lost))
winning = True

elif if_computer_can_win(board[0][0], board[2][0], board[4][0], "X", " ") == True:
print(computer_win(board[0][0], board[2][0], board[4][0], "X", " ", lost))
winning = True

elif if_computer_can_win(board[0][2], board[2][2], board[4][2], "X", " ") == True:
print(computer_win(board[0][2], board[2][2], board[4][2], "X", " ", lost))
winning = True

elif if_computer_can_win(board[0][4], board[2][4], board[4][4], "X", " ") == True:
print(computer_win(board[0][4], board[2][4], board[4][4], "X", " ", lost))
winning = True

elif if_computer_can_win(board[0][0], board[2][2], board[4][4], "X", " ") == True:
print(computer_win(board[0][0], board[2][2], board[4][4], "X", " ", lost))
winning = True

elif if_computer_can_win(board[0][4], board[2][2], board[4][0], "X", " ") == True:
print(computer_win(board[0][4], board[2][2], board[4][0], "X", " ", lost))
winning = True

elif if_computer_needs_to_block(board[0][0], board[0][2], board[0][4], "O", " ") == True:
computer_block(board[0][0], board[0][2], board[0][4], "O", " ", "X")

elif if_computer_needs_to_block(board[2][0], board[2][2], board[2][4], "O", " ") == True:
computer_block(board[2][0], board[2][2], board[2][4], "O", " ", "X")

elif if_computer_needs_to_block(board[4][0], board[4][2], board[4][4], "O", " ") == True:
computer_block(board[4][0], board[4][2], board[4][4], "O", " ", "X")

elif if_computer_needs_to_block(board[0][0], board[2][0], board[4][0], "O", " ") == True:
computer_block(board[0][0], board[2][0], board[4][0], "O", " ", "X")

elif if_computer_needs_to_block(board[0][2], board[2][2], board[4][2], "O", " ") == True:
computer_block(board[0][2], board[2][2], board[4][2], "O", " ", "X")

elif if_computer_needs_to_block(board[0][4], board[2][4], board[4][4], "O", " ") == True:
computer_block(board[0][4], board[2][4], board[4][4], "O", " ", "X")

elif if_computer_needs_to_block(board[0][0], board[2][2], board[4][4], "O", " ") == True:
computer_block(board[0][0], board[2][2], board[4][4], "O", " ", "X")

elif if_computer_needs_to_block(board[0][4], board[2][2], board[4][0], "O", " ") == True:
computer_block(board[0][4], board[2][2], board[4][0], "O", " ", "X")

else:
computer_move(board, " ", "X")
``````

print_board(board)

``<do not remove the three backticks above>``

Are you saying that there’s a bug in code that you have written that is supposed to cause it to take another turn?
Or that you’re looking for a way to implement it?

Either way, you’re missing a lot of context to your question, what you’ve done is to file a bug report for your code. But you’re the developer, so you’re the one who needs to consider that bug report.

If you’ve got more well defined questions then that’s fine. But this one isn’t.

it should have printed the x’s as after the first one it didn’t print out any of the x’s that it should have e.g.
the first turn:
|O|
±-+
X| |

the second turn:
|O|O
±-+
X| |

in the second turn the x wasn’t printed out.

You’re still just filing a bug report. It’s your code. Not anybody else’s.

Here’s what I suggest you do:
Consider what events should be taking place in your code in order for it to be carrying out a second turn.
Then investigate whether those things are happening. When you’ve found which one isn’t, then you would consider which part of the code was responsible for doing that right, and now you’ll have pin-pointed where the problem is.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.