Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- board=[[0,0,0,0],[0,3,5,3],[0,5,3,5],[0,3,5,3]]
- def initialize():
- print("Before initialize the board")
- print(board)
- for i in range(1,4):
- for j in range(1,4):
- board[i][j]=2
- print("\nThe board is initialized\n")
- print("After initialize the board\n")
- print(board)
- def printboard():
- for i in range(1,4):
- for j in range(1,4):
- if j!=3:
- if board[i][j]==2:
- print(" |",end="")
- elif board[i][j]==3:
- print(" X |",end="")
- else:
- print(" O |",end="")
- else:
- if board[i][j]==2:
- print(" ",end="")
- elif board[i][j]==3:
- print(" X",end="")
- else:
- print(" O",end="")
- if i!=3:
- print("\n-----------\n",end="")
- def make2():
- if board[2][2]==2:
- return 5
- elif board[1][2]==2:
- return 2
- elif board[2][1]==2:
- return 4
- elif board[2][3]==2:
- return 6
- else:
- return 8
- def Go(player,position):
- if position==1:
- board[1][1]=player
- elif position==2:
- board[1][2]=player
- elif position==3:
- board[1][3]=player
- elif position==4:
- board[2][1]=player
- elif position==5:
- board[2][2]=player
- elif position==6:
- board[2][3]=player
- elif position==7:
- board[3][1]=player
- elif position==8:
- board[3][2]=player
- elif position==9:
- board[3][3]=player
- def posswinx():
- if board[1][1]*board[1][2]*board[1][3]==18:
- if board[1][1]==2:
- return 1
- elif board[1][2]==2:
- return 2
- elif board[1][3]==2:
- return 3
- elif board[2][1]*board[2][2]*board[2][3]==18:
- if board[2][1]==2:
- return 4
- elif board[2][2]==2:
- return 5
- elif board[2][3]==2:
- return 6
- elif board[3][1]*board[3][2]*board[3][3]==18:
- if board[3][1]==2:
- return 7
- elif board[3][2]==2:
- return 8
- elif board[3][3]==2:
- return 9
- elif board[1][1]*board[2][1]*board[3][1]==18:
- if board[1][1]==2:
- return 1
- elif board[2][1]==2:
- return 4
- elif board[3][1]==2:
- return 7
- elif board[1][2]*board[2][2]*board[3][2]==18:
- if board[1][2]==2:
- return 2
- elif board[2][2]==2:
- return 5
- elif board[3][2]==2:
- return 8
- elif board[1][3]*board[2][3]*board[3][3]==18:
- if board[1][3]==2:
- return 3
- elif board[2][3]==2:
- return 6
- elif board[3][3]==2:
- return 9
- elif board[1][1]*board[2][2]*board[3][3]==18:
- if board[1][1]==2:
- return 1
- elif board[2][2]==2:
- return 5
- elif board[3][3]==2:
- return 9
- elif board[1][3]*board[2][2]*board[3][1]==18:
- if board[1][3]==2:
- return 3
- elif board[2][2]==2:
- return 5
- elif board[3][1]==2:
- return 7
- else:
- return 0
- def posswino():
- if board[1][1]*board[1][2]*board[1][3]==50:
- if board[1][1]==2:
- return 1
- elif board[1][2]==2:
- return 2
- elif board[1][3]==2:
- return 3
- elif board[2][1]*board[2][2]*board[2][3]==50:
- if board[2][1]==2:
- return 4
- elif board[2][2]==2:
- return 5
- elif board[2][3]==2:
- return 6
- elif board[3][1]*board[3][2]*board[3][3]==50:
- if board[3][1]==2:
- return 7
- elif board[3][2]==2:
- return 8
- elif board[3][3]==2:
- return 9
- elif board[1][1]*board[2][1]*board[3][1]==50:
- if board[1][1]==2:
- return 1
- elif board[2][1]==2:
- return 4
- elif board[3][1]==2:
- return 7
- elif board[1][2]*board[2][2]*board[3][2]==50:
- if board[1][2]==2:
- return 2
- elif board[2][2]==2:
- return 5
- elif board[3][2]==2:
- return 8
- elif board[1][3]*board[2][3]*board[3][3]==50:
- if board[1][3]==2:
- return 3
- elif board[2][3]==2:
- return 6
- elif board[3][3]==2:
- return 9
- elif board[1][1]*board[2][2]*board[3][3]==50:
- if board[1][1]==2:
- return 1
- elif board[2][2]==5:
- return 5
- elif board[3][3]==2:
- return 9
- elif board[1][3]*board[2][2]*board[3][1]==50:
- if board[1][3]==2:
- return 3
- elif board[2][2]==2:
- return 5
- elif board[3][1]==2:
- return 7
- else:
- return 0
- def check():
- if board[1][1]*board[1][2]*board[1][3]==27:
- return 3
- elif board[2][1]*board[2][2]*board[2][3]==27:
- return 3
- elif board[3][1]*board[3][2]*board[3][3]==27:
- return 3
- elif board[1][1]*board[2][1]*board[3][1]==27:
- return 3
- elif board[1][2]*board[2][2]*board[3][2]==27:
- return 3
- elif board[1][3]*board[2][3]*board[3][3]==27:
- return 3
- elif board[1][1]*board[2][2]*board[3][3]==27:
- return 3
- elif board[1][3]*board[2][2]*board[3][1]==27:
- return 3
- elif board[1][1]*board[1][2]*board[1][3]==125:
- return 5
- elif board[2][1]*board[2][2]*board[2][3]==125:
- return 5
- elif board[3][1]*board[3][2]*board[3][3]==125:
- return 5
- elif board[1][1]*board[2][1]*board[3][1]==125:
- return 5
- elif board[1][2]*board[2][2]*board[3][2]==125:
- return 5
- elif board[1][3]*board[2][3]*board[3][3]==125:
- return 5
- elif board[1][1]*board[2][2]*board[3][3]==125:
- return 5
- elif board[1][3]*board[2][2]*board[3][1]==125:
- return 5
- else:
- return 0
- def anywhere():
- for i in range(1,4):
- for j in range(1,4):
- if board[i][j]==2:
- return (3*(i-1)+j)
- break
- def check_input(position):
- if position==1:
- if board[1][1]==2:
- return 0
- else:
- return 1
- elif position==2:
- if board[1][2]==2:
- return 0
- else:
- return 1
- elif position==3:
- if board[1][3]==2:
- return 0
- else:
- return 1
- elif position==4:
- if board[2][1]==2:
- return 0
- else:
- return 1
- elif position==5:
- if board[2][2]==2:
- return 0
- else:
- return 1
- elif position==6:
- if board[2][3]==2:
- return 0
- else:
- return 1
- elif position==7:
- if board[3][1]==2:
- return 0
- else:
- return 1
- elif position==8:
- if board[3][2]==2:
- return 0
- else:
- return 1
- elif position==9:
- if board[3][3]==2:
- return 0
- else:
- return 1
- def play_x():
- initialize()
- printboard()
- print("\nEnter the position of your move ::: ")
- position=int(input())
- Go(3,position)
- print("\nAfter turn 1 the board is as below :::\n")
- printboard()
- if board[2][2]==2:
- Go(5,5)
- else:
- Go(5,1)
- print("\nAfter turn 2 the board is as below :::\n")
- printboard()
- print("\nEnter the position of your move ::: ")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position.\n")
- Go(3,position)
- print("\nAfter turn 3 the board is as below :::\n")
- printboard()
- if posswinx():
- Go(5,posswinx())
- else:
- Go(5,make2())
- print("\nAfter turn 4 the board is as below :::\n")
- printboard()
- print("\nEnter the position of your move ::: ")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position.\n")
- Go(3,position)
- print("\nAfter turn 5 the board is as below :::\n")
- printboard()
- if check()==3:
- print("\nHuman wins\n")
- TicTacToe()
- if posswino():
- Go(5,posswino())
- elif posswinx():
- Go(5,posswinx())
- else:
- Go(5,make2())
- print("\nAfter turn 6 the board is as below :::\n")
- printboard()
- if check()==5:
- print("\nComputer wins\n")
- TicTacToe()
- print("\nEnter the position of your move ::: ")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position.\n")
- Go(3,position)
- print("\nAfter turn 7 the board is as below :::\n")
- printboard()
- if check()==3:
- print("\nHuman wins\n")
- TicTacToe()
- if posswino():
- Go(5,posswino())
- elif posswinx():
- Go(5,posswinx())
- else:
- Go(5,anywhere())
- print("\nAfter turn 8 the board is as below :::\n")
- printboard()
- if check()==5:
- print("\nComputer wins\n")
- TicTacToe()
- print("\nEnter the positon of your move ::: ")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position.\n")
- Go(3,position)
- print("\nAfter turn 9 the board is as below :::\n")
- printboard()
- if check()==3:
- print("\nHuman wins\n")
- else:
- print("\nDraw\n")
- TicTacToe()
- def play_o():
- initialize()
- printboard()
- Go(3,1)
- print("\nAfter turn 1 the board is as below :::\n")
- printboard()
- print("\nEnter the position of your move ::: ")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position\n")
- Go(5,position)
- print("\nAfter turn 2 the board is as below :::\n")
- printboard()
- if board[3][3]==2:
- Go(3,9)
- else:
- Go(3,3)
- print("\nAfter turn 3 the board is as below :::\n")
- printboard()
- print("\nEnter the position of your move ::: ")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position\n")
- Go(5,position)
- print("\nAfter turn 4 the board is as below :::\n")
- printboard()
- if posswinx():
- Go(3,posswinx())
- elif posswino():
- Go(3,posswino())
- elif board[3][1]==2:
- Go(3,7)
- else:
- Go(3,3)
- print("\nAfter turn 5 the board is as below :::\n")
- printboard()
- if check():
- print("\nComputer wins\n")
- exit()
- print("\nEnter the position of your move :::")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position\n")
- Go(5,position)
- print("\nAfter turn 6 the board is as below :::\n")
- printboard()
- if check():
- print("\nHuman wins\n")
- exit()
- if posswinx():
- Go(3,posswinx())
- elif posswino():
- Go(3,posswino())
- else:
- Go(3,anywhere())
- print("\nAfter turn 7 the board is as below :::\n")
- printboard()
- if check():
- print("\nComputer wins\n")
- exit()
- print("\nEnter the position of your move ::: ")
- while 1:
- position=int(input())
- if check_input(position)==0:
- break
- else:
- print("\nPosition is previously taken, enter another position\n")
- Go(5,position)
- print("\nAfter turn 8 the board is as below :::\n")
- printboard()
- if check():
- print("\nHuman wins\n")
- exit()
- if posswinx():
- Go(3,posswinx())
- elif posswino():
- Go(3,posswino())
- else:
- Go(3,anywhere())
- print("\nAfter turn 9 the board is as below :::\n")
- printboard()
- if check()==3:
- print("\nComputer wins\n")
- else:
- print("\nDraw\n")
- def TicTacToe():
- while True:
- print("\nEnter 1 for initialize board\n")
- print("\nEnter 2 for printboard")
- print("\nEnter 3 for playing as X")
- print("\nEnter 5 for playing as O")
- print("\nEnter 100 for exit")
- choice=int(input("Enter your choice ::: "))
- if choice==100:
- break
- elif choice==1:
- initialize()
- elif choice==2:
- printboard()
- elif choice==3:
- play_x()
- elif choice==5:
- play_o()
- else:
- print("\nYour Choice is Wrong !!!!\n")
- TicTacToe()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement