Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
My code for Tic-Tac-Toe (beginner), compared to my mate that works at google's code.

Thought it would be interesting seeing someone beginner code, vs someone's professional code on the same project. After I sent him my code he replied with how he would have done it, as a comparison.

**My code:**

import random

def plyr1wintest(): #checks if player has 3 in a row
global winner
if (1 in plyr1list) and (2 in plyr1list) and (3 in plyr1list) or \
(4 in plyr1list) and (5 in plyr1list) and (6 in plyr1list) or \
(7 in plyr1list) and (8 in plyr1list) and (9 in plyr1list) or \
(1 in plyr1list) and (4 in plyr1list) and (7 in plyr1list) or \
(2 in plyr1list) and (5 in plyr1list) and (8 in plyr1list) or \
(3 in plyr1list) and (6 in plyr1list) and (9 in plyr1list) or \
(1 in plyr1list) and (5 in plyr1list) and (9 in plyr1list) or \
(3 in plyr1list) and (5 in plyr1list) and (7 in plyr1list):
print ((name1) + ' wins as X')
winner = True

def plyr2wintest(): #checks if player has three in a row
global winner
if (1 in plyr2list) and (2 in plyr2list) and (3 in plyr2list) or \
(4 in plyr2list) and (5 in plyr2list) and (6 in plyr2list) or \
(7 in plyr2list) and (8 in plyr2list) and (9 in plyr2list) or \
(1 in plyr2list) and (4 in plyr2list) and (7 in plyr2list) or \
(2 in plyr2list) and (5 in plyr2list) and (8 in plyr2list) or \
(3 in plyr2list) and (6 in plyr2list) and (9 in plyr2list) or \
(1 in plyr2list) and (5 in plyr2list) and (9 in plyr2list) or \
(3 in plyr2list) and (5 in plyr2list) and (7 in plyr2list):
print ((name2) + ' wins as O')
winner = True

def printboard(): #print board
print(' ')
print(' '+ position[0] +' | '+ position[1] +' | '+ position[2] + ' ' + ' '+ '1' +' | '+ '2' +' | '+ '3')
print('-----------' + ' ' + '-----------')
print(' '+ position[3] +' | '+ position[4] +' | '+ position[5] + ' ' + ' '+ '4' +' | '+ '5' +' | '+ '6')
print('-----------' + ' ' + '-----------')
print(' '+ position[6] +' | '+ position[7] +' | '+ position[8] + ' ' + ' '+ '7' +' | '+ '8' +' | '+ '9')
print(' ')

winner = False #win checker
movecount = 0 #counts amount of turns
playgame = True

print ('welcome to Noughts & Crosses') #title

while playgame == True:
position = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] #sets the board spaces blank
plyr1list = []
plyr2list = []
gamelist = []

winner = False
movecount = 0

print (' ')
#Name input
print ('What is player 1s name?')
name1 = input()
print ('thanks '+ (name1) +', Whats player 2s name?')
name2 = input()
print ('so '+ (name1) +' is X and '+ (name2) + ' is O')
input("Press Enter to continue...")

printboard()

while (movecount < 9) and (winner == False):

if (movecount % 2 == 0): #player 1 turn

print ((name1) + 's ( X ) turn, please choose placement (1-9)')
move = input()
if move in ('1', '2', '3', '4', '5', '6', '7', '8', '9') and (int(move) not in (gamelist)):
plyr1list.append(int(move))
gamelist.append(int(move))
print (gamelist) #debug
position[int(move)-1] = ('X')
printboard()
movecount = movecount + 1
plyr1wintest()
elif move not in ('1', '2', '3', '4', '5', '6', '7', '8', '9'):
print ('That is not a valid move! Try again')
else: print ('That move is taken!, Try again')

else: #player 2 turn

print ((name2) + 's ( O ) turn, please choose placement (1-9)')