Noob learns to code in 3 months

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 18th, 2017, 10:21 pm

Bit hung over today but trying to get at least some coding in there
Fixed a bug with the scoring in simon says,

Recursion practice

Problem 1, from the book
Spoiler:
You're working at a company. The salary your first year is 15k a month (swedish book guys :p)
After that, every year you get a 4% increase on your salary + another 400 sek.
Write a function that calculates the salary after x number years. The function should be recursive and only take the number of years as argument.
My solution

Code: Select all

int Increase(int years)
{
	if (years == 1)
	{
		return 15000;
	}
	return Increase(years - 1) * 1.04 + 400;
}
Problem 2

https://community.topcoder.com/stat?c=p ... nt&pm=6011

my function

Code: Select all

int NumTrucks(int numCrates, int loadSize)
{ 
	int pile1 = numCrates / 2;
	int pile2 = numCrates - pile1;
	if (numCrates <= loadSize)
	{
		return 1;
	}
	else if (pile2 <= loadSize)
	{
		return 2;
	}
	else if (pile1 <= loadSize)
	{
		return NumTrucks(pile2, loadSize) + 1;
	}
	return NumTrucks(pile1, loadSize) + NumTrucks(pile2, loadSize);
}
I really am bad at this... this second one took me quite a bit of time before I made it work.

@Chili
Thanks chili :) I guess I'm pretty stubborn/persistent or whatever which might be good.
13 is pretty good :D I managed to get to 18 ones before my brain exploded

@Zedtho
I'm not sure what you mean Zed.
I divide by 6 since I want to find out the odds. The value returned will be less than 1 (0.0154) 1.54% so I can't use integers.
The function is dumb and just for practice so it's only supposed to be called with 6 as parameter (one for each side of a dice)
the reason 0 returns 1 is that when I reach number 1 on the dice, I want to return 1 / 6
The function call in that return statement will now have 0 as argument so it will return 1 which means 1 will return 1 * 1/6 which is the same thing as 1/6.

I could however change it like this
if (n <= 1.0f)
{
return 1.0f / 6;
}

which would give the same result.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 19th, 2017, 12:35 am

Want to try and make a xonix clone. But can't really figure out how to go about it...
Any tips? Should I have all the tools needed for this at this point?

Image

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » June 19th, 2017, 3:25 am

Should be doable with the stuff shown so far in the tutorials. I would represent territory as basically a surface (array of pixels). Then drawing is just copying the pixels to the screen surface (sprite blt operation).
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 19th, 2017, 12:17 pm

chili wrote:Should be doable with the stuff shown so far in the tutorials. I would represent territory as basically a surface (array of pixels). Then drawing is just copying the pixels to the screen surface (sprite blt operation).
Not 100% how it'll work with the "collapse" after you cut off an area.
But wanna give it a try. However I'm having issues with keyboard. Event is private, so it's inaccessible. Changed it to public for now, not sure if this is fine

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » June 19th, 2017, 1:27 pm

Wait, how is event private?? That can't be right, unless I fucked something over in the last patch. :P Can you give me a link or show me the code where it is private?

Edit:

Nevermind, I just checked the repo. Fuck me, how did that happen :P
Chili

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » June 19th, 2017, 1:34 pm

Fixed now, thanks for the head up bro.
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 19th, 2017, 2:10 pm

Haha great :D I thought I was going insane. Even opened up space balls to check how I managed to pull it off for that project and noticed event was public in there.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 19th, 2017, 10:26 pm

Study update:

Watched this video and tried to create xonix based on it.
https://www.youtube.com/watch?v=_5W5sYjDBnA

Here's the result
https://github.com/Yumtard/xonix

I did not enjoy his coding. I don't know if it's just me but a lot of it seemed so unclear. And I would've liked more classes.
Not sure how much I actually learned trying to just follow him, I think at least a little since I had to analyze the code at some points to try and understand how it worked. Like the recursive drop function.

Might give it a try to make the code "nicer" maybe adding classes somehow and change from ints to enums to represent different tiles etc.
And also try and make it more of a game (win conditions and adding a new enemy each level)

I also signed up for a site which has different stuff like algorithm challenges. Solved a few of them but didn't like how it was determined wether I was correct or not, it was too easy to pass.
For example they wanted me to make a function that takes an array of 5 ints and then couts the biggest possible sum when adding for of those numbers and the smallest possible sum when adding 4 of those numbers.
the sample input said 1,2,3,4,5
sample output said: 14, 10

So just for lolz I wrote nothing but: cout << 14 << " " << 10;
this matched the expected output and I cleared the challenge lol

If anyone knows any good sites with code challenges for practice let me know

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 20th, 2017, 4:03 am

Phew, restructuring this code is proving pretty difficult :p

Feels a bit nicer now though, at least in my eyes.
Let me know if it seems ok.
Branch "shipitholaballa"
https://github.com/Yumtard/xonix

Will finish the game tomorrow... or later today or whatever #uptil6am

User avatar
Zedtho
Posts: 189
Joined: February 14th, 2017, 7:32 pm

Re: Noob learns to code in 3 months

Post by Zedtho » June 20th, 2017, 4:52 am


Post Reply