/*
C# 언어 대리자 개념은 최고 수준의 언어 지원 및 해당 개념 관련 형식 안정성을 제공합니다.
함수 포인터는 호출 규칙을 더욱 세부적으로 제어해야 하는 유사한 시나리오용으로 C# 9에 추가되었습니다. 
대리자와 연결된 코드는 대리자 형식에 추가된 가상 메서드를 사용하여 호출됩니다.
*/

/*----------------------------------------------------------------------------------
>>대리자

1.  ElapsedEventHandler 대리자
1.1 public delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e);

>>대리자 호출

1.  Timer.Elapsed 이벤트
1.1 public event System.Timers.ElapsedEventHandler Elapsed;
----------------------------------------------------------------------------------*/


c# 객체지향 언어이긴 하지만 함수지향형 언어가 갖는 특징을 갖기도 합니다.
이를테면 c언어의 함수 포인터에 해당하는 기능이 c#에도 있다는 겁니다.
오히려, c언어의 함수포인터보다 기능이 더 강화되었습니다.

1. 대리자(Delegate)

c# 초기 버젼부터 있던 기능입니다. c언어의 함수포인터를 그대로 차용한거나 다름없습니다.
메소드의 위치를 간직하고 있으면서, 그 메서드를 실행해 주는 역할을 합니다.

Ex)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TEST
{
class Program
{
delegate void DelegateMethod(int a);

static void method1(int a){Console.WriteLine("method1 called!! a = {0}", ++a);}
static void method2(int a){Console.WriteLine("method2 called!! a = {0}", ++a);}
static void method3(int a){Console.WriteLine("method3 called!! a = {0}", ++a);}
static void method4(int a){Console.WriteLine("method4 called!! a = {0}", ++a);}
static void method5(int a){Console.WriteLine("method5 called!! a = {0}", ++a);}
static void method6(int a){Console.WriteLine("method6 called!! a = {0}", ++a);}
static void method7(int a){Console.WriteLine("method7 called!! a = {0}", ++a);}
static void method8(int a){Console.WriteLine("method8 called!! a = {0}", ++a);}
static void method9(int a){Console.WriteLine("method9 called!! a = {0}", ++a);}

static void Main()
{
DelegateMethod method = null;

method += method1;
method += method2;
method += method3;
method += method4;
method += method5;
method += method6;
method += method7;
method += method8;
method += method9;

method(0);
}
}
}


결과) 동시에 실행이 된다고 볼수 있다. a의 값을 주목

D:\tmp\console>sample.exe
method1 called!! a = 1
method2 called!! a = 1
method3 called!! a = 1
method4 called!! a = 1
method5 called!! a = 1
method6 called!! a = 1
method7 called!! a = 1
method8 called!! a = 1
method9 called!! a = 1
D:\tmp\console>

/*Handler
- DoWorkEventHandler
- RunWorkerCompletedEventHandler
*/
using System.ComponentModel

 

System.ComponentModel 네임스페이스

구성 요소와 컨트롤의 런타임 및 디자인 타임 동작을 구현하는 데 사용되는 클래스를 제공합니다. Provides classes that are used to implement the run-time and design-time behavior of components and controls. 이 네임스

docs.microsoft.com

class TrisCls
{
     int xpos,ypos;
     BackgroundWorker sendWork;

    public void running()
    {
        ConsoleKeyInfo keyinfo;

        init();
        while(true)
        {
            //add
        }
    }
    void init()
    {
        xpos=ypos=0;
       
        sendWorker = new BackgroundWorker();
        sendWorker.DoWork += new DoWorkEventHandler(sendWorker_DoWork);
        sendWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(sendWorker_RunWorkerCompleted);
    }
    void DoWorkEventHandler(object sender, DoWorkEventArgs e)
    {
         Console.WriteLine("DoWorkEventHandler>>" + xpos.ToString("0000") );
         xpos++;
    }
    void sendWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("sendWorker_RunWorkerCompleted>>" + ypos.ToString("0000") );
        ypos++;
    }
}

int exchange_check()
{
int ii,kk,ff,tmp;
int[,] tristmp = new int[MAPY,MAPX];
int[,] designtmp = new int[ARR,ARR];

for(ii=0; ii<MAPY; ii++)
for(kk=0; kk<MAPX; kk++)
{
tristmp[ii,kk]=tris[ii,kk];
}

for(ii=0; ii<ARR; ii++)
for(kk=0; kk<ARR; kk++)
{
designtmp[ii,kk]=design[ii,kk];
}

for(ii=0; ii<ARR; ii++)
for(kk=0; kk<ARR; kk++)
{
if(ypos+ii>MAPY-1) continue;
if(xpos+kk>MAPX-1) continue;
if(xpos+kk<0) continue;

if(designtmp[ii,kk]==1) tristmp[ypos+ii,xpos+kk]=0;
}

//exchange//exchange//exchange//exchange//exchange
//exchange//exchange//exchange//exchange//exchange
//exchange//exchange//exchange//exchange//exchange
//exchange//exchange//exchange//exchange//exchange

/*
{0,0},{0,1},{0,2},{0,3},
{1,0},{1,1},{1,2},{1,3},
{2,0},{2,1},{2,2},{2,3},
{3,0},{3,1},{3,2},{3,3},
*/

for(ii=0; ii<3; ii++)
{
//add
}
tmp=designtmp[1,1];
designtmp[1,1]=designtmp[1,2];
designtmp[1,2]=designtmp[2,2];
designtmp[2,2]=designtmp[2,1];
designtmp[2,1]=tmp;

for(ii=0; ii<ARR; ii++)
for(kk=0; kk<ARR; kk++)
{
if(ypos+ii>MAPY-1) continue;
if(xpos+kk>MAPX-1) continue;
if(xpos+kk<0) continue;

if(designtmp[ii,kk]==1) tristmp[ypos+ii,xpos+kk] += 1;
}

ff=0;
for(ii=0; ii<ARR; ii++)
for(kk=0; kk<ARR; kk++)
{
if(ypos+ii>MAPY-1) continue;
if(xpos+kk>MAPX-1) continue;
if(xpos+kk<0) continue;

if(tristmp[ypos+ii,xpos+kk] > 1) ff++;
}

if(ff > 0)
{
return(FAIL);
}
else
{
for(ii=0; ii<MAPY; ii++)
for(kk=0; kk<MAPX; kk++)
{
tris[ii,kk]=tristmp[ii,kk];
}

for(ii=0; ii<ARR; ii++)
for(kk=0; kk<ARR; kk++)
{
design[ii,kk]=designtmp[ii,kk];
}
}
return(SUCC);
}

/*TrisPackage.Tris/draw();*/

void draw()
{
    string formattext="";
    string succsign="{}";
    string failsign="..";
    int ii,kk,ff=0;

    now=DateTime.Now;
    ____draw(0,ref ff, "Time:[" + now.ToString("yyyy-MM-dd HH:mm:ss") + "]");
    for(ii=0; ii<MAPY-1; ii++)
    {
        for(kk=1; kk<MAPX-1; kk++)
        {
            if(tris[ ii,kk] == 1) formattext=formattext+succsign;
            else formattext=formattext+failsign;
        }
        ____draw(0,ref ff,formattext);
    }
    ____draw(0,ref ff,"Score:[" + score.ToString("00000000") + "]");
}
void ____draw(int x,ref int y, string str)
{
    try
    {
        Console.SetCursorPosition(x,y);
        y++;
        Console.Write(str);
    }
    catch(ArgumentOutOfRangeException e)
    {
        Console.WriteLine(e.Message);
    }
}
    
    

/*TrisPackage.Tris/running();*/

public void running()
{
    int rc;

    init();
    while(true)
    {
        keyinfo = Console.ReadKey(true);

        if(keyinfo.Key==ConsoleKey.RightArrow) rc=common_check(D_RIGHT);
        if(keyinfo.Key==ConsoleKey.LeftArrow) rc=common_check(D_LEFT);
        if(keyinfo.Key==ConsoleKey.Enter) rc=direct_check();
        if(keyinfo.Key==ConsoleKey.Escape) break;

        draw();
    }
    stop();
}

 

/*TrisPackage.Tris/check_horizon();*/

int check_horizon()
{
    int ii,kk,ff;

    for(ii=0; ii<MAPY-1; ii++)
    {
        ff=0;
        for(kk=1; kk<MAPX-2; kk++)
        {
            if(tris[ii, kk] != 0)
            {
                if(tris[ii, kk] == tris[ii, kk + 1]) ff++;
                else break;
            }
        }
        if(ff == MAPX-3)
        {
            for(kk=1; kk<MAPX-1; kk++) delete_column(ii,kk);

            score++;
            return(SUCC); 
        }
    }
    //NOT FOUND,Then FAIL!!
    //NOT FOUND,Then FAIL!!
    return(FAIL); 

'c# 언어' 카테고리의 다른 글

TrisPackage.Tris/draw();____draw();  (0) 2021.11.19
TrisPackage.Tris/running();  (0) 2021.11.19
TrisPackage.Tris/delete_column();  (0) 2021.11.19
TrisPackage.Tris/definition/variables/controls  (0) 2021.11.17
TrisPackage.Tris/common_check();  (0) 2021.11.17

/*TrisPackage.Tris/delete_column();*/

void delete_column(int y, int x)
{
    int ii;

    for(ii=y; ii >= 1; ii--)
    {
        tris[ii, x] = tris[ii-1, x];
    }
}

'c# 언어' 카테고리의 다른 글

TrisPackage.Tris/running();  (0) 2021.11.19
TrisPackage.Tris/check_horizon();  (0) 2021.11.19
TrisPackage.Tris/definition/variables/controls  (0) 2021.11.17
TrisPackage.Tris/common_check();  (0) 2021.11.17
TrisPackage.Tris/check_horizon();  (0) 2021.11.17

/*TrisPackage.Tris/definition/variables/controls*/
class TrisCls
{
    //definition
    const int MAPY=22; //final int MAPY=22; //#define MAPY 22
    const int MAPX=31;
    //variables
    int[,] tris = new int[MAPY,MAPX]; //int[][] tris = new int[MAPY][MAPX]; //int tris[MAPY][MAPX];
    //controls
    Timer tm; //class
    Random rr; //class
    ConsoleKeyInfo keyinfo; //structure
    DateTime now; //structure
    //method
    public void running()
    {
        //add code
    }
}

'c# 언어' 카테고리의 다른 글

TrisPackage.Tris/check_horizon();  (0) 2021.11.19
TrisPackage.Tris/delete_column();  (0) 2021.11.19
TrisPackage.Tris/common_check();  (0) 2021.11.17
TrisPackage.Tris/check_horizon();  (0) 2021.11.17
TrisPackage.Tris/tris_time_tick();  (0) 2021.11.17

+ Recent posts