Learning basics

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Drug Mile
Posts: 6
Joined: February 27th, 2020, 1:33 pm

Learning basics

Post by Drug Mile » February 27th, 2020, 4:54 pm

Hi to all of you and especially Mr. Chilli,

Currently I'm at tutorial 9 (beginner) and I love this way of teaching. I really learned a lot so far.

I'm curious, where will this tutorials lead me? I desire to create a building simulation game, like an old SimCity. I want to be able to create custom terrains, with items that I can place there, with my own economy and other rules. Not so much of graphics or other modern stuff, I'm mostly interested in creating rules of the game and a lot of different objects that interact by themself following those rules.

At the moment I have no idea how to do it but it motivates me a lot so I keep feeding this dream.

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

Re: Learning basics

Post by chili » February 28th, 2020, 6:01 am

The tutorials up to advanced teach the core concepts required for all programming, and the main features of the C++ language. After advanced they focus mostly on 3D graphics techniques and APIs.
Chili

Drug Mile
Posts: 6
Joined: February 27th, 2020, 1:33 pm

Re: Learning basics

Post by Drug Mile » February 28th, 2020, 7:39 am

Wonderful, I hope to reach those levels one day.

Drug Mile
Posts: 6
Joined: February 27th, 2020, 1:33 pm

Re: Learning basics

Post by Drug Mile » March 5th, 2020, 2:09 pm

I watched Beginner Tutorial 14a and only taking notes what I need to do to create Snake. Then I closed youtube and tried to code by myself. At the end, I get error in Snake Constructor:
the default constructor of "Snake::Segment" cannot be referenced -- it is a deleted function
.

Could someone take a look and tell me where did I go wrong? Do I need to upload whole Framework also? BTW, Visual Studio 17, if important.

Code: Select all

#pragma once

#include "Board.h"
#include "Location.h"

class Snake
{
public:
	Snake(const Location& loc);
private:
	class Segment
	{
	private:
		Location loc;
		Color c;
	public:
		void Follow(const Segment& Next);
		void Draw(Board& brd);
		void InitBody();
		void InitHead(const Location& getLoc);
		void MoveBy(const Location& delta_loc);
	};
private:
	static constexpr int nSegmentsMax = 100;
	Segment segments[nSegmentsMax];
	int nSegments = 1;
	static constexpr Color headColor = Colors::Yellow;
	static constexpr Color bodyColor = Colors::Green;
public:
	void MoveBy(const Location& delta_loc);
	void Grow();
	void Draw();
};

Code: Select all

#include "Snake.h"
#include "Location.h"

Snake::Snake(const Location & loc)
{
	segments[0].InitHead(loc);

}

void Snake::MoveBy(const Location & delta_loc)
{
	for (int i = nSegments; i > 0; i--)
	{
		segments[i].Follow(segments[i - 1]);
	}
	segments[0].MoveBy(delta_loc);
}

void Snake::Grow()
{
	if (nSegments < nSegmentsMax)
	{
		nSegments++;
	}
}

void Snake::Draw()
{
	for (int i = 0; i < nSegments; i++)
	{
		segments[i].Draw;
	}
}


void Snake::Segment::Follow(const Segment & Next)
{
	loc = Next.loc;
}

void Snake::Segment::Draw(Board & brd)
{
	brd.drawCell(loc, c);
}

void Snake::Segment::InitBody()
{
	c = Snake::bodyColor;
}

void Snake::Segment::InitHead(const Location & getLoc)
{
	loc = getLoc;
	c = Snake::headColor;
}

void Snake::Segment::MoveBy(const Location & delta_loc)
{
	loc.Add(delta_loc);
}

Drug Mile
Posts: 6
Joined: February 27th, 2020, 1:33 pm

Re: Learning basics

Post by Drug Mile » March 6th, 2020, 8:54 am

Found it, loc was undefined.

I replaced this:

Code: Select all

Location loc;
With this:

Code: Select all

Location loc = { 10,10 };

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Learning basics

Post by albinopapa » March 11th, 2020, 4:01 pm

Well done with the fix.

The reason this probably happened though is that Location probably doesn't have a default constructor.

A default constructor is a constructor that either has no parameters or all the parameters have default values.
Example:

Code: Select all

class Location
{
public:
     Location();  // Normal default constructor
     Location( int x_ = 0, int y_ = 0 );  // Abnormal default constructor

public:
     int x,y;
};
You can also have the compiler generate a default constructor for you by declaring the "normal" default constructor followed by "= default;"

Location() = default;

The only time this is really needed is when you have other user defined constructors, like the "abnormal" default constructor above, but without default parameters.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Drug Mile
Posts: 6
Joined: February 27th, 2020, 1:33 pm

Re: Learning basics

Post by Drug Mile » March 14th, 2020, 1:53 pm

Thank you, I believe you are right. In tutorials that followed there are a lot of examples with default constructors. If I did not have this problem myself, probably I would not understand why to declare default constructor when we already have a good one.

Now I tried to change my code to use default constructors when necessary and it works much better.

Post Reply