using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class nifu_shogi : MonoBehaviour
{
 int flg=0;int count=0;int spacewait=0;int level=0;int score=0;int nbyou=0;
 int komakazu=0;int komacount=0;int putban=0;int loser=0;int ans=-1;
 int[] p=new int[81];int[] atari=new int[2];int[] jun=new int[36];
 int i;int j;int k;int z;int zx;int zy;int r;int ee;int a;int aa;int aaa;int x;int y;int r1;int r2;
 int ii;int jj;int e1;int e2;int n1;int n2;int ax;int ay;int aax;int aay;int cx;int cy;
 int a1;int a2;int a3;int a4;int moves;int mousex;int mousey;
 float stimef;float ntimef;float xf;float yf;float stime2f;float ntime2f;
 string c1;string c2;string c3;string c4;
 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[281];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;AudioSource bgm4;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  obj[1]=GameObject.Find("koma-1"); // 1  koma-fu-moto
  obj[2]=GameObject.Find("koma-2"); // 2  koma-to-moto
  obj[3]=GameObject.Find("koma-title"); // 3  koma-title
  obj[4]=GameObject.Find("nifu-title"); // 4  title
  obj[5]=GameObject.Find("nifu-ban-red-1"); // 5  ban-red-1
  obj[6]=GameObject.Find("nifu-ban-red-2"); // 6  ban-red-2
  obj[7]=GameObject.Find("nifu-moji-1"); // 7  moji-seikai
  obj[8]=GameObject.Find("nifu-moji-2"); // 8  moji-shu-kyoku
  for (i=0;i<=9;i++)
  {
   obj[i+10]=GameObject.Find("num-"+i); // 10-19  nums-moto
  }
  for (i=0;i<=2;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+20]=Object.Instantiate(obj[j+10]) as GameObject;} // 10-49 nums
  }
  for (i=5;i<=8;i++)
  {
   obj[i+45]=GameObject.Find("nifu-button-"+i); // 50-53 button-red
  }
  for (i=0;i<=80;i++) {obj[i+100]=Object.Instantiate(obj[1]) as GameObject;} // 100-180 fu
  for (i=0;i<=80;i++) {obj[i+200]=Object.Instantiate(obj[2]) as GameObject;} // 200-280 to
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();
  bgm3=obj[3].GetComponent();bgm4=obj[4].GetComponent();
  style1=new GUIStyle();style1.fontSize=40;
  style2=new GUIStyle();style2.fontSize=16;
  style3=new GUIStyle();style3.fontSize=24;style3.normal.textColor=Color.red; 
  c1="";c2="";c3="";c4="";
  level=0;
  alldel();z=0;zx=0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update ()
 {
  Vector3 mpos=Input.mousePosition;
  xf=mpos.x;yf=mpos.y;mousex=(int)xf;mousey=(int)yf;
  if (flg==-1)
  {
   count=count-1;
   if (Input.GetMouseButtonDown(0)) {count=0;}
   if (Input.GetMouseButtonDown(1)) {count=0;}
   if (Input.GetKeyDown(KeyCode.Space)) {count=0;spacewait=2;}
   if (Input.GetKeyDown(KeyCode.Return)) {count=0;}
   if (count<=0)
   {
    titleput();
    flg=0;spacewait=2;
   }
  }
  if (flg==0)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8))
   {
    level=level-1;if (level<0) {level=3;}
    z=3;zx=-100;zy=100-level*40;objput();
   }
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
   {
    level=level+1;if (level>3) {level=0;}
    z=3;zx=-100;zy=100-level*40;objput();
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
  }
  if (flg==1 || flg==2)
  {
   ntimef=Time.time;
   if (ntimef-stimef>=27f) {bgm1.Play();stimef=Time.time;}
   ntime2f=Time.time;
   if (ntime2f-stime2f>=1f)
   {
    nbyou=nbyou-1;timeput();
    stime2f=stime2f+1f;
    if (nbyou<=0)
    {
     for (i=0;i<=1;i++)
     {
      y=atari[i]/9;x=atari[i]%9;
      z=i+5;zx=x*40-160;zy=160-y*40;objput();
     }
     z=7;zx=-1000;zy=0;objput();z=8;zx=-120;zy=220;objput();
     bgmstop();bgm3.Play();flg=99;count=0;
    }
   }
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();flg=0;spacewait=2;}
  }
  if (flg==1)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetMouseButtonDown(0)) {pic();}
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6)) {ans=1;}
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4)) {ans=3;}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) {ans=2;}
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) {ans=0;}
   if (ans>=0)
   {
    for (i=0;i<=1;i++)
    {
     y=atari[i]/9;x=atari[i]%9;
     z=i+5;zx=x*40-160;zy=160-y*40;objput();
    }
    if (ans==0) {z=50;zx=0;zy=-220;objput();}
    if (ans==1) {z=51;zx=220;zy=0;objput();}
    if (ans==2) {z=52;zx=0;zy=220;objput();}
    if (ans==3) {z=53;zx=-220;zy=0;objput();}
    if (ans==loser)
    {
     z=7;zx=-120;zy=220;objput();bgm2.Play();
     score=score+1;if (score>99) {score=99;}
     scoreput(); // bgm
     flg=2;count=100;
    }
    else
    {
     z=8;zx=-120;zy=220;objput();
     bgmstop();bgm4.Play();flg=99;count=0;
    }
   }
  }
  if (flg==2)
  {
   count=count-1;
   if (count<=0)
   {
    komakazu=komakazu+1;newstage();
    flg=1;spacewait=0;
   }
  }
  if (flg==99)
  {
   count=count+1;
   if (count>=300)
   {
    titleput();
    count=0;flg=0;spacewait=2;
   }
  }
 }

 void gamestart()
 {
  alldel();score=0;nbyou=60;scoreput();timeput();
  komakazu=level*10+5;komacount=0;newstage();
  stimef=Time.time;stime2f=Time.time;bgmstop();bgm1.Play();
  flg=1;
 }

 void newstage()
 {
  for (i=0;i<=80;i++)
  {
   p[i]=0;
   z=i+100;objput();obj[z].transform.rotation=Quaternion.identity;
   z=i+200;objput();obj[z].transform.rotation=Quaternion.identity;
  }
  for (i=0;i<=3;i++) {for (j=0;j<=8;j++) {jun[i*9+j]=j;}}
  loser=Random.Range(0,4);
  putban=loser;makexy(putban);p[y*9+x]=putban+1;atari[0]=y*9+x;komacount=1;
  if (loser==0 || loser==2) {jun[loser*9+x]=8;jun[loser*9+8]=x;}
  if (loser==1 || loser==3) {jun[loser*9+y]=8;jun[loser*9+8]=y;}
  for (i=0;i<=3;i++)
  {
   if (i==loser) {a1=8;} else {a1=9;}
   for (j=0;j<100;j++)
   {
    r1=Random.Range(0,a1);r2=Random.Range(0,a1);
    a=jun[i*9+r1];jun[i*9+r1]=jun[i*9+r2];jun[i*9+r2]=a;
   }
  }
  for (i=0;i<=3;i++)
  {
   a=jun[putban*9];if (putban==loser) {a=jun[putban*9+8];}
   for (j=0;j<2;j++)
   {
    r=Random.Range(0,8);
    if (putban==0) {x=a;y=r+1;}
    if (putban==1) {x=r+1;y=a;}
    if (putban==2) {x=a;y=r;}
    if (putban==3) {x=r;y=a;}
    if (p[y*9+x]==0) {break;} else {j=0;}
   }
   p[y*9+x]=putban+1;if (putban==loser) {atari[1]=y*9+x;}
   putban=putban+1;if (putban>3) {putban=0;}
  }
  komacount=5;
  for (i=0;i<=23;i++)
  {
   if (komacount3) {putban=0;}
    komacount=komacount+1;if (komacount>=komakazu) {break;}
   }
  }
  for (i=0;i<=50;i++)
  {
   if (komacount3) {putban=0;}
    komacount=komacount+1;if (komacount>=komakazu) {break;}
   }
  }
  for (i=0;i<=80;i++)
  {
   z=i+100;zx=-1000;zy=0;objput();z=i+200;objput();
   if (p[i]>0)
   {
    y=i/9;x=i%9;a1=(p[i]-1)/4;a2=(p[i]-1)%4;
    z=i+a1*100+100;zx=x*40-160;zy=160-y*40;objput();
    obj[z].transform.Rotate(0,0,a2*90);
   }
  }
  for (i=0;i<=-1;i++)
  {
   y=atari[i]/9;x=atari[i]%9;
   z=i+5;zx=x*40-160;zy=160-y*40;objput();
  }
  zx=-1000;zy=0;
  for (i=5;i<=7;i++) {z=i;objput();}
  for (i=50;i<=53;i++) {z=i;objput();}
  ans=-1;
 }

 void makexy(int n)
 {
  r1=Random.Range(0,9);r2=Random.Range(0,8);
  if (n==0) {x=r1;y=r2+1;}
  if (n==1) {x=r2+1;y=r1;}
  if (n==2) {x=r1;y=r2;}
  if (n==3) {x=r2;y=r1;}
 }

 void pic()
 {
  if (mousex>=620 && mousex<=660 && mousey>=120 && mousey<=160) {ans=0;}
  if (mousex>=840 && mousex<=880 && mousey>=340 && mousey<=380) {ans=1;}
  if (mousex>=620 && mousex<=660 && mousey>=560 && mousey<=600) {ans=2;}
  if (mousex>=400 && mousex<=440 && mousey>=340 && mousey<=380) {ans=3;}
 }

 void scoreput()
 {
  for (i=10;i<=29;i++) {z=i;zx=-1000;zy=0;objput();}
  z=score/10+10;zx=172;zy=234;objput();
  z=score%10+20;zx=184;zy=234;objput();
 }

 void timeput()
 {
  for (i=30;i<=49;i++) {z=i;zx=-1000;zy=0;objput();}
  z=nbyou/10+30;zx=172;zy=210;objput();
  z=nbyou%10+40;zx=184;zy=210;objput();
 }

 void titleput()
 {
  alldel();z=3;zx=-100;zy=100-level*40;objput();z=4;zx=0;zy=0;objput();
  bgmstop();spacewait=2;flg=0;count=0;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (900,200,100,30),c1,style2);
  GUI.Label (new Rect (330,100,100,30),c2,style2);
  GUI.Label (new Rect (330,200,100,30),c3,style1);
  GUI.Label (new Rect (530,300,100,30),c4,style3);
 }

 void objput()
 {
  Vector3 pos=obj[z].transform.position;
  pos.x=zx*0.01f;pos.y=zy*0.01f;
  obj[z].transform.position=pos;
 }

 void bgmstop()
 {
  bgm1.Stop();bgm2.Stop();bgm3.Stop();bgm4.Stop();
 }

 void alldel()
 {
  z=0;zx=-2000;zy=0;objput();
  zx=-1000;zy=0;for (i=3;i<=8;i++) {z=i;objput();}
  for (i=10;i<=53;i++) {z=i;objput();}
  for (i=100;i<=180;i++) {z=i;objput();obj[i].transform.rotation=Quaternion.identity;}
  for (i=200;i<=280;i++) {z=i;objput();obj[i].transform.rotation=Quaternion.identity;}
 }
}