From 3a6b573d8f122263720eda128f5110c8575ddcd0 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Fri, 11 May 2018 15:37:12 -0400 Subject: [PATCH 1/3] resolved multiple compile issues. Project now compiles and runs with no issues. Not 100% sure if it still matches the original intent, but it should now be useable. --- Code-Sleep-Python/Classification/code.py | 88 ++++++++++-------------- 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/Code-Sleep-Python/Classification/code.py b/Code-Sleep-Python/Classification/code.py index 130cde5..db3151d 100644 --- a/Code-Sleep-Python/Classification/code.py +++ b/Code-Sleep-Python/Classification/code.py @@ -1,88 +1,76 @@ +import random + +import matplotlib.pyplot as plt +import numpy as np import pandas as pd -data = pd.read_csv('https://s3.amazonaws.com/demo-datasets/wine.csv') +import sklearn.decomposition +from matplotlib.colors import ListedColormap +# More accuracy +from sklearn.neighbors import KNeighborsClassifier +def accuracy(predictions, outcomes): + # Enter your code here! + occur = 0 + v = np.array(predictions) == np.array(outcomes) + occur = np.sum(v) + return occur -df2 = data.drop('color', axis=1) # color is redundant +data = pd.read_csv('https://s3.amazonaws.com/demo-datasets/wine.csv') -import numpy as np +df2 = data.drop('color', axis=1) # color is redundant +numeric_data = df2.values +numeric_data = (numeric_data - np.mean(numeric_data)) / (np.std(numeric_data)) -numeric_data = (numeric_data - np.mean(numeric_data))/(np.std(numeric_data)) -import sklearn.decomposition pca = sklearn.decomposition.PCA(n_components=2) principal_components = pca.fit(numeric_data).transform(numeric_data) - - - - -import matplotlib.pyplot as plt -from matplotlib.colors import ListedColormap -from matplotlib.backends.backend_pdf import PdfPages observation_colormap = ListedColormap(['red', 'blue']) -x = principal_components[:,0] -y = principal_components[:,1] +x = principal_components[:, 0] +y = principal_components[:, 1] plt.title("Principal Components of Wine") -plt.scatter(x, y, alpha = 0.2, - c = data['high_quality'], cmap = observation_colormap, edgecolors = 'none') -plt.xlim(-8, 8); plt.ylim(-8, 8) -plt.xlabel("Principal Component 1"); plt.ylabel("Principal Component 2") +plt.scatter(x, y, alpha=0.2, + c=data['high_quality'], cmap=observation_colormap, + edgecolors='none') +plt.xlim(-8, 8) +plt.ylim(-8, 8) +plt.xlabel("Principal Component 1") +plt.ylabel("Principal Component 2") plt.show() +x = np.array([1, 2, 3]) +y = np.array([1, 2, 4]) +print(accuracy(x, y)) +print(accuracy([], data["high_quality"])) - -def accuracy(predictions, outcomes): - # Enter your code here! - occur =0 - for i in range(len(predictions)): - if(predictions[i] == outcomes[i]): - occur += 1 - return occur - - -x = np.array([1,2,3]) -y = np.array([1,2,4]) - -print (accuracy(x,y)) - - -print(accuracy(0,data["high_quality"])) - - -############################## More accuracy -from sklearn.neighbors import KNeighborsClassifier -knn = KNeighborsClassifier(n_neighbors = 5) +knn = KNeighborsClassifier(n_neighbors=5) knn.fit(numeric_data, data['high_quality']) # Enter your code here! library_predictions = knn.predict(numeric_data) -print(accuracy(library_predictions,data['high_quality'])) +print(accuracy(library_predictions, data['high_quality'])) -################ n_rows = data.shape[0] +print() random.seed(123) selection = random.sample(range(n_rows), 10) - predictors = np.array(numeric_data) training_indices = [i for i in range(len(predictors)) if i not in selection] outcomes = np.array(data["high_quality"]) -my_predictions = [knn_predict(p, predictors[training_indices,:], outcomes, k=5) for p in predictors[selection]] -percentage = accuracy(my_predictions,data.high_quality[selection]) +my_predictions = [ + knn.predict(predictors[training_indices, :]) for p in + predictors[selection]] +percentage = accuracy(my_predictions, outcomes) print(percentage) - - - - - From 6c4c0d4a5083f2fe812c43dd0fbeab99df03e173 Mon Sep 17 00:00:00 2001 From: Molex Date: Fri, 11 May 2018 19:34:38 -0400 Subject: [PATCH 2/3] Fixed several import issues. Resolved a number of syntax errors and Pep8 errors. I also fixed the class initialization. I commented out some code for running the app. There was a mash-up of 2 implementations I chose one and went with it. --- .../Inception TicTacToe/inceptionTTT.py | 667 ++++++++++-------- 1 file changed, 366 insertions(+), 301 deletions(-) diff --git a/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py b/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py index 45b80ae..a4c4846 100644 --- a/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py +++ b/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py @@ -1,182 +1,4 @@ -from Tkinter import * -import tkMessageBox -import random - - -class pvp_app(Frame): - def __init__(self): - Frame.__init__(self) - - def PlayerGame(self): - self.pvp_button.grid_forget() - self.back_s_button.grid_forget() - self.startgame_label.grid_forget() - - self.start_label=Label(self,text="Who starts the game??") - self.start_label.grid(row=0,columnspan=11) - self.p1_button=Button(self,text="Player 1",fg="blue",command=lambda: self.startGame(TRUE,FALSE)) - self.p1_button.grid(row=0,column=0, columnspan=2) - self.p2_button=Button(self,text="Player 2",fg="blue",command=lambda: self.startGame(FALSE,TRUE)) - self.p2_button.grid(row=0,column=8, columnspan=2) - self.back_p_button=Button(self,text="Back",fg="blue",bg="green",command=self.back_r_button,height=1,width=2) - self.back_p_button.grid(row=12,column=5) - - self.button_lst=[[0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9], - [0,1,2,3,4,5,6,7,8,9]] - - for i in range(1,10): - for j in range(1,10): - self.button_lst[i][j]=Button(self,text=" ",fg="yellow",bg="red",state=DISABLED,command=lambda x=i,y=j: self.handle_button(x,y), height=1,width=2,font=("Purisa", 18)) - - L = [[[1,2,0], [2,2,1], [3,2,2], - [4,3,0], [5,3,1], [6,3,2], - [7,4,0], [8,4,1], [9,4,2]], - - [[1,2,4], [2,2,5], [3,2,6], - [4,3,4], [5,3,5], [6,3,6], - [7,4,4], [8,4,5], [9,4,6]], - - [[1,2,7], [2,2,8], [3,2,9], - [4,3,7], [5,3,8], [6,3,9], - [7,4,7], [8,4,8], [9,4,9]], - - [[1,5,0], [2,5,1], [3,5,2], - [4,6,0], [5,6,1], [6,6,2], - [7,7,0], [8,7,1], [9,7,2]], - - [[1,5,4], [2,5,5], [3,5,6], - [4,6,4], [5,6,5], [6,6,6], - [7,7,4], [8,7,5], [9,7,6]], - - [[1,5,7], [2,5,8], [3,5,9], - [4,6,7], [5,6,8], [6,6,9], - [7,7,7], [8,7,8], [9,7,9]], - - [[1,8,0], [2,8,1], [3,8,2], - [4,9,0], [5,9,1], [6,9,2], - [7,10,0], [8,10,1], [9,10,2]], - - [[1,8,4], [2,8,5], [3,8,6], - [4,9,4], [5,9,5], [6,9,6], - [7,10,4], [8,10,5], [9,10,6]], - - [[1,8,7], [2,8,8], [3,8,9], - [4,9,7], [5,9,8], [6,9,9], - [7,10,7], [8,10,8], [9,10,9]]] - - count=1 - for j in L: - for i in j: - self.button_lst[count][i[0]].grid(row=i[1], column=i[2]) - count+=1 - - def startGame(self,p1,p2): - self.p1_button.config(state=DISABLED) - self.p2_button.config(state=DISABLED) - self.back_p_button.config(state=DISABLED) - for i in range(1,10): - for j in range(1,10): - self.button_lst[i][j].config(state=NORMAL) - - if(p1==TRUE): - self.start_label.config(text="Player 1 starts with X") - self.turn=TRUE - - else: - self.start_label.config(text="Player 2 starts with O") - self.turn=FALSE - - self.bState=[[10,10,10,10,10,10,10,10,10,10], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0], - [10,0,0,0,0,0,0,0,0,0]] - - self.bSum= [[0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0],] - - self.fState=[10,0,0,0,0,0,0,0,0,0] - - self.bWin=[10] - - self.GameDone=FALSE - - def handle_button(self,i,j): - if(self.turn==TRUE): - self.button_lst[i][j].config(text="X",state=DISABLED) - self.turn=FALSE - self.bState[i][j]=1 - if i in self.bWin: - for a in range(1,10): - for b in range(1,10): - self.button_lst[a][b].config(state=DISABLED,bg="red") - for a in range(1,10): - self.button_lst[j][a].config(state=NORMAL,bg="blue") - else: - for a in self.bWin: - for b in range(1,10): - self.button_lst[a][b].config(state=DISABLED,bg="red") - for a in range(1,10): - self.button_lst[j][a].config(state=NORMAL,bg="blue") - - - else: - self.button_lst[i][j].config(text="O",state=DISABLED) - self.turn=TRUE - self.bState[i][j]=-1 - for a in range(1,10): - for b in range(1,10): - self.button_lst[a][b].config(state=DISABLED,bg="red") - for a in range(1,10): - self.button_lst[j][a].config(state=NORMAL,bg="blue") - - self.check() - - def check(self): - - self.sum() - - count=0 - for i in self.bSum: - count+=1 - for j in i: - if(j==3): - self.fState[count]=1 - self.bWin.append(count) - elif(j==-3): - self.fState[count]=-1 - self.bWin.append(count) - else: - check=0 - for i in self.bState: - for j in i: - if j!=0 and j!=10: - check=1 - if check==0: - for a in range(1,10): - for b in range(1,10): - self.button_lst[a][b].config(state=DISABLED) - break +import tkinter ''' #list of possible win combinations in the form of button numbers WinCombo=[[1,2,3], [4,5,6], [7,8,9], #rows @@ -185,140 +7,383 @@ def check(self): #after doing sums look for 3 or -3 to find if there was a winner for i in WinCombo: - if((self.bState[i[0]]+self.bState[i[1]]+self.bState[i[2]])==3): #3 means player1 has won - self.start_label.config(text="Player 1 won!!") #label if player 1 is won + if((self.bState[i[0]]+self.bState[i[1]]+self.bState[i[2]])==3): + #3 means player1 has won + self.start_label.config(text="Player 1 won!!") + #label if player 1 is won self.GameDone=TRUE for k in range(1,10): self.button_lst[k].configure(state=DISABLED) break - elif((self.bState[i[0]]+self.bState[i[1]]+self.bState[i[2]])==-3): #-3 means player2 has won - self.start_label.config(text="Player 2 won!!") #label if player 2 is won + elif((self.bState[i[0]]+self.bState[i[1]]+self.bState[i[2]])==-3): + #-3 means player2 has won + self.start_label.config(text="Player 2 won!!") + #label if player 2 is won self.GameDone=TRUE for k in range(1,10): self.button_lst[k].configure(state=DISABLED) break else: - - if not (0 in self.bState): #if there is no 0 in button state , the game is done - self.start_label.config(text="Match drawn!!") #label if match is drawn + + if not (0 in self.bState): + # if there is no 0 in button state , the game is done + self.start_label.config(text="Match drawn!!") + # label if match is drawn self.GameDone=TRUE for k in range(1,10): self.button_lst[k].configure(state=DISABLED) break ''' - if(self.GameDone==TRUE): - self.message=tkMessageBox.askquestion(" ","Do you want to play the game again?") - if(self.message=="no"): - self.QuitGame() - else: - self.p1_button.config(state=NORMAL) - self.p2_button.config(state=NORMAL) - self.back_p_button.config(state=NORMAL) - self.start_label.config(text="Who will start the game?") - for a in range(1,10): - for b in range(1,10): - self.button_lst[a][b].config(state=NORMAL) - - def sum(self): - WinCombo=[[[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]], - [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]]] - - count1=0 - for i in WinCombo: - count=0 - for k in i: - self.bSum[count1][count]=0 - for j in k: - self.bSum[count1][count]+=self.bState[count1+1][j] - count+=1 - count1+=1 - - def back_r_button(self): - self.p1_button.grid_forget() - self.p2_button.grid_forget() - self.back_p_button.grid_forget() - self.start_label.grid_forget() - for a in range(1,10): - for b in range(1,10): - self.button_lst[a][b].grid_forget() - self.StartGame() - -#------------------------------------------------------------------------------------------------------------------------ - - -class MainGUI(Frame,pvp_app): - def __init__(self,master): - Frame.__init__(self,master) - self.grid() - self.configure(background="green") - self.ShowHomepage() - - - def ShowHomepage(self): - self.homepage_label = Label(self,text="WELCOME TO THE WORLD OF TIC-TAC-TOE",font=("courier",15),fg="red",bg="yellow") - self.homepage_label.grid(row=0 , column=1 , padx=20 , pady=25 ) - - self.start_button = Button(self,text="Start Game",command=self.StartGame,fg="blue") - self.start_button.grid(row=1,column=1,padx=100,pady=50) - - self.rules_button = Button(self,text="Rules",command=self.ShowRule,fg="red") - self.rules_button.grid(row=2,column=1,padx=100,pady=50) - - self.quit_button = Button(self,text="Quit",command=self.QuitGame,fg="purple") - self.quit_button.grid(row=3,column=1,padx=100,pady=50) - - def StartGame(self): - self.rules_button.grid_forget() - self.quit_button.grid_forget() - self.homepage_label.grid_forget() - self.start_button.grid_forget() - self.startgame_label = Label(self,text="Whom do you want to play against?",font=("courier",15),fg="red",bg="yellow") - self.startgame_label.grid(row=0 , column=1 , padx=20 , pady=25 ) - self.pvp_button = Button(self,text="2 Player Mode",command=lambda: pvp_app.PlayerGame(self)) - self.pvp_button.grid(row=2 , column=1 ,padx=100 , pady=50) - - self.back_s_button = Button(self,text="Back",command=self.back_startGame) - self.back_s_button.grid(row=4,column=1,padx=100,pady=50) - - - def ShowRule(self): - self.homepage_label.grid_forget() - self.start_button.grid_forget() - self.rules_button.grid_forget() - self.quit_button.grid_forget() - self.rules_label = Label(self,text="Game for two players,X and O,\nwho take turns marking the spaces\nin a 3x3 grid.The player who\nsucceeds in placing three of their marks\nin a horizontal,vertical\nor diagonal row wins the game.",font=("pursia",15),fg="red",bg="green") - self.rules_label.grid(row=0 , column=1 , padx=45, pady=35 ) - self.back_r_button = Button(self,text="Back",command=self.back_rules) - self.back_r_button.grid(row=2,column=1,padx=100,pady=50) - - def QuitGame(self): - self.message=tkMessageBox.askquestion("Exit","Do you want to exit ?") - if (self.message == 'yes'): - root.destroy() - - def back_rules(self): - self.rules_label.grid_forget() - self.back_r_button.grid_forget() - self.ShowHomepage() - - def back_startGame(self): - self.pvp_button.grid_forget() - self.back_s_button.grid_forget() - self.startgame_label.grid_forget() - self.ShowHomepage() - - -root=Tk() -root.title("Tic Tac Toe") -root.geometry("500x530") -root.config(bg="green") -appl=MainGUI(root) -root.mainloop() + +class pvp_app(): + def __init__(self): + tkinter.Frame.__init__(self) + + def PlayerGame(self): + self.pvp_button.grid_forget() + self.back_s_button.grid_forget() + self.startgame_label.grid_forget() + + self.start_label = tkinter.Label(self, text="Who starts the game??") + self.start_label.grid(row=0, columnspan=11) + self.p1_button = tkinter.Button(self, text="Player 1", fg="blue", + command=lambda: self.startGame( + tkinter.TRUE, tkinter.FALSE)) + self.p1_button.grid(row=0, column=0, columnspan=2) + self.p2_button = tkinter.Button(self, text="Player 2", fg="blue", + command=lambda: self.startGame( + tkinter.FALSE, tkinter.TRUE)) + self.p2_button.grid(row=0, column=8, columnspan=2) + self.back_p_button = tkinter.Button(self, text="Back", fg="blue", + bg="green", + command=self.back_r_button, + height=1, + width=2) + self.back_p_button.grid(row=12, column=5) + + self.button_lst = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] + + for i in range(1, 10): + for j in range(1, 10): + self.button_lst[i][j] = tkinter.Button(self, text=" ", + fg="yellow", + bg="red", + state=tkinter.DISABLED, + command=lambda x=i, + y=j: self.handle_button( + x, y), height=1, + width=2, + font=("Purisa", 18)) + + L = [[[1, 2, 0], [2, 2, 1], [3, 2, 2], + [4, 3, 0], [5, 3, 1], [6, 3, 2], + [7, 4, 0], [8, 4, 1], [9, 4, 2]], + + [[1, 2, 4], [2, 2, 5], [3, 2, 6], + [4, 3, 4], [5, 3, 5], [6, 3, 6], + [7, 4, 4], [8, 4, 5], [9, 4, 6]], + + [[1, 2, 7], [2, 2, 8], [3, 2, 9], + [4, 3, 7], [5, 3, 8], [6, 3, 9], + [7, 4, 7], [8, 4, 8], [9, 4, 9]], + + [[1, 5, 0], [2, 5, 1], [3, 5, 2], + [4, 6, 0], [5, 6, 1], [6, 6, 2], + [7, 7, 0], [8, 7, 1], [9, 7, 2]], + + [[1, 5, 4], [2, 5, 5], [3, 5, 6], + [4, 6, 4], [5, 6, 5], [6, 6, 6], + [7, 7, 4], [8, 7, 5], [9, 7, 6]], + + [[1, 5, 7], [2, 5, 8], [3, 5, 9], + [4, 6, 7], [5, 6, 8], [6, 6, 9], + [7, 7, 7], [8, 7, 8], [9, 7, 9]], + + [[1, 8, 0], [2, 8, 1], [3, 8, 2], + [4, 9, 0], [5, 9, 1], [6, 9, 2], + [7, 10, 0], [8, 10, 1], [9, 10, 2]], + + [[1, 8, 4], [2, 8, 5], [3, 8, 6], + [4, 9, 4], [5, 9, 5], [6, 9, 6], + [7, 10, 4], [8, 10, 5], [9, 10, 6]], + + [[1, 8, 7], [2, 8, 8], [3, 8, 9], + [4, 9, 7], [5, 9, 8], [6, 9, 9], + [7, 10, 7], [8, 10, 8], [9, 10, 9]]] + + count = 1 + for j in L: + for i in j: + self.button_lst[count][i[0]].grid(row=i[1], column=i[2]) + count += 1 + + def startGame(self, p1, p2): + self.p1_button.config(state=tkinter.DISABLED) + self.p2_button.config(state=tkinter.DISABLED) + self.back_p_button.config(state=tkinter.DISABLED) + for i in range(1, 10): + for j in range(1, 10): + self.button_lst[i][j].config(state=tkinter.NORMAL) + + if (p1 == tkinter.TRUE): + self.start_label.config(text="Player 1 starts with X") + self.turn = tkinter.TRUE + + else: + self.start_label.config(text="Player 2 starts with O") + self.turn = tkinter.FALSE + + self.bState = [[10, 10, 10, 10, 10, 10, 10, 10, 10, 10], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [10, 0, 0, 0, 0, 0, 0, 0, 0, 0]] + + self.bSum = [[0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], ] + + self.fState = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0] + + self.bWin = [10] + + self.GameDone = tkinter.FALSE + + def handle_button(self, i, j): + if (self.turn == tkinter.TRUE): + self.button_lst[i][j].config(text="X", state=tkinter.DISABLED) + self.turn = tkinter.FALSE + self.bState[i][j] = 1 + if i in self.bWin: + for a in range(1, 10): + for b in range(1, 10): + self.button_lst[a][b].config(state=tkinter.DISABLED, + bg="red") + for a in range(1, 10): + self.button_lst[j][a].config(state=tkinter.NORMAL, + bg="blue") + else: + for a in self.bWin: + for b in range(1, 10): + self.button_lst[a][b].config(state=tkinter.DISABLED, + bg="red") + for a in range(1, 10): + self.button_lst[j][a].config(state=tkinter.NORMAL, + bg="blue") + + + else: + self.button_lst[i][j].config(text="O", state=tkinter.DISABLED) + self.turn = tkinter.TRUE + self.bState[i][j] = -1 + for a in range(1, 10): + for b in range(1, 10): + self.button_lst[a][b].config(state=tkinter.DISABLED, + bg="red") + for a in range(1, 10): + self.button_lst[j][a].config(state=tkinter.NORMAL, bg="blue") + + self.check() + + def check(self): + + self.sum() + + count = 0 + for i in self.bSum: + count += 1 + for j in i: + if (j == 3): + self.fState[count] = 1 + self.bWin.append(count) + elif (j == -3): + self.fState[count] = -1 + self.bWin.append(count) + else: + check = 0 + for i in self.bState: + for j in i: + if j != 0 and j != 10: + check = 1 + if check == 0: + for a in range(1, 10): + for b in range(1, 10): + self.button_lst[a][b].config( + state=tkinter.DISABLED) + break + if self.GameDone == tkinter.TRUE: + self.message = tkinter.messagebox.askquestion(" ", + "Do you want to play the " + "game again?") + if self.message == "no": + self.QuitGame() + else: + self.p1_button.config(state=tkinter.NORMAL) + self.p2_button.config(state=tkinter.NORMAL) + self.back_p_button.config(state=tkinter.NORMAL) + self.start_label.config(text="Who will start the game?") + for a in range(1, 10): + for b in range(1, 10): + self.button_lst[a][b].config(state=tkinter.NORMAL) + + def sum(self): + WinCombo = [ + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]], + [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], + [1, 5, 9], [3, 5, 7]]] + + count1 = 0 + for i in WinCombo: + count = 0 + for k in i: + self.bSum[count1][count] = 0 + for j in k: + self.bSum[count1][count] += self.bState[count1 + 1][j] + count += 1 + count1 += 1 + + def back_r_button(self): + self.p1_button.grid_forget() + self.p2_button.grid_forget() + self.back_p_button.grid_forget() + self.start_label.grid_forget() + for a in range(1, 10): + for b in range(1, 10): + self.button_lst[a][b].grid_forget() + self.StartGame() + + # ------------------------------------------------------------------------ + + +class MainGUI(tkinter.Frame, pvp_app): + def __init__(self, master=None): + tkinter.Frame.__init__(self, master) + self.grid() + self.configure(background="green") + self.master.title("Tic Tac Toe") + self.master.geometry("500x530") + self.ShowHomepage() + self.label = tkinter.Label(self) + self.label.grid(row=0, column=0) + + def ShowHomepage(self): + self.homepage_label = tkinter.Label(self, + text="WELCOME TO THE WORLD OF TIC-TAC-TOE", + font=("courier", 15), fg="red", + bg="yellow") + self.homepage_label.grid(row=0, column=1, padx=20, pady=25) + + self.start_button = tkinter.Button(self, text="Start Game", + command=self.StartGame, fg="blue") + self.start_button.grid(row=1, column=1, padx=100, pady=50) + + self.rules_button = tkinter.Button(self, text="Rules", + command=self.ShowRule, + fg="red") + self.rules_button.grid(row=2, column=1, padx=100, pady=50) + + self.quit_button = tkinter.Button(self, text="Quit", + command=self.QuitGame, + fg="purple") + self.quit_button.grid(row=3, column=1, padx=100, pady=50) + + def StartGame(self): + self.rules_button.grid_forget() + self.quit_button.grid_forget() + self.homepage_label.grid_forget() + self.start_button.grid_forget() + self.startgame_label = tkinter.Label(self, + text="Whom do you want to play against?", + font=("courier", 15), fg="red", + bg="yellow") + self.startgame_label.grid(row=0, column=1, padx=20, pady=25) + self.pvp_button = tkinter.Button(self, text="2 Player Mode", + command=lambda: pvp_app.PlayerGame( + self)) + self.pvp_button.grid(row=2, column=1, padx=100, pady=50) + + self.back_s_button = tkinter.Button(self, text="Back", + command=self.back_startGame) + self.back_s_button.grid(row=4, column=1, padx=100, pady=50) + + def ShowRule(self): + self.homepage_label.grid_forget() + self.start_button.grid_forget() + self.rules_button.grid_forget() + self.quit_button.grid_forget() + self.rules_label = tkinter.Label(self, + text="Game for two players,X and O,\nwho " + "take turns marking the spaces\nin a " + "3x3 grid.The player who\nsucceeds in " + "placing three of their marks\nin a " + "horizontal,vertical\nor diagonal row " + "wins the game.", + font=("pursia", 15), fg="red", + bg="green") + self.rules_label.grid(row=0, column=1, padx=45, pady=35) + self.back_r_button = tkinter.Button(self, text="Back", + command=self.back_rules) + self.back_r_button.grid(row=2, column=1, padx=100, pady=50) + + def QuitGame(self): + self.message = tkinter.messagebox.askquestion("Exit", + "Do you want to exit ?") + if self.message == 'yes': + appl.destroy() + + def back_rules(self): + self.rules_label.grid_forget() + self.back_r_button.grid_forget() + self.ShowHomepage() + + def back_startGame(self): + self.pvp_button.grid_forget() + self.back_s_button.grid_forget() + self.startgame_label.grid_forget() + self.ShowHomepage() + + +# Below lines are no longer necessary +# root = Tk() +# root.title("Tic Tac Toe") +# root.geometry("500x530") +# root.config(bg="green") +# leaving in case it's changed to this implementation type +appl = MainGUI() +appl.mainloop() From 73c9ba9a6cf975877f85ec102c97b00f7c840e47 Mon Sep 17 00:00:00 2001 From: Molex Date: Fri, 11 May 2018 20:02:33 -0400 Subject: [PATCH 3/3] Additional Pep 8 changes --- .../Inception TicTacToe/inceptionTTT.py | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py b/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py index a4c4846..2985b47 100644 --- a/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py +++ b/Code-Sleep-Python/Inception TicTacToe/inceptionTTT.py @@ -1,6 +1,13 @@ import tkinter -''' #list of possible win combinations in the form of button numbers + +class pvp_app(): + + def __init__(self): + tkinter.Frame.__init__(self) + + ''' + #list of possible win combinations in the form of button numbers WinCombo=[[1,2,3], [4,5,6], [7,8,9], #rows [1,4,7], [2,5,8], [3,6,9], #columns [1,5,9], [3,5,7]] #diagonals @@ -33,12 +40,7 @@ for k in range(1,10): self.button_lst[k].configure(state=DISABLED) break -''' - - -class pvp_app(): - def __init__(self): - tkinter.Frame.__init__(self) + ''' def PlayerGame(self): self.pvp_button.grid_forget() @@ -80,7 +82,8 @@ def PlayerGame(self): bg="red", state=tkinter.DISABLED, command=lambda x=i, - y=j: self.handle_button( + y=j: + self.handle_button( x, y), height=1, width=2, font=("Purisa", 18)) @@ -234,7 +237,8 @@ def check(self): break if self.GameDone == tkinter.TRUE: self.message = tkinter.messagebox.askquestion(" ", - "Do you want to play the " + "Do you want to " + "play the " "game again?") if self.message == "no": self.QuitGame() @@ -304,7 +308,8 @@ def __init__(self, master=None): def ShowHomepage(self): self.homepage_label = tkinter.Label(self, - text="WELCOME TO THE WORLD OF TIC-TAC-TOE", + text="WELCOME TO THE WORLD OF " + "TIC-TAC-TOE", font=("courier", 15), fg="red", bg="yellow") self.homepage_label.grid(row=0, column=1, padx=20, pady=25)