/*console 3*3 tris game source
*cmd>type *.cs | findstr formattext
*/
using System;
using System.IO;
using System.Timers;
namespace ____java_is_package
{
class Program
{
public static void Main(string[] args)
{
____tris_overloading_cls nm = new ____tris_overloading_cls();
nm.running();
}
}
class ____tris_overloading_cls
{
//properties
const int TRUE=1;
const int FAIL=0;
const int D_RIGHT=1000;
const int D_LEFT=1001;
const int D_DOWN=1002;
const int MAPY=22;
const int MAPX=22;
const int ARR=3;
const int MAXDESIGN=10;
int xpos,ypos,hyun_design,next_design;
int[,] tris=new int[MAPY,MAPX];
int[,] design=new int[ARR,ARR];
int[,,] realdesign=new int[,,]
{
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
{{1,1,1},{1,1,1},{1,1,1}},
};
Timer tm;
//method
void draw_tris()
{
string formattext="";
string spacetext=" ";
int ii,kk;
for(ii=0; ii<MAPY-1; ii++)
{
formattext=formattext+spacetext;
for(kk=1; kk<MAPX-1; kk++)
{
if(tris[ii,kk]==1) formattext=formattext+"U";
else formattext=formattext+"_";
}
formattext=formattext+"\n";
}
Console.SetCursorPosition(0,0);
Console.Write(formattext);
}
int common_check(int direction)
{
int[,] tris_tmp=new int[MAPY,MAPX];
int ii,kk,ff;
for(ALL)for(ALL)tris_tmp[ii,kk]=tris[ii,kk];
for(ARR)for(ARR)if(design[ii,kk]==1) tris_tmp[ypos+ii,xpos+kk]=0;
if(direction==D_RIGHT) xpos++;
if(xpos+2>MAX-1) xpos--; //Array's MAX-1 Access Possible
if(direction==D_LEFT) xpos--;
if(xpos+0<0) xpos++ //Array's 0 Access Possible
if(direction==D_DOWN) ypos++;
for(ARR)for(ARR)if(design[ii,kk]==1) tris_tmp[ypos+ii,xpos+kk]=1;
ff=0;
for(ARR)for(ARR)if(tris_tmp[ypos+ii,xpos+kk]>1) ff++;
if(ff > 0)
{if(direction==D_RIGHT) xpos--;
if(direction==D_LEFT) xpos++;
if(direction==D_DOWN) ypos--; return(FAIL)}
else
{
for(ALL)for(ALL)tris[ii,kk]=tris_tmp[ii,kk];}
}
return(TRUE);
}
void init()
{
int ii,kk;
tm=new Timer();
tm.Elapsed += new ElapsedEventHandler(____time_tick);
tm.Interval=500;
tm.Start();
for(ALL)for(ALL)tris[ii,kk]=0;
for(ii=0; ii<MAPY; ii++) tris[ii,0]=1; tris[ii,MAPX-1]=1;
for(kk=0; kk<MAPX; kk++) tris[MAPY-1,kk]=1;
xpos=MAPX/2; ypos=0; score=0;
Random rr=new Random(); hyun_design=rr.Next() % MAXDESIGN; next_design=rr.Next() % MAXDESIGN;
for(ARR)for(ARR) design[ii,kk]=realdesign[hyun_design,ii,kk];
for(ARR)for(ARR) tris[ypos+ii,xpos+kk]=design[ii,kk];
Console.Clear(); draw_tris();
}
int enter_check()
{
int ii,kk,rc;
while(true)
{
rc=common_check(D_DOWN);
if(rc==FAIL) break;
draw_tris();
}
while(true)
{
if(check_horizon()==FAIL) break;
}
xpos=MAPX/2; ypos=0;
hyun_design=next_design;
Random rr=new Random(); next_design=rr.Next() % MAXDESIGN;
for(ARR)for(ARR) design=realdesign[hyun_design];
for(ARR)for(ARR) tris[ypos+ii,xpos+kk]=design[ii,kk];
draw_tris();
return(TRUE);
}
void ____time_check(object sender, ElapsedEventArgs e)
{
int ii,kk,rc;
draw_tris();
rc=common_check(D_DOWN);
if(rc==FAIL)
{
if(ypos==0) stop();
else
{
while(true)
{
//no detect!!, while loop break!!
//no detect!!, while loop break!!
if(check_horizon()==FAIL) break;
}
}
xpos=MAPX/2; ypos=0;
hyun_design=next_design;
Random rr=new Random(); next_design=rr.Next() % MAXDESIGN;
for(ARR)for(ARR) design[ii,kk]=realdesign[hyun_design,ii,kk];
for(ARR)for(ARR) tris[ypos+ii,xpos+kk]=design[ii,kk];
}
}
public void running()
{
int rc; ConsoleKeyInfo keyinfo;
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.Escape) break;
if(keyinfo.Key==ConsoleKey.Enter) rc=enter_check();
draw_tris();
}
stop();
}
void draw_tris()
{
int ii,kk;
string formattext="";
for(ii=0; ii<MAPY-1; ii++)
{
for(kk=1; kk<MAPX-1; kk++)
{
if(tris[ii,kk]==1) formattext=formattext+"U";
else formattext=formattext+"_";
}
formattext=formattext+"\n";
}
Console.SetCursorPosition(0,0);
Console.Write(formattext);
}
void delete_column(int x, int y)
{
int ii;
for(ii=y; ii>=1; ii--)
{
tris[ii.x]=tris[ii-1,x];
}
}
int check_horizon()
{
int ii,kk,idx,horizon;
horizon=MAPX-3;
for(ii=0; ii<MAPY-1; ii++)
{
idx=0;
for(kk=1; kk<MAPX-2; kk++)
{
if(tris[ii,kk]==tris[ii,kk+1]) idx++;
else break;
}
if(idx==horizon)
{
for(kk=1; kk<MAPX-1; kk++) delete_column(ii,kk);
score++;
return(TRUE);
}
}
return(FAIL);
}
}//end of class
}
'c# 언어' 카테고리의 다른 글
C# - 엑셀 COM 레퍼런스 추가하기 (0) | 2021.10.20 |
---|---|
문제 풀이 : Lv1. 숫자 문자열과 영단어 (0) | 2021.10.18 |
테트리스.디버깅.단계(1)-Console 출력및 테스트 (0) | 2021.09.10 |
데이타 읽고,쓰기 속성 선언및 사용방법(get/set) (0) | 2021.09.07 |
Console Time Handling & Log Overloading (0) | 2021.08.27 |