Battleship on a Website

This is a game of battleship run on a website. The preview is a bit wonky.

Game of Rock, Paper, Scissors on a Website

A game of rock, paper, scissors run on a website.

Guessing Game run on Website

This is a guessing game that is run on a website. The backend was coded in javascript. The submit button checks to see if the number entered in the textBox matches the correct number, the hint button tells you if the number was lower or higher than the last number that you guessed, and the start over button lets you start over whenever you click it. All buttons are disabled upon running out of guesses.

Game of Battleship in JavaScript

This game of battleship has a 1x1 battleship randomly generated in a 5x5 board. The player has 4 guesses to guess the exact location of the ship. Each miss is marked with a "X" and a "V" is put in place of the battleship's location if you guess it correctly. The coordinates of the ship are given after you run out of guesses.


console.log("Let's play battleship! Try to guess correctly in 4 guesses"); var board = [ ["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"], ["O", "O", "O", "O", "O"] ] console.log(board); function battleship(board, turns){ var compx = Math.floor(Math.random() * 5); var compy = Math.floor(Math.random() * 5); var playerx = prompt("Guess row: "); var playery = prompt("Guess column: "); var i = 1; while (i < (turns)) { if ((compx != playerx && compy != playery) || (compx != playerx && compy == playery) || (compx == playerx && compy != playery)){ board[playerx][playery]= "X"; console.log("You missed my battleship! Turn "+i+" of "+turns); console.log(board); playerx = prompt("Guess row: "); playery = prompt("Guess column: "); } else if (compx == playerx && compy == playery){ board[playerx][playery]= "V"; console.log("You sunk my battleship!"); console.log(board); break; } i+=1; } console.log("The coordinates were "+compx+","+compy); } battleship(board, 4);

Game of Rock, Paper, Scissors in JavaScript

This game of Rock, Paper, Scissors is played between the computer and the user. The choice is given in all lowercase and standard rules for the game apply. After 10 rounds, the scores are tallied and the player with the higher round wins is the winner and the number of wins is displayed at the end.


var choices = ["rock", "paper", "scissors"]; function rps(timesPlayed){ var choices = ["rock", "paper", "scissors"]; var messsage; var final; var playerWins = 0; var compWins = 0; for (var i=0;i<timesPlayed;i++){ var playerChoice = prompt("rock, paper, or scissors? "); var compChoice = choices[Math.floor(Math.random() * choices.length)]; if (playerChoice == "rock" && compChoice == "scissors") { message = "The rock broke the scissors. Player wins!"; playerWins++; } else if (playerChoice == "paper" && compChoice == "rock") { message = "The paper covers the rock. Player wins!"; playerWins++; } else if (playerChoice == "scissors" && compChoice == "paper") { message = "The scissors cut the paper. Player wins!"; playerWins++; } else if (playerChoice == "scissors" && compChoice == "rock") { message = "The rock broke the scissors. Computer wins!"; compWins++; } else if (playerChoice == "paper" && compChoice == "scissors") { message = "The scissors cut the paper. Computer wins!"; compWins++; } else if (playerChoice == "rock" && compChoice == "paper") { message = "The paper covers the rock. Computer wins!"; compWins++; } else if (playerChoice == compChoice) { message = "Draw!"; } console.log(message); } playerWins > compWins ? final = "Player wins! Wins: "+playerWins+"/10" : final = "Computer wins! Wins: "+compWins+"/10"; console.log(final); } rps(10);

k nearest neighbor


from statistics import mode from math import sqrt import operator data = [(1,1,"red"),(2,4,"red"),(6,3,"red"),(7,4,"blue"),(4,2,"blue"),(3,4,"green")] x = int(input("What is the x value?")) y = int(input("What is the y value?")) k = int(input("How many closest points would you like?")) point = (x,y) print(point) def distance(x2,y2): dist = ((x2[0]-x)**2)+((y2[1]-y)**2) return sqrt(dist) if k>len(data): print("Enter a different number of points") Dictionary = {} for i in range(0,len(data)): var = distance(data[i],data[i]) print(var) Dictionary[data[i]]=var sorted_Dictionary = sorted(Dictionary.items(), key=operator.itemgetter(1)) print(sorted_Dictionary) for i in range(0,k): var1 = (sorted_Dictionary[0][0][2]) print(var1)

Car Class

Lets you create cars and store them in a Garage. You can drive cars, you can repair them, refuel them and change their tires. You can also specify whether the car is electric or not.

import random import math class Car: def __init__(self, name, capacity, mpg, cost): #the name of the car self.name = name #the car's odometer self.miles = 0.0 #the car's current fuel self.fuel = float(capacity) #the car's maximum fuel self.capacity = float(capacity) #the car's fuel efficiency self.mpg = float(mpg) #the car's tire health self.tiretreads = 5000.0 #the car's body damage self.damage = 0 #total costs spent on the car self.cost = float(cost) #def repair, def drive,def refuel,def tire rotation,def info def drive(self,distance): if (self.fuel*self.mpg < distance) and self.capacity != math.inf: print(" you need to refuel ") elif self.tiretreads < distance: print(" you have to change tires") elif self.damage > 50: print(" your car is to damaged to drive") else: crashchance = distance/1000 if round(random.uniform(0,100),2) <= crashchance: newdistance = random.randint(1,distance) print("your car crashed but it drove " + str(newdistance) + " miles") self.damage+=random.randint(1,100) print("your car got in an accident and aquired some damage " + str(self.damage)) self.miles+=newdistance self.tiretreads-=newdistance print("you drove " + str(newdistance) + " miles") if self.capacity != math.inf: self.fuel-=newdistance/self.mpg print("you now have " + str(self.fuel) + " gallons of fuel") else: self.miles+=distance self.tiretreads-=distance print("=====================") print("you drove " + str(distance) + " miles") if self.capacity != math.inf: self.fuel-=distance/self.mpg print("you now have " + str(self.fuel) + " gallons of fuel") print("=====================") def refuel(self): if self.capacity == math.inf: print ("cannot refuel electric car") return print("your refuel cost $" + str((self.capacity - self.fuel)* 4.20)) self.cost += (self.capacity - self.fuel)* 4.20 self.fuel = self.capacity def tirerotation(self): self.tiretreads = 5000.0 self.cost += 420 print("your tire rotation cost $420") def repair(self): self.cost += self.damage * (42) self.damage = 0 print("your repair cost $" + str(self.damage * 42)) def printinfo(self): print("====================================================") if self.capacity != math.inf: print("Name:" + str(self.name)) else: print("Name:" + str(self.name) + "(electric vehicle)") print("Odometer:" + str(self.miles)) if self.capacity != math.inf: print("Fuel: " + str(self.fuel) + '/' + str(self.capacity)) print("Fuel Effiency:" + str(self.mpg)) print("Miles remaning before maintenance:" + str(self.tiretreads)) print("Body damage:" + str(self.damage) + '%') print("Total money spent:$" + str((self.capacity - self.fuel)* 4.20)) print("====================================================") class Garage: def __init__(self): self.carobject = [] def mainloop(self): answer = "" while answer != "Q": print("===========================") print("1. Create Car") print("2. View Cars") print("3. Car Action") print("4. Print Financial Report") print("5. Save Garage") print("6. Load Garage") print("Q. Exit program") print("===========================") answer = (input("Enter your selection")) if answer == "1": self.CreateCar() elif answer == "2": self.Displaycar() elif answer == "3": self.caraction() elif answer == "4": self.financialreport() elif answer == "5": self.save() elif answer == "6": self.open() def financialreport(self): totalcost = 0 for i in self.carobject: totalcost+=i.cost print("your total spending was $" + str(totalcost)) def CreateCar(self): name = input("what is the name of your car?") capacity = input("how many gallons of gas can your car store?(E for electric car)") if capacity == "E": capacity = math.inf mpg = math.inf else: capacity =float(capacity) mpg = float(input("what is the fuel effiency of your car?")) cost = float(input("How much does your car cost?")) car = Car(name,capacity,mpg,cost) self.carobject.append(car) print("Successfully created a car") def Displaycar(self): count = 1 for i in self.carobject: print("Car " + str(count) + ": " + i.name) count+=1 def caraction(self): self.Displaycar() selection = int(input("enter the number of the car you want to view?")) self.carobject[selection-1].printinfo print("1. Refuel Car") print("2. Repair Car") print("3. Change your tires") print("4. Display information about the care") print("5. Drive the car") print("6. Would you like to go back?") answer = int(input("Enter your selection?")) if answer == 1: self.carobject[selection-1].refuel() elif answer == 2: self.carobject[selection-1].repair() elif answer == 3: self.carobject[selection-1].tirerotation() elif answer == 4: self.carobject[selection-1].printinfo() elif answer == 5: distance = int(input("How many miles would you like to drive?")) self.carobject[selection-1].drive(distance) elif answer == 6: self.Displaycar() else: print("Invalid Input") def save(self): file = open("garage.dat","w") for i in self.carobject: file.write(i.name + '\n') file.write(str(i.capacity) + '\n') file.write(str(i.mpg) + '\n') file.write(str(i.cost) + '\n') file.write(str(i.fuel) + '\n') file.write(str(i.tiretreads) + '\n') file.write(str(i.damage) + '\n') file.write(str(i.miles) + '\n') file.close() print("Succesfuly saved in garage.dat") def open(self): if len(self.carobject) != 0: print("using load will overwrite current garage") answer = input("would you like to continue(y/n)") if answer == "n": return self.carobject = [] file = open("garage.dat","r") lines = 0 for line in file: lines+=1 file.close() if lines%8 != 0: print("file is corrupted") return file = open("garage.dat","r") for i in range(int(lines/8)): car1 = Car(str(file.readline().rstrip()),float(file.readline().rstrip()),float(file.readline().rstrip()),float(file.readline().rstrip())) car1.fuel = float(file.readline().rstrip()) car1.tiretreads = file.readline().rstrip() car1.damage = file.readline().rstrip() car1.miles = file.readline().rstrip() self.carobject.append(car1) print(" Succesfully loaded " + str(len(self.carobject)) + " cars") file.close()

drive function in car class

The user creates their own car and the drive function drives the car and displays statistics on how the drive went


import random class Car: def __init__(self, name, capacity, mpg, cost): #the name of the car self.name = name #the car's odometer self.miles = 0.0 #the car's current fuel self.fuel = float(capacity) #the car's maximum fuel self.capacity = float(capacity) #the car's fuel efficiency self.mpg = float(mpg) #the car's tire health self.tiretreads = 5000.0 #the car's body damage self.damage = 0 #total costs spent on the car self.costs = float(cost) #def repair, def drive,def refuel,def tire rotation,def info def drive(self,distance): if self.fuel*self.mpg < distance: print(" you need to refuel ") elif self.tiretreads < distance: print(" you have to change tires") elif self.damage > 50: print(" your car is to damaged to drive") else: crashchance = distance/1000 if round(random.uniform(0,100),2) <= crashchance: newdistance = random.randint(1,distance) print("your car crashed but it drove " + str(newdistance) + " miles") self.damage+=random.randint(1,100) print("your car was damaged " + str(self.damage)) self.miles+=newdistance self.tiretreads-=newdistance self.fuel-=newdistance/self.mpg print("you drove " + str(newdistance) + " miles") print("you now have " + str(self.fuel) + " gallons of fuel") else: self.miles+=distance self.tiretreads-=distance self.fuel-=distance/self.mpg print("=====================") print("you drove " + str(distance) + " miles") print("you now have " + str(self.fuel) + " gallons of fuel") print("=====================") for i in range(200): car1= Car("Tesla",50,25,50000) car1.drive(600)

Number Guessing Game

Asks a user to guess a number and gives hints to the user based on how close the users guess is. Displays Users statistics about their games.


from random import randint condition = "yes" numgames = 0 numcount =[] while condition == "yes": num = randint(1,100) count = 0 answer = 0 while answer != num: answer = int(input(" guess a number from 1 to 100?")) count+=1 if answer < num: print( " guess a little higher") elif answer == num: print ("you guessed it") numgames+=1 numcount.append(count) else: print ("guess a little lower") condition = input(" Would you like to play again ?") print(" Here are your results") print("total games:" + str(len(numcount))) avg = 0 for i in numcount: avg+=i avg/=len(numcount) print( "Here is your average:" + str(avg)) least = numcount[0] for i in numcount: if i < least: least = i print("here was your best game:" + str(least))
Akshay Jun 05

This is amazing!

Rock, Paper Scissors

Play rock paper scissors with a computer as many times as you want

import random userInput = raw_input("Rock, Paper, or Scissors? ") computer = random.randint(1,3) if computer == 1: print("CPU chose Rock") if computer == 2: print("CPU chose Paper") if computer == 3: print("CPU chose Scissors") if userInput == "rock" and computer == 1: print("Tie") if userInput == "rock" and computer == 2: print("You Lose") if userInput == "rock" and computer == 3: print("You win") if userInput == "paper" and computer == 1: print("You win") if userInput == "paper" and computer == 2: print("Tie") if userInput == "paper" and computer == 3: print("You lose") if userInput == "scissors" and computer == 1: print("You lose") if userInput == "scissors" and computer == 2: print("you win") if userInput == "scissors" and computer == 3: print("tie")
Akshay Jun 05

This is amazing!

Mad Libs

When provided with a MadLibs file that has specified parts of speech can take in user input to create a MadLib


filename = input("Enter Filename") f = open(filename,'r') wordList = f.read().split() position = 0 for i in wordList: if i[0] == "<": wordList[position] = input("Input " + i) position+=1 for i in wordList: print(i,end = " ") f.close
Akshay Jun 04

This is amazing.

1