Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- #einleitung
- print("*****************");
- print('+++TIC-TAC-TOE+++')
- print("*****************");
- print('')
- class Board:
- def __init__(self):
- self.state = [0, 0, 0, 0, 0, 0, 0, 0, 0]
- def make_turn(self, cell, player):
- if self.is_valid_turn(cell):
- self.state[cell] = player.symbol
- return True
- return False
- def is_valid_turn(self, cell):
- if self.state[cell] == 0:
- return True
- return False
- def reset_game(self):
- player_a = Player(1)
- player_b = Player(-1)
- self.__init__()
- activ_player = player_a
- def is_full(self):
- for i in self.state:
- if i == 0:
- return False
- return True
- print('Untenschieden!')
- def is_one_placed(self):
- 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:
- print('erster zug der Ki')
- return False
- return True
- def check_win(self, player):
- s = player.symbol
- if self.state[0] == s and self.state[1] == s and self.state[2] == s:
- return True
- elif self.state[3] == s and self.state[4] == s and self.state[5] == s:
- return True
- elif self.state[6] == s and self.state[7] == s and self.state[8] == s:
- return True
- elif self.state[0] == s and self.state[3] == s and self.state[6] == s:
- return True
- elif self.state[1] == s and self.state[4] == s and self.state[7] == s:
- return True
- elif self.state[2] == s and self.state[5] == s and self.state[8] == s:
- return True
- elif self.state[0] == s and self.state[4] == s and self.state[8] == s:
- return True
- elif self.state[2] == s and self.state[4] == s and self.state[6] == s:
- return True
- def player_change(self, player):
- if player == player_a:
- return player_b
- else:
- return player_a
- def print_board(self):
- print(" "+ self.make_printable(self.state[0]) + " | "+ self.make_printable(self.state[1]) + " | "+ self.make_printable(self.state[2]) + " ")
- print(" " + self.make_printable(self.state[3]) + " | " + self.make_printable(self.state[4]) + " | " + self.make_printable(self.state[5]) + " ")
- print(" " + self.make_printable(self.state[6]) + " | " + self.make_printable(self.state[7]) + " | " + self.make_printable(self.state[8]) + " ")
- def make_printable(self, sign):
- if sign == 0:
- return ' '
- elif sign == 1:
- return 'X'
- else:
- return 'O'
- def ki_turneasy(self, activ_player):
- cellki = random.randint(0, 8)
- if self.state[cellki] == 0:
- self.state[cellki] = activ_player.symbol
- else:
- self.ki_turneasy(activ_player)
- if self.check_win(activ_player):
- self.print_board()
- print('Die Ki hat das Spiel gewonnen')
- weiter = int(input("[1]Ja [2]Nein\n"))
- if weiter == 1:
- board.reset_game()
- else:
- print('Danke fürs Spielen')
- def ki_turn(self, activ_player):
- placed = False
- if self.state[0] == 1 and self.state[2] == 1:
- if self.state[1] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[1] = activ_player.symbol
- if self.state[0] == 1 and self.state[6] == 1:
- if self.state[3] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[3] = activ_player.symbol
- if self.state[0] == 1 and self.state[8] == 1:
- if self.state[4] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[4] = activ_player.symbol
- if self.state[0] == 1 and self.state[1] == 1:
- if self.state[2] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[2] = activ_player.symbol
- if self.state[0] == 1 and self.state[3] == 1:
- if self.state[6] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[6] = activ_player.symbol
- if self.state[0] == 1 and self.state[4] == 1:
- if self.state[8] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[8] = activ_player.symbol
- # feld 1
- if self.state[1] == 1 and self.state[7] == 1:
- if self.state[4] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[4] = activ_player.symbol
- if self.state[1] == 1 and self.state[4] == 1:
- if self.state[7] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[7] = activ_player.symbol
- # feld 2
- if self.state[2] == 1 and self.state[8] == 1:
- if self.state[5] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[5] = activ_player.symbol
- if self.state[2] == 1 and self.state[6] == 1:
- if self.state[4] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[4] = activ_player.symbol
- if self.state[2] == 1 and self.state[1] == 1:
- if self.state[0] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[0] = activ_player.symbol
- if self.state[2] == 1 and self.state[5] == 1:
- if self.state[8] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[8] = activ_player.symbol
- if self.state[2] == 1 and self.state[6] == 1:
- if self.state[4] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[4] = activ_player.symbol
- # feld 3
- if self.state[3] == 1 and self.state[5] == 1:
- if self.state[4] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[4] = activ_player.symbol
- if self.state[3] == 1 and self.state[4] == 1:
- if self.state[5] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[5] = activ_player.symbol
- if self.state[3] == 1 and self.state[6] == 1:
- if self.state[0] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[0] = activ_player.symbol
- # feld 4
- if self.state[4] == 1 and self.state[7] == 1:
- if self.state[1] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[1] = activ_player.symbol
- if self.state[4] == 1 and self.state[1] == 1:
- if self.state[7] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[7] = activ_player.symbol
- if self.state[4] == 1 and self.state[5] == 1:
- if self.state[3] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[3] = activ_player.symbol
- if self.state[4] == 1 and self.state[3] == 1:
- if self.state[5] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[5] = activ_player.symbol
- if self.state[4] == 1 and self.state[2] == 1:
- if self.state[6] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[6] = activ_player.symbol
- if self.state[4] == 1 and self.state[6] == 1:
- if self.state[2] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[2] = activ_player.symbol
- if self.state[4] == 1 and self.state[0] == 1:
- if self.state[8] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[8] = activ_player.symbol
- if self.state[4] == 1 and self.state[8] == 1:
- if self.state[0] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[0] = activ_player.symbol
- # feld 5
- if self.state[5] == 1 and self.state[2] == 1:
- if self.state[8] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[8] = activ_player.symbol
- if self.state[5] == 1 and self.state[8] == 1:
- if self.state[2] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[2] = activ_player.symbol
- if self.state[5] == 1 and self.state[4] == 1:
- if self.state[3] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[3] = activ_player.symbol
- # feld 6
- if self.state[6] == 1 and self.state[8] == 1:
- if self.state[7] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[7] = activ_player.symbol
- if self.state[6] == 1 and self.state[3] == 1:
- if self.state[0] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[0] = activ_player.symbol
- if self.state[6] == 1 and self.state[7] == 1:
- if self.state[8] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[8] = activ_player.symbol
- if self.state[6] == 1 and self.state[4] == 1:
- if self.state[2] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[2] = activ_player.symbol
- # feld 7
- if self.state[7] == 1 and self.state[4] == 1:
- if self.state[1] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[1] = activ_player.symbol
- if self.state[7] == 1 and self.state[8] == 1:
- if self.state[6] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[6] = activ_player.symbol
- if self.state[7] == 1 and self.state[6] == 1:
- if self.state[8] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[8] = activ_player.symbol
- # feld 8
- if self.state[8] == 1 and self.state[5] == 1:
- if self.state[2] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[2] = activ_player.symbol
- if self.state[8] == 1 and self.state[4] == 1:
- if self.state[0] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[0] = activ_player.symbol
- if self.state[8] == 1 and self.state[7] == 1:
- if self.state[6] != 0:
- print('Suche nach möglichkeit ...')
- else:
- placed = True
- self.state[6] = activ_player.symbol
- # anders
- if not self.is_one_placed() or not placed:
- cellki = random.randint(0, 8)
- if self.state[cellki] == 0:
- self.state[cellki] = activ_player.symbol
- else:
- if not self.is_full():
- self.ki_turn(activ_player)
- else:
- print('Unentschieden!')
- self.reset_game()
- if self.check_win(activ_player):
- self.print_board()
- print('Die Ki hat das Spiel gewonnen')
- weiter = int(input("[1]Ja [2]Nein\n"))
- if weiter == 1:
- board.reset_game()
- else:
- print('Danke fürs Spielen')
- class Player:
- def __init__(self, symbol):
- self.symbol = symbol
- if __name__ == '__main__':
- player_a = Player(1)
- player_b = Player(-1)
- board = Board()
- activ_player = player_a
- difficulty = int(input('Welche schwierigkeit [1]Leicht [2]Schwer'))
- while not board.is_full():
- try:
- print('')
- print('##########################')
- board.print_board()
- print('##########################')
- print('')
- cell = int(input('Wo willst du dein zeichen setzen? [1-9]'))
- except ValueError:
- continue
- cell = cell - 1
- if cell < 0 or cell > 8:
- print('Bitte gebe eine Zahl zwischen [1-9] an')
- continue
- if not board.make_turn(cell, activ_player):
- print('Das darfst du nicht!')
- continue
- if board.check_win(activ_player):
- board.print_board()
- print('Der Spieler hat das Spiel gewonnen!')
- print('')
- try:
- weiter = int(input("[1]Ja [2]Nein\n"))
- if weiter == 1:
- board.reset_game()
- else:
- print('Danke fürs Spielen')
- break
- except ValueError:
- continue
- activ_player = board.player_change(activ_player)
- if activ_player == player_b:
- print('')
- print('!!!!!!!!!!!!!!!!!!!!!!!!!')
- print('es spielt die Ki')
- print('!!!!!!!!!!!!!!!!!!!!!!!!!')
- print('')
- if difficulty == 2:
- board.ki_turn(activ_player)
- else:
- board.ki_turneasy(activ_player)
- activ_player = board.player_change(activ_player)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement