def topLeftPress(): global gameOver #No moves can be made if game is over. if gameOver == 0: global turn #These are required to call the function in the checkWin.py global topLeftComp global AIturn global Xwin global Owin global aiSkill if topLeftComp == 0: #check if space is filled if turn == 0: place[0] = 1 turn = 1 TopLeft.configure(text=("X")) else: turn = 0 place[0] = 2 TopLeft.configure(text="O") topLeftComp = 1 L = checkWin(place,AIturn,End,Xwin,Owin,turn, aiSkill) Xwin = Xwin+L[0] Owin = Owin+L[1] gameOver = L[2] if AIturn%2 == 0: #Call AI turn every two turns AIturn = AIturn+1 AI(aiSkill,place,turn,AIturn) else: AIturn = AIturn+1 else: print("Already Set Box") return place
def botRightPress(): global gameOver if gameOver == 0: global turn global botRightComp global AIturn global Xwin global Owin global aiSkill if botRightComp == 0: if turn == 0: place[8] = 1 turn = 1 BotRight.configure(text="X") else: turn = 0 place[8] = 2 BotRight.configure(text="O") botRightComp = 1 L = checkWin(place,AIturn,End,Xwin,Owin,turn, aiSkill) Xwin = Xwin+L[0] Owin = Owin+L[1] gameOver = L[2] if AIturn%2 == 0: AIturn = AIturn+1 AI(aiSkill,place,turn,AIturn) else: AIturn = AIturn+1 else: print("Already Set Box") return place
def main(): init = dJeu.debutJeu() variables.joueurA = init[0] variables.joueurB = init[1] pPlat.printMap(variables.plateau) while variables.statutParti == "ok": if variables.tour % 2 == 0: print(f"{variables.joueurA} c'est a ton tour") else: print(f"{variables.joueurB} c'est a ton tour") axeY = str( input( "veuillez choisir la ligne ou mettre votre pion (entre a et c) : " )) while axeY != "a" and axeY != "b" and axeY != "c": axeY = str(input("entre a et c : ")) axeX = int( input( "veuillez choisir la colonne ou mettre votre pion (entre 1 et 3) : " )) while axeX > 3 or axeX < 1: axeX = int(input("entre 1 et 3 : ")) while checkPion.checkPion(axeY, axeX, variables.plateau) == "ko": print("cette case est deja prise") axeY = str( input( "veuillez choisir la ligne ou mettre votre pion (entre a et c) : " )) while axeY != "a" and axeY != "b" and axeY != "c": axeY = str(input("entre a et c : ")) axeX = int( input( "veuillez choisir la colonne ou mettre votre pion (entre 1 et 3) : " )) while axeX > 3 or axeX < 1: axeX = int(input("entre 1 et 3 : ")) variables.plateau = placePion.placePion(axeY, axeX, variables.tour, variables.plateau) variables.plateauBin = placePion.placeBin(axeY, axeX, variables.tour, variables.plateauBin) verifWin = checkWin.checkWin(variables.plateauBin, variables.conditionWin) variables.tour += 1 if variables.tour > 0: clear() pPlat.printMap(variables.plateau) if verifWin > 0: if verifWin == 1: print( f"Félicitatin a {variables.joueurA} qui a gagné la parti avec les X." ) elif verifWin == 2: print( f"Félicitatin a {variables.joueurB} qui a gagné la parti avec les O." ) variables.statutParti = "win"
def playRPS(maxGames): uWin, cWin, gameRound = 0, 0, 1 gameCalc = (maxGames * (1 / 2)) checkWin = True winTrue = checkWin while winTrue == True: computerChoice = random.randint(0, 2) print("") print("0 = Rock") print("1 = Paper") print("2 = Scissors") print("Round: {2} of {4} Score: {0} to {1} ".format( uWin, cWin, gameRound, computerChoice, maxGames, gameCalc, winTrue)) while True: try: userChoice = int(input("[0 - 2], [99 to Quit]: ")) break except: print("Invalid choice") if userChoice == computerChoice: print("Tie. Try again") continue if userChoice == 0: if computerChoice == 1: print("You Lose, paper covers rock") cWin += 1 else: print("You Win, paper smashes scissors") uWin += 1 elif userChoice == 1: if computerChoice == 0: print("You Win, paper covers rock") uWin += 1 else: print("You Lose, scissors cuts paper") cWin += 1 elif userChoice == 2: if computerChoice == 0: print("You Lose, rock crushes scissors") cWin += 1 else: print("You Win, scissors cuts paper") uWin += 1 elif userChoice == 99: break else: print("Invalid choice, try again.") gameRound += 1 winTrue = c.checkWin(uWin, cWin, gameCalc)
def checkGameOver(self): print(self.boardList[0], '\n', self.boardList[1], '\n', self.boardList[2], '\n', self.boardList[3] ) # show the boardlist, but so it looks like a board! # Now lets check to see if there's a winner WINNER = checkWin.checkWin(self.boardList) print(WINNER) #============================================================== # STOP! READ! # You want to check for a winner all the time, but a STALEMATE # only happens when teh board is FULL. So we'll check for winners # but if we get a stalemate, we'll also check to see that the board # is full! #============================================================== gameOver = False # some variable to tell if my game is over. # USE THIS TO DRAW YOUR GAMEOVER SCREEN (if you're doing that) if type(WINNER) == str and WINNER != self.empty: print(WINNER + ' is the winner!') gameOver = True # we have a winner, so the game is done. else: for row in self.boardList: if self.empty in row: # are there any values equal to our "empty space" value?? # if so, gameOver isn't happening! gameOver = False else: gameOver = True print("It's a draw! No winner! \n", '\n', "Play again?") if gameOver: # From PC06 gameOver = turtle.Turtle() gameOver.color('red') style = ('Helvetica', 80, 'bold') gameOver.up() gameOver.hideturtle() gameOver.goto(0, -50) # gameover condition gameOver.write('GAMEOVER!', font=style, align='center') turtle.done()
def match(player1, player2, n, win1, win2, loss1, loss2, draw1, draw2): """ The function is used to play a game between player1 and player2 player1,player2 - vector containing the weights associated with the player n - size of the board Fitness parameters: win1 - winning the match as player1 win2 - winning the match as player2 ... Returns: Fitness parameter for player1 and player2 which depends on result of the game played """ board = mat(zeros((n, n))) #empty boxes - boxes that have not been used by either of the players empty_boxes = [x for x in range(n * n)] turnNo = 0 while not checkWin(board) and not checkComplete(board): turnNo += 1 if turnNo % 2: #choosing the move that has the highest confidence #among those that haven't been played yet confidences = predict(player1, board) confidences = [(i, x) for i, x in enumerate(confidences)] confidences = sorted(confidences, key=operator.itemgetter(1), reverse=True) for i, confidence in confidences: if i in empty_boxes: row = floor(i / n) col = i % n board[row, col] = 1 empty_boxes.remove(i) break else: #changing the board so as to make the player2 the current player pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 #choosing the move that has the highest confidence #among those that haven't been played yet confidences = predict(player2, board) confidences = [(i, x) for i, x in enumerate(confidences)] confidences = sorted(confidences, key=operator.itemgetter(1), reverse=True) for i, confidence in confidences: if i in empty_boxes: row = floor(i / n) col = i % n board[row, col] = 1 empty_boxes.remove(i) break #inveritng the board so as to make player1 the current player pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 #code to return the fitness of player1 and player2 check_win = checkWin(board) if not check_win: #draw return (draw1, draw2) elif turnNo % 2: #player1 has won return (win1, loss2) else: #player2 has won return (loss1, win2)
def checkValidClick(coords): import main, checkWin x, y = coords if x < 200 and y < 200: if main.boardMatrix[0][0] == 0: if main.PLYR_X == True: main.drawX(40, -10) main.boardMatrix[0][0] = 1 else: main.drawO(18, -70) main.boardMatrix[0][0] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 400 and y < 200: if main.boardMatrix[1][0] == 0: if main.PLYR_X == True: main.drawX(240, -10) main.boardMatrix[1][0] = 1 else: main.drawO(218, -70) main.boardMatrix[1][0] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 600 and y < 200: if main.boardMatrix[2][0] == 0: if main.PLYR_X == True: main.drawX(440, -10) main.boardMatrix[2][0] = 1 else: main.drawO(418, -70) main.boardMatrix[2][0] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 200 and y < 400: if main.boardMatrix[0][1] == 0: if main.PLYR_X == True: main.drawX(40, 190) main.boardMatrix[0][1] = 1 else: main.drawO(18, 130) main.boardMatrix[0][1] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 400 and y < 400: if main.boardMatrix[1][1] == 0: if main.PLYR_X == True: main.drawX(240, 190) main.boardMatrix[1][1] = 1 else: main.drawO(218, 130) main.boardMatrix[1][1] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 600 and y < 400: if main.boardMatrix[2][1] == 0: if main.PLYR_X == True: main.drawX(440, 190) main.boardMatrix[2][1] = 1 else: main.drawO(418, 130) main.boardMatrix[2][1] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 200 and y < 600: if main.boardMatrix[0][2] == 0: if main.PLYR_X == True: main.drawX(40, 390) main.boardMatrix[0][2] = 1 else: main.drawO(18, 330) main.boardMatrix[0][2] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 400 and y < 600: if main.boardMatrix[1][2] == 0: if main.PLYR_X == True: main.drawX(240, 390) main.boardMatrix[1][2] = 1 else: main.drawO(218, 330) main.boardMatrix[1][2] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer() elif x < 600 and y < 600: if main.boardMatrix[2][2] == 0: if main.PLYR_X == True: main.drawX(440, 390) main.boardMatrix[2][2] = 1 else: main.drawO(418, 330) main.boardMatrix[2][2] = 2 if checkWin.checkWin() == True: main.onWin() main.swapPlayer()
def match(player1,player2,n,win1,win2,loss1,loss2,draw1,draw2) : """ The function is used to play a game between player1 and player2 player1,player2 - vector containing the weights associated with the player n - size of the board Fitness parameters: win1 - winning the match as player1 win2 - winning the match as player2 ... Returns: Fitness parameter for player1 and player2 which depends on result of the game played """ board = mat(zeros((n,n))) #empty boxes - boxes that have not been used by either of the players empty_boxes = [x for x in range(n*n)] turnNo = 0 while not checkWin(board) and not checkComplete(board) : turnNo += 1 if turnNo%2 : #choosing the move that has the highest confidence #among those that haven't been played yet confidences = predict(player1,board) confidences = [ (i,x) for i,x in enumerate(confidences)] confidences = sorted(confidences,key=operator.itemgetter(1),reverse=True) for i,confidence in confidences : if i in empty_boxes : row = floor(i/n) col = i%n board[row,col] = 1 empty_boxes.remove(i) break else : #changing the board so as to make the player2 the current player pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 #choosing the move that has the highest confidence #among those that haven't been played yet confidences = predict(player2,board) confidences = [ (i,x) for i,x in enumerate(confidences)] confidences = sorted(confidences,key=operator.itemgetter(1),reverse=True) for i,confidence in confidences : if i in empty_boxes : row = floor(i/n) col = i%n board[row,col] = 1 empty_boxes.remove(i) break #inveritng the board so as to make player1 the current player pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 #code to return the fitness of player1 and player2 check_win = checkWin(board) if not check_win : #draw return (draw1,draw2) elif turnNo%2 : #player1 has won return (win1,loss2) else : #player2 has won return (loss1,win2)
yPos = int(position[1]) # If guess position invalid, print error message and reprompt if (checkMove(playerInfo.guess, xPos, yPos) == 0): print("Invalid location, or you've already guessed " \ "this position.\n") print("Please enter a position to guess: ") continue # Place guess on player's board placeGuess(playerInfo, xPos, yPos) # If a player sinks both ships, print win message and exit program if (checkWin(playerInfo)): print("\nPLAYER " + str(player) + " WINS!\n") break # Switch to other player player = (player % 2) + 1 playerInfo = playerInfo.otherPlayer # Print player's turn print("\nPlayer " + str(player) + "'s turn.\n") # Board of player's guesses print("Player " + str(player) + "'s guesses:\n\n") # Print the player's board of guesses printBoard(playerInfo.guess)
def match(player, preference): board = mat(zeros((n, n))) #empty boxes contain the boxes that are not filled yet empty_boxes = [x for x in range(n * n)] turnNo = 0 while not checkWin(board) and not checkComplete(board): turnNo += 1 if preference == 2 and turnNo % 2: #the computer starts the game and it's computer's move #the computer's move confidences = predict(player, board) confidences = [(i, x) for i, x in enumerate(confidences)] confidences = sorted(confidences, key=operator.itemgetter(1), reverse=True) for i, confidence in confidences: if i in empty_boxes: row = floor(i / n) col = i % n board[row, col] = 1 empty_boxes.remove(i) break elif preference == 2 and not turnNo % 2: #the computer starts the game and it's user's move print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) while box_no not in empty_boxes: print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) row = floor(box_no / n) col = box_no % n board[row, col] = -1 empty_boxes.remove(box_no) elif preference == 1 and turnNo % 2: #the user starts the game and it's user's move print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) while box_no not in empty_boxes: print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) row = floor(box_no / n) col = box_no % n board[row, col] = 1 empty_boxes.remove(box_no) else: #the user starts the game and it's computer's move #changing the board so as to make the player2 the current player pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 confidences = predict(player, board) confidences = [(i, x) for i, x in enumerate(confidences)] confidences = sorted(confidences, key=operator.itemgetter(1), reverse=True) for i, confidence in confidences: if i in empty_boxes: row = floor(i / n) col = i % n board[row, col] = 1 empty_boxes.remove(i) break pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 print board #code to find the output of the game check_win = checkWin(board) if not check_win: #draw print "draw" elif turnNo % 2: #player1 has won if preference == 1: print "user has won" else: print "computer has won" else: #player2 has won if preference == 2: print "user has won" else: print "computer has won" return 0
def match(player,preference) : board = mat(zeros((n,n))) #empty boxes contain the boxes that are not filled yet empty_boxes = [x for x in range(n*n)] turnNo = 0 while not checkWin(board) and not checkComplete(board) : turnNo += 1 if preference == 2 and turnNo%2: #the computer starts the game and it's computer's move #the computer's move confidences = predict(player,board) confidences = [ (i,x) for i,x in enumerate(confidences)] confidences = sorted(confidences,key=operator.itemgetter(1),reverse=True) for i,confidence in confidences : if i in empty_boxes : row = floor(i/n) col = i%n board[row,col] = 1 empty_boxes.remove(i) break elif preference == 2 and not turnNo%2 :#the computer starts the game and it's user's move print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) while box_no not in empty_boxes : print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) row = floor(box_no/n) col = box_no%n board[row,col] = -1 empty_boxes.remove(box_no) elif preference == 1 and turnNo%2: #the user starts the game and it's user's move print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) while box_no not in empty_boxes : print "enter a number between 0-8 that is a legal move" box_no = int(raw_input()) row = floor(box_no/n) col = box_no%n board[row,col] = 1 empty_boxes.remove(box_no) else : #the user starts the game and it's computer's move #changing the board so as to make the player2 the current player pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 confidences = predict(player,board) confidences = [ (i,x) for i,x in enumerate(confidences)] confidences = sorted(confidences,key=operator.itemgetter(1),reverse=True) for i,confidence in confidences : if i in empty_boxes : row = floor(i/n) col = i%n board[row,col] = 1 empty_boxes.remove(i) break pos = nonzero(board == 1) neg = nonzero(board == -1) board[pos] = -1 board[neg] = 1 print board #code to find the output of the game check_win = checkWin(board) if not check_win : #draw print "draw" elif turnNo%2 : #player1 has won if preference == 1 : print "user has won" else : print "computer has won" else : #player2 has won if preference == 2 : print "user has won" else : print "computer has won" return 0
def playRPS(maxGames): uWin, cWin, gameRound = 0, 0, 1 gameCalc = (maxGames * (1 / 2)) checkWin = True winTrue = checkWin while winTrue == True: computerChoice = random.randint(0, 4) print("") print("0 = Rock") print("1 = Paper") print("2 = Scissors") print("3 = Lizard") print("4 = Spock") print("Round: {2} of {4} Score: {0} to {1} Debug: {3} {5} {6} ".format( uWin, cWin, gameRound, computerChoice, maxGames, gameCalc, winTrue)) while True: try: userChoice = int(input("[0 - 4], [99 to Quit]: ")) break except: print("Invalid choice") if userChoice == computerChoice: print("Tie. Try again") continue if userChoice == 0: if computerChoice == 1: print("You Lose, paper covers rock") cWin += 1 elif computerChoice == 2: print("You Win, paper smashes scissors") uWin += 1 elif computerChoice == 3: print("You Win, rock crushes lizard") uWin += 1 else: #computerChoice = 4 print("You Lose, Spock vaporizes rock") cWin += 1 elif userChoice == 1: if computerChoice == 0: print("You Win, paper covers rock") uWin += 1 elif computerChoice == 2: print("You Lose, scissors cuts paper") cWin += 1 elif computerChoice == 3: print("You Lose, lizard eats paper") cWin += 1 else: #computerChoice == 4: print("You Win, paper disproves Spock") uWin += 1 elif userChoice == 2: if computerChoice == 0: print("You Lose, rock crushes scissors") cWin += 1 elif computerChoice == 1: print("You Win, scissors cuts paper") uWin += 1 elif computerChoice == 3: print("You Win, scissors decapitates lizard") uWin += 1 else: # computerChoice == 4: print("You Lose, Spock crushes scissors") cWin += 1 elif userChoice == 3: if computerChoice == 0: print("You Lose, rock crushes lizard") cWin += 1 elif computerChoice == 1: print("You Win, lizard eats paper") uWin += 1 elif computerChoice == 2: print("You Lose, scissors decapitates lizard") cWin += 1 else: # computerChoice == 4: print("You Win, Lizard poisons Spock") uWin += 1 elif userChoice == 4: if computerChoice == 0: print("You Win, Spock Vaporizes rock") uWin += 1 elif computerChoice == 1: print("You Lose, paper disproves Spock") cWin += 1 elif computerChoice == 2: print("You Win, Spock crushes scissors") uWin += 1 else: # computerChoice == 3: print("You Lose, lizard poisons Spock") cWin += 1 elif userChoice == 99: break else: print("Invalid choice, try again.") gameRound += 1 winTrue = c.checkWin(uWin, cWin, gameCalc)
def xTurn(self, caseLocation): source = self.sender() if caseLocation == 'A': self.Abtn.hide() self.Abtn.setEnabled(False) globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[0] = 'X' self.symbolAX.show() if caseLocation == 'B': self.Bbtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[1] = 'X' self.symbolBX.show() if caseLocation == 'C': self.Cbtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[2] = 'X' self.symbolCX.show() if caseLocation == 'D': self.Dbtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[3] = 'X' self.symbolDX.show() if caseLocation == 'E': self.Ebtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[4] = 'X' self.symbolEX.show() if caseLocation == 'F': self.Fbtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[5] = 'X' self.symbolFX.show() if caseLocation == 'G': self.Gbtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[6] = 'X' self.symbolGX.show() if caseLocation == 'H': self.Hbtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[7] = 'X' self.symbolHX.show() if caseLocation == 'I': self.Ibtn.hide() globalVars.playerOTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[8] = 'X' self.symbolIX.show() globalVars.playerOTurn = True print(globalVars.caseInfo) if (checkWin.checkWin(self) == True): hideAll(self) else: self.statusbar.showMessage('Player O turn')
def oTurn(self, caseLocation): source = self.sender() if caseLocation == 'A': self.Abtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[0] = 'O' self.symbolAO.show() if caseLocation == 'B': self.Bbtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[1] = 'O' self.symbolBO.show() if caseLocation == 'C': self.Cbtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[2] = 'O' self.symbolCO.show() if caseLocation == 'D': self.Dbtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[3] = 'O' self.symbolDO.show() if caseLocation == 'E': self.Ebtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[4] = 'O' self.symbolEO.show() if caseLocation == 'F': self.Fbtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[5] = 'O' self.symbolFO.show() if caseLocation == 'G': self.Gbtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[6] = 'O' self.symbolGO.show() if caseLocation == 'H': self.Hbtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[7] = 'O' self.symbolHO.show() if caseLocation == 'I': self.Ibtn.hide() globalVars.playerXTurn = True self.statusbar.showMessage('Clicked : {}'.format(source.text())) globalVars.caseInfo[8] = 'O' self.symbolIO.show() globalVars.playerXTurn = True print(globalVars.caseInfo) if (checkWin.checkWin(self) == True): hideAll(self) else: self.statusbar.showMessage('Player X turn')