Tanish69420

TIC TAC TOE

Jun 2nd, 2022 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #To check whether the computer or Human has won"
  2. def check(l):
  3. #The z here is just a counter to see whether you have completed all the three squares
  4.     z=0
  5. #List a are all the ways you can win
  6.     a=[[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,6],[3,6,9],[1,5,9],[3,5,7]]
  7.     for i in a:
  8. #The for loop extracts through the List a
  9.         z=0
  10.         for j in i:
  11. #This for loop extracts through i,which is an element of list a
  12.            if j in l:
  13.                z=z+1
  14. #If z is 3 it means that the person has completed all the squares
  15.         if z==3:
  16.             break
  17.        
  18.     if z==3:
  19.         return(3)
  20. #This function shows the board currently
  21. def board(l1,l2):
  22.     a=['-','-','-','-','-','-','-','-','-']
  23. #This for loop is to check where the computer and the human has placed their mark
  24.     for i in range (1,10):
  25. #This if checks where the mark is and places it on the respective squares
  26.         if i in l1:
  27. #This puts cross where the human has marked and knot for where the computer has marked in the respective list position
  28.             a[i-1]="X"
  29.         if i in l2:
  30.             a[i-1]="O"
  31.     print(a[0],a[1],a[2])
  32.     print(a[3],a[4],a[5])
  33.     print(a[6],a[7],a[8])
  34.  
  35.            
  36. import random
  37. print("The layout is ")
  38. print()
  39. print(" 1 2 3")
  40. print(" 4 5 6")
  41. print(" 7 8 9")
  42. #Here l is all the valid moves
  43. l=[1,2,3,4,5,6,7,8,9]
  44. l1=[]
  45. l2=[]
  46. for i in range (1,6):
  47.     print("Your valid positions are ",l)
  48.     while True:
  49.         a=float(input("Enter where you would like to place the cross "))
  50.         if a not in l:
  51.             print("Wrong input,enter again")
  52.             continue
  53.         if a in l:
  54.             break
  55. #Here the append function appends your move in list1/l1
  56.     l1.append(int(a))
  57.     print(l1)
  58. #Here the l.remove removes one of the valid position,which is a. a is the position chosen by you
  59.     l.remove(a)
  60. #Calling the check function defined above
  61.     y=check(l1)
  62. #As the check function returns 3 if all the 3 sqaures are marked
  63.     if y==3:
  64.         print("You win ")
  65. #Board is the function defined above
  66.         board(l1,l2)
  67.         break
  68.     if i==5:
  69.         break
  70. #random.choice chooses a random option from list L
  71.     c=random.choice(l)
  72. #Here the l.remove removes one of the valid position,which is a. a is the position chosen by the computer
  73.     l.remove(c)
  74.     l2.append(c)
  75.     print("I placed my knot in number ",c)
  76.     board(l1,l2)
  77.     y=check(l2)
  78. #As the check function returns 3 if all the 3 sqaures are marked
  79.     if y==3:
  80.         print("I win ")
  81.         board(l1,l2)
  82.         break
Add Comment
Please, Sign In to add comment