Skip to main content
Code Review

Return to Answer

Notice removed Insufficient justification by Mast
Notice added Insufficient justification by Mast
Update wording, spelling
Source Link

I think youryou’re over thinking the problem. This is tic-tac-toe after all.

If you don’t know that format that makes a string of length 99 and all spaces. II am going to do the same thing with a list here:

If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in oneone of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say ifdenote whether the game is running or not. YouYou could totally get rid of the lists and strings and just do this with one integer. ThenThen it would only be OneOne loop and 8 tests and you know if you have a winner and who it is, win, loose or draw. IfIf you search online I am sure you can find someone to show you how to do that.

LetsLet’s do this the easiest way with a python list though. YouYou can modify this to work with your list of lists but that is more complicated than this.

I have created a display_board function to show the board. ThenThen I have created the check_board function. YouYou will notice I don’t use all the transforming andand just use list slicing and comparison. ForFor example something like this:

I think your over thinking the problem. This is tic-tac-toe after all.

If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:

If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.

Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.

I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this

I think you’re over thinking the problem. This is tic-tac-toe after all.

If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:

If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to denote whether the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win, loose or draw. If you search online I am sure you can find someone to show you how to do that.

Let’s do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.

I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this:

Trying to remove the horizontal scroll bar
Source Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114

I think your over thinking the problem. This is tic-tac-toe after all.

All you need is one of the following: A list of 9 items or if you want to use half the memory 66 bytes instead of 128 on my machine with python 3.96. You could use just a string and treat it like a list. I will use a list but you can easily replace it with a string like:

I think your over thinking the problem. This is tic-tac-toe after all. All you need is one of the following: A list of 9 items or if you want to use half the memory 66 bytes instead of 128 on my machine with python 3.96. You could use just a string and treat it like a list. I will use a list but you can easily replace it with a string like:
Board " "*9 
If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:
Board=[‘ ‘]*9
If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.
Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.
I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this
Board[‘X’,’ ‘,’ ‘,’ ‘,’X’,’ ‘,’ ‘,’ ‘,’X’]
Is a winning down slope in tic tac toe. To test this you can simply do:
Board[::4]==[‘X’]*3
Will return true so you can do slices to test just against another array which I made with [‘X’]*3.
First I wanted to be able to look at the board so I made a board display function first. Even though I am sure it could be made with loops it is a simple function so I wrote it without and added it to the post since I made it with the checker. 
Second I created the check_winner function with all four checks. As the comment says it returns ‘X’, ‘O’ if either win, it returns ‘ ‘ if there are still moves, and finally 0 if a draw, and The entire function without comments is 11 lines of code.
I followed that up with tests for all possibilities to make sure my function worked and that is also below. You can block and copy this into a source file and run it.
#tic-tac example
# create blank board
board = [" "] * 9
def display_board():
 """this function displays a board define in a single list"""
 temp_board = board[:]
 for i in range(len(temp_board)):
 if temp_board[i] == " ":
 temp_board[i] = i + 1
 print(f"{temp_board[0]} | {temp_board[1]} | {temp_board[2]}")
 print(f"--|---|--")
 print(f"{temp_board[3]} | {temp_board[4]} | {temp_board[5]}")
 print(f"--|---|--")
 print(f"{temp_board[6]} | {temp_board[7]} | {temp_board[8]}")
def check_winner():
 """checks for a winner
 * returns
 'X' for X wins
 * 'O' for O wins
 * ' ' for no winner yet
 * 0 for draw
 """
 # first row check
 for i in range(0, 9, 3):
 if board[i : i + 3] == ["X"] * 3 or board[i : i + 3] == ["O"] * 3:
 return board[i]
 # now column check
 for i in range(3):
 if board[i::3] == ["X"] * 3 or board[i::3] == ["O"] * 3:
 return board[i]
 # now test down slope
 if board[0::4] == ["X"] * 3 or board[0::4] == ["O"] * 3:
 return board[0]
 # now test up slope
 if board[2:7:2] == ["X"] * 3 or board[2:7:2] == ["O"] * 3:
 return board[2]
 # now test if there are more moves possible.
 if " " in board:
 return " "
 # now return a draw
 return 0
# X win tests
board = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "X", "X", "X", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "X", "X", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", "X", " ", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "X", " ", " ", "X", " ", " ", "X", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", " ", "X", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", "X", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
# tests 'O' wins
board = ["O", "O", "O", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "O", "O", "O", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", "O", " ", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "O", " ", " ", "O", " ", " ", "O", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", " ", "O", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", " ", "O", " ", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", "O", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
# test draw
board = ["X", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is a draw")
 Board " "*9 

If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:

 Board=[‘ ‘]*9

If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.

Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.

I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this

 Board[‘X’,’ ‘,’ ‘,’ ‘,’X’,’ ‘,’ ‘,’ ‘,’X’]

Is a winning down slope in tic tac toe. To test this you can simply do:

 Board[::4]==[‘X’]*3

Will return true so you can do slices to test just against another array which I made with [‘X’]*3.

First I wanted to be able to look at the board so I made a board display function first. Even though I am sure it could be made with loops it is a simple function so I wrote it without and added it to the post since I made it with the checker.

Second I created the check_winner function with all four checks. As the comment says it returns ‘X’, ‘O’ if either win, it returns ‘ ‘ if there are still moves, and finally 0 if a draw, and The entire function without comments is 11 lines of code.

I followed that up with tests for all possibilities to make sure my function worked and that is also below. You can block and copy this into a source file and run it.

 #tic-tac example
 # create blank board
 board = [" "] * 9
 
 
 def display_board():
 """this function displays a board define in a single list"""
 temp_board = board[:]
 for i in range(len(temp_board)):
 if temp_board[i] == " ":
 temp_board[i] = i + 1
 print(f"{temp_board[0]} | {temp_board[1]} | {temp_board[2]}")
 print(f"--|---|--")
 print(f"{temp_board[3]} | {temp_board[4]} | {temp_board[5]}")
 print(f"--|---|--")
 print(f"{temp_board[6]} | {temp_board[7]} | {temp_board[8]}")
 
 
 def check_winner():
 """checks for a winner
 * returns
 'X' for X wins
 * 'O' for O wins
 * ' ' for no winner yet
 * 0 for draw
 """
 # first row check
 for i in range(0, 9, 3):
 if board[i : i + 3] == ["X"] * 3 or board[i : i + 3] == ["O"] * 3:
 return board[i]
 
 # now column check
 for i in range(3):
 if board[i::3] == ["X"] * 3 or board[i::3] == ["O"] * 3:
 return board[i]
 
 # now test down slope
 if board[0::4] == ["X"] * 3 or board[0::4] == ["O"] * 3:
 return board[0]
 
 # now test up slope
 if board[2:7:2] == ["X"] * 3 or board[2:7:2] == ["O"] * 3:
 return board[2]
 
 # now test if there are more moves possible.
 if " " in board:
 return " "
 
 # now return a draw
 return 0
 
 
 # X win tests
 board = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", "X", "X", "X", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", " ", " ", " ", "X", "X", "X"]
 print(f"{check_winner()} is the winner.")
 board = ["X", " ", " ", "X", " ", " ", "X", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", "X", " ", " ", "X", " ", " ", "X", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "X", " ", " ", "X", " ", " ", "X"]
 print(f"{check_winner()} is the winner.")
 board = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "X", " ", "X", " ", "X", " ", " "]
 print(f"{check_winner()} is the winner.")
 
 
 # tests 'O' wins
 board = ["O", "O", "O", " ", " ", " ", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", "O", "O", "O", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
 print(f"{check_winner()} is the winner.")
 board = ["O", " ", " ", "O", " ", " ", "O", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", "O", " ", " ", "O", " ", " ", "O", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "O", " ", " ", "O", " ", " ", "O"]
 print(f"{check_winner()} is the winner.")
 board = ["O", " ", " ", " ", "O", " ", " ", " ", "O"]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "O", " ", "O", " ", "O", " ", " "]
 print(f"{check_winner()} is the winner.")
 # test draw
 board = ["X", "O", "X", "O", "X", "O", "O", "X", "O"]
 print(f"{check_winner()} is a draw")
 
 # test still moves to go
 board = [" ", "O", "X", "O", "X", "O", "O", "X", "O"]
 print(f"{check_winner()} is still more spaces")

# test still moves to go
board = [" ", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is still more spaces")
I think your over thinking the problem. This is tic-tac-toe after all. All you need is one of the following: A list of 9 items or if you want to use half the memory 66 bytes instead of 128 on my machine with python 3.96. You could use just a string and treat it like a list. I will use a list but you can easily replace it with a string like:
Board " "*9 
If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:
Board=[‘ ‘]*9
If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.
Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.
I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this
Board[‘X’,’ ‘,’ ‘,’ ‘,’X’,’ ‘,’ ‘,’ ‘,’X’]
Is a winning down slope in tic tac toe. To test this you can simply do:
Board[::4]==[‘X’]*3
Will return true so you can do slices to test just against another array which I made with [‘X’]*3.
First I wanted to be able to look at the board so I made a board display function first. Even though I am sure it could be made with loops it is a simple function so I wrote it without and added it to the post since I made it with the checker. 
Second I created the check_winner function with all four checks. As the comment says it returns ‘X’, ‘O’ if either win, it returns ‘ ‘ if there are still moves, and finally 0 if a draw, and The entire function without comments is 11 lines of code.
I followed that up with tests for all possibilities to make sure my function worked and that is also below. You can block and copy this into a source file and run it.
#tic-tac example
# create blank board
board = [" "] * 9
def display_board():
 """this function displays a board define in a single list"""
 temp_board = board[:]
 for i in range(len(temp_board)):
 if temp_board[i] == " ":
 temp_board[i] = i + 1
 print(f"{temp_board[0]} | {temp_board[1]} | {temp_board[2]}")
 print(f"--|---|--")
 print(f"{temp_board[3]} | {temp_board[4]} | {temp_board[5]}")
 print(f"--|---|--")
 print(f"{temp_board[6]} | {temp_board[7]} | {temp_board[8]}")
def check_winner():
 """checks for a winner
 * returns
 'X' for X wins
 * 'O' for O wins
 * ' ' for no winner yet
 * 0 for draw
 """
 # first row check
 for i in range(0, 9, 3):
 if board[i : i + 3] == ["X"] * 3 or board[i : i + 3] == ["O"] * 3:
 return board[i]
 # now column check
 for i in range(3):
 if board[i::3] == ["X"] * 3 or board[i::3] == ["O"] * 3:
 return board[i]
 # now test down slope
 if board[0::4] == ["X"] * 3 or board[0::4] == ["O"] * 3:
 return board[0]
 # now test up slope
 if board[2:7:2] == ["X"] * 3 or board[2:7:2] == ["O"] * 3:
 return board[2]
 # now test if there are more moves possible.
 if " " in board:
 return " "
 # now return a draw
 return 0
# X win tests
board = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "X", "X", "X", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "X", "X", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", "X", " ", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "X", " ", " ", "X", " ", " ", "X", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", " ", "X", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", "X", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
# tests 'O' wins
board = ["O", "O", "O", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "O", "O", "O", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", "O", " ", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "O", " ", " ", "O", " ", " ", "O", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", " ", "O", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", " ", "O", " ", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", "O", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
# test draw
board = ["X", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is a draw")

# test still moves to go
board = [" ", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is still more spaces")

I think your over thinking the problem. This is tic-tac-toe after all.

All you need is one of the following: A list of 9 items or if you want to use half the memory 66 bytes instead of 128 on my machine with python 3.96. You could use just a string and treat it like a list. I will use a list but you can easily replace it with a string like:

 Board " "*9 

If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:

 Board=[‘ ‘]*9

If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.

Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.

I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this

 Board[‘X’,’ ‘,’ ‘,’ ‘,’X’,’ ‘,’ ‘,’ ‘,’X’]

Is a winning down slope in tic tac toe. To test this you can simply do:

 Board[::4]==[‘X’]*3

Will return true so you can do slices to test just against another array which I made with [‘X’]*3.

First I wanted to be able to look at the board so I made a board display function first. Even though I am sure it could be made with loops it is a simple function so I wrote it without and added it to the post since I made it with the checker.

Second I created the check_winner function with all four checks. As the comment says it returns ‘X’, ‘O’ if either win, it returns ‘ ‘ if there are still moves, and finally 0 if a draw, and The entire function without comments is 11 lines of code.

I followed that up with tests for all possibilities to make sure my function worked and that is also below. You can block and copy this into a source file and run it.

 #tic-tac example
 # create blank board
 board = [" "] * 9
 
 
 def display_board():
 """this function displays a board define in a single list"""
 temp_board = board[:]
 for i in range(len(temp_board)):
 if temp_board[i] == " ":
 temp_board[i] = i + 1
 print(f"{temp_board[0]} | {temp_board[1]} | {temp_board[2]}")
 print(f"--|---|--")
 print(f"{temp_board[3]} | {temp_board[4]} | {temp_board[5]}")
 print(f"--|---|--")
 print(f"{temp_board[6]} | {temp_board[7]} | {temp_board[8]}")
 
 
 def check_winner():
 """checks for a winner
 * returns
 'X' for X wins
 * 'O' for O wins
 * ' ' for no winner yet
 * 0 for draw
 """
 # first row check
 for i in range(0, 9, 3):
 if board[i : i + 3] == ["X"] * 3 or board[i : i + 3] == ["O"] * 3:
 return board[i]
 
 # now column check
 for i in range(3):
 if board[i::3] == ["X"] * 3 or board[i::3] == ["O"] * 3:
 return board[i]
 
 # now test down slope
 if board[0::4] == ["X"] * 3 or board[0::4] == ["O"] * 3:
 return board[0]
 
 # now test up slope
 if board[2:7:2] == ["X"] * 3 or board[2:7:2] == ["O"] * 3:
 return board[2]
 
 # now test if there are more moves possible.
 if " " in board:
 return " "
 
 # now return a draw
 return 0
 
 
 # X win tests
 board = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", "X", "X", "X", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", " ", " ", " ", "X", "X", "X"]
 print(f"{check_winner()} is the winner.")
 board = ["X", " ", " ", "X", " ", " ", "X", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", "X", " ", " ", "X", " ", " ", "X", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "X", " ", " ", "X", " ", " ", "X"]
 print(f"{check_winner()} is the winner.")
 board = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "X", " ", "X", " ", "X", " ", " "]
 print(f"{check_winner()} is the winner.")
 
 
 # tests 'O' wins
 board = ["O", "O", "O", " ", " ", " ", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", "O", "O", "O", " ", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
 print(f"{check_winner()} is the winner.")
 board = ["O", " ", " ", "O", " ", " ", "O", " ", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", "O", " ", " ", "O", " ", " ", "O", " "]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "O", " ", " ", "O", " ", " ", "O"]
 print(f"{check_winner()} is the winner.")
 board = ["O", " ", " ", " ", "O", " ", " ", " ", "O"]
 print(f"{check_winner()} is the winner.")
 board = [" ", " ", "O", " ", "O", " ", "O", " ", " "]
 print(f"{check_winner()} is the winner.")
 # test draw
 board = ["X", "O", "X", "O", "X", "O", "O", "X", "O"]
 print(f"{check_winner()} is a draw")
 
 # test still moves to go
 board = [" ", "O", "X", "O", "X", "O", "O", "X", "O"]
 print(f"{check_winner()} is still more spaces")

Trying to remove the horizontal scroll bar
Source Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114
"""
I think your over thinking the problem. This is tic-tac-toe after all. All you need is one of the following: A list of 9 items or if you want to use half the memory 66 bytes instead of 128 on my machine with python 3.96. You could use just a string and treat it like a list. I will use a list but you can easily replace it with a string like:
Board " "*9 
If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:
Board=[‘ ‘]*9
If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.
Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.
I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this
Board[‘X’,’ ‘,’ ‘,’ ‘,’X’,’ ‘,’ ‘,’ ‘,’X’]
Is a winning down slope in tic tac toe. To test this you can simply do:
Board[::4]==[‘X’]*3
Will return true so you can do slices to test just against another array which I made with [‘X’]*3.
First I wanted to be able to look at the board so I made a board display function first. Even though I am sure it could be made with loops it is a simple function so I wrote it without and added it to the post since I made it with the checker. 
Second I created the check_winner function with all four checks. As the comment says it returns ‘X’, ‘O’ if either win, it returns ‘ ‘ if there are still moves, and finally 0 if a draw, and The entire function without comments is 11 lines of code.
I followed that up with tests for all possibilities to make sure my function worked and that is also below. You can block and copy this into a source file and run it.
"""
#tic-tac example
# create blank board
board = [" "] * 9
def display_board():
 """this function displays a board define in a single list"""
 temp_board = board[:]
 for i in range(len(temp_board)):
 if temp_board[i] == " ":
 temp_board[i] = i + 1
 print(f"{temp_board[0]} | {temp_board[1]} | {temp_board[2]}")
 print(f"--|---|--")
 print(f"{temp_board[3]} | {temp_board[4]} | {temp_board[5]}")
 print(f"--|---|--")
 print(f"{temp_board[6]} | {temp_board[7]} | {temp_board[8]}")
def check_winner():
 """checks for a winner
 * returns
 'X' for X wins
 * 'O' for O wins
 * ' ' for no winner yet
 * 0 for draw
 """
 # first row check
 for i in range(0, 9, 3):
 if board[i : i + 3] == ["X"] * 3 or board[i : i + 3] == ["O"] * 3:
 return board[i]
 # now column check
 for i in range(3):
 if board[i::3] == ["X"] * 3 or board[i::3] == ["O"] * 3:
 return board[i]
 # now test down slope
 if board[0::4] == ["X"] * 3 or board[0::4] == ["O"] * 3:
 return board[0]
 # now test up slope
 if board[2:7:2] == ["X"] * 3 or board[2:7:2] == ["O"] * 3:
 return board[2]
 # now test if there are more moves possible.
 if " " in board:
 return " "
 # now return a draw
 return 0
# X win tests
board = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "X", "X", "X", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "X", "X", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", "X", " ", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "X", " ", " ", "X", " ", " ", "X", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", " ", "X", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", "X", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
# tests 'O' wins
board = ["O", "O", "O", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "O", "O", "O", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", "O", " ", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "O", " ", " ", "O", " ", " ", "O", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", " ", "O", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", " ", "O", " ", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", "O", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
# test draw
board = ["X", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is a draw")
# test still moves to go
board = [" ", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is still more spaces")
"""
I think your over thinking the problem. This is tic-tac-toe after all. All you need is one of the following: A list of 9 items or if you want to use half the memory 66 bytes instead of 128 on my machine with python 3.96. You could use just a string and treat it like a list. I will use a list but you can easily replace it with a string like:
Board " "*9 
If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:
Board=[‘ ‘]*9
If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.
Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.
I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this
Board[‘X’,’ ‘,’ ‘,’ ‘,’X’,’ ‘,’ ‘,’ ‘,’X’]
Is a winning down slope in tic tac toe. To test this you can simply do:
Board[::4]==[‘X’]*3
Will return true so you can do slices to test just against another array which I made with [‘X’]*3.
First I wanted to be able to look at the board so I made a board display function first. Even though I am sure it could be made with loops it is a simple function so I wrote it without and added it to the post since I made it with the checker. 
Second I created the check_winner function with all four checks. As the comment says it returns ‘X’, ‘O’ if either win, it returns ‘ ‘ if there are still moves, and finally 0 if a draw, and The entire function without comments is 11 lines of code.
I followed that up with tests for all possibilities to make sure my function worked and that is also below. You can block and copy this into a source file and run it.
"""
#tic-tac example
# create blank board
board = [" "] * 9
def display_board():
 """this function displays a board define in a single list"""
 temp_board = board[:]
 for i in range(len(temp_board)):
 if temp_board[i] == " ":
 temp_board[i] = i + 1
 print(f"{temp_board[0]} | {temp_board[1]} | {temp_board[2]}")
 print(f"--|---|--")
 print(f"{temp_board[3]} | {temp_board[4]} | {temp_board[5]}")
 print(f"--|---|--")
 print(f"{temp_board[6]} | {temp_board[7]} | {temp_board[8]}")
def check_winner():
 """checks for a winner
 * returns
 'X' for X wins
 * 'O' for O wins
 * ' ' for no winner yet
 * 0 for draw
 """
 # first row check
 for i in range(0, 9, 3):
 if board[i : i + 3] == ["X"] * 3 or board[i : i + 3] == ["O"] * 3:
 return board[i]
 # now column check
 for i in range(3):
 if board[i::3] == ["X"] * 3 or board[i::3] == ["O"] * 3:
 return board[i]
 # now test down slope
 if board[0::4] == ["X"] * 3 or board[0::4] == ["O"] * 3:
 return board[0]
 # now test up slope
 if board[2:7:2] == ["X"] * 3 or board[2:7:2] == ["O"] * 3:
 return board[2]
 # now test if there are more moves possible.
 if " " in board:
 return " "
 # now return a draw
 return 0
# X win tests
board = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "X", "X", "X", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "X", "X", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", "X", " ", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "X", " ", " ", "X", " ", " ", "X", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", " ", "X", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", "X", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
# tests 'O' wins
board = ["O", "O", "O", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "O", "O", "O", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", "O", " ", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "O", " ", " ", "O", " ", " ", "O", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", " ", "O", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", " ", "O", " ", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", "O", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
# test draw
board = ["X", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is a draw")
# test still moves to go
board = [" ", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is still more spaces")
I think your over thinking the problem. This is tic-tac-toe after all. All you need is one of the following: A list of 9 items or if you want to use half the memory 66 bytes instead of 128 on my machine with python 3.96. You could use just a string and treat it like a list. I will use a list but you can easily replace it with a string like:
Board " "*9 
If you don’t know that format that makes a string of length 9 and all spaces. I am going to do the same thing with a list here:
Board=[‘ ‘]*9
If I was going to write the game fully though I would do it with a bitmap to store everything including player, state of board, and both X and O positions. That could all be stored in one of python’s integers because python uses 24 bits on my machine to store an integer. You only need 16 bits to store board state and one bit to store turn and maybe 1 bit to say if the game is running or not. You could totally get rid of the lists and strings and just do this with one integer. Then it would only be One loop and 8 tests and you know if you have a winner and who it is, win loose or draw. If you search online I am sure you can find someone to show you how to do that.
Lets do this the easiest way with a python list though. You can modify this to work with your list of lists but that is more complicated than this.
I have created a display_board function to show the board. Then I have created the check_board function. You will notice I don’t use all the transforming and just use list slicing and comparison. For example something like this
Board[‘X’,’ ‘,’ ‘,’ ‘,’X’,’ ‘,’ ‘,’ ‘,’X’]
Is a winning down slope in tic tac toe. To test this you can simply do:
Board[::4]==[‘X’]*3
Will return true so you can do slices to test just against another array which I made with [‘X’]*3.
First I wanted to be able to look at the board so I made a board display function first. Even though I am sure it could be made with loops it is a simple function so I wrote it without and added it to the post since I made it with the checker. 
Second I created the check_winner function with all four checks. As the comment says it returns ‘X’, ‘O’ if either win, it returns ‘ ‘ if there are still moves, and finally 0 if a draw, and The entire function without comments is 11 lines of code.
I followed that up with tests for all possibilities to make sure my function worked and that is also below. You can block and copy this into a source file and run it.
#tic-tac example
# create blank board
board = [" "] * 9
def display_board():
 """this function displays a board define in a single list"""
 temp_board = board[:]
 for i in range(len(temp_board)):
 if temp_board[i] == " ":
 temp_board[i] = i + 1
 print(f"{temp_board[0]} | {temp_board[1]} | {temp_board[2]}")
 print(f"--|---|--")
 print(f"{temp_board[3]} | {temp_board[4]} | {temp_board[5]}")
 print(f"--|---|--")
 print(f"{temp_board[6]} | {temp_board[7]} | {temp_board[8]}")
def check_winner():
 """checks for a winner
 * returns
 'X' for X wins
 * 'O' for O wins
 * ' ' for no winner yet
 * 0 for draw
 """
 # first row check
 for i in range(0, 9, 3):
 if board[i : i + 3] == ["X"] * 3 or board[i : i + 3] == ["O"] * 3:
 return board[i]
 # now column check
 for i in range(3):
 if board[i::3] == ["X"] * 3 or board[i::3] == ["O"] * 3:
 return board[i]
 # now test down slope
 if board[0::4] == ["X"] * 3 or board[0::4] == ["O"] * 3:
 return board[0]
 # now test up slope
 if board[2:7:2] == ["X"] * 3 or board[2:7:2] == ["O"] * 3:
 return board[2]
 # now test if there are more moves possible.
 if " " in board:
 return " "
 # now return a draw
 return 0
# X win tests
board = ["X", "X", "X", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "X", "X", "X", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "X", "X", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", "X", " ", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "X", " ", " ", "X", " ", " ", "X", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", " ", "X", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = ["X", " ", " ", " ", "X", " ", " ", " ", "X"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "X", " ", "X", " ", "X", " ", " "]
print(f"{check_winner()} is the winner.")
# tests 'O' wins
board = ["O", "O", "O", " ", " ", " ", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", "O", "O", "O", " ", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", " ", " ", " ", " ", "O", "O", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", "O", " ", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
board = [" ", "O", " ", " ", "O", " ", " ", "O", " "]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", " ", "O", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = ["O", " ", " ", " ", "O", " ", " ", " ", "O"]
print(f"{check_winner()} is the winner.")
board = [" ", " ", "O", " ", "O", " ", "O", " ", " "]
print(f"{check_winner()} is the winner.")
# test draw
board = ["X", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is a draw")
# test still moves to go
board = [" ", "O", "X", "O", "X", "O", "O", "X", "O"]
print(f"{check_winner()} is still more spaces")
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /