# Κέντρο Ανοικτών Διαδικτυακών Μαθημάτων Mathesis των Πανεπιστημιακών Εκδόσεων Κρήτης (ΠΕΚ) # http://mathesis.cup.gr # # Εισαγωγή στην Python Διδάσκων Καθ. Νίκος Αβούρης # http://mathesis.cup.gr/courses/course-v1:ComputerScience+CS1.1+2017_T2/about # Εβδομάδα 4η - Μάθημα 16ο - Τρίλιζα (Εργασία) # https://www.youtube.com/watch?v=urD5Mb9wppA # Λύση της εργασίας «Τρίλιζα» # Copyright (C) 2017 Χρήστος Ιωσηφίδης chiossif@yahoo.com # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . import random import time marker = {'Παίκτης 1': 'X', 'Παίκτης 2': 'O', } def display_board(board): # 4 μονάδες #εμφάνισε την κατάσταση της τρίλιζα print("+------+------+------+") print("|7 |8 |9 |") print("| ",board[7]," | ",board[8]," | ",board[9]," |") print("| | | |") print("+------+------+------+") print("|4 |5 |6 |") print("| ",board[4]," | ",board[5]," | ",board[6]," |") print("| | | |") print("+------+------+------+") print("|1 |2 |3 |") print("| ",board[1]," | ",board[2]," | ",board[3]," |") print("| | | |") print("+------+------+------+") def choose_first(): # 2 μονάδες #κλήρωση για το ποιος θα παίξει πρώτος # επιστρέφει είτε 'Παίκτης 1' είτε 'Παίκτης 2' if random.randint(1,2) == 1: return 'Παίκτης 1' else: return 'Παίκτης 2' def display_score(score): # 2 μονάδες #Τυπώνει το τελικό σκορ print("ΤΕΛΙΚΟ ΣΚΟΡ") for i in score: print(i,":",score[i]) def place_marker(board, marker, position): # 2 μονάδες #Τοποθετεί στη θέση position του board τον marker board[position]=marker def win_check(board,mark): # 4 μονάδες * #επιστρέφει True αν το σύμβολο mark έχει σχηματίσει τρίλιζα winning=[[7,8,9],[4,5,6],[1,2,3],[7,4,1],[8,5,2],[9,6,3],[7,5,3],[9,5,1]] for i in winning: if mark==board[i[0]] and mark==board[i[1]] and mark==board[i[2]]: return True return False def board_check(board): # 2 μονάδες #επιστρέφει False αν υπάρχουν ακόμη κενά τετράγωνα if ' ' in board[1:]: return False return True def player_choice(board, turn): # 2 μονάδες * # Ο Παίκτης turn επιλέγει τετράγωνο # Επιστρέφει έναν ακέραιο στο διάστημα [1,9] print(turn,"[",marker[turn],"]",end=' ') while True: x=input("Διάλεξε τετράγωνο (1-9) ") if x.isdigit(): x=int(x) if x>0 and x<10 and board[x]==' ': return x def replay(): # 1 μονάδα # Ρωτάει τον χρήστη αν θέλει να ξαναπαίξει και επιστρέφει True αν ναι. nai=["Nai","nai","NAI","ΝΑΙ","Ναι","ναι","Ν","ν","N","n"] oxi=["Oxi","oxi","OXI","ΌΧΙ","Όχι","όχι","ΟΧΙ","Οχι","οχι","O","o","Ο","ο","Ό","ό"] while True: x=input("Θέλεις να παίξεις ξανά (Ναι/Όχι): ") if x in nai: return True if x in oxi: return False def next_player(turn): # 1 μονάδα #επιστρέφει τον επόμενο παίκτη που πρέπει να παίξει if turn == 'Παίκτης 2': return 'Παίκτης 1' else: return 'Παίκτης 2' def main(): score = {} # λεξικό με το σκορ των παικτών print('Αρχίζουμε!\nΓίνεται κλήρωση ', end = '') for t in range(10): print(".", flush='True', end=' ') time.sleep(0.2) print() # η μεταβλητή turn αναφέρεται στον παίκτη που παίζει turn = choose_first() print("\nΟ " + turn + ' παίζει πρώτος.') # η μεταβλητή first αναφέρεται στον παίκτη που έπαιξε πρώτος first = turn game_round = 1 # γύρος παιχνιδιού while True: # Καινούργιο παιχνίδι # Δημιουργία λίστας 10 στοιχείων βλέπε μάθημα 2 σελ.7 σημειώσεων theBoard = [' '] * 10 # Αφήστε το πρώτο στοιχείο δηλαδή το theBoard[0] κενό έτσι ώστε # το index να αντιστοιχεί στην ονοματοδότηση των τετραγώνων game_on = True #ξεκινάει το παιχνίδι while game_on: display_board(theBoard) #Εμφάνισε την τρίλιζα # ο παίκτης turn επιλέγει θέση position = player_choice(theBoard, turn) # τοποθετείται η επιλογή του place_marker(theBoard, marker[turn], position) if win_check(theBoard, marker[turn]): # έλεγχος αν νίκησε display_board(theBoard) print('Νίκησε ο '+ turn) score[turn] = score.get(turn, 0) + 1 game_on = False # έλεγχος αν γέμισε το ταμπλό χωρίς νικητή elif board_check(theBoard): display_board(theBoard) print('Ισοπαλία!') game_on = False else: # αλλιώς συνεχίζουμε με την κίνηση του επόμενου παίκτη turn = next_player(turn) if not replay(): ending = '' if game_round>1 : ending = 'υς' print("Μετά {} γύρο{}".format(game_round, ending)) display_score(score) # έξοδος ... τελικό σκορ break else : game_round += 1 # στο επόμενο παιχνίδι ξεκινάει ο άλλος παίκτης turn = next_player(first) first = turn main()