Nexclap

K Means Clustering - in progress

So far, I have used a recursive function to keep changing the centroid until the clusters are the same. Next, I found the variance for each centroid. Now, I need to find where the variance is the least

data = [(3,5 , 10), (4, 12, 1), (7, 8, 5), (2, 6, 11), (3, 1, 4), (4, 1, 7), (1, 2, 3), (5, 1, 3), (6, 2, 3)] length2 = len(data) NewData = [] def FindClosest(centroid, xvalue, yvalue, zvalue): base = 100000 length = len(centroid) for i in range(length): a = centroids[i] x = a[0] y = a[1] z = a[2] distance = math.sqrt(abs(xvalue - x)**2 + abs(yvalue-y)**2 + abs(zvalue-z)**2) if distance < base: base = distance place = i return ((base, place)) def ChooseColor(place, centroids): a = centroids[place] color = a[3] return (color) BestCentroids = [(0, 0, 0, "blue"), (0, 0, 0, "green"), (0, 0, 0, "red")] minimum = float('inf') def Recursion(centroids, data, NewerData, i, stdDev, BestCentroids): distances = [] NewData = [] f = data[i] xvalue = f[0] yvalue = f[1] zvalue = f[2] place = FindClosest(centroids, xvalue, yvalue, zvalue)[1] color = ChooseColor(place, centroids) distance = FindClosest(centroids, xvalue, yvalue, zvalue)[0] distances.append(distance) NewData.append((xvalue, yvalue, zvalue,color)) if NewData != NewerData: return (Recursion(centroids, data, NewData, i+1, stdDev, BestCentroids)) else: stDev = statistics.stdev(distances) if stDev < minimum: minimum = stDev BestCentroids = [(random.randint(0, 12), random.randint(0, 12), random.randint(0, 12), "blue"), (random.randint(0, 12), random.randint(0, 12), random.randint(0, 12), "green"), (random.randint(0, 12), random.randint(0, 12), random.randint(0, 12), "red")] return ((stDev, BestCentroids)) for i in range(12): centroids = [(random.randint(0, 12), random.randint(0, 12), random.randint(0, 12), "blue"), (random.randint(0, 12), random.randint(0, 12), random.randint(0, 12), "green"), (random.randint(0, 12), random.randint(0, 12), random.randint(0, 12), "red")] Recursion(centroids, data, NewData, 0, minimum, BestCentroids)

Snake Pygame

This is the popular game snake. I specified a block size to build the blocks and make the snake faster.


import pygame import time import random pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,155,0) width = 800 height = 600 gameDisplay = pygame.display.set_mode((width,height)) pygame.display.set_caption('Slither') clock = pygame.time.Clock() blockSize = 23 FPS = 30 font = pygame.font.SysFont(None, 25) def snake(blockSize, snakelist): for XnY in snakelist: pygame.draw.rect(gameDisplay, green, [XnY[0],XnY[1],blockSize,blockSize]) def text_objects(text,color): textSurface = font.render(text, True, color) return textSurface, textSurface.get_rect() def message_to_screen(msg,color): textSurf, textRect = text_objects(msg,color) textRect.center = (width / 2), (height / 2) gameDisplay.blit(textSurf, textRect) def gameLoop(): gameExit = False gameOver = False xCoord = width/2 yCoord = height/2 xChange = 0 yChange = 0 snakeList = [] snakeLength = 1 randAppleX = round(random.randrange(0, width-blockSize)) randAppleY = round(random.randrange(0, height-blockSize)) while not gameExit: while gameOver == True: gameDisplay.fill(white) message_to_screen("Game over, press C to play again or Q to quit", red) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: gameOver = False gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: gameExit = True gameOver = False if event.key == pygame.K_c: gameLoop() for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: xChange = -blockSize yChange = 0 elif event.key == pygame.K_RIGHT: xChange = blockSize yChange = 0 elif event.key == pygame.K_UP: yChange = -blockSize xChange = 0 elif event.key == pygame.K_DOWN: yChange = blockSize xChange = 0 if xCoord >= width or xCoord < 0 or yCoord >= height or yCoord < 0: gameOver = True xCoord += xChange yCoord += yChange gameDisplay.fill(white) AppleThickness = 30 pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, AppleThickness, AppleThickness]) snakeHead = [] snakeHead.append(xCoord) snakeHead.append(yCoord) snakeList.append(snakeHead) if len(snakeList) > snakeLength: del snakeList[0] for eachSegment in snakeList[:-1]: if eachSegment == snakeHead: gameOver = True snake(blockSize, snakeList) pygame.display.update() if xCoord > randAppleX and xCoord < randAppleX + AppleThickness or xCoord + blockSize > randAppleX and xCoord + blockSize < randAppleX+ AppleThickness: if yCoord > randAppleY and yCoord < randAppleY + AppleThickness: randAppleX = round(random.randrange(0, width-blockSize)) randAppleY = round(random.randrange(0, height-blockSize)) snakeLength += 1 elif yCoord + blockSize > randAppleY and yCoord + blockSize < randAppleY+ AppleThickness: randAppleX = round(random.randrange(0, width-blockSize)) randAppleY = round(random.randrange(0, height-blockSize))#/10.0)*10.0 snakeLength += 1 clock.tick(FPS) pygame.quit() quit() gameLoop()

Battleship

You have 4 tries to sink the ship and win.

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