SHARE
TWEET

Untitled

Lelp05 Jul 23rd, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import time
  3.  
  4. #einleitung
  5. print("*****************");
  6. print('+++TIC-TAC-TOE+++')
  7. print("*****************");
  8. print('')
  9.  
  10.  
  11. class Board:
  12.  
  13.     def __init__(self):
  14.         self.state = [0, 0, 0, 0, 0, 0, 0, 0, 0]
  15.  
  16.     def make_turn(self, cell, player):
  17.         if self.is_valid_turn(cell):
  18.             self.state[cell] = player.symbol
  19.             return True
  20.         return False
  21.  
  22.     def is_valid_turn(self, cell):
  23.         if self.state[cell] == 0:
  24.             return True
  25.         return False
  26.  
  27.     def reset_game(self):
  28.         player_a = Player(1)
  29.         player_b = Player(-1)
  30.         self.__init__()
  31.         activ_player = player_a
  32.  
  33.     def is_full(self):
  34.         for i in self.state:
  35.             if i == 0:
  36.                 return False
  37.         return True
  38.     print('Untenschieden!')
  39.  
  40.     def is_one_placed(self):
  41.         if not self.state[0] == 0 and self.state[1] == 0 and self.state[2] == 0 and self.state[3] == 0 and self.state[4] == 0 and self.state[5] == 0 and self.state[6] == 0 and self.state[7] == 0 and self.state[8] == 0:
  42.             print('erster zug der Ki')
  43.             return False
  44.         return True
  45.  
  46.     def check_win(self, player):
  47.         s = player.symbol
  48.  
  49.         if self.state[0] == s and self.state[1] == s and self.state[2] == s:
  50.             return True
  51.         elif self.state[3] == s and self.state[4] == s and self.state[5] == s:
  52.             return True
  53.         elif self.state[6] == s and self.state[7] == s and self.state[8] == s:
  54.             return True
  55.  
  56.         elif self.state[0] == s and self.state[3] == s and self.state[6] == s:
  57.             return True
  58.         elif self.state[1] == s and self.state[4] == s and self.state[7] == s:
  59.             return True
  60.         elif self.state[2] == s and self.state[5] == s and self.state[8] == s:
  61.             return True
  62.  
  63.         elif self.state[0] == s and self.state[4] == s and self.state[8] == s:
  64.             return True
  65.         elif self.state[2] == s and self.state[4] == s and self.state[6] == s:
  66.             return True
  67.  
  68.     def player_change(self, player):
  69.  
  70.        if player == player_a:
  71.            return player_b
  72.        else:
  73.            return  player_a
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.     def print_board(self):
  83.         print(" "+ self.make_printable(self.state[0]) + " | "+ self.make_printable(self.state[1]) + "  | "+ self.make_printable(self.state[2]) + "  ")
  84.         print(" " + self.make_printable(self.state[3]) + " | " + self.make_printable(self.state[4]) + "  | " + self.make_printable(self.state[5]) + "  ")
  85.         print(" " + self.make_printable(self.state[6]) + " | " + self.make_printable(self.state[7]) + "  | " + self.make_printable(self.state[8]) + "  ")
  86.  
  87.     def make_printable(self, sign):
  88.         if sign == 0:
  89.             return ' '
  90.         elif sign == 1:
  91.             return 'X'
  92.         else:
  93.             return 'O'
  94.  
  95.     def ki_turneasy(self, activ_player):
  96.  
  97.         cellki = random.randint(0, 8)
  98.  
  99.         if self.state[cellki] == 0:
  100.             self.state[cellki] = activ_player.symbol
  101.         else:
  102.             self.ki_turneasy(activ_player)
  103.  
  104.         if self.check_win(activ_player):
  105.             self.print_board()
  106.             print('Die Ki hat das Spiel gewonnen')
  107.             weiter = int(input("[1]Ja [2]Nein\n"))
  108.             if weiter == 1:
  109.                 board.reset_game()
  110.             else:
  111.                 print('Danke f√ľrs Spielen')
  112.  
  113.     def ki_turn(self, activ_player):
  114.  
  115.         placed = False
  116.  
  117.         if self.state[0] == 1 and self.state[2] == 1:
  118.             if self.state[1] != 0:
  119.                 print('Suche nach m√∂glichkeit ...')
  120.             else:
  121.                 placed = True
  122.                 self.state[1] = activ_player.symbol
  123.         if self.state[0] == 1 and self.state[6] == 1:
  124.             if self.state[3] != 0:
  125.                 print('Suche nach m√∂glichkeit ...')
  126.             else:
  127.                 placed = True
  128.                 self.state[3] = activ_player.symbol
  129.         if self.state[0] == 1 and self.state[8] == 1:
  130.             if self.state[4] != 0:
  131.                 print('Suche nach m√∂glichkeit ...')
  132.             else:
  133.                 placed = True
  134.                 self.state[4] = activ_player.symbol
  135.         if self.state[0] == 1 and self.state[1] == 1:
  136.             if self.state[2] != 0:
  137.                 print('Suche nach m√∂glichkeit ...')
  138.             else:
  139.                 placed = True
  140.                 self.state[2] = activ_player.symbol
  141.         if self.state[0] == 1 and self.state[3] == 1:
  142.             if self.state[6] != 0:
  143.                 print('Suche nach m√∂glichkeit ...')
  144.             else:
  145.                 placed = True
  146.                 self.state[6] = activ_player.symbol
  147.         if self.state[0] == 1 and self.state[4] == 1:
  148.             if self.state[8] != 0:
  149.                 print('Suche nach m√∂glichkeit ...')
  150.             else:
  151.                 placed = True
  152.                 self.state[8] = activ_player.symbol
  153.         # feld 1
  154.  
  155.         if self.state[1] == 1 and self.state[7] == 1:
  156.             if self.state[4] != 0:
  157.                 print('Suche nach m√∂glichkeit ...')
  158.             else:
  159.                 placed = True
  160.                 self.state[4] = activ_player.symbol
  161.         if self.state[1] == 1 and self.state[4] == 1:
  162.             if self.state[7] != 0:
  163.                 print('Suche nach m√∂glichkeit ...')
  164.             else:
  165.                 placed = True
  166.                 self.state[7] = activ_player.symbol
  167.  
  168.         # feld 2
  169.  
  170.         if self.state[2] == 1 and self.state[8] == 1:
  171.             if self.state[5] != 0:
  172.                 print('Suche nach m√∂glichkeit ...')
  173.             else:
  174.                 placed = True
  175.                 self.state[5] = activ_player.symbol
  176.         if self.state[2] == 1 and self.state[6] == 1:
  177.             if self.state[4] != 0:
  178.                 print('Suche nach m√∂glichkeit ...')
  179.             else:
  180.                 placed = True
  181.                 self.state[4] = activ_player.symbol
  182.         if self.state[2] == 1 and self.state[1] == 1:
  183.             if self.state[0] != 0:
  184.                 print('Suche nach m√∂glichkeit ...')
  185.             else:
  186.                 placed = True
  187.                 self.state[0] = activ_player.symbol
  188.         if self.state[2] == 1 and self.state[5] == 1:
  189.             if self.state[8] != 0:
  190.                 print('Suche nach m√∂glichkeit ...')
  191.             else:
  192.                 placed = True
  193.                 self.state[8] = activ_player.symbol
  194.         if self.state[2] == 1 and self.state[6] == 1:
  195.             if self.state[4] != 0:
  196.                 print('Suche nach m√∂glichkeit ...')
  197.             else:
  198.                 placed = True
  199.                 self.state[4] = activ_player.symbol
  200.  
  201.         # feld 3
  202.  
  203.         if self.state[3] == 1 and self.state[5] == 1:
  204.             if self.state[4] != 0:
  205.                 print('Suche nach m√∂glichkeit ...')
  206.             else:
  207.                 placed = True
  208.                 self.state[4] = activ_player.symbol
  209.         if self.state[3] == 1 and self.state[4] == 1:
  210.             if self.state[5] != 0:
  211.                 print('Suche nach m√∂glichkeit ...')
  212.             else:
  213.                 placed = True
  214.                 self.state[5] = activ_player.symbol
  215.         if self.state[3] == 1 and self.state[6] == 1:
  216.             if self.state[0] != 0:
  217.                 print('Suche nach m√∂glichkeit ...')
  218.             else:
  219.                 placed = True
  220.                 self.state[0] = activ_player.symbol
  221.  
  222.         # feld 4
  223.  
  224.         if self.state[4] == 1 and self.state[7] == 1:
  225.             if self.state[1] != 0:
  226.                 print('Suche nach m√∂glichkeit ...')
  227.             else:
  228.                 placed = True
  229.                 self.state[1] = activ_player.symbol
  230.         if self.state[4] == 1 and self.state[1] == 1:
  231.             if self.state[7] != 0:
  232.                 print('Suche nach m√∂glichkeit ...')
  233.             else:
  234.                 placed = True
  235.                 self.state[7] = activ_player.symbol
  236.         if self.state[4] == 1 and self.state[5] == 1:
  237.             if self.state[3] != 0:
  238.                 print('Suche nach m√∂glichkeit ...')
  239.             else:
  240.                 placed = True
  241.                 self.state[3] = activ_player.symbol
  242.         if self.state[4] == 1 and self.state[3] == 1:
  243.             if self.state[5] != 0:
  244.                 print('Suche nach m√∂glichkeit ...')
  245.             else:
  246.                 placed = True
  247.                 self.state[5] = activ_player.symbol
  248.         if self.state[4] == 1 and self.state[2] == 1:
  249.             if self.state[6] != 0:
  250.                 print('Suche nach m√∂glichkeit ...')
  251.             else:
  252.                 placed = True
  253.                 self.state[6] = activ_player.symbol
  254.         if self.state[4] == 1 and self.state[6] == 1:
  255.             if self.state[2] != 0:
  256.                 print('Suche nach m√∂glichkeit ...')
  257.             else:
  258.                 placed = True
  259.                 self.state[2] = activ_player.symbol
  260.         if self.state[4] == 1 and self.state[0] == 1:
  261.             if self.state[8] != 0:
  262.                 print('Suche nach m√∂glichkeit ...')
  263.             else:
  264.                 placed = True
  265.                 self.state[8] = activ_player.symbol
  266.         if self.state[4] == 1 and self.state[8] == 1:
  267.             if self.state[0] != 0:
  268.                 print('Suche nach m√∂glichkeit ...')
  269.             else:
  270.                 placed = True
  271.                 self.state[0] = activ_player.symbol
  272.         # feld 5
  273.  
  274.         if self.state[5] == 1 and self.state[2] == 1:
  275.             if self.state[8] != 0:
  276.                 print('Suche nach m√∂glichkeit ...')
  277.             else:
  278.                 placed = True
  279.                 self.state[8] = activ_player.symbol
  280.         if self.state[5] == 1 and self.state[8] == 1:
  281.             if self.state[2] != 0:
  282.                 print('Suche nach m√∂glichkeit ...')
  283.             else:
  284.                 placed = True
  285.                 self.state[2] = activ_player.symbol
  286.         if self.state[5] == 1 and self.state[4] == 1:
  287.             if self.state[3] != 0:
  288.                 print('Suche nach m√∂glichkeit ...')
  289.             else:
  290.                 placed = True
  291.                 self.state[3] = activ_player.symbol
  292.  
  293.         # feld 6
  294.  
  295.         if self.state[6] == 1 and self.state[8] == 1:
  296.             if self.state[7] != 0:
  297.                 print('Suche nach m√∂glichkeit ...')
  298.             else:
  299.                 placed = True
  300.                 self.state[7] = activ_player.symbol
  301.         if self.state[6] == 1 and self.state[3] == 1:
  302.             if self.state[0] != 0:
  303.                 print('Suche nach m√∂glichkeit ...')
  304.             else:
  305.                 placed = True
  306.                 self.state[0] = activ_player.symbol
  307.         if self.state[6] == 1 and self.state[7] == 1:
  308.             if self.state[8] != 0:
  309.                 print('Suche nach m√∂glichkeit ...')
  310.             else:
  311.                 placed = True
  312.                 self.state[8] = activ_player.symbol
  313.         if self.state[6] == 1 and self.state[4] == 1:
  314.             if self.state[2] != 0:
  315.                 print('Suche nach m√∂glichkeit ...')
  316.             else:
  317.                 placed = True
  318.                 self.state[2] = activ_player.symbol
  319.  
  320.         # feld 7
  321.  
  322.         if self.state[7] == 1 and self.state[4] == 1:
  323.             if self.state[1] != 0:
  324.                 print('Suche nach m√∂glichkeit ...')
  325.             else:
  326.                 placed = True
  327.                 self.state[1] = activ_player.symbol
  328.         if self.state[7] == 1 and self.state[8] == 1:
  329.             if self.state[6] != 0:
  330.                 print('Suche nach m√∂glichkeit ...')
  331.             else:
  332.                 placed = True
  333.                 self.state[6] = activ_player.symbol
  334.         if self.state[7] == 1 and self.state[6] == 1:
  335.             if self.state[8] != 0:
  336.                 print('Suche nach m√∂glichkeit ...')
  337.             else:
  338.                 placed = True
  339.                 self.state[8] = activ_player.symbol
  340.  
  341.         # feld 8
  342.  
  343.         if self.state[8] == 1 and self.state[5] == 1:
  344.             if self.state[2] != 0:
  345.                 print('Suche nach m√∂glichkeit ...')
  346.             else:
  347.                 placed = True
  348.                 self.state[2] = activ_player.symbol
  349.         if self.state[8] == 1 and self.state[4] == 1:
  350.             if self.state[0] != 0:
  351.                 print('Suche nach m√∂glichkeit ...')
  352.             else:
  353.                 placed = True
  354.                 self.state[0] = activ_player.symbol
  355.         if self.state[8] == 1 and self.state[7] == 1:
  356.             if self.state[6] != 0:
  357.                 print('Suche nach m√∂glichkeit ...')
  358.             else:
  359.                 placed = True
  360.                 self.state[6] = activ_player.symbol
  361.  
  362.         # anders
  363.         if not self.is_one_placed() or not placed:
  364.  
  365.             cellki = random.randint(0, 8)
  366.  
  367.             if self.state[cellki] == 0:
  368.                 self.state[cellki] = activ_player.symbol
  369.             else:
  370.                 if not self.is_full():
  371.                     self.ki_turn(activ_player)
  372.                 else:
  373.                     print('Unentschieden!')
  374.                     self.reset_game()
  375.  
  376.         if self.check_win(activ_player):
  377.             self.print_board()
  378.             print('Die Ki hat das Spiel gewonnen')
  379.             weiter = int(input("[1]Ja [2]Nein\n"))
  380.             if weiter == 1:
  381.                 board.reset_game()
  382.             else:
  383.                 print('Danke f√ľrs Spielen')
  384.  
  385.  
  386.  
  387. class Player:
  388.  
  389.     def __init__(self, symbol):
  390.             self.symbol = symbol
  391.  
  392.  
  393.  
  394. if __name__ == '__main__':
  395.     player_a = Player(1)
  396.     player_b = Player(-1)
  397.     board = Board()
  398.     activ_player = player_a
  399.  
  400.     difficulty = int(input('Welche schwierigkeit  [1]Leicht [2]Schwer'))
  401.  
  402.     while not board.is_full():
  403.  
  404.             try:
  405.  
  406.                 print('')
  407.                 print('##########################')
  408.                 board.print_board()
  409.                 print('##########################')
  410.                 print('')
  411.  
  412.  
  413.                 cell = int(input('Wo willst du dein zeichen setzen? [1-9]'))
  414.  
  415.  
  416.             except ValueError:
  417.                 continue
  418.             cell = cell - 1
  419.  
  420.             if cell < 0 or cell > 8:
  421.                 print('Bitte gebe eine Zahl zwischen [1-9] an')
  422.                 continue
  423.  
  424.             if not board.make_turn(cell, activ_player):
  425.                 print('Das darfst du nicht!')
  426.                 continue
  427.  
  428.             if board.check_win(activ_player):
  429.                 board.print_board()
  430.                 print('Der Spieler hat das Spiel gewonnen!')
  431.                 print('')
  432.                 try:
  433.                     weiter = int(input("[1]Ja [2]Nein\n"))
  434.                     if weiter == 1:
  435.                         board.reset_game()
  436.                     else:
  437.                         print('Danke f√ľrs Spielen')
  438.                         break
  439.                 except ValueError:
  440.                     continue
  441.  
  442.  
  443.  
  444.             activ_player = board.player_change(activ_player)
  445.             if activ_player == player_b:
  446.                 print('')
  447.                 print('!!!!!!!!!!!!!!!!!!!!!!!!!')
  448.                 print('es spielt die Ki')
  449.                 print('!!!!!!!!!!!!!!!!!!!!!!!!!')
  450.                 print('')
  451.  
  452.                 if difficulty == 2:
  453.                     board.ki_turn(activ_player)
  454.                 else:
  455.                     board.ki_turneasy(activ_player)
  456.  
  457.                 activ_player = board.player_change(activ_player)
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top