Python TIC TAC TOE Tutorial | Beginner Friendly Tutorial

preview_player
Показать описание
Tic tac toe tutorial in python. Hey everyone! In today's video I showcase an ultra-beginner friendly project all beginners should try. We reinforce many beginner skills and end up with a great product! As I said in the video, linked below is the source code:

On this channel we focus on all things Python. Whether it is a project tutorial/showcase, teaching different Python concepts, or just live coding this is one of the best channels for programmers. Check out my links down below  👇

Links:
Рекомендации по теме
Комментарии
Автор

board = ['-', '-', '-',
'-', '-', '-',
'-', '-', '-']
currentPlayer = "X"
winner = None
gameRunning = True

#printing board

def printBoard(board):
print(board[0] + " | " + board[1] + " | " + board[2])
print("-" * 9)
print(board[3] + " | " + board[4] + " | " + board[5])
print("-" * 9)
print(board[6] + " | " + board[7] + " | " + board[8])

#take player input
def playerInput(board):
while True:
if currentPlayer == "X":
inp = int(input(f"Enter a number 1-9 \033[1;34m Player (X) \033[0;0m : "))
else:
inp = int(input(f"Enter a number 1-9 \033[1;31m Player (0) \033[0;0m : "))
if inp >= 1 and inp <= 9 and board[inp-1] == "-":
board[inp-1] = currentPlayer
break
else:
if currentPlayer == "X":
print(f"Oops! Try again! Player - \033[1;34m Player (X) \033[0;0m ! ")
else:
print(f"Oops! Try again! Player - \033[1;31m Player (0) \033[0;0m ! ")
printBoard(board)


#check for win or tie
def checkHorizontal(board):
global winner
if (board[0] == board[1] == board[2] and board[0] != "-") or (board[3] == board[4] == board[5] and board[3] != "-") or (board[6] == board[7] == board[8] and board[6] != "-"):
winner = currentPlayer
return True
def checkRow(board):
global winner
if (board[0] == board[3] == board[6] and board[0] != "-") or (board[1] == board[4] == board[7] and board[1] != "-") or (board[2] == board[5] == board[8] and board[2] != "-"):
winner = currentPlayer
return True
def checkDiagonal(board):
global winner
if (board[0] == board[4] == board[5] and board[0] != "-") or (board[2] == board[4] == board[6] and board[2] != "-"):
winner = currentPlayer
return True
def checkTie(board):
global gameRunning
if "-" not in board:
printBoard(board)
print("Its a tie")
gameRunning = False

def checkWin():
if checkDiagonal(board) or checkHorizontal(board) or checkRow(board):
print(f"The winner is {winner}")

#switch the player
def switchPlayer():
global currentPlayer
if currentPlayer == "X":
currentPlayer = "O"
else:
currentPlayer = "X"



#check for win or tie again

while gameRunning:
printBoard(board)
if winner != None:
break
playerInput(board)
checkWin()
checkTie(board)
switchPlayer()

Fresco
Автор

after dealing with some hiccups throughout this tutorial. I noticed there were some indentation errors for me, but overall I got it to work in the end! Thank you for your tutorial as this was my first ever project and I am getting a grasp on the terminology.

Yinyang
Автор

You explained this better than how my professor explain python... thank you!!

denisem
Автор

Hey when the game ends with a horizontal or diag or row as the last input (no "-" on the board remaining but last input made one of 3 lines) it still says that it's a tie despite clearly being a win for one of the players. How can I fix this?

alzhanvoid
Автор

the most easy way and literally easy to understand. The best tutorial

_iam_seven_
Автор

hi just wanted to ask, what does
"-1" in board[inp-1] == "-" ?

exED
Автор

while running the "gamerunning func at 9:30, its saying ""bool object is not callable""////--> how do we solve this??

a.hannan_
Автор

For those who found the bug that appears when the last input is a winning move, and it displays both the Win AND Tie text, this should fix it. If you add a "exit()" command inside of the "def checkWin()" segment, it should work itself out:

def checkWin():
if checkDiagonal(board) or checkHorizontal(board) or checkRow(board):
print(f"The winner is {winner}")
gameRunning = False
exit()

Since the gameRunning loop checks for a Win BEFORE it checks for a Tie, you only have to apply this to the "def checkWin()" segment :)

River
Автор

I tried running it @9:39 but it keeps telling me “oops player is already in that spot” but there’s no player. I need help

riskatjukyungssi
Автор

Obviously you are coming from JS, in python we do board = ["-"] * 9 and you don't need to pass board to the function cause it's on global scope and functions defined after that has access to the board.

xzex
Автор

so damn awesome my man. i loved it all the way. my first big project in learning python. thanks for the tutorial.

subhransu
Автор

Great beginner-friendly tutorial on how to build a simple project like TicTacToe!

vanlang
Автор

in the function userInput you should call it again recursively in the else statement like this:
def playerInput(board):
inp = int(input("Select a spot 1-9: "))
if board[inp-1] == "-":
board[inp-1] = currentPlayer
else:
print("Oops player is already at that spot.")
playerInput(board)

Because if the user chooses a number which was already chosen it will display the message and then go to the next player, but we want the same user to try again in another spot, not switch players after an error

karimeissa
Автор

you make it so easy to understand, but actually making it and developing the program is a whole different story!

Caroline-pbxx
Автор

This is a great learning tool. One thing we might be able to improve upon is the computer play. I was able to get it to show both "X" wins and "O" wins on the same board. It should shut down immediately after the X move

larryhatcher
Автор

I m wondering how to get the game to reset after the win. I was thinking that after gamerunning = false you could break the loop... but doesn't seem to work for me... any tips?

woovs
Автор

Hi @coachcode for the code board(inp-1) == "-" at 8:02 of the video
I understand that it is to check if the position picked on the board by the player is empty. But why is there -1 from inp? Shouldn't it be just board(inp) to check the availability of the position picked by the player on the board?

edenlee
Автор

Something is false I think. If you use a number which is already taken, the player switch. So one person can play two times if the other took a number which has alraedy been used (sry for bad english but hope y'all understand)

drsoundy
Автор

Hi, i've found 2 big problems...first if you pplay against the computer he can't win, second if then computer win the game continue and if you win the game give you the win and when the board is full it's a tie. If you play against yourself using this combination of number (1-2-3-4-5-6-8-7-9) the game give you the win but it's a tie and immediately after the game print that it's a tie because the board is full...I'm trying to find where the problems, did you had already found them?

mxo
Автор

Why is the switchPlayer() at the end of the def computer(board) and not behind checkTie(board) in the end?

DiegoSabajo-vf
welcome to shbcf.ru