def gameOver(self, event): over = "None" full = True row = getRow(event.GetEventObject().GetId()) col = getCol(event.GetEventObject().GetId()) # Check vertical and horizontal lines if self.game.Board[row][0].GetName() == self.game.Board[row][1].GetName() == self.game.Board[row][2].GetName() \ or self.game.Board[0][col].GetName() == self.game.Board[1][col].GetName() \ == self.game.Board[2][col].GetName(): over = self.game.User + " Wins!" # Check diagonal lines if (abs(row - col) != 1) and (self.game.Board[0][0].GetName() == self.game.Board[1][1].GetName() == self.game.Board[2][2].GetName() != "button" or self.game.Board[0][2].GetName() == self.game.Board[1][1].GetName() == self.game.Board[2][0].GetName() != "button"): over = self.game.User + " Wins!" # Check if there are any empty spots on the board for i in range(3): for j in range(3): if self.game.Board[i][j].GetName() == "button": full = False if over == "None" and full: over = "Tie Game!" switchUser(self) return over
def pressPlay(self, event): # Adds current score to total score self.PlayerScore.SetLabel( str( int(self.PlayerScore.GetLabel()) + int(self.Points.GetLabel()))) self.Points.SetLabel('0') self.click = -1 for tiles in self.new_tiles: tile = self.game.Board[getRow(tiles)][getCol(tiles)] tile.SetBackgroundColour("light blue") PlacePiece(tile.GetName(), tile, self.size) tile.Disable() self.new_tiles = set() # Replaces empty spaces on the rack with new tiles from the bag for children in self.rack.GetChildren(): if children.GetWindow().GetName() == "button" and len( self.tiles) > 0: takeTileFromBag(self, children.GetWindow()) if len(self.tiles) < 7: self.Trade.Disable() # Disable Play button again after word has been played self.Play.Disable() self.Recall.Disable()
def pressRecall(self, event): self.click = -1 if self.trade: for i in self.trade_tiles: i.SetBitmap( wx.Image("assets/IMAGES/" + i.GetName() + ".png", wx.BITMAP_TYPE_ANY).Scale( self.size, self.size).ConvertToBitmap()) for children in self.game.grid.GetChildren(): if children.GetWindow().GetName() == "button": children.GetWindow().Enable() self.Shuffle.Enable() self.Trade.Enable() self.Recall.SetLabel('Recall') self.trade = False else: while len(self.new_tiles): # Removes tile from the board tileRemoved = self.new_tiles.pop() old_row = getRow(tileRemoved) old_col = getCol(tileRemoved) board_space = self.game.Board[old_row][old_col] multiplier = self.multipliers[old_row][old_col] # Iterates through rack until an empty space is found i = 0 while self.rack_arr[i].GetName() != "button": i += 1 # Places tile from the board to the rack name = board_space.GetName() if name[:5] == "BLANK": name = "BLANK" PlacePiece(name, self.rack_arr[i], self.size) PlacePiece("button", board_space, self.size) board_space.SetLabel(multiplier) self.click = -1 self.Recall.Disable()
def space(self, event): rowID = getRow(event.GetEventObject().GetId()) colID = getCol(event.GetEventObject().GetId()) # First click establishes which piece the user wants to move if self.click == 0: if event.GetEventObject().GetName()[:5] == self.game.User: self.click = event.GetEventObject().GetId() showMoves(self.allMoves[rowID][colID], self.game.Board) # Second click moves the piece to the clicked spot # A new piece can be chosen to be moved by clicking on it else: self.moves += 0.5 row_click = getRow(self.click) col_click = getCol(self.click) checkered(self.game, self.allMoves[row_click][col_click]) if (rowID, colID) in self.allMoves[row_click][col_click]: pawnGraduate = False # Set castle boolean to False if one of the pieces are moved if self.game.Board[row_click][col_click].GetName()[6:] == "ROOK": if col_click == 7: if self.king[0] == 7: if self.king[1] == 4: self.UserK_castle = False else: self.UserQ_castle = False else: if self.king[1] == 4: self.OppK_castle = False else: self.OppQ_castle = False else: if self.king[0] == 7: if self.king[1] == 4: self.UserQ_castle = False else: self.UserK_castle = False else: if self.king[1] == 4: self.OppQ_castle = False else: self.OppK_castle = False # Move the rook if the king castles elif self.game.Board[row_click][col_click].GetName()[6:] == "KING": if row_click == 7: self.UserK_castle = False self.UserQ_castle = False else: self.OppK_castle = False self.OppQ_castle = False if abs(self.king[1] - colID) == 2: if colID == 6: PlacePiece(self.game.Board[rowID][7].GetName(), self.game.Board[rowID][5], self.game.size) PlacePiece("button", self.game.Board[rowID][7], self.game.size) elif colID == 5: PlacePiece(self.game.Board[rowID][7].GetName(), self.game.Board[rowID][4], self.game.size) PlacePiece("button", self.game.Board[rowID][7], self.game.size) elif colID == 2: PlacePiece(self.game.Board[rowID][0].GetName(), self.game.Board[rowID][3], self.game.size) PlacePiece("button", self.game.Board[rowID][0], self.game.size) elif colID == 1: PlacePiece(self.game.Board[rowID][0].GetName(), self.game.Board[rowID][2], self.game.size) PlacePiece("button", self.game.Board[rowID][0], self.game.size) # Check for en poisson elif self.game.Board[row_click][col_click].GetName()[6:] == "PAWN": self.moves = 0 if col_click != colID and event.GetEventObject().GetName() == "button": self.pieces.remove(self.game.Board[self.poisson[0]][self.poisson[1]].GetName()) PlacePiece("button", self.game.Board[self.poisson[0]][self.poisson[1]], self.game.size) elif abs(row_click - rowID) == 2: self.poisson = (rowID, colID) else: self.poisson = () if rowID == 0 or rowID == 7: pawnGraduate = True # Remove taken piece from list of pieces if event.GetEventObject().GetName() != "button": self.pieces.remove(event.GetEventObject().GetName()) self.moves = 0 # Place piece in new space PlacePiece(self.game.Board[row_click][col_click].GetName(), event.GetEventObject(), self.game.size) # Remove piece from old space PlacePiece("button", self.game.Board[row_click][col_click], self.game.size) # Open a dialog to choose a piece when the pawn graduates if pawnGraduate: dlg = wx.SingleChoiceDialog(self, "", "Choose a piece.", ["QUEEN", "ROOK", "BISHOP", "KNIGHT"], wx.OK) if dlg.ShowModal() == wx.ID_OK: PlacePiece(self.game.User + "_" + dlg.GetStringSelection(), event.GetEventObject(), self.game.size) self.pieces.append(self.game.User + "_" + dlg.GetStringSelection()) self.pieces.remove(self.game.User + "_PAWN") dlg.Destroy() self.click = 0 userMoved(self, gameOver(self)) else: if event.GetEventObject().GetName()[:5] == self.game.User: checkered(self.game, self.allMoves[row_click][col_click]) showMoves(self.allMoves[rowID][colID], self.game.Board) self.click = event.GetEventObject().GetId() else: self.click = 0
def moveTile(self, event): # Condition if the user selected a piece from the rack if len(str(self.click)) == 1: previous_click = self.rack_arr[self.click] multiplier = "" # Condition if the user selected a piece from the board else: old_row = getRow(self.click) old_col = getCol(self.click) previous_click = self.game.Board[old_row][old_col] multiplier = self.multipliers[old_row][old_col] self.new_tiles.remove(self.click) event.GetEventObject().SetName(previous_click.GetName()) # Open a dialog for the user to choose what they want to use the blank piece as if event.GetEventObject().GetName()[:5] == "BLANK": # Condition if the blank tile is selected from the rack if len(str(self.click)) == 1: # Only open dialog if it moves from the rack to the board if len(str(event.GetEventObject().GetId())) > 1: dlg = wx.TextEntryDialog(None, "Choose a letter") dlg.ShowModal() event.GetEventObject().SetName("BLANK_" + dlg.GetValue()) dlg.Destroy() elif len(str(event.GetEventObject().GetId())) == 1: # Set the tile back to blank when it is returned to the rack event.GetEventObject().SetName("BLANK") # Condition that a space on the board was clicked if len(str(event.GetEventObject().GetId())) > 1: self.new_tiles.add(event.GetEventObject().GetId()) # Condition that a space on the rack was clicked else: PlacePiece(event.GetEventObject().GetName(), event.GetEventObject(), self.size) self.click = -1 # Remove tile from space that was clicked previously event.GetEventObject().SetLabel("") PlacePiece("button", previous_click, self.size) previous_click.SetLabel(multiplier) # Creates a temporary list to modify temp_list = list(self.new_tiles) words = [] word_multipliers = [] # Parses through placed tiles if there is at least one placed tile if len(self.new_tiles): # Only enable recall button when there are new tiles on the board self.Recall.Enable() tile = temp_list[0] row = getRow(tile) col = getCol(tile) # Sets direction of move if there are at least 2 new tiles direction = "HORIZONTAL" if len(temp_list) > 1 and getRow(temp_list[1]) != row: direction = "VERTICAL" touchCheck = False # Creates word by concatenating all connected tiles h_word = "" h_word_m = "" for i in range(2): while -1 < col < 15 and -1 < row < 15 and self.game.Board[row][ col].GetName() != "button": # Starts from first tile and parses to the end, then goes back and parses to the start letter = self.game.Board[row][col] # Store multipliers in a string in the word_multiplier list if i: # Make the letter lowercase to represent a blank tile used if len(letter.GetName()) == 1: h_word = letter.GetName()[-1] + h_word else: h_word = letter.GetName()[-1].lower() + h_word if len(self.multipliers[row][col]) and letter.IsEnabled(): h_word_m = self.multipliers[row][col] + h_word_m else: h_word_m = "--" + h_word_m else: if len(letter.GetName()) == 1: h_word += letter.GetName()[-1] else: h_word += letter.GetName()[-1].lower() if len(self.multipliers[row][col]) and letter.IsEnabled(): h_word_m += self.multipliers[row][col] else: h_word_m += "--" # Checks if at least one placed tile is touching a previously placed tile if self.game.Board[row][col].IsEnabled(): # Only check if this test has not already passed if not touchCheck: if row < 14 and not self.game.Board[ row + 1][col].IsEnabled(): touchCheck = True elif row > 0 and not self.game.Board[ row - 1][col].IsEnabled(): touchCheck = True elif col < 14 and not self.game.Board[row][ col + 1].IsEnabled(): touchCheck = True elif col > 0 and not self.game.Board[row][ col - 1].IsEnabled(): touchCheck = True # The centre piece must be filled on the first turn elif self.game.Board[7][7].GetName( ) != "button" and self.game.Board[7][7].IsEnabled(): touchCheck = True # Removes connected tiles so that duplicate parsing does not occur temp_list.remove(self.game.Board[row][col].GetId()) # Checks the opposite direction for each newly placed tile v_word = "" v_word_m = "" for j in range(2): while -1 < col < 15 and -1 < row < 15 and self.game.Board[ row][col].GetName() != "button": letter = self.game.Board[row][col] if j: if len(letter.GetName()) == 1: v_word = letter.GetName()[-1] + v_word else: v_word = letter.GetName()[-1].lower( ) + v_word if len(self.multipliers[row] [col]) and letter.IsEnabled(): v_word_m = self.multipliers[row][ col] + v_word_m else: v_word_m = "--" + v_word_m else: if len(letter.GetName()) == 1: v_word += letter.GetName()[-1] else: v_word += letter.GetName()[-1].lower() if len(self.multipliers[row] [col]) and letter.IsEnabled(): v_word_m += self.multipliers[row][col] else: v_word_m += "--" if direction == "HORIZONTAL": row += 1 - j * 2 else: col += 1 - j * 2 if not j: if direction == "HORIZONTAL": row = getRow(tile) - 1 else: col = getCol(tile) - 1 if len(v_word) > 1: words.append(v_word) word_multipliers.append(v_word_m) # Iterates whichever direction is currently being checked if direction == "HORIZONTAL": col += 1 - i * 2 row = getRow(tile) else: row += 1 - i * 2 col = getCol(tile) # Resets to the original tile and parse to the start if not i: if direction == "HORIZONTAL": row = getRow(tile) col = getCol(tile) - 1 else: row = getRow(tile) - 1 col = getCol(tile) # Add string to the list of words if len(h_word) > 1: words.append(h_word) word_multipliers.append(h_word_m) # Checks if each string in the list of words is a valid scrabble word wordCheck = True for word in words: if (word.lower(), ) not in self.dictionary: wordCheck = False # Checks if all tests are passed that qualifies the play as a valid play if touchCheck and wordCheck and not len(temp_list) and len(words): self.Play.Enable() for i in self.new_tiles: tile = self.game.Board[getRow(i)][getCol(i)] tile.SetBitmap( wx.Image("assets/IMAGES/GREEN_" + tile.GetName() + ".png", wx.BITMAP_TYPE_ANY).Scale( self.size, self.size).ConvertToBitmap()) points = 0 # Score the play based on the tiles and multipliers for i in range(len(words)): score = 0 multiply = 1 for j in range(len(words[i])): if words[i][j].isupper(): letter_score = int(self.tiles_file[ord(words[i][j]) - 65][2]) else: letter_score = 0 if word_multipliers[i][j * 2] != "-": if word_multipliers[i][j * 2 + 1] == "L": letter_score *= int(word_multipliers[i][j * 2]) else: multiply *= int(word_multipliers[i][j * 2]) score += letter_score points += score * multiply if len(self.new_tiles) == 7: points += 50 self.Points.SetLabel(str(points)) else: self.Play.Disable() self.Points.SetLabel('0') for i in self.new_tiles: tile = self.game.Board[getRow(i)][getCol(i)] tile.SetBitmap( wx.Image("assets/IMAGES/RED_" + tile.GetName() + ".png", wx.BITMAP_TYPE_ANY).Scale( self.size, self.size).ConvertToBitmap()) else: self.Play.Disable() self.Recall.Disable() self.Points.SetLabel('0')
def space(self, event): rowID = getRow(event.GetEventObject().GetId()) colID = getCol(event.GetEventObject().GetId()) # First click establishes which piece the user wants to move if self.click == 0: if getColour(event.GetEventObject()) == self.game.User: self.click = event.GetEventObject().GetId() showMoves(self.allMoves[rowID][colID], self.game.Board) # Second click moves the piece to the clicked spot # A new piece can be chosen to be moved by clicking on it else: row_click = getRow(self.click) col_click = getCol(self.click) checkered(self.game, self.allMoves[row_click][col_click]) moved = False for clicked_piece in range(len( self.allMoves[row_click][col_click])): if (rowID, colID ) == self.allMoves[row_click][col_click][clicked_piece]: moved = True piece = self.game.Board[row_click][col_click] # Set boolean to determine if a piece becomes a king in the current turn kingMe = False # Set piece to king if it reaches the end if len(piece.GetName()) < 6 and (rowID == 0 or rowID == 7): piece.SetName("KING_" + piece.GetName()) # Turn ends when a piece becomes a king kingMe = True # Place piece in new space PlacePiece(piece.GetName(), event.GetEventObject(), self.game.size) # Remove piece from old space PlacePiece("button", piece, self.game.size) # Remove skipped pieces if len(self.oppSkipped[row_click][col_click] [clicked_piece]): PlacePiece( "button", self.game.Board[self.oppSkipped[row_click] [col_click][clicked_piece][0]] [self.oppSkipped[row_click][col_click] [clicked_piece][1]], self.game.size) if self.game.player.GetStringSelection( ) == self.game.User: self.OppP -= 1 opp = -1 else: self.UserP -= 1 opp = 1 # Disables all other clicks except mandatory extra skips when they are available if not kingMe: more_skips = skipOpp(self, rowID, colID, opp) if len(more_skips): self.skip_again = True self.allMoves[rowID][colID] = more_skips self.click = event.GetEventObject().GetId() for skips in more_skips: self.game.Board[skips[0]][ skips[1]].SetBackgroundColour("green") else: self.skip_again = False else: self.skip_again = False if not self.skip_again: self.click = 0 userMoved(self, gameOver(self)) break if not moved: if getColour(self.game.Board[rowID][colID]) == self.game.User: checkered(self.game, self.allMoves[row_click][col_click]) showMoves(self.allMoves[rowID][colID], self.game.Board) self.click = event.GetEventObject().GetId() else: self.click = 0