daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Apr 30th, 2016 75,434 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import tkinter as tk
  3. from tkinter import ttk
  4. import tkinter.messagebox
  5.  
  6. root = tk.Tk()
  7.  
  8. """ *** Memory *** """
  9. board2 = [["0", "1", "2"], ["3", "4", "5"], ["6", "7", "8"]]
  10. a = ["X"]
  11.  
  12. """ *** Functions *** """
  13. player = tkinter.messagebox.askyesno("First thing first", "Playing against another player?")
  14. print("is there another player? ", player)
  15.  
  16.  
  17. def turn(pos1, board2, button, a):
  18.     """ enter choice to memory and change the board and interface """
  19.     print("playing with", a)
  20.     for i in range(len(board2)):
  21.         for n in range(len(board2[i])):
  22.             if board2[i][n] == str(pos1):
  23.                 board2[i][n] = str(a[0])
  24.  
  25.     # toggle button text
  26.  
  27.     if button["text"] == " ":
  28.         button["text"] = str(a[0])
  29.  
  30.         # change a to new turn
  31.         if player:
  32.  
  33.             if a[0] == "X":
  34.                 a[0] = "O"
  35.                 print("changed x to o")
  36.  
  37.             else:
  38.                 a[0] = "X"
  39.                 print("changed o to x")
  40.  
  41.     else:
  42.         print("cant, already taken")
  43.         aiturn = True
  44.         return aiturn
  45.  
  46.     # check win state
  47.     aiturn = win()
  48.  
  49.     # Ai
  50.  
  51.     if not player:
  52.  
  53.         print("fired ai")
  54.  
  55.         if not aiturn:
  56.             board2, aiturn = checkatk(board2, aiturn)
  57.             print("ai turn for atk is ", aiturn)
  58.  
  59.         if not aiturn:
  60.             board2, aiturn = checkdef(board2, aiturn)
  61.             print("ai turn for def is ", aiturn)
  62.  
  63.         if not aiturn:
  64.             board2, pos1, a, button, aiturn = aiplay(board2, pos1, a, button, aiturn)
  65.             print("ai turn for random is ", aiturn)
  66.  
  67.     # aiturn = False
  68.  
  69.     for i in range(3):
  70.         print(board2[i][0], board2[i][1], board2[i][2])
  71.  
  72.     # check win state
  73.     aiturn = win()
  74.  
  75.     return board2, pos1, button, a, aiturn
  76.  
  77.  
  78. def aiplay(board2, pos1, a, button, aiturn):
  79.     """ chose a random number and play it"""
  80.     pos1 = "".join(random.sample(["0", "1", "2", "3", "4", "5", "6", "7", "8"], 1))
  81.  
  82.     for i in range(len(board2)):
  83.         for n in range(len(board2[i])):
  84.             if str(board2[i][n]) == str(pos1):
  85.                 print(str(board2[i][n]), " is the changed value")
  86.                 board2[i][n] = "O"
  87.                 options[str(pos1)]()
  88.                 aiturn = True
  89.  
  90.                 return board2, pos1, a, button, aiturn
  91.     else:
  92.         # repeat if choice is taken
  93.         board2, pos1, a, button, aiturn = aiplay(board2, pos1, a, button, aiturn)
  94.         return board2, pos1, a, button, aiturn
  95.  
  96.  
  97. def win():
  98.     """check win state"""
  99.     for i in range(3):
  100.  
  101.         if "XXX" == str("".join(board2[i][0:3])) or "XXX" == (board2[0][i] + board2[1][i] + board2[2][i]):
  102.             reset(1)
  103.             aiturn = True
  104.             if player:
  105.                 a[0] = "X"
  106.             return aiturn
  107.  
  108.     if (board2[0][0] + board2[1][1] + board2[2][2]) == "XXX" or (
  109.                     board2[0][2] + board2[1][1] + board2[2][0]) == "XXX":
  110.         reset(1)
  111.         aiturn = True
  112.         if player:
  113.             a[0] = "X"
  114.         return aiturn
  115.  
  116.     for i in range(3):
  117.  
  118.         if "OOO" == str("".join(board2[i][0:3])) or "OOO" == (board2[0][i] + board2[1][i] + board2[2][i]):
  119.             reset(2)
  120.  
  121.     if (board2[0][0] + board2[1][1] + board2[2][2]) == "OOO" or (
  122.                     board2[0][2] + board2[1][1] + board2[2][0]) == "OOO":
  123.         reset(2)
  124.  
  125.     info = 0
  126.     for i in range(3):
  127.         for n in range(3):
  128.             if board2[i][n] == "X":
  129.                 info += 1
  130.  
  131.             if board2[i][n] == "O":
  132.                 info += 1
  133.  
  134.     if info == 9:
  135.         reset(0)
  136.         aiturn = True
  137.         return aiturn
  138.  
  139.  
  140.  
  141. def reset(n):
  142.     """ win state, reset board """
  143.     if n == 0:
  144.         tkinter.messagebox.showinfo('No one won', 'No one won, restarting')
  145.     else:
  146.         tkinter.messagebox.showinfo('There is a winner!', 'Player ' + str(n) + ' is the winner!')
  147.     numb = 0
  148.     button1["text"] = " "
  149.     button2["text"] = " "
  150.     button3["text"] = " "
  151.     button4["text"] = " "
  152.     button5["text"] = " "
  153.     button6["text"] = " "
  154.     button7["text"] = " "
  155.     button8["text"] = " "
  156.     button9["text"] = " "
  157.     for i in range(len(board2)):
  158.         for n in range(len(board2[i])):
  159.             board2[i][n] = str(numb)
  160.             numb += 1
  161.  
  162.     return board2
  163.  
  164.  
  165. def checkatk(board2, aiturn):
  166.     """win the game on the next move"""
  167.     danger = 0
  168.     danger2 = 0
  169.     danger3 = 0
  170.     danger4 = 0
  171.  
  172.     for n in range(3):
  173.         for i in range(3):
  174.             if i == 0:
  175.                 danger = 0
  176.             if board2[n][i] == "O":
  177.                 danger += 1
  178.  
  179.         if danger == 2:
  180.             for i in range(3):
  181.                 if board2[n][i] != "O" and board2[n][i] != "X" and not aiturn:
  182.                     options[board2[n][i]]()
  183.                     print("Ai played")
  184.                     board2[n][i] = "O"
  185.                     danger = 0
  186.                     aiturn = True
  187.  
  188.         for i in range(3):
  189.             if i == 0:
  190.                 danger2 = 0
  191.             if board2[i][n] == "O":
  192.                 danger2 += 1
  193.  
  194.         if danger2 == 2:
  195.             for i in range(3):
  196.                 if board2[i][n] != "O" and board2[i][n] != "X" and not aiturn:
  197.                     options[board2[i][n]]()
  198.                     print("Ai played")
  199.                     board2[i][n] = "O"
  200.                     danger2 = 0
  201.                     aiturn = True
  202.  
  203.     if board2[1][1] == "O":
  204.         danger3 += 1
  205.         danger4 += 1
  206.  
  207.     if board2[0][0] == "O":
  208.         danger3 += 1
  209.     if board2[2][2] == "O":
  210.         danger3 += 1
  211.  
  212.     if board2[2][0] == "O":
  213.         danger4 += 1
  214.     if board2[0][2] == "O":
  215.         danger4 += 1
  216.  
  217.     if danger3 == 2 and not aiturn:
  218.         if board2[0][0] != "O" and board2[0][0] != "X":
  219.             options[board2[0][0]]()
  220.             print("Ai played")
  221.             board2[0][0] = "O"
  222.             aiturn = True
  223.  
  224.         if board2[1][1] != "O" and board2[1][1] != "X":
  225.             options[board2[1][1]]()
  226.             print("Ai played")
  227.             board2[1][1] = "O"
  228.             aiturn = True
  229.  
  230.         if board2[2][2] != "O" and board2[2][2] != "X":
  231.             options[board2[2][2]]()
  232.             print("Ai played")
  233.             board2[2][2] = "O"
  234.             aiturn = True
  235.  
  236.     if danger4 == 2 and not aiturn:
  237.         if board2[0][2] != "O" and board2[0][2] != "X":
  238.             options[board2[0][2]]()
  239.             print("Ai played")
  240.             board2[0][2] = "O"
  241.             aiturn = True
  242.  
  243.         if board2[1][1] != "O" and board2[1][1] != "X":
  244.             options[board2[1][1]]()
  245.             print("Ai played")
  246.             board2[1][1] = "O"
  247.             aiturn = True
  248.  
  249.         if board2[2][0] != "O" and board2[2][0] != "X":
  250.             options[board2[2][0]]()
  251.             print("Ai played")
  252.             board2[2][0] = "O"
  253.             aiturn = True
  254.  
  255.     return board2, aiturn
  256.  
  257.  
  258. def checkdef(board2, aiturn):
  259.     """block player in the next move"""
  260.     danger = 0
  261.     danger2 = 0
  262.     danger3 = 0
  263.     danger4 = 0
  264.  
  265.     for n in range(3):
  266.         for i in range(3):
  267.             if i == 0:
  268.                 danger = 0
  269.             if board2[n][i] == "X":
  270.                 danger += 1
  271.  
  272.         if danger == 2:
  273.             for i in range(3):
  274.                 if board2[n][i] != "O" and board2[n][i] != "X" and not aiturn:
  275.                     options[board2[n][i]]()
  276.                     print("Ai played")
  277.                     board2[n][i] = "O"
  278.                     danger = 0
  279.                     aiturn = True
  280.  
  281.         for i in range(3):
  282.             if i == 0:
  283.                 danger2 = 0
  284.             if board2[i][n] == "X":
  285.                 danger2 += 1
  286.  
  287.         if danger2 == 2:
  288.             for i in range(3):
  289.                 if board2[i][n] != "O" and board2[i][n] != "X" and not aiturn:
  290.                     options[board2[i][n]]()
  291.                     print("Ai played")
  292.                     board2[i][n] = "O"
  293.                     danger2 = 0
  294.                     aiturn = True
  295.  
  296.     if board2[1][1] == "X":
  297.         danger3 += 1
  298.         danger4 += 1
  299.  
  300.     if board2[0][0] == "X":
  301.         danger3 += 1
  302.     if board2[2][2] == "X":
  303.         danger3 += 1
  304.  
  305.     if board2[2][0] == "X":
  306.         danger4 += 1
  307.     if board2[0][2] == "X":
  308.         danger4 += 1
  309.  
  310.     if danger3 == 2 and not aiturn:
  311.         if board2[0][0] != "O" and board2[0][0] != "X":
  312.             options[board2[0][0]]()
  313.             print("Ai played")
  314.             board2[0][0] = "O"
  315.             aiturn = True
  316.  
  317.         if board2[1][1] != "O" and board2[1][1] != "X":
  318.             options[board2[1][1]]()
  319.             print("Ai played")
  320.             board2[1][1] = "O"
  321.             aiturn = True
  322.  
  323.         if board2[2][2] != "O" and board2[2][2] != "X":
  324.             options[board2[2][2]]()
  325.             print("Ai played")
  326.             board2[2][2] = "O"
  327.             aiturn = True
  328.  
  329.     if danger4 == 2 and not aiturn:
  330.         if board2[0][2] != "O" and board2[0][2] != "X":
  331.             options[board2[0][2]]()
  332.             print("Ai played")
  333.             board2[0][2] = "O"
  334.             aiturn = True
  335.  
  336.         if board2[1][1] != "O" and board2[1][1] != "X":
  337.             options[board2[1][1]]()
  338.             print("Ai played")
  339.             board2[1][1] = "O"
  340.             aiturn = True
  341.  
  342.         if board2[2][0] != "O" and board2[2][0] != "X":
  343.             options[board2[2][0]]()
  344.             print("Ai played")
  345.             board2[2][0] = "O"
  346.             aiturn = True
  347.  
  348.     return board2, aiturn
  349.  
  350.  
  351. # functions to change the buttons the ai chose
  352.  
  353. def ch1():
  354.     button1["text"] = "O"
  355.  
  356.  
  357. def ch2():
  358.     button2["text"] = "O"
  359.  
  360.  
  361. def ch3():
  362.     button3["text"] = "O"
  363.  
  364.  
  365. def ch4():
  366.     button4["text"] = "O"
  367.  
  368.  
  369. def ch5():
  370.     button5["text"] = "O"
  371.  
  372.  
  373. def ch6():
  374.     button6["text"] = "O"
  375.  
  376.  
  377. def ch7():
  378.     button7["text"] = "O"
  379.  
  380.  
  381. def ch8():
  382.     button8["text"] = "O"
  383.  
  384.  
  385. def ch9():
  386.     button9["text"] = "O"
  387.  
  388.  
  389. # dictionary
  390.  
  391. options = {"0": ch1,
  392.            "1": ch2,
  393.            "2": ch3,
  394.            "3": ch4,
  395.            "4": ch5,
  396.            "5": ch6,
  397.            "6": ch7,
  398.            "7": ch8,
  399.            "8": ch9,
  400.            }
  401.  
  402. """ *** Layout *** """
  403.  
  404. button1 = ttk.Button(root, text=" ", command=lambda: turn("0", board2, button1, a))
  405. button2 = ttk.Button(root, text=" ", command=lambda: turn("1", board2, button2, a))
  406. button3 = ttk.Button(root, text=" ", command=lambda: turn("2", board2, button3, a))
  407. button4 = ttk.Button(root, text=" ", command=lambda: turn("3", board2, button4, a))
  408. button5 = ttk.Button(root, text=" ", command=lambda: turn("4", board2, button5, a))
  409. button6 = ttk.Button(root, text=" ", command=lambda: turn("5", board2, button6, a))
  410. button7 = ttk.Button(root, text=" ", command=lambda: turn("6", board2, button7, a))
  411. button8 = ttk.Button(root, text=" ", command=lambda: turn("7", board2, button8, a))
  412. button9 = ttk.Button(root, text=" ", command=lambda: turn("8", board2, button9, a))
  413.  
  414. root.grid_columnconfigure(0, weight=1)
  415. root.grid_columnconfigure(1, weight=1)
  416. root.grid_columnconfigure(2, weight=1)
  417.  
  418. root.grid_rowconfigure(0, weight=1)
  419. root.grid_rowconfigure(1, weight=1)
  420. root.grid_rowconfigure(2, weight=1)
  421.  
  422. button1.grid(row=0, column=0, sticky="nsew", padx=4, pady=4)
  423. button2.grid(row=0, column=1, sticky="nsew", padx=4, pady=4)
  424. button3.grid(row=0, column=2, sticky="nsew", padx=4, pady=4)
  425. button4.grid(row=1, column=0, sticky="nsew", padx=4, pady=4)
  426. button5.grid(row=1, column=1, sticky="nsew", padx=4, pady=4)
  427. button6.grid(row=1, column=2, sticky="nsew", padx=4, pady=4)
  428. button7.grid(row=2, column=0, sticky="nsew", padx=4, pady=4)
  429. button8.grid(row=2, column=1, sticky="nsew", padx=4, pady=4)
  430. button9.grid(row=2, column=2, sticky="nsew", padx=4, pady=4)
  431.  
  432. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top