예제 #1
0
 def _on_canvas_clicked(self, event: tkinter.Event) -> None:
     '''
     When the canvas is clicked, it checks whether the place clicked is a valid place to put the current player's piece.
     If so, it puts the piece down and redraws the board. Otherwise, it just redraws the board and still requires input from
     the same player.
     '''
     self._play_button.destroy()#Destroys the play button when the game starts
     self._greeting_text.set('Game Started')
     state = self._state#game state
     width = self._canvas.winfo_width()#canvas width
     height = self._canvas.winfo_height()#canvas height
     for i in range(0,state._rows):#runs through the entire board
         for x in range(0,state._col):
             #Checks if location clicked is a valid place to put a piece
             if event.x > x*self._canvas.winfo_width()/state._col and event.x < (x+1)*self._canvas.winfo_width()/state._col:
                 if event.y > i*self._canvas.winfo_height()/state._rows and event.y < (i+1)*self._canvas.winfo_height()/state._rows:
                     #Checks if that place has already been filled by a piece
                     if (x/state._col,i/state._rows,(x+1)/state._col,(i+1)/state._rows,'black','black') not in self._spaces and (x/state._col,i/state._rows,(x+1)/state._col,(i+1)/state._rows,'black','white') not in self._spaces:
                         valid = gameLogic.validateMove(state,state.getTurn(),[i+1,x+1])#Gets the pieces to be flipped and if it was a valid move
                         if valid[0]:#If valid move
                             if state.getTurn() == gameLogic.BLACK:
                                 self._spaces.append((x/state._col,i/state._rows,(x+1)/state._col,(i+1)/state._rows,'black','black'))#Black piece coordinates
                                 state.makeBlack(x+1,i+1)#places black piece
                                 state.changeTurn()#Changes turn
                                 state.flipPiece(valid[1])#Flips the pieces
                                 for z in valid[1]:#Adds the flipped pieces to coordinates of circles that need to be drawn
                                     self._spaces.append((z[0]/state._col,z[1]/state._rows,(z[0]+1)/state._col,(z[1]+1)/state._rows,'black','black'))
                             else:
                                 self._spaces.append((x/state._col,i/state._rows,(x+1)/state._col,(i+1)/state._rows,'black','white'))#White piece coordinates
                                 state.makeWhite(x+1,i+1)#Places white pieces
                                 state.changeTurn()#Changes turn
                                 state.flipPiece(valid[1])#Flips the pieces
                                 for z in valid[1]:#Adds the flipped pieces to coordinates of circles that need to be drawn
                                     self._spaces.append((z[0]/state._col,z[1]/state._rows,(z[0]+1)/state._col,(z[1]+1)/state._rows,'black','white'))
     self._redraw_board()#redraws the board
예제 #2
0
def makeMove(a: gameLogic.gameState, turn: str):
    """
    Asks the user to enter a space on the board in row,col format. Then checks if its
    valid and places it on the board. If invalid, it remprompts.
    """
    count = 0
    while True:
        temp = [18, 18]  # Initializing
        move = []
        while len(move) != 2 or gameLogic.inBoard(a, turn, temp) == False:  # Checks if move is in the board
            try:
                move = input(
                    "Please select a space on the board by entering (row #,col #) like 3,4 or q to quit: "
                )  # Asks for the move from the user
                if move.upper() == "Q":
                    os._exit(0)
                move = move.split(",")
                move[0] = int(move[0])
                move[1] = int(move[1])
                temp = [move[0], move[1]]
            except:
                print("Please enter a valid move")  # Prints when invalid characters are used.
            if count > 0 and gameLogic.inBoard(a, turn, temp) == False:
                print("Enter a move on the board please")  # Prints if not on board
            count += 1
        legal = gameLogic.validateMove(a, turn, temp)  # Validates whether the move is legal
        flipPieces = legal[1]  # Gets the list of pieces that were to be flipped.
        if turn == gameLogic.BLACK:  # Checks if black's turn
            if a.getBoard()[int(move[1]) - 1][int(move[0]) - 1] == "-" and legal[0]:  # Checks if move is valid
                a.flipPiece(flipPieces)  # Flips pieces
                a.makeBlack(int(move[1]), int(move[0]))  # Makes new piece black
                break
            else:
                if (
                    a.getBoard()[int(move[1]) - 1][int(move[0]) - 1] == "-"
                ):  # Checks if space is blank which means invalid move would have to been entered
                    print("Please enter a valid move.")
                else:
                    print("Please enter an unfilled space.")  # Otherwise the space is taken already.
        else:
            if a.getBoard()[int(move[1] - 1)][int(move[0]) - 1] == "-" and legal[0]:  # Checks if move is valid
                a.flipPiece(flipPieces)  # Flips Pieces
                a.makeWhite(int(move[1]), int(move[0]))  # Places new white piece
                break
            else:
                if (
                    a.getBoard()[int(move[1]) - 1][int(move[0]) - 1] == "-"
                ):  # Checks if space is blank which means invalid move would have to been entered
                    print("Please enter a valid move.")
                else:
                    print("Please enter an unfilled space.")  # Otherwise the space is taken already.