This content originally appeared on DEV Community and was authored by Hachikoi
Tick Tack Toe
From a course I bought in Udemy
I think I covered any possible type error that the user could make, please tell me if I´m missing something!!
What it does?
-Ask the user to choose between X or O as a marker
-Random to choose who is first
-Verifies it´s an empty space
-Then places the marker in a place that the player chooses
-Verifies is someone has won
-So on, so on
-At the end ask if they want to play again
I first try it without any help for 4 days,
then finished whit some kind of walkthrough they give and then I mixed what I like and what they used to optimize my project! Whit a total of 7 days and approx. 800 written lines of code!!
Pro tip:
To upload code just go to the programs you use, select all the code + tab + control c
Then just paste it here!
code
import random
def final_display(display1,numepad): #Yei! すき!好き!スキ!1-9r
print(''*3)
print(display1[:3],'°-°',numepad[:3])
print('---------------',' + ','---------')
print(display1[3:6],'°_°',numepad[3:6])
print('---------------',' + ','---------')
print(display1[6:],'°_°',numepad[6:])
print('---------------',' + ','---------')
def choice_xy():
print("Welcome to my tic tac toe game!~")
choice = input("Do you want X or O?: ")
while choice.lower() not in ['x','o']:
choice = input("I said X or O?... ")
if choice.lower() == 'x':
return 'X','O'
else:
return 'O','X'
def place_symbol(display2,symbol,where): #just places the correct symbol
display2[where] = symbol
def win_check(board,mark):
#Checks for all the Rows
#then all the Columns
#at least if the diagonals are equial to each other and the Marker
if board[0] == mark and board[1] == mark and board[2] == mark:
return True
elif board[3] == mark and board[4] == mark and board[5] == mark:
return True
elif board[6] == mark and board[7] == mark and board[8] == mark:
return True
elif board[0] == mark and board[3] == mark and board[6] == mark:
return True
elif board[1] == mark and board[4] == mark and board[7] == mark:
return True
elif board[2] == mark and board[5] == mark and board[8] == mark:
return True
elif board[6] == mark and board[4] == mark and board[2] == mark:
return True
elif board[0] == mark and board[4] == mark and board[8] == mark:
return True
else:
return False
def random_turn():
turn = random.randint(1,2)
if turn == 1:
#print('Player 1')
return turn
else:
#print('Player 2')
return turn
def available_check(display3,position3):
#Returns bool to check is the space is available
return not display3[position3-1].isupper()
#True == Occupied, False == Empty
def full_display(display7):
#Checks if the display is full == True
counter = 0
for i in [1,2,3,4,5,6,7,8,9]:
if display7[i-1] in 'X' or display7[i-1] in 'O':
counter += 1
return counter == 9
def where_put_it(): #Ask where to put the marker and verifies is in 1-9
where = input('Where do you want to make your move?(1-9) ')
while where not in ('1','2','3','4','5','6','7','8','9'):
where = input('違う Invalid move, try again .... ')
where2 = int(where)
return where2
def play_again_ask(): #If wanna play again, returns True
choice = input('Wanna play again?(Yes or No): ')
while choice.lower() not in ['yes', 'no']:
choice = input('あのさ。。Yes or No: ')
if choice.lower() == 'yes':
return 'yes'
else: #Just can be 'yes' or 'no'
return 'no'
def pc_gaming():
display0_0 = [' ',' ',' ',
' ',' ',' ',
' ',' ',' ']
numepad = [1, 2, 3,
4, 5, 6,
7, 8, 9]
player1S,player2S = choice_xy()
if random_turn() == 1:
turnCast = 1
print('Player 1 goose first~')
else:
turnCast = 2
print('Player 2 goose first~')
while not full_display(display0_0):
if turnCast % 2 == 0:
print('Player 2´s turn~')
final_display(display0_0, numepad)
castPosition = where_put_it()-1 # Checks is in 1-9
##################
while display0_0[castPosition].isupper():
print('-Occupied field-')
castPosition = where_put_it()-1
place_symbol(display0_0,player2S,castPosition)
if win_check(display0_0,player2S):
final_display(display0_0, numepad)
print('Player 2 WINS!')
break
elif turnCast % 2 != 0:
print('Player 1´s turn~')
final_display(display0_0,numepad)
castPosition = where_put_it()-1 # Checks is in 1-9
##############
while display0_0[castPosition].isupper():
print('-Occupied field-')
castPosition = where_put_it()-1
place_symbol(display0_0,player1S,castPosition)
if win_check(display0_0,player1S):
final_display(display0_0, numepad)
print('Player 1 WINS!')
break
turnCast += 1
if not(win_check(display0_0,player1S) and win_check(display0_0,player1S)) and full_display(display0_0):
print('Nobody wins...')
pc_gaming()
response = play_again_ask()
if response.lower == 'yes':
pc_gaming()
else:
print('Thanks for playing!')
This content originally appeared on DEV Community and was authored by Hachikoi
Hachikoi | Sciencx (2021-04-22T02:56:01+00:00) My first python project!. Retrieved from https://www.scien.cx/2021/04/22/my-first-python-project/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.