Daily Learner

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

Daily Learner

Post by Alacaster » March 11th, 2018, 6:17 pm

I am going to post small programs that I make. Care or don't care doesn't really matter to me. Feel free to critique or steal some code. Anyone that feels inclined to maybe learn something might find something useful here. (Or GitHub links)

Hey, even post your own code. Nothing more than 150 lines or so. Just a code melting pot of maybe interesting ideas.

I don't watch Chili's tutorials. I am learning from the "The C programming Language" By Brian W. Kernighan and Dennis M. Ritchie. Which in my opinion goes much more in depth and is much more useful than YouTube, Though Mr. Roberts videos are still a valid option I encourage all of you to read books, and lots of them.
  • :geek: The C programming Language By Brian W. Kernighan and Dennis M. Ritchie.

    :geek: Introduction to 3D GAME PROGRAMMING WITH DIRECTX® 11 Frank D. Luna

    :geek: The C+ + Programming Language Third Edition Bjarne Stroustrup
Post your code bois. (and girls)
Last edited by Alacaster on March 11th, 2018, 6:39 pm, edited 2 times in total.
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

Re: Dayily Learner

Post by Alacaster » March 11th, 2018, 6:34 pm

Fahrenheit to Celsius Converter

Uses functions Main() and FerhToCel()

Code: Select all

#include <stdio.h>

float FerhToCel(int Ferh);

int main() {

    short int i;
    int nr=0;
    #define START 200
    #define END -100
    #define NRES 20

    // Title
        printf("Fahrenheit\tCelsius");

    for(i=START; i>=END; i=(i-(START-END)/NRES))
    {
        if(nr>=NRES){break;}
        printf("\n%6.1d --------- %5.1f", i, FerhToCel(i));
        ++nr;
    }
    printf("\n\nResults: %d\n", nr);
    return 0;
}

float FerhToCel(int Ferh){
    return (5*((float)Ferh-32)/9);
}
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

User avatar
Radical
Posts: 38
Joined: January 15th, 2017, 9:16 pm
Location: Ontario

Re: Daily Learner

Post by Radical » March 11th, 2018, 11:03 pm

What brings you to the chili forum if you don't watch the tutorials? Why not something more popular like stackoverflow for example?

User avatar
krautersuppe
Posts: 91
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

Re: Daily Learner

Post by krautersuppe » March 12th, 2018, 12:59 am

Alacaster wrote: I don't watch Chili's tutorials. [..]Which in my opinion goes much more in depth and is much more useful than YouTube
Oh really?
I will play along and post here some of my console code in C.
This one is my "awesome" console traffic light simulator; as i didn't know how to use time functions in console i just made cursor write and delete an empty space 40000 times and count it as 1 second :)

Code: Select all

int main()
{
   
CONSOLE_CURSOR_INFO ci = {10,FALSE};//makes cursor transparent
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE) , &ci);
    int i=0;
    int a=10;
    char kreis ='*';
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE) , 0*16+12);//for red light
    printf("  %c\n %c%c%c\n%c%c%c%c%c\n %c%c%c\n  \n  %c\n",kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis);
    for(i=0;i<=400000;i++){
            if(i%40000==0){ //modulus division 
                system("cls"); //Deletes console content
                    printf("  %c\n %c%c%c\n%c%c%c%c%c\n %c%c%c  \n  %c\n",kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis);
                     printf("\tTime: %ds \n",a);
                    --a;
            }
    printf(" ");//writes a space on console screen(right after "traffic light"
    printf("\b"); //sets cursor 1 character to left
    }

    system("cls");
    a=5;
    printf("\n\n\n\n\n\n\n\n\n  %c\n %c%c%c\n%c%c%c%c%c\n %c%c%c  \n  %c\n",kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis);
    i=0; //for yellow light
    for(i=0;i<=200000;i++){
            if(i%40000==0){
                system("cls");
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE) , 0*16+14);//yellow
                printf("\n\n\n\n\n\n\n\n\n  %c\n %c%c%c\n%c%c%c%c%c\n %c%c%c  \n  %c\n",kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis);
                printf("\tTime: %ds \n",a);//time remaining
                --a;
    printf(" ");
    printf("\b");
    }
    system("cls");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE) , 0*16+10);//green light
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  %c\n %c%c%c\n%c%c%c%c%c\n %c%c%c  \n  %c\n",kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis);
    a=10;
    i=0;
    for(i=0;i<=400000;i++){
        if(i%40000==0){
                system("cls");
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  %c\n %c%c%c\n%c%c%c%c%c\n %c%c%c  \n  %c\n",kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis,kreis);
                printf("\tTime: %ds \n",a);
                --a;
            }
    printf(" ");
    printf("\b");}
    system("cls");
   
    return 0;
}
DSU
Discord: dsu1, GitHub: https://github.com/DSpUz

User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

Re: Daily Learner

Post by Alacaster » March 12th, 2018, 3:19 am

This does powers, I don't know how but I found it on the internet and it's amazing.

Code: Select all

float power( a, n ){
    if (n == 0){
        return(1);
    }
    int x;
    x = power(a,n/2);

    if ( (float)n/2 <= (n/2) ){

        return(x*x);
        } else {

        return(a*x*x);
        }
}
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

Re: Daily Learner

Post by Alacaster » March 12th, 2018, 3:23 am

Radical wrote:What brings you to the chili forum if you don't watch the tutorials? Why not something more popular like stackoverflow for example?
This place has a looser feel and a friendlier atmosphere. You don't get nagged on for talking about your personal life etc. And chili is awesome
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

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

Re: Daily Learner

Post by Zedtho » March 12th, 2018, 7:00 am

I recently played risk and thought rolling the dice got really tedious towards the end. I tried to automate that:

Code: Select all

// Risk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>


int main()
{
	int AttackerSoldierAmount;
	int AttackerSoldierUse;
	int DefenderSoldierAmount;
	//Initialization of the Soldiers
	std::cout << "Please enter the amount of soldiers of the attacker: \n"; 
	std::cin >> AttackerSoldierAmount;
	while (AttackerSoldierAmount < 2)
	{
		std::cout << "Insufficient amount of soldiers, please enter a larger amount: \n";
		std::cin >> AttackerSoldierAmount;
	}
	const int INITIALSTANDIGNATTACKSOLDIERS = AttackerSoldierAmount;

	std::cout << "Please enter the amount of soldiers that the attacker wants to use in his attack: \n";
	std::cin >> AttackerSoldierUse;
	while(AttackerSoldierUse + 1 > AttackerSoldierAmount)
	{
		std::cout << "The amount of soldiers you want to use is too large, please insert a new amount: \n";
		std::cin >> AttackerSoldierUse;
	}
	const int INITIALATTACKSOLDIERS = AttackerSoldierUse;

	std::cout << "Please enter the amount of soldiers of the defender: \n";
	std::cin >> DefenderSoldierAmount;
	while (DefenderSoldierAmount <= 0)
	{
		std::cout << "The amount of defenders you want to use is too small, please enter a larger amount: \n";
		std::cin >> DefenderSoldierAmount;
	}
	const int INITIALDEFENDSOLDIERS = DefenderSoldierAmount;

	//Math for the soldiers
	while (DefenderSoldierAmount > 0 && AttackerSoldierUse > 0)
	{

		int AttackDice1 = rand() % 6 + 1;
		int AttackDice2 = 0;
		int AttackDice3 = 0;
		int DefendDice1 = rand() % 6 + 1;
		int DefendDice2 = 0;
		int LargestAttackDiceRoll = NULL;
		int SecondLargestAttackDiceRoll = NULL;
		int LargestDefenderDiceRoll = NULL;
		int SecondLargestDefenderDiceRoll = NULL;
	

		if (AttackerSoldierUse > 1)
		{
			AttackDice2 = rand() % 6 + 1;
		}
		if (AttackerSoldierUse > 2)
		{
			AttackDice3 = rand() % 6 + 1;
		}
		if (DefenderSoldierAmount > 1)
		{
			DefendDice2 = rand() % 6 + 1;
		}
		//LargestAttackDiceRoll initialization
		if (AttackDice1 >= AttackDice2 && AttackDice1 >= AttackDice3)
		{
			LargestAttackDiceRoll = AttackDice1;
			AttackDice1 = 0;
		}else 
		if (AttackDice2 >= AttackDice1 && AttackDice2 >= AttackDice3)
		{
			LargestAttackDiceRoll = AttackDice2;
			AttackDice2 = 0;
		}else 
		if (AttackDice3 >= AttackDice1 && AttackDice3 >= AttackDice2)
		{
			LargestAttackDiceRoll = AttackDice3;
			AttackDice3 = 0;
		}
		//SecondLargestAttackDiceRoll initialization 
		if (AttackDice1 >= AttackDice2 && AttackDice1 >= AttackDice3)
		{
			SecondLargestAttackDiceRoll = AttackDice1;
			AttackDice1 = 0;
		}else
		if (AttackDice2 >= AttackDice1 && AttackDice2 >= AttackDice3)
			{
				SecondLargestAttackDiceRoll = AttackDice2;
				AttackDice2 = 0;
			}else
		if (AttackDice3 >= AttackDice1 && AttackDice3 >= AttackDice2)
				{
					SecondLargestAttackDiceRoll = AttackDice3;
					AttackDice3 = 0;
				}
		//DefenderDiceRoll initialization
		if (DefendDice1 >= DefendDice2)
			{
				LargestDefenderDiceRoll = DefendDice1;
				SecondLargestDefenderDiceRoll = DefendDice2;
				DefendDice1 = 0;
			}
		else
			{
				LargestDefenderDiceRoll = DefendDice2;
				SecondLargestDefenderDiceRoll = DefendDice1;
				DefendDice2 = 0;
			}

		if (LargestAttackDiceRoll <= LargestDefenderDiceRoll)
		{
			AttackerSoldierAmount--;
			AttackerSoldierUse--;
		}else
		{
			DefenderSoldierAmount--;
		}
		if (AttackerSoldierUse >= 1 && DefenderSoldierAmount >= 1)
		{
			if (SecondLargestAttackDiceRoll <= SecondLargestDefenderDiceRoll)
			{
				AttackerSoldierAmount--;
				AttackerSoldierUse--;
			}
			else
			{
				DefenderSoldierAmount--;
			}
		}
		
	}
	if (DefenderSoldierAmount == 0)
	{
		std::cout << "While the Attackers lost " << INITIALATTACKSOLDIERS - AttackerSoldierUse << " soldiers, the Attackers won!" << "\n" << INITIALSTANDIGNATTACKSOLDIERS - INITIALATTACKSOLDIERS + AttackerSoldierUse << "soldiers are left alive on the attackers side.";

	}
	else
	{
		std::cout << "While the Defenders lost " << INITIALDEFENDSOLDIERS - DefenderSoldierAmount << " soldiers, the Defenders won!" << "\n" << DefenderSoldierAmount << " soldiers are left alive on the defenders side.";
	}
	std::cout << "\n\n\n\n Please insert something and type enter to quit";
	std::cin >> AttackerSoldierAmount;
    return 0;
}
Anyone catch a logic error?

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

Re: Daily Learner

Post by chili » March 12th, 2018, 8:50 am

This Risk code could be immensely simplified with proper application of the C++ standard library.
Chili

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

Re: Daily Learner

Post by albinopapa » March 13th, 2018, 7:21 am

Melting pot, sure I'll join.

Not sure how useful this 'bit' of code is outside of helping with blending colors, but here ya go.

__m128i is the SSE data type that deals with integral data

Unpacked_8_m128i_16 was the worst and only name that came to mind. When I learned SSE from chili, he pointed me in the direction of splitting the four pixels and expanding them into two registers containing two pixels each. When you unpack using _mm_setzero_si128() ( which generates a 0 filled register ) it is like promoting a char to a short, ex: 0x05 to 0x0005. Then you can use the 16 bit operations on those two registers. _mm_mullo_epi16 for example, multiplies the first 8 bits of A and the first 8 bits of B and produces a 16 bit result.

The Pack function calls the _mm_packus_epi16 intrinsic which packs the lo and hi elements back into one, saturating or clamping the values of each 8 bits to 255 before packing.

Code: Select all


class Unpacked_8_m128i_16
{
public:
	Unpacked_8_m128i_16( __m128i Value )
		:
		lo( _mm_unpacklo_epi8( Value, _mm_setzero_si128() ) ),
		hi( _mm_unpackhi_epi8( Value, _mm_setzero_si128() ) )
	{}

	Unpacked_8_m128i_16 operator*( __m128i multiplier )const
	{
		return {
			_mm_mullo_epi16( lo, other ),
			_mm_mullo_epi16( hi, other )
		};
	}
	Unpacked_8_m128i_16 operator+( Unpacked_8_m128i_16 other )const
	{
		return {
			_mm_add_epi16( lo,other.lo ),
			_mm_add_epi16( hi,other.hi )
		};
	}
	Unpacked_8_m128i_16 operator>>( const int ShiftCount )const
	{
		return {
			_mm_srli_epi16( lo, ShiftCount ),
			_mm_srli_epi16( hi, ShiftCount )
		};
	}

	__m128i Pack()const
	{
		return _mm_packus_epi16( lo, hi );
	}

private:
	Unpacked_8_m128i_16( __m128i lo, __m128i hi )
		:
		lo( lo ), hi( hi )
	{}

private:
	__m128i lo, hi;
};
Usage: Lerp between colors

Code: Select all

// First, set the blend factor value between 0 and 255
__m128i blendFactor = _mm_set1_epi16( 128 );

// Then, set the inverse blend factor to 255 - blendFactor
__m128i invBlendFactor = _mm_set1_epi16(255);
invBlendFactor = _mm_sub_epi16( invBlendFactor, blendFactor );

// Finally, create the Unpacked_8_m128i_16 objects for the source and destination colors and perform
// the operations like you would in x86 code.  The >> 8 is the same as dividing by 256, SSE integer instructions do not have division operations, only bit shifts.

Unpacked_8_m128i_16 result = ( 
	( Unpacked_8_m128i_16( SrcColor ) * InvBlendFactor ) + 
	( Unpacked_8_m128i_16( DstColor ) * BlendFactor ) ) >> 8;

// Once you have completed your blending operation, pack the elements to get the resulting four blended pixels.
return result.Pack();
The Windows API and maybe others has something called LARGE_INTEGER, which is just two ints (lo,hi) and is worked on like a long long int ( 64 bit int ). I could call this LARGE_REGISTER, but meh. I also considered m256, but that is confusing as AVX has the __m256i integral type and really, it's just expanding the four elements into two registers. Have any better names, please share lol.

As stated, I don't know of any other uses, but it should be general enough to be useful elsewhere.
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

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

Re: Daily Learner

Post by Zedtho » March 13th, 2018, 7:34 pm

chili wrote:This Risk code could be immensely simplified with proper application of the C++ standard library.
Yeah I think using swap a few times and other things could make it look a lot nicer, I was quite lazy that morning. I'll clean it up when I have the time, thanks for the feedback :D

Post Reply