Nexclap

War Card Game

This is a version of the card game War, and it is also my final Python A project.

# War Card game # A player wins if they gain 52 cards or have more cards after 5 rounds. These amounts can be adjusted if needed. # There are 2 checks placed in the code for player and opponent deck contents, these show the movement of cards. # A tie breaker within a tie breaker is decided with a coin flip in this version of the game. from random import randint total_cards = ["2", "2", "2", "2", "3", "3", "3", "3", "4", "4", "4", "4", "5", "5", "5", "5", "6", "6", "6", "6", "7", "7", "7", "7", "8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11", "12", "12", "12", "12", "13", "13", "13", "13", "14", "14", "14", "14"] player_cards = [] opponent_cards = [] for x in range(0, 26): y = 51-x card_number = randint(0, y) card = total_cards[card_number] player_cards.append(card) total_cards.pop(card_number) for x in range(0, 26): y = 25-x card_number = randint(0, y) card = total_cards[card_number] opponent_cards.append(card) total_cards.pop(card_number) print("player") print(player_cards) print("opponent") print(opponent_cards) # This is a check to see the starting moves, feel free to remove if using the code. p = len(player_cards) o = len(opponent_cards) moves = 0 while p != 52 and o != 52 and moves < 5: p_card = int(player_cards[0]) o_card = int(opponent_cards[0]) print("You have placed down a ", p_card) print("The opponent has placed down a ", o_card) moves = moves + 1 if p_card > o_card: continue_playing = input("Your card is higher, take all cards and keep playing? Enter yes to continue: ") if continue_playing == "yes": player_cards.append(str(o_card)) player_cards.pop(0) player_cards.append(str(p_card)) opponent_cards.pop(0) elif o_card > p_card: continue_playing = input("Your card is lower, give opponent cards and keep playing? Enter yes to continue: ") if continue_playing == "yes": opponent_cards.append(str(p_card)) opponent_cards.pop(0) opponent_cards.append(str(o_card)) player_cards.pop(0) elif o_card == p_card: continue_playing = input("The cards are the same, time for a tie-breaker? Enter yes to continue: ") if continue_playing == "yes": print("You and the opponent place 3 cards face down and one face up. Lets see who wins the war!") p_card1 = player_cards[1] p_card2 = player_cards[2] p_card3 = player_cards[3] p_card4 = int(player_cards[4]) o_card1 = opponent_cards[1] o_card2 = opponent_cards[2] o_card3 = opponent_cards[3] o_card4 = int(opponent_cards[4]) print("You place down a ", p_card4, "and your opponent has placed down a ", o_card4) if p_card4 > o_card4: print("Congrats! You won the war and gain a ", o_card1, o_card2, o_card3, "and", o_card4) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) elif o_card4 > p_card4: print("You lost the war, your opponent gains a ", p_card1, p_card2, p_card3, "and", p_card4) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) opponent_cards.append(str(o_card)) opponent_cards.append(str(o_card1)) opponent_cards.append(str(o_card2)) opponent_cards.append(str(o_card3)) opponent_cards.append(str(o_card4)) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) opponent_cards.append(str(p_card)) opponent_cards.append(str(p_card1)) opponent_cards.append(str(p_card2)) opponent_cards.append(str(p_card3)) opponent_cards.append(str(p_card4)) elif o_card4 == p_card4: print("Wow, another tie! You placed a ", p_card4, "and your opponent placed a ", o_card4, ".") p_coin = input("Lets decide this one with a coin flip. Heads or tails? ") continue_playing = input("Would you like to keep playing? Enter yes to continue: ") coin = randint(1, 2) if coin == 1 and p_coin == "heads" and continue_playing == "yes": print("You won the coin flip and gain the cards!") player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) elif coin == 2 and p_coin == "tails": print("You won the coin flip and gain the cards!") player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) else: print("You lost the coin flip and your opponent gains the cards.") opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) opponent_cards.append(str(o_card)) opponent_cards.append(str(o_card1)) opponent_cards.append(str(o_card2)) opponent_cards.append(str(o_card3)) opponent_cards.append(str(o_card4)) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) opponent_cards.append(str(p_card)) opponent_cards.append(str(p_card1)) opponent_cards.append(str(p_card2)) opponent_cards.append(str(p_card3)) opponent_cards.append(str(p_card4)) # This is another check to see how the content of the decks has changed during the round. print(player_cards) print(opponent_cards) p = len(player_cards) o = len(opponent_cards) if p > o: print("You have won the game!") elif o > p: print("You lost, the opponent won this game.") else: print("You and the opponent have tied.")

K NEAREST NEIGHBOR (machine learning)


import operator import math from math import sqrt data = [(1, 10, "blue"), (3, 9, "blue"), (4,8, "red"),(6, 0, "blue"), (6, 7, "red"), (5,9, "red"),(3, 2, "blue"), (4, 6, "blue"), (4,2, "red"),(3, 0, "blue"), (4, 7, "blue"), (7,4, "red"),(3, 11, "blue"), (11, 9, "blue"), (4,54, "red"),(1, 14, "blue"), (2, 2, "blue"), (12,8, "red")] mydict = {} xcor = int(input("Enter a x-coordinate: ")) ycor = int(input("Enter a y-coordinate: ")) k = int(input("Enter value for K: ")) for y in data: distance = sqrt((xcor - y[0])**2 + (ycor - y[1])**2) mydict[y] = distance sorted_x = sorted(mydict.items(), key = operator.itemgetter(1)) list = [] colors = [] for i in sorted_x: list.append(i[1]) for z in range(k): print(sorted_x[z][1]) for n in range(k): colors.append(sorted_x[n][0][2]) def most_frequent(colors): return max(set(colors), key = colors.count) print(most_frequent(colors))

Hangman

I made a program for Hangman, where the user is given 5 wrong letters to try to guess the random word.


import random wordbank = ["dinosaur","peanut","pencil","apple","pineapple"] separatedword = [] usedletters = [] #chooses a word from the list randomly random = random.randint(0,4) word = wordbank[random] guesses = 0 amountwrong = 5 wrong = "true" #welcomes user print("Hi welcome to Royce's Hangman Game" "\n" "Rule include: you have 5 wrong guesses", "Good Luck :)") #function to check if the user has already guessed the letter def guesscheck(userguesses): for x in range(len(usedletters)): if userguesses == usedletters[x]: print("oops you have already guessed that letter") global userguess userguess = input("\n" "Guess a letter") #separates the word up into a list by letter def separateword(): for x in range(0,len(word),1): separatedword.append(word[x]) separateword() letterleft = len(separatedword) length = len(separatedword) print("For testing purposes I'm including the random word" , word) print("The random word is", letterleft, "letters long") ''' loop to see if they can guess the word as long as the player hasn't run out of turns or has guessed it the loop will continue ''' while amountwrong != 0 and letterleft != 0: #asks user for a letter userguess = input("\n""Guess a letter") if guesses >= 1: guesscheck(userguess) #iterates through the list and checks to see if the guessed letter is in the #word for x in range(0,length,1): if userguess == separatedword[x]: print("You got the letter right!", "The word had a(n)",userguess) letterleft -= 1 wrong = "false" usedletters.append(userguess) guesses += 1 ''' uses a true or false to determine if the user has guessed wrong if true then it takes away a chance and tells the user ''' if wrong == "true": amountwrong -=1 print("oops that letter isn't in the word try again") #if not then it just continues and prints out the stats else: wrong = "true" print("You can still get" , amountwrong, "wrong" ) print("There are still", letterleft, "letters left to guess") #end of game print("\n" "You took an amount of",guesses,"guesses") if amountwrong == 0: print("Too bad you lost all your chances. Better luck next time!") else: print("Congrats! you guessed the word!", "The word was",word)
Announcements
  • Test Announcement from Nexclap

    Test message from nexclap - please ignore

    2023-02-13

  • Test message

    Test message from nexclap - please ignore

    2023-02-11

  • Test message

    Test message from nexclap - please ignore

    2023-02-11

  • View all