Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Napisz funkcje, ktora wyswietla plansze
- # Napisz funkcje, ktora wczyta ruchy graczy i wyswietli je na ekranie komputera
- # Napisz funkcje, ktora po kazdym ruchu sprawdzi czy rozgrywka sie zakonczyla i wyswietli wynik
- import random
- board = ["-", "-", "-",
- "-", "-", "-",
- "-", "-", "-"]
- game_is_not_over = True
- current_player = "X"
- winner = None
- pos = None
- def display_board():
- print(" -------------")
- print(" | " + board[0] + " | " + board[1] + " | " + board[2] + " | ")
- print(" | " + board[3] + " | " + board[4] + " | " + board[5] + " | ")
- print(" | " + board[6] + " | " + board[7] + " | " + board[8] + " | ")
- print(" -------------")
- def choose_position(player):
- print("Ruch Gracza: ", player)
- position = input("Wybierz wolna pozycje 1-9: ")
- correct = False
- while not correct:
- while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
- position = input("Wybierz wolna pozycje 1-9: ")
- position = int(position) - 1
- if board[position] == "-":
- correct = True
- else:
- print("Wybrana pozycja jest juz zajeta")
- board[position] = player
- display_board()
- def play_ai():
- display_board()
- while game_is_not_over:
- choose_position_ai(current_player)
- end_of_the_game()
- player_change()
- if winner == "X" or winner == "O":
- print(winner, " wygrywa.")
- elif winner == None:
- print("Remis.")
- def play():
- display_board()
- while game_is_not_over:
- choose_position(current_player)
- end_of_the_game()
- player_change()
- if winner == "X" or winner == "O":
- print(winner, " wygrywa.")
- elif winner == None:
- print("Remis.")
- # Plan dzialania:
- # 1. Wyswietl tablice
- # 2. Pozwol graczowi 1 wybrac pole 1-9
- # 3. Sprawdz czy to pole nie jest juz zajete
- # 4. Sprawdz czy nastapila wygrana/remis
- # 5. Zmien gracza
- # 6. ... i tak do skutku
- # Wybierz pole
- def choose_position_ai(player):
- print("Ruch Gracza: ", player)
- if player == "O":
- correct = False
- while not correct:
- position = random.randint(0, 8)
- print(random.randint(0, 8))
- position = int(position)
- if board[position] == "-":
- correct = True
- else:
- print("Wybrana pozycja jest juz zajeta")
- board[position] = player
- display_board()
- elif player == "X":
- position = input("Wybierz wolna pozycje 1-9: ")
- correct = False
- while not correct:
- while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
- position = input("Wybierz wolna pozycje 1-9: ")
- position = int(position) - 1
- if board[position] == "-":
- correct = True
- else:
- print("Wybrana pozycja jest juz zajeta")
- board[position] = player
- display_board()
- # Zmien gracza
- def player_change():
- global current_player
- if current_player == "X":
- current_player = "O"
- elif current_player == "O":
- current_player = "X"
- def end_of_the_game():
- check_winner()
- check_tie()
- def check_columns():
- global game_is_not_over
- column_1 = board[0] == board[3] == board[6] != "-"
- column_2 = board[1] == board[4] == board[7] != "-"
- column_3 = board[2] == board[5] == board[8] != "-"
- if column_1 or column_2 or column_3:
- game_is_not_over = False
- if column_1:
- return board[0]
- elif column_2:
- return board[1]
- elif column_3:
- return board[2]
- else:
- return None
- def check_rows():
- global game_is_not_over
- row_1 = board[0] == board[1] == board[2] != "-"
- row_2 = board[3] == board[4] == board[5] != "-"
- row_3 = board[6] == board[7] == board[8] != "-"
- if row_1 or row_2 or row_3:
- game_is_not_over = False
- if row_1:
- return board[0]
- elif row_2:
- return board[3]
- elif row_3:
- return board[6]
- else:
- return None
- def check_diagonals():
- global game_is_not_over
- diagonal_1 = board[0] == board[4] == board[8] != "-"
- diagonal_2 = board[2] == board[4] == board[6] != "-"
- if diagonal_1 or diagonal_2:
- game_is_not_over = False
- if diagonal_1:
- return board[0]
- elif diagonal_2:
- return board[2]
- else:
- return None
- def check_winner():
- global winner
- column_winner = check_columns()
- row_winner = check_rows()
- diagonal_winner = check_diagonals()
- if column_winner:
- winner = column_winner
- elif row_winner:
- winner = row_winner
- elif diagonal_winner:
- winner = diagonal_winner
- else:
- winner = None
- def check_tie():
- global game_is_not_over
- if "-" not in board:
- game_is_not_over = False
- return True
- else:
- return False
- def losuj():
- x = random.randint(0,9)
- return x
- def menu():
- print("Wybierz tryb gry: ")
- print("1. Gra dwuosobowa")
- print("2. Gra przeciwko komputerowi")
- wybor = input()
- wybor = int(wybor)
- decision = False
- while not decision:
- if wybor == 1:
- play()
- decision = True
- elif wybor == 2:
- play_ai()
- decision = True
- else:
- print("Dokonano niewlasciwego wyboru. Sprobuj ponownie")
- wybor = input()
- wybor = int(wybor)
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement