Advertisement
Guest User

XO

a guest
Apr 14th, 2016
3,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.39 KB | None | 0 0
  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.  
  13. """ *** Functions *** """
  14. player = tkinter.messagebox.askyesno("First thing first", "Playing against another player?")
  15. print("is there another player? ", player)
  16.  
  17.  
  18. def turn(pos1, board2, button, a):
  19.     """ enter choice to memory and change the board and interface """
  20.     print("playing with", a)
  21.     for i in range(len(board2)):
  22.         for n in range(len(board2[i])):
  23.             if board2[i][n] == str(pos1):
  24.                 board2[i][n] = str(a[0])
  25.  
  26.     # toggle button text
  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.  
  44.     # check win state
  45.     win()
  46.  
  47.     # Ai
  48.     aiturn = False
  49.     if not player:
  50.  
  51.         print("fired ai")
  52.  
  53.         if not aiturn:
  54.             board2, aiturn = checkatk(board2, aiturn)
  55.             print("ai turn for atk is ", aiturn)
  56.  
  57.  
  58.         if not aiturn:
  59.             board2, aiturn = checkdef(board2, aiturn)
  60.             print("ai turn for def is ", aiturn)
  61.  
  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.     win()
  74.  
  75.     return board2, pos1, button, a
  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.  
  104.     if (board2[0][0] + board2[1][1] + board2[2][2]) == "XXX" or (
  105.             board2[0][2] + board2[1][1] + board2[2][0]) == "XXX":
  106.         reset(1)
  107.  
  108.     for i in range(3):
  109.  
  110.         if "OOO" == str("".join(board2[i][0:3])) or "OOO" == (board2[0][i] + board2[1][i] + board2[2][i]):
  111.             reset(2)
  112.  
  113.     if (board2[0][0] + board2[1][1] + board2[2][2]) == "OOO" or (
  114.             board2[0][2] + board2[1][1] + board2[2][0]) == "OOO":
  115.         reset(2)
  116.  
  117.     info = 0
  118.     for i in range(3):
  119.         for n in range(3):
  120.             if board2[i][n] == "X":
  121.                 info += 1
  122.  
  123.             if board2[i][n] == "O":
  124.                 info += 1
  125.  
  126.     if info == 9:
  127.         reset(0)
  128.  
  129.  
  130. def reset(n):
  131.     """ win state, reset board """
  132.     if n == 0:
  133.         tkinter.messagebox.showinfo('No one won', 'No one won, restarting')
  134.     else:
  135.         tkinter.messagebox.showinfo('There is a winner!', 'Player ' + str(n) + ' is the winner!')
  136.     numb = 0
  137.     button1["text"] = " "
  138.     button2["text"] = " "
  139.     button3["text"] = " "
  140.     button4["text"] = " "
  141.     button5["text"] = " "
  142.     button6["text"] = " "
  143.     button7["text"] = " "
  144.     button8["text"] = " "
  145.     button9["text"] = " "
  146.     for i in range(len(board2)):
  147.         for n in range(len(board2[i])):
  148.             board2[i][n] = str(numb)
  149.             numb += 1
  150.  
  151.  
  152.     return board2
  153.  
  154. def checkatk(board2, aiturn):
  155.     """win in the game in the next move"""
  156.     danger = 0
  157.     danger2 = 0
  158.     danger3 = 0
  159.     danger4 = 0
  160.  
  161.     for n in range(3):
  162.         for i in range(3):
  163.             if i == 0:
  164.                 danger = 0
  165.             if board2[n][i] == "O":
  166.                 danger += 1
  167.  
  168.         if danger == 2:
  169.             for i in range(3):
  170.                 if board2[n][i] != "O" and board2[n][i] != "X" and not aiturn:
  171.  
  172.                     options[board2[n][i]]()
  173.                     print("Ai played")
  174.                     board2[n][i] = "O"
  175.                     danger = 0
  176.                     aiturn = True
  177.  
  178.         for i in range(3):
  179.             if i == 0:
  180.                 danger2 = 0
  181.             if board2[i][n] == "O":
  182.                 danger2 += 1
  183.  
  184.         if danger2 == 2:
  185.             for i in range(3):
  186.                 if board2[i][n] != "O" and board2[i][n] != "X" and not aiturn:
  187.  
  188.                     options[board2[i][n]]()
  189.                     print("Ai played")
  190.                     board2[i][n] = "O"
  191.                     danger2 = 0
  192.                     aiturn = True
  193.  
  194.     if board2[1][1] == "O":
  195.         danger3 += 1
  196.         danger4 += 1
  197.  
  198.     if board2[0][0] == "O":
  199.         danger3 += 1
  200.     if board2[2][2] == "O":
  201.         danger3 += 1
  202.  
  203.     if board2[2][0] == "O":
  204.         danger4 += 1
  205.     if board2[0][2] == "O":
  206.         danger4 += 1
  207.  
  208.     if danger3 == 2 and not aiturn:
  209.         if board2[0][0] != "O" and board2[0][0] != "X":
  210.  
  211.             options[board2[0][0]]()
  212.             print("Ai played")
  213.             board2[0][0] = "O"
  214.             aiturn = True
  215.  
  216.         if board2[1][1] != "O" and board2[1][1] != "X":
  217.  
  218.             options[board2[1][1]]()
  219.             print("Ai played")
  220.             board2[1][1] = "O"
  221.             aiturn = True
  222.  
  223.         if board2[2][2] != "O" and board2[2][2] != "X":
  224.  
  225.             options[board2[2][2]]()
  226.             print("Ai played")
  227.             board2[2][2] = "O"
  228.             aiturn = True
  229.  
  230.     if danger4 == 2 and not aiturn:
  231.         if board2[0][2] != "O" and board2[0][2] != "X":
  232.  
  233.             options[board2[0][2]]()
  234.             print("Ai played")
  235.             board2[0][2] = "O"
  236.             aiturn = True
  237.  
  238.         if board2[1][1] != "O" and board2[1][1] != "X":
  239.  
  240.             options[board2[1][1]]()
  241.             print("Ai played")
  242.             board2[1][1] = "O"
  243.             aiturn = True
  244.  
  245.         if board2[2][0] != "O" and board2[2][0] != "X":
  246.  
  247.             options[board2[2][0]]()
  248.             print("Ai played")
  249.             board2[2][0] = "O"
  250.             aiturn = True
  251.  
  252.     return board2, aiturn
  253.  
  254.  
  255. def checkdef(board2, aiturn):
  256.     """block player in the next move"""
  257.     danger = 0
  258.     danger2 = 0
  259.     danger3 = 0
  260.     danger4 = 0
  261.  
  262.     for n in range(3):
  263.         for i in range(3):
  264.             if i == 0:
  265.                 danger = 0
  266.             if board2[n][i] == "X":
  267.                 danger += 1
  268.  
  269.         if danger == 2:
  270.             for i in range(3):
  271.                 if board2[n][i] != "O" and board2[n][i] != "X" and not aiturn:
  272.  
  273.                     options[board2[n][i]]()
  274.                     print("Ai played")
  275.                     board2[n][i] = "O"
  276.                     danger = 0
  277.                     aiturn = True
  278.  
  279.         for i in range(3):
  280.             if i == 0:
  281.                 danger2 = 0
  282.             if board2[i][n] == "X":
  283.                 danger2 += 1
  284.  
  285.         if danger2 == 2:
  286.             for i in range(3):
  287.                 if board2[i][n] != "O" and board2[i][n] != "X" and not aiturn:
  288.  
  289.                     options[board2[i][n]]()
  290.                     print("Ai played")
  291.                     board2[i][n] = "O"
  292.                     danger2 = 0
  293.                     aiturn = True
  294.  
  295.     if board2[1][1] == "X":
  296.         danger3 += 1
  297.         danger4 += 1
  298.  
  299.     if board2[0][0] == "X":
  300.         danger3 += 1
  301.     if board2[2][2] == "X":
  302.         danger3 += 1
  303.  
  304.     if board2[2][0] == "X":
  305.         danger4 += 1
  306.     if board2[0][2] == "X":
  307.         danger4 += 1
  308.  
  309.     if danger3 == 2 and not aiturn:
  310.         if board2[0][0] != "O" and board2[0][0] != "X":
  311.  
  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.  
  319.             options[board2[1][1]]()
  320.             print("Ai played")
  321.             board2[1][1] = "O"
  322.             aiturn = True
  323.  
  324.         if board2[2][2] != "O" and board2[2][2] != "X":
  325.  
  326.             options[board2[2][2]]()
  327.             print("Ai played")
  328.             board2[2][2] = "O"
  329.             aiturn = True
  330.  
  331.     if danger4 == 2 and not aiturn:
  332.         if board2[0][2] != "O" and board2[0][2] != "X":
  333.  
  334.             options[board2[0][2]]()
  335.             print("Ai played")
  336.             board2[0][2] = "O"
  337.             aiturn = True
  338.  
  339.         if board2[1][1] != "O" and board2[1][1] != "X":
  340.  
  341.             options[board2[1][1]]()
  342.             print("Ai played")
  343.             board2[1][1] = "O"
  344.             aiturn = True
  345.  
  346.         if board2[2][0] != "O" and board2[2][0] != "X":
  347.  
  348.             options[board2[2][0]]()
  349.             print("Ai played")
  350.             board2[2][0] = "O"
  351.             aiturn = True
  352.  
  353.     return board2, aiturn
  354.  
  355. # functions to change the buttons the ai chose
  356.  
  357. def ch1():
  358.     button1["text"] = "O"
  359.  
  360.  
  361. def ch2():
  362.     button2["text"] = "O"
  363.  
  364.  
  365. def ch3():
  366.     button3["text"] = "O"
  367.  
  368.  
  369. def ch4():
  370.     button4["text"] = "O"
  371.  
  372.  
  373. def ch5():
  374.     button5["text"] = "O"
  375.  
  376.  
  377. def ch6():
  378.     button6["text"] = "O"
  379.  
  380.  
  381. def ch7():
  382.     button7["text"] = "O"
  383.  
  384.  
  385. def ch8():
  386.     button8["text"] = "O"
  387.  
  388.  
  389. def ch9():
  390.     button9["text"] = "O"
  391.  
  392.  
  393. # dictionary
  394.  
  395. options = {"0": ch1,
  396.            "1": ch2,
  397.            "2": ch3,
  398.            "3": ch4,
  399.            "4": ch5,
  400.            "5": ch6,
  401.            "6": ch7,
  402.            "7": ch8,
  403.            "8": ch9,
  404.            }
  405.  
  406. """ *** Layout *** """
  407.  
  408. button1 = ttk.Button(root, text=" ", command=lambda: turn("0", board2, button1, a))
  409. button2 = ttk.Button(root, text=" ", command=lambda: turn("1", board2, button2, a))
  410. button3 = ttk.Button(root, text=" ", command=lambda: turn("2", board2, button3, a))
  411. button4 = ttk.Button(root, text=" ", command=lambda: turn("3", board2, button4, a))
  412. button5 = ttk.Button(root, text=" ", command=lambda: turn("4", board2, button5, a))
  413. button6 = ttk.Button(root, text=" ", command=lambda: turn("5", board2, button6, a))
  414. button7 = ttk.Button(root, text=" ", command=lambda: turn("6", board2, button7, a))
  415. button8 = ttk.Button(root, text=" ", command=lambda: turn("7", board2, button8, a))
  416. button9 = ttk.Button(root, text=" ", command=lambda: turn("8", board2, button9, a))
  417.  
  418. root.grid_columnconfigure(0, weight=1)
  419. root.grid_columnconfigure(1, weight=1)
  420. root.grid_columnconfigure(2, weight=1)
  421.  
  422. root.grid_rowconfigure(0, weight=1)
  423. root.grid_rowconfigure(1, weight=1)
  424. root.grid_rowconfigure(2, weight=1)
  425.  
  426. button1.grid(row=0, column=0, sticky="nsew", padx=4, pady=4)
  427. button2.grid(row=0, column=1, sticky="nsew", padx=4, pady=4)
  428. button3.grid(row=0, column=2, sticky="nsew", padx=4, pady=4)
  429. button4.grid(row=1, column=0, sticky="nsew", padx=4, pady=4)
  430. button5.grid(row=1, column=1, sticky="nsew", padx=4, pady=4)
  431. button6.grid(row=1, column=2, sticky="nsew", padx=4, pady=4)
  432. button7.grid(row=2, column=0, sticky="nsew", padx=4, pady=4)
  433. button8.grid(row=2, column=1, sticky="nsew", padx=4, pady=4)
  434. button9.grid(row=2, column=2, sticky="nsew", padx=4, pady=4)
  435.  
  436. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement