Well the thing is, when I use System.out.println() on the list before I send it in to shuffle. It displays every element, and now I also got it to work after shuffle by removing the random node I inserted to list2 and then removed from list. If I just delete it from my code and run it, it works perfectly. The print is not the most beautifulest but it works.

So I have writen a program that takes a linkedlist and shuffles it randomly using mergesort algorithm. The problam I'm having now is that the original list loses elements after the shuffle, and after som checking with some outputs it seems that it always one of the extra lists who have elements still left inside them.

This is an assignment and here is the task:

Assume you have as input ia singly-linked list containing N
items (an instance of a LinkedList class in Java). You should rearrange the items in the
LinkedList uniformly at random. Your algorithm should consume a logarithmic (or
constant) amount of extra memory and run in time proportional to NlogN in the worst
case.

my code:

 import java.util.LinkedList;

    public class Q02 {

    public static void main(String[] args) {

        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(0);
        System.out.println("Shuffled list: "+shuffle(list));
    }

    public static LinkedList shuffle(LinkedList list){
        Integer node = 10;
        if(list.size()<=1){
            return list;
        }
        LinkedList<Integer>list1 = new LinkedList<>();
        LinkedList<Integer>list2 = new LinkedList<>();

        while(!list.isEmpty()){
            list1.add((Integer) list.removeFirst());
            if(!list.isEmpty()){
                list2.add((Integer) list.removeFirst());
            }
        }
        shuffle(list1);
        shuffle(list2);
        if(list2.size() < list1.size()){
            int i = (int)(Math.random() * list2.size());
            list2.set(i, node);
        }

        while(!list1.isEmpty()&&!list2.isEmpty()){
            int rand = (int)(Math.round(Math.random()));
            if(rand == 1){
                list.add(list1.removeFirst());
            }
            else if(rand == 0){
                list.add(list2.removeFirst());
            }
        }
        if(!list1.isEmpty()){
            list.add(list2.clone());
        }
        if(!list2.isEmpty()){
            list.add(list1.clone());
        }
        list.remove(node);
        return list;
    }

    }

okey ignore that last bit...

Okey, im almost one with this just have a quick question. How do I check how much there is left to get to 10? Let's say I have the number 7 and want to check how many numbers there are left to 10, wich is 3, but how do I do that in java?

alright, ill try that

So I guess for the subtraction method I will have to do the same but instead of carrying one over to the next, I take one away from the second one and add 10 to the first one?

this kinda what you was thinking?

Yeah it's a stupid task, but like JamesCherrill said I still need to do it.

Yeah I been thinking about that and just found out how our teacher thinks we should do it, this is his pseudocode

public static String addera(String tal1, String tal2)
    {
        StringBuilder sum = new StringBuilder();
        int t1Last = tal1.length() - 1, 
                t2Last = tal2.length() - 1, 
                t1Pointer = t1Last, 
                t2Pointer = t2Last, // Pointervariabels.
                t1Del = 0,
                t2Del = 0, // Numbers to calculate with.
                carry = 0, // Carry.
                calc = 0; // Holder for calculated value.

        while(t1Pointer >= 0 && t2Pointer >= 0)
        {
            t1Del = (int) Character.getNumericValue(tal1.charAt(t1Pointer));
            t2Del = (int) Character.getNumericValue(tal2.charAt(t2Pointer));
            calc = t1Del + t2Del + carry;
            if(calc > 9)
            {
                carry = calc  / 10;
                calc = calc % 10;
            }

            t1Pointer--;
            t2Pointer--;
            sum.insert(0, calc);
        }

        while(t1Pointer >= 0)
        {
            t1Del = (int) Character.getNumericValue(tal1.charAt(t1Pointer));
            calc = t1Del + carry;
            if(calc > 9)
            {
                carry = calc  / 10;
                calc = calc % 10;
            }

            t1Pointer--;
            sum.insert(0, calc);
        }

        while(t2Pointer >= 0)
        {
            t2Del = (int) Character.getNumericValue(tal2.charAt(t2Pointer));
            calc = t2Del + carry;
            if(calc > 9)
            {
                carry = calc  / 10;
                calc = calc % 10;
            }

            t2Pointer--;
            sum.insert(0, calc);
        }

        if (carry != 0)
            sum.insert(0, carry);   

        return sum.toString();
    }

Hi, I need to make a program that can count and solve basic math problems (addition, subtraction) with natural integers using strings.
I'm not aloved to use parseInt so thats basicly where my world started to fall apart...
Would realy appreciate if someone could help me with this.

The code I have been given

import java.util.*;     //Scanner
import static java.lang.System.out;

public class OperationerMedNaturligaHeltalGivnaSomTeckenstranger {

    public static void main(String[] args) {

        out.println ("OPERATIONS WITH NATURAL INTEGES GIVEN " + "AS CHARACTER STRINGS\n");

        // input the natural integers
        Scanner in = new Scanner (System.in);
        out.println ("two natural integers");
        String tal1 = in.next();
        String tal2 = in.next();
        out.println();

        // add up the integers
        String tal = addera (tal1,tal2);

        // set an appropiate length on the integers and the result (this should be here)
        int len1 = tal1.length ();
        int len2 = tal2.length ();
        int len = tal.length ();
        int maxLen = Math.max(Math.max (len1, len2), len);
        tal1 = sattLen (tal1, maxLen - len1);
        tal2 = sattLen (tal2, maxLen - len2);
        tal = sattLen (tal, maxLen - len);

        // show the integers and the result
        out.println (" " + tal1);
        out.println(" " + tal2);
        for (int i = 0; i < maxLen + 2; i++){
            out.println ("-");
        }
        out.println ();
        out.println (" " + tal + "\n");

        // the numbers should also subtract, and show the diferential
        // here I just setup the call to the metod
    //  String diffTal = subtrahera (tal1, tal2);

        // here I just setup how it should be displayed
    // ...

Thanks for the help everyone, Schol-R-LEA you are truly awesome! Every problem I have had and posted on here you have shown me how to do it. Great thanks to you man!

Yeah well, that works perfectly if I only run the HelloWorld.py file, but the thing is I want to send a name from the main.py to the HelloWorld.py file and thats the thing I don't get how to do properly

Hey guys, I'm trying to learn how to use classes and work with seperate files, and I been reading about it and now I tryed making a little short "Hello World" thingy but I don't really understand the self. things, I thought I did but it's not working so I guess I diden't

Heres the code anyways:
HelloWorld.py

class HelloWorld :

        def __init__(self):

            print("Hi")

        def Talk(self ,name):

            self.nameA = name
            print("Hello",self.nameA)

Main.py

import HelloWorld

HelloWorld.HelloWorld.Talk("Kevin")

I get the error:

Traceback (most recent call last):
  File "D:\PygameSpel\Test med klasser\Main.py", line 3, in <module>
    HelloWorld.HelloWorld.Talk("Kevin")
TypeError: Talk() takes exactly 2 arguments (1 given)

what am I missing?

I have a qucik question for you guys. Is there a way to change the taskbar icon? Im done with my game and im gonna show it in class on tuesday 14 and I just thought it would be cool to have an icon that represents the game and not just the standard code bit.
I tried pygame.display.set_icon(icon) but that only changed the window icon in the taskbar its still the same onld standard one.

Okey solved the problem, Im officialy calling myself stupid lol!
the problem was that I was drawing the same thing twice, the only problem was that I hadent fixed it as I thought was still drawing out the background and the houses everytime I check to see wich mobs to blit. took that away and now it runs smooth, also moved the bliting of hp bar and life text and mute text to after the checking of wich mob to blit. Where I blit the player

the files

I have a serious problem with my game right now, after each kill I get the game starts lagging more and more. I thought it was because he was drawing the same thing twice and stuff and went trough some functions that was useless. But I cleaned it up as far as I can see anyways but it dident help. So now I turn to you guys, please help me, I really want to have enemies and not just one boss and I know that having one mayor file with code makes it a bit more difficoult then it would have been if I had used classes but please try with thie big chunk of code.

The code is quite large now but here it is:

# -*- coding: cp1252 -*-
import time
import pygame
from pygame.locals import*

pygame.init()

music = pygame.mixer.Sound('GameSoundtrack.wav')
music.play(1,0,10000)
vol = 1
Oldvol = 0
mute = False
Sfont = pygame.font.SysFont('Arial',20,False,True)
mutetxt = Sfont.render("Mute",1,(255,255,255))

screenWidth = 1018
screenHeight = 720
screen = pygame.display.set_mode((screenWidth,screenHeight))
facingLeft = False
clock = pygame.time.Clock()
fps = 50
shootLeft = False
shootRight = False
go = False

#Text
textSize = 55
font = pygame.font.SysFont('Arial',textSize,True,True)
Wintext = font.render("Congratulations, you have killed the evil boss!", 1, (0,255,0))
Losetext = font.render("You have failed this city!", 1, (255,0,0))
font2 = pygame.font.SysFont('Arial',40,True)
Lifetxt = font2.render("Life:",1,(255,255,255))

#Houses
house = pygame.image.load('Husen.png')

#Player
Player_facing_screen = pygame.image.load('GoodGuyFramPng.png')
Player_left = pygame.image.load('GubbeVanster.png')
Player_right = pygame.image.load('GubbeSidan.png')
Player = Player_facing_screen
PlayerRect = Player.get_rect()
Health = 100

#Boss
Boss_right = pygame.image.load('OndgubbeHoger.png')
Boss_left = pygame.image.load('OndgubbeVanster.png')
Bossen = ...

thanks man, way to easy lol feel abit shamed for even asking this question x)

Okey so I need help with how to make a health bar that starts out full with green color but then when I lose hp it get's smaller and smaller.
I have now that my hp is 100, and then when I get hit by a bullet from the boss it hurts me 25hp per hit, so need to make a health bar that looses 25 hp everytime I get hit by a bullet.

and just solved the problem

hit = None
    for index, bullet in enumerate(bullets):
        crect = pygame.Rect(bullet[0] - 3, bullet[1] - 3,3*2,3*2)
        shoot = pygame.draw.circle(screen,(255,255,255),(crect.center),3)
        if crect.colliderect(Boss):
            print("hit")
            hit = index
            del bullets[index]

by placing it in the same for loop as the shots and not making a function for it seperatly works

Ah I accedently put the hit = None outside the main loop, so changed it and now it works, the only problem I find now is more like a bug and either ill just have to live with it but would rather not to. When it removes the shoot it removes all that is shot, not just the one that hit

when I put this into my code and when the bullet hit the boss, then it makes an error saying:

del bullets[index]
IndexError: list assignment index out of range

Okey I fixed it, at line 183 where I draw the bullets I just added this

    for bullet in bullets:
        crect = pygame.Rect(bullet[0] - 3, bullet[1] - 3,3*2,3*2)
        shoot = pygame.draw.circle(screen,(255,255,255),(crect.center),3)
        if crect.colliderect(Boss):
            print('hit')

so now I can use the rect's hit box instead, however a new problem comes in hand. I don't know how to remove the shoot when they hit the boss. Is it possible to do it my way (the sloppy way) or do I have to make a function of some sort or even worse, a class?

Yeah I been thinking about using the get_rect() but I don't know how to do it and since im really unsecure about using classes, is there another way to make shoots that can hit if you use my code? Let's say we take away the shoot code I have right now and we start over, any sugestions then?

I was planing on using the Rect class, but just couldent find how to use it with the bullets, can't sort out how to do with the bullets since I can't find what their rect is

Im making a 2D platformer-ish shooter its really simple and not much effort put in it, it is my first game Im making in python and its for a school project and needs to be finnised really soon. Everything is working just fine but Im having problem with some collision detection with my shoots, I don't know how to make it. So would really appreciate any help I could get.

Feel free to tell me if anything is wrong or could be better and use this code for your own learnings.

And I know its a bit messy but I just want this to be finnished so havent done anything with classes or functions since Im not really that sure how to do them correct.

import time
import pygame
from pygame.locals import*

pygame.init()
screenWidth = 1018
screenHeight = 720
screen = pygame.display.set_mode((screenWidth,screenHeight))
facingLeft = False
clock = pygame.time.Clock()
fps = 50
shootLeft = False
shootRight = False

#Player img

Player_facing_screen = pygame.image.load('GoodGuyFramPng.png')
Player_left = pygame.image.load('GubbeVanster.png')
Player_right = pygame.image.load('GubbeSidan.png')
Player = Player_facing_screen
PlayerRect = Player.get_rect()

#Boss img
Boss_right = pygame.image.load('OndgubbeHoger.png')
Boss_left = pygame.image.load('OndgubbeVanster.png')
Bossen = Boss_left
Boss = Bossen.get_rect()

#commented out my first little try with shoots
#Shot
#shot_left = pygame.image.load('Shot.png')
#shot_right = pygame.image.load('Shot_right.png')
#shotIm = shot_right
#shot = shotIm.get_rect()

done = False
#Player variabels
x = 0
PlayerRect.y = screenHeight - 111
vx = 0
vy = 0
g = 2

onGround = False
jumping = False
moving = False

#Boss variabels
Boss.x = 500
Boss.y = screenHeight - 111 ...

Oh wow, the biggest problem I have with classes is that I can't really figure out what to put in wich class, but I guess that in my time frame I could probably learn that and get it working if it only tok you about four hours.

putting images in variables was a great idé, and it worked perfectly without calsses, I will probably not make this game with classes cause I should be done with this project in like 2 - 3 weeks, so will focus on that it works first hand. But my next game I will deffinetly try working with classes

So after changeing to clock.tick() it doesen't crash, so thanks for that. Have another question if you are interested.

at line 87 I have this to

    if not moving:
        Player = pygame.image.load('GoodGuyFramPng.png')

and at line 94

if event.type == KEYUP and (event.key == K_RIGHT or event.key == K_LEFT):
    moving = False

so this basicly makes the player face the screen when you arent moving, but the problem is if you are walking to the right and you press the left key to go to the left before you release the right. Then when you release the right key the character is facing the screen but walking to the left. It's not weird since the code is created that way, but I can't come up with a way to make it not do that. You have any sugestions on how to fix it?

For me it doesent give an error it just stops working, but I will make changes after what you guys just told me, I really appreciate the help and I will let you know if it works.

and feel free to take the code and do what you want with it :) it's only for a school project and Im the onlyone in my group of 5 that does the programming, the rest arent doing much, exept the one who drew the pictures.

Im doing this game for school and it's supposed to be finished real soon and then of course al the problems start coming up, the game now crashes, sometimes after a few seconds and sometimes directly on start. Don't really know why, a thought is that its doing to much at the same time, or doing something it doesent need to do. The code is verry badly done, should use classes and functions but I have really goten in to that since im really new to this (first game im doing on my own). So please, if anyone could help I would appreciate it, give me tips on what to improve and if someone could fix my problem that would be great!

The pics will be attached to this post

import time
import pygame
from pygame.locals import*

pygame.init()
screenWidth = 1018
screenHeight = 720
screen = pygame.display.set_mode((screenWidth,screenHeight))
facingLeft = False
#Player

Player = pygame.image.load('GoodGuyFramPng.png')
PlayerRect = Player.get_rect()

#Boss
BossIm = pygame.image.load('Boss.png')
Boss = BossIm.get_rect()

#Shot for use later
#shotIm = pygame.image.load('Bullet.png')
#shot = shotIm.get_rect()

counter = 1
secondsPerFrame = 1.0/30
done = False
#Player var
x = 0
y = 0
vx = 0
vy = 0
g = 2

onGround = False
jumping = False
moving = False

#Boss var
bx = 560
by = 0
goingLeft = True
goingRight = False
bvx = 0
bvy = 0
radius = 10
BonGround = False
Bjumping = False

while not done:
    delayTime = time.clock()

    #Comands
    for event in pygame.event.get():
        if ...