Advertisement
Md_hosen_zisad

Tic_Tac_Toe-python

Nov 5th, 2019
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.08 KB | None | 0 0
  1. board=[[0,0,0,0],[0,3,5,3],[0,5,3,5],[0,3,5,3]]
  2.  
  3. def initialize():
  4.     print("Before initialize the board")
  5.     print(board)
  6.     for i in range(1,4):
  7.         for j in range(1,4):
  8.                 board[i][j]=2
  9.     print("\nThe board is initialized\n")
  10.     print("After initialize the board\n")
  11.     print(board)
  12.  
  13. def printboard():
  14.     for i in range(1,4):
  15.         for j in range(1,4):
  16.             if j!=3:
  17.                 if board[i][j]==2:
  18.                     print("   |",end="")
  19.                 elif board[i][j]==3:
  20.                     print(" X |",end="")
  21.                 else:
  22.                     print(" O |",end="")
  23.             else:
  24.                 if board[i][j]==2:
  25.                     print("  ",end="")
  26.                 elif board[i][j]==3:
  27.                     print(" X",end="")
  28.                 else:
  29.                     print(" O",end="")
  30.         if i!=3:
  31.             print("\n-----------\n",end="")
  32.  
  33. def make2():
  34.     if board[2][2]==2:
  35.         return 5
  36.     elif board[1][2]==2:
  37.         return 2
  38.     elif board[2][1]==2:
  39.         return 4
  40.     elif board[2][3]==2:
  41.         return 6
  42.     else:
  43.         return 8
  44.        
  45. def Go(player,position):
  46.     if position==1:
  47.         board[1][1]=player
  48.     elif position==2:
  49.         board[1][2]=player
  50.     elif position==3:
  51.         board[1][3]=player
  52.     elif position==4:
  53.         board[2][1]=player
  54.     elif position==5:
  55.         board[2][2]=player
  56.     elif position==6:
  57.         board[2][3]=player
  58.     elif position==7:
  59.         board[3][1]=player
  60.     elif position==8:
  61.         board[3][2]=player
  62.     elif position==9:
  63.         board[3][3]=player
  64.        
  65. def posswinx():
  66.     if board[1][1]*board[1][2]*board[1][3]==18:
  67.         if board[1][1]==2:
  68.             return 1
  69.         elif board[1][2]==2:
  70.             return 2
  71.         elif board[1][3]==2:
  72.             return 3
  73.     elif board[2][1]*board[2][2]*board[2][3]==18:
  74.         if board[2][1]==2:
  75.             return 4
  76.         elif board[2][2]==2:
  77.             return 5
  78.         elif board[2][3]==2:
  79.             return 6
  80.     elif board[3][1]*board[3][2]*board[3][3]==18:
  81.         if board[3][1]==2:
  82.             return 7
  83.         elif board[3][2]==2:
  84.             return 8
  85.         elif board[3][3]==2:
  86.             return 9
  87.     elif board[1][1]*board[2][1]*board[3][1]==18:
  88.        if board[1][1]==2:
  89.            return 1
  90.        elif board[2][1]==2:
  91.            return 4
  92.        elif board[3][1]==2:
  93.            return 7
  94.     elif board[1][2]*board[2][2]*board[3][2]==18:
  95.        if board[1][2]==2:
  96.            return 2
  97.        elif board[2][2]==2:
  98.            return 5
  99.        elif board[3][2]==2:
  100.            return 8
  101.     elif board[1][3]*board[2][3]*board[3][3]==18:
  102.        if board[1][3]==2:
  103.            return 3
  104.        elif board[2][3]==2:
  105.            return 6
  106.        elif board[3][3]==2:
  107.            return 9
  108.     elif board[1][1]*board[2][2]*board[3][3]==18:
  109.        if board[1][1]==2:
  110.            return 1
  111.        elif board[2][2]==2:
  112.            return 5
  113.        elif board[3][3]==2:
  114.            return 9
  115.     elif board[1][3]*board[2][2]*board[3][1]==18:
  116.        if board[1][3]==2:
  117.            return 3
  118.        elif board[2][2]==2:
  119.            return 5
  120.        elif board[3][1]==2:
  121.            return 7
  122.     else:
  123.        return 0
  124.  
  125. def posswino():
  126.     if board[1][1]*board[1][2]*board[1][3]==50:
  127.         if board[1][1]==2:
  128.             return 1
  129.         elif board[1][2]==2:
  130.             return 2
  131.         elif board[1][3]==2:
  132.             return 3
  133.     elif board[2][1]*board[2][2]*board[2][3]==50:
  134.         if board[2][1]==2:
  135.             return 4
  136.         elif board[2][2]==2:
  137.             return 5
  138.         elif board[2][3]==2:
  139.             return 6
  140.     elif board[3][1]*board[3][2]*board[3][3]==50:
  141.         if board[3][1]==2:
  142.             return 7
  143.         elif board[3][2]==2:
  144.             return 8
  145.         elif board[3][3]==2:
  146.             return 9
  147.     elif board[1][1]*board[2][1]*board[3][1]==50:
  148.         if board[1][1]==2:
  149.             return 1
  150.         elif board[2][1]==2:
  151.             return 4
  152.         elif board[3][1]==2:
  153.             return 7
  154.     elif board[1][2]*board[2][2]*board[3][2]==50:
  155.         if board[1][2]==2:
  156.             return 2
  157.         elif board[2][2]==2:
  158.             return 5
  159.         elif board[3][2]==2:
  160.             return 8
  161.     elif board[1][3]*board[2][3]*board[3][3]==50:
  162.         if board[1][3]==2:
  163.             return 3
  164.         elif board[2][3]==2:
  165.             return 6
  166.         elif board[3][3]==2:
  167.             return 9
  168.     elif board[1][1]*board[2][2]*board[3][3]==50:
  169.         if board[1][1]==2:
  170.             return 1
  171.         elif board[2][2]==5:
  172.             return 5
  173.         elif board[3][3]==2:
  174.             return 9
  175.     elif board[1][3]*board[2][2]*board[3][1]==50:
  176.         if board[1][3]==2:
  177.             return 3
  178.         elif board[2][2]==2:
  179.             return 5
  180.         elif board[3][1]==2:
  181.             return 7
  182.     else:
  183.         return 0
  184.        
  185. def check():
  186.     if board[1][1]*board[1][2]*board[1][3]==27:
  187.         return 3
  188.     elif board[2][1]*board[2][2]*board[2][3]==27:
  189.         return 3
  190.     elif board[3][1]*board[3][2]*board[3][3]==27:
  191.         return 3
  192.     elif board[1][1]*board[2][1]*board[3][1]==27:
  193.         return 3
  194.     elif board[1][2]*board[2][2]*board[3][2]==27:
  195.         return 3
  196.     elif board[1][3]*board[2][3]*board[3][3]==27:
  197.         return 3
  198.     elif board[1][1]*board[2][2]*board[3][3]==27:
  199.         return 3
  200.     elif board[1][3]*board[2][2]*board[3][1]==27:
  201.         return 3
  202.     elif board[1][1]*board[1][2]*board[1][3]==125:
  203.         return 5
  204.     elif board[2][1]*board[2][2]*board[2][3]==125:
  205.         return 5
  206.     elif board[3][1]*board[3][2]*board[3][3]==125:
  207.         return 5
  208.     elif board[1][1]*board[2][1]*board[3][1]==125:
  209.         return 5
  210.     elif board[1][2]*board[2][2]*board[3][2]==125:
  211.         return 5
  212.     elif board[1][3]*board[2][3]*board[3][3]==125:
  213.         return 5
  214.     elif board[1][1]*board[2][2]*board[3][3]==125:
  215.         return 5
  216.     elif board[1][3]*board[2][2]*board[3][1]==125:
  217.         return 5
  218.     else:
  219.         return 0
  220.  
  221. def anywhere():
  222.     for i in range(1,4):
  223.         for j in range(1,4):
  224.             if board[i][j]==2:
  225.                 return (3*(i-1)+j)
  226.                 break
  227.  
  228. def check_input(position):
  229.     if position==1:
  230.         if board[1][1]==2:
  231.             return 0
  232.         else:
  233.             return 1
  234.     elif position==2:
  235.         if board[1][2]==2:
  236.             return 0
  237.         else:
  238.             return 1
  239.     elif position==3:
  240.         if board[1][3]==2:
  241.             return 0
  242.         else:
  243.             return 1
  244.     elif position==4:
  245.         if board[2][1]==2:
  246.             return 0
  247.         else:
  248.             return 1
  249.     elif position==5:
  250.         if board[2][2]==2:
  251.             return 0
  252.         else:
  253.             return 1
  254.     elif position==6:
  255.         if board[2][3]==2:
  256.             return 0
  257.         else:
  258.             return 1
  259.     elif position==7:
  260.         if board[3][1]==2:
  261.             return 0
  262.         else:
  263.             return 1
  264.     elif position==8:
  265.         if board[3][2]==2:
  266.             return 0
  267.         else:
  268.             return 1
  269.     elif position==9:
  270.         if board[3][3]==2:
  271.             return 0
  272.         else:
  273.             return 1
  274.  
  275. def play_x():
  276.     initialize()
  277.     printboard()
  278.     print("\nEnter the position of your move ::: ")
  279.     position=int(input())
  280.     Go(3,position)
  281.     print("\nAfter turn 1 the board is as below :::\n")
  282.     printboard()
  283.     if board[2][2]==2:
  284.         Go(5,5)
  285.     else:
  286.         Go(5,1)
  287.     print("\nAfter turn 2 the board is as below :::\n")
  288.     printboard()
  289.     print("\nEnter the position of your move ::: ")
  290.     while 1:
  291.         position=int(input())
  292.         if check_input(position)==0:
  293.             break
  294.         else:
  295.             print("\nPosition is previously taken, enter another position.\n")
  296.     Go(3,position)
  297.     print("\nAfter turn 3 the board is as below :::\n")
  298.     printboard()
  299.     if posswinx():
  300.         Go(5,posswinx())
  301.     else:
  302.         Go(5,make2())
  303.     print("\nAfter turn 4 the board is as below :::\n")
  304.     printboard()
  305.     print("\nEnter the position of your move ::: ")
  306.     while 1:
  307.         position=int(input())
  308.         if check_input(position)==0:
  309.             break
  310.         else:
  311.             print("\nPosition is previously taken, enter another position.\n")
  312.     Go(3,position)
  313.     print("\nAfter turn 5 the board is as below :::\n")
  314.     printboard()
  315.     if check()==3:
  316.         print("\nHuman wins\n")
  317.         TicTacToe()
  318.     if posswino():
  319.         Go(5,posswino())
  320.     elif posswinx():
  321.         Go(5,posswinx())
  322.     else:
  323.         Go(5,make2())
  324.     print("\nAfter turn 6 the board is as below :::\n")
  325.     printboard()
  326.     if check()==5:
  327.         print("\nComputer wins\n")
  328.         TicTacToe()
  329.     print("\nEnter the position of your move ::: ")
  330.     while 1:
  331.         position=int(input())
  332.         if check_input(position)==0:
  333.             break
  334.         else:
  335.             print("\nPosition is previously taken, enter another position.\n")
  336.     Go(3,position)
  337.     print("\nAfter turn 7 the board is as below :::\n")
  338.     printboard()
  339.     if check()==3:
  340.         print("\nHuman wins\n")
  341.         TicTacToe()
  342.     if posswino():
  343.         Go(5,posswino())
  344.     elif posswinx():
  345.         Go(5,posswinx())
  346.     else:
  347.         Go(5,anywhere())
  348.     print("\nAfter turn 8 the board is as below :::\n")
  349.     printboard()
  350.     if check()==5:
  351.         print("\nComputer wins\n")
  352.         TicTacToe()
  353.     print("\nEnter the positon of your move ::: ")
  354.     while 1:
  355.         position=int(input())
  356.         if check_input(position)==0:
  357.             break
  358.         else:
  359.             print("\nPosition is previously taken, enter another position.\n")
  360.     Go(3,position)
  361.     print("\nAfter turn 9 the board is as below :::\n")
  362.     printboard()
  363.     if check()==3:
  364.         print("\nHuman wins\n")
  365.     else:
  366.         print("\nDraw\n")
  367.     TicTacToe()
  368.  
  369. def play_o():
  370.     initialize()
  371.     printboard()
  372.     Go(3,1)
  373.     print("\nAfter turn 1 the board is as below :::\n")
  374.     printboard()
  375.     print("\nEnter the position of your move ::: ")
  376.     while 1:
  377.         position=int(input())
  378.         if check_input(position)==0:
  379.             break
  380.         else:
  381.             print("\nPosition is previously taken, enter another position\n")
  382.     Go(5,position)
  383.     print("\nAfter turn 2 the board is as below :::\n")
  384.     printboard()
  385.     if board[3][3]==2:
  386.         Go(3,9)
  387.     else:
  388.         Go(3,3)
  389.     print("\nAfter turn 3 the board is as below :::\n")
  390.     printboard()
  391.     print("\nEnter the position of your move ::: ")
  392.     while 1:
  393.         position=int(input())
  394.         if check_input(position)==0:
  395.             break
  396.         else:
  397.             print("\nPosition is previously taken, enter another position\n")
  398.     Go(5,position)
  399.     print("\nAfter turn 4 the board is as below :::\n")
  400.     printboard()
  401.     if posswinx():
  402.         Go(3,posswinx())
  403.     elif posswino():
  404.         Go(3,posswino())
  405.     elif board[3][1]==2:
  406.         Go(3,7)
  407.     else:
  408.         Go(3,3)
  409.     print("\nAfter turn 5 the board is as below :::\n")
  410.     printboard()
  411.     if check():
  412.         print("\nComputer wins\n")
  413.         exit()
  414.     print("\nEnter the position of your move :::")
  415.     while 1:
  416.         position=int(input())
  417.         if check_input(position)==0:
  418.             break
  419.         else:
  420.             print("\nPosition is previously taken, enter another position\n")
  421.     Go(5,position)
  422.     print("\nAfter turn 6 the board is as below :::\n")
  423.     printboard()
  424.     if check():
  425.         print("\nHuman wins\n")
  426.         exit()
  427.     if posswinx():
  428.         Go(3,posswinx())
  429.     elif posswino():
  430.         Go(3,posswino())
  431.     else:
  432.         Go(3,anywhere())
  433.     print("\nAfter turn 7 the board is as below :::\n")
  434.     printboard()
  435.     if check():
  436.         print("\nComputer wins\n")
  437.         exit()
  438.     print("\nEnter the position of your move ::: ")
  439.     while 1:
  440.         position=int(input())
  441.         if check_input(position)==0:
  442.             break
  443.         else:
  444.             print("\nPosition is previously taken, enter another position\n")
  445.     Go(5,position)
  446.     print("\nAfter turn 8 the board is as below :::\n")
  447.     printboard()
  448.     if check():
  449.         print("\nHuman wins\n")
  450.         exit()
  451.     if posswinx():
  452.         Go(3,posswinx())
  453.     elif posswino():
  454.         Go(3,posswino())
  455.     else:
  456.         Go(3,anywhere())
  457.     print("\nAfter turn 9 the board is as below :::\n")
  458.     printboard()
  459.     if check()==3:
  460.         print("\nComputer wins\n")
  461.     else:
  462.         print("\nDraw\n")
  463.        
  464. def TicTacToe():
  465.     while True:
  466.         print("\nEnter 1 for initialize board\n")
  467.         print("\nEnter 2 for printboard")
  468.         print("\nEnter 3 for playing as X")
  469.         print("\nEnter 5 for playing as O")
  470.         print("\nEnter 100 for exit")
  471.         choice=int(input("Enter your choice ::: "))
  472.         if choice==100:
  473.             break
  474.         elif choice==1:
  475.             initialize()
  476.         elif choice==2:
  477.             printboard()
  478.         elif choice==3:
  479.             play_x()
  480.         elif choice==5:
  481.             play_o()
  482.         else:
  483.             print("\nYour Choice is Wrong !!!!\n")
  484.  
  485. TicTacToe()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement