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

public class sk_suijaku : MonoBehaviour
{
 int flg=0;int count=0;int spacewait=0;int level=0;int yax=0;int yay=0;int ycount=0;
 int px;int py;int teban;int mai;int num1;int num2;int cardx;int cardy;
 int kuru=0;int kurucount=0;int atari=0;int cardnum;int tgn;
 int[] p=new int[16];int[] cp=new int[32];int[] cpp=new int[9];
 int[] pl=new int[2];int[] score=new int[2];int[] kurukazu=new int[5];
 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;
 float stimef;float ntimef;float xf;float yf;
 string c1;string c2;string c3;string c4;
 string d;string d1;string d2;string savedata;string keyname;
 GUIStyle style1;GUIStyle style2;GUIStyle style3;
 GameObject[] obj=new GameObject[704];
 AudioSource bgm1;AudioSource bgm2;AudioSource bgm3;AudioSource bgm4;AudioSource bgm5;

 void Start()
 {
  obj[0]=GameObject.Find("ng-mes"); // 0  ng-mes
  for (i=1;i<=3;i++)
  {
   obj[i]=GameObject.Find("sks-ya-y-"+i); // 1-3  ya-yoko
  }
  obj[4]=GameObject.Find("sks-ya-n"); // 4  ya-naname
  for (i=0;i<=3;i++)
  {
   obj[i+5]=GameObject.Find("sks-"+i); // 5-8  card-moto
  }
  obj[9]=GameObject.Find("sks-title"); // 9  title
  obj[10]=GameObject.Find("sks-scores"); // 10  scores
  for (i=0;i<=9;i++)
  {
   obj[i+11]=GameObject.Find("sks-names-"+i); // 11-20  names-moto
  }
  for (i=0;i<=11;i++)
  {
   obj[i+21]=GameObject.Find("sks-num-"+i); // 21-32  nums-moto
  }
  obj[33]=GameObject.Find("sks-ya-y-lb"); // 33  ya-lightblue
  obj[34]=GameObject.Find("sks-gameend-1"); // 34  gameend-1
  obj[35]=GameObject.Find("sks-gameend-2"); // 35  gameend-2
  obj[36]=GameObject.Find("to-end"); // 36  esc-to-end
  for (i=0;i<=15;i++) {obj[i+100]=Object.Instantiate(obj[5]) as GameObject;} // 100-115 card-ura
  for (i=0;i<=3;i++)
  {
   obj[i*4+116]=Object.Instantiate(obj[6]) as GameObject; // 116 120 124 128 // card-tateyoko
   obj[i*4+117]=Object.Instantiate(obj[6]) as GameObject; // 117 121 125 129 // 2 maime
   obj[i*4+118]=Object.Instantiate(obj[7]) as GameObject; // 118 122 126 130 // card-naname
   obj[i*4+119]=Object.Instantiate(obj[7]) as GameObject; // 119 123 127 131 // 2 maime
   for (j=0;j<=3;j++) {obj[i*4+j+116].transform.Rotate(0,0,i*90);}
  }
  obj[132]=Object.Instantiate(obj[8]) as GameObject; // 132 card-kurukuru
  obj[133]=Object.Instantiate(obj[8]) as GameObject; // 133 card-kurukuru
  for (i=0;i<=1;i++)
  {
   for (j=0;j<=9;j++) {obj[i*10+j+134]=Object.Instantiate(obj[j+11]) as GameObject;} // 134-153 plname
  }
  for (i=0;i<=1;i++)
  {
   for (j=0;j<=11;j++) {obj[i*12+j+154]=Object.Instantiate(obj[j+21]) as GameObject;} // 154-177 nums
  }
  bgm1=obj[1].GetComponent();bgm2=obj[2].GetComponent();bgm3=obj[3].GetComponent();
  bgm4=obj[4].GetComponent();bgm5=obj[5].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="";pl[0]=0;pl[1]=1;
  kurukazu[0]=99;kurukazu[1]=8;kurukazu[2]=4;kurukazu[3]=2;kurukazu[4]=1;level=1;
  alldel();z=0;zx=0;zy=0;objput();
  count=500;flg=-1;
 }

 void Update ()
 {
  Vector3 mpos=Input.mousePosition;
  xf=mpos.x;yf=mpos.y;
  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;ycount=0;
   }
  }
  if (flg==0)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4))
   {
    yax=yax-1;
    if (yax<0) {yax=2;}
   }
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6))
   {
    yax=yax+1;
    if (yax>2) {yax=0;}
   }
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8))
   {
    if (yax<=1)
    {
     pl[yax]=pl[yax]-1;if (pl[yax]<0) {pl[yax]=4;}
     z=yax+1;zx=yax*104-140;zy=60-pl[yax]*20;objput();
     if (count<20) {z=33;objput();}
    }
    else
    {
     level=level-1;if (level<0) {level=4;}
     z=3;zx=68;zy=60-level*20;objput();
     if (count<20) {z=33;objput();}
    }
   }
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2))
   {
    if (yax<=1)
    {
     pl[yax]=pl[yax]+1;if (pl[yax]>4) {pl[yax]=0;}
     z=yax+1;zx=yax*104-140;zy=60-pl[yax]*20;objput();
     if (count<20) {z=33;objput();}
    }
    else
    {
     level=level+1;if (level>4) {level=0;}
     z=3;zx=68;zy=60-level*20;objput();
     if (count<20) {z=33;objput();}
    }
   }
   if (Input.GetKeyDown(KeyCode.Space))
   {
    if (spacewait<=0) {gamestart();}
   }
   if (yax==0) {yay=60-pl[0]*20;}
   if (yax==1) {yay=60-pl[1]*20;}
   if (yax==2) {yay=60-level*20;}
   if (flg==0)
   {
    ycount=ycount+1;
    if (ycount==20) {z=33;zx=-1000;zy=0;objput();}
    if (ycount==40) {z=33;zx=yax*104-140;zy=yay;objput();ycount=0;}
   }
  }
  if (flg==1 || flg==5)
  {
   if (spacewait>0) {spacewait=spacewait-1;}
   if (Input.GetMouseButtonDown(0))
   {
    pic();
    z=4;zx=px*70-70;zy=70-py*70;objput();
   }
   if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.Keypad6)) {px=px+1;if (px>3) {px=0;}}
   if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.Keypad4)) {px=px-1;if (px<0) {px=3;}}
   if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Keypad8)) {py=py-1;if (py<0) {py=3;}}
   if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.Keypad2)) {py=py+1;if (py>3) {py=0;}}
   if (Input.GetKeyDown(KeyCode.Space) && spacewait<=0) {selecard(py*4+px);}
   if (flg==1 || flg==5) {z=4;zx=px*70-70;zy=70-py*70;objput();}
  }
  if (flg==2)
  {
   if (kuru%2==0) {a1=0;a2=10;} else {a1=10;a2=0;}
   count=count+1;
   obj[num1+100].transform.Rotate(a1,a2,0);
   if (count>=9)
   {
    z=num1+100;zx=-1000;zy=0;objput();obj[num1+100].transform.Rotate(a1*-9,a2*-9,0);
    a=(int)((p[num1]-2)/4);
    if ((a+kuru)%2==0) {a1=0;a2=-90;} else {a1=-90;a2=0;}
    z=p[num1]+114;zx=(num1%4)*70-105;zy=105-((int)(num1/4))*70;objput();obj[p[num1]+114].transform.Rotate(a1,a2,0);
    flg=3;count=0;
   }
  }
  if (flg==3)
  {
   a=(int)((p[num1]-2)/4);
   if ((a+kuru)%2==0) {a1=0;a2=10;} else {a1=10;a2=0;}
   count=count+1;
   obj[p[num1]+114].transform.Rotate(a1,a2,0);
   if (count>=9)
   {
    if (p[num1]==18 || p[num1]==19) {bgm4.Play();flg=13;count=0;}
    else
    {
     if (pl[teban]==0) {flg=5;spacewait=2;} else {flg=4;count=0;}
    }
   }
  }
  if (flg==4)
  {
   count=count+1;
   if (count>=50) {complay2();}
  }
  if (flg==6)
  {
   if (kuru%2==0) {a1=0;a2=10;} else {a1=10;a2=0;}
   count=count+1;
   obj[num2+100].transform.Rotate(a1,a2,0);
   if (count>=9)
   {
    z=num2+100;zx=-1000;zy=0;objput();obj[num2+100].transform.Rotate(a1*-9,a2*-9,0);
    a=(int)((p[num2]-2)/4);
    if ((a+kuru)%2==0) {a1=0;a2=-90;} else {a1=-90;a2=0;}
    z=p[num2]+114;zx=(num2%4)*70-105;zy=105-((int)(num2/4))*70;objput();obj[p[num2]+114].transform.Rotate(a1,a2,0);
    flg=7;count=0;
   }
  }
  if (flg==7)
  {
   a=(int)((p[num2]-2)/4);
   if ((a+kuru)%2==0) {a1=0;a2=10;} else {a1=10;a2=0;}
   count=count+1;
   obj[p[num2]+114].transform.Rotate(a1,a2,0);
   if (count>=9)
   {
    if (p[num2]==18 || p[num2]==19) {bgm4.Play();flg=13;count=0;}
    else
    {
     hantei();
     if (atari==0) {flg=8;count=0;}
     if (atari==1)
     {
      if (score[0]+score[1]<7) {flg=8;count=0;}
      else
      {
       z=1;zx=-1000;zy=0;objput();z=4;objput();z=33;objput();z=36;objput();z=142;objput();z=153;objput();
       if (score[0]>score[1]) {z=140;zx=-86;zy=-190;objput();z=151;zx=-86;zy=-214;objput();}
       if (score[0]100)
   {
    if (atari==1)
    {
     z=p[num1]+114;zx=-1000;zy=0;objput();z=p[num2]+114;objput();
     p[num1]=0;p[num2]=0;cp[num1]=0;cp[num2]=0;cp[num1+16]=0;cp[num2+16]=0;
     num1=-1;num2=-1;mai=0;comforget();
     if (level>0) {kurucount=kurucount+1;}
     if (kurucount>=kurukazu[level]) {flg=11;count=0;}
     else {if (pl[teban]==0) {flg=1;spacewait=2;} else {z=4;zx=-1000;zy=0;objput();flg=12;count=0;}}
    }
    else {flg=9;count=0;}
   }
  }
  if (flg==9)
  {
   count=count+1;
   a=(int)((p[num1]-2)/4);
   if ((a+kuru)%2==0) {a1=0;a2=-10;} else {a1=-10;a2=0;}
   obj[p[num1]+114].transform.Rotate(a1,a2,0);
   a=(int)((p[num2]-2)/4);
   if ((a+kuru)%2==0) {a3=0;a4=-10;} else {a3=-10;a4=0;}
   obj[p[num2]+114].transform.Rotate(a3,a4,0);
   if (count>=9)
   {
    z=p[num1]+114;zx=-1000;zy=0;objput();obj[p[num1]+114].transform.Rotate(a1*-9,a2*-9,0);
    z=p[num2]+114;zx=-1000;zy=0;objput();obj[p[num2]+114].transform.Rotate(a3*-9,a4*-9,0);
    if (kuru%2==0) {a1=0;a2=-90;} else {a1=-90;a2=0;}
    z=num1+100;zx=(num1%4)*70-105;zy=105-((int)(num1/4))*70;objput();obj[num1+100].transform.Rotate(a1,a2,0);
    z=num2+100;zx=(num2%4)*70-105;zy=105-((int)(num2/4))*70;objput();obj[num2+100].transform.Rotate(a1,a2,0);
    flg=10;count=0;
   }
  }
  if (flg==10)
  {
   count=count+1;
   if (kuru%2==0) {a1=0;a2=10;} else {a1=10;a2=0;}
   obj[num1+100].transform.Rotate(a1,a2,0);obj[num2+100].transform.Rotate(a1,a2,0);
   if (count>=9)
   {
    comforget();commemory();
    if (teban==0) {teban=1;} else {teban=0;}
    if (level>0) {kurucount=kurucount+1;}
    if (kurucount=9)
   {
    comforget();
    kuru=kuru+1;if (kuru>=4) {kuru=0;}
    num1=-1;num2=-1;mai=0;kurucount=0;
    if (pl[teban]==0) {flg=1;spacewait=2;} else {z=4;zx=-1000;zy=0;objput();flg=12;count=0;}
   }
  }
  if (flg==12)
  {
   count=count+1;
   if (count>=50) {complay1();}
  }
  if (flg==13)
  {
   count=count+1;
   if (count>=100)
   {
    for (i=100;i<=133;i++) {z=i;zx=-1000;zy=0;objput();}
    for (i=0;i<=31;i++) {cp[i]=0;}
    for (i=0;i<1000;i++)
    {
     r1=Random.Range(0,16);r2=Random.Range(0,16);
     a=p[r1];p[r1]=p[r2];p[r2]=a;
    }
    for (i=0;i<=3;i++)
    {
     for (j=0;j<=3;j++)
     {
      if (p[i*4+j]>=2) {z=i*4+j+100;zx=j*70-105;zy=105-i*70;objput();}
     }
    }
    if (teban==0) {teban=1;} else {teban=0;}
    if (level>0) {kurucount=kurucount+1;}
    if (kurucount=1 && flg<=13)
  {
   ycount=ycount+1;
   if (ycount>=40) {ycount=0;}
   z=1;zx=-117;zy=-189-teban*24;objput();
   if (ycount>=20) {zx=-1000;zy=0;}
   z=33;objput();
   ntimef=Time.time;
   if (ntimef-stimef>=53f) {bgm1.Play();stimef=Time.time;}
   if (Input.GetKeyDown(KeyCode.Escape)) {titleput();flg=0;spacewait=2;}
  }
  if (flg==99)
  {
   count=count+1;
   if (count%25==0 && count<275)
   {
    a=count/25;
    if (a%2==1) {z=34;zx=-1000;zy=0;objput();z=35;zx=0;zy=0;objput();}
    else {z=34;zx=0;zy=0;objput();z=35;zx=-1000;zy=0;objput();}
   }
   if (count>=400)
   {
    titleput();
    count=0;flg=0;spacewait=2;
   }
  }
 }

 void gamestart()
 {
  alldel();
  for (i=0;i<=15;i++) {p[i]=i+2;}
  r1=Random.Range(0,16);p[r1]=18;
  for (i=0;i<1000;i++)
  {
   r1=Random.Range(0,16);r2=Random.Range(0,16);
   a=p[r1];p[r1]=p[r2];p[r2]=a;
  }
  for (i=0;i<=31;i++) {cp[i]=0;}
  for (i=0;i<=8;i++) {cpp[i]=0;}
  for (i=0;i<=3;i++)
  {
   for (j=0;j<=3;j++)
   {
    z=i*4+j+100;zx=j*70-105;zy=105-i*70;objput();
   }
  }
  stimef=Time.time;bgmstop();bgm1.Play();
  kuru=0;kurucount=0;teban=0;mai=0;num1=-1;num2=-1;px=0;py=0;score[0]=0;score[1]=0;scoreput();
  z=10;zx=0;zy=-200;objput();z=4;zx=-70;zy=70;objput();
  z=135+pl[0];zx=-6;zy=-190;objput();z=145+pl[1];zx=-6;zy=-214;objput();
  z=142;zx=-86;zy=-188;objput();z=153;zx=-86;zy=-212;objput();
  z=36;zx=80;zy=-68;objput();
  if (pl[0]==0) {flg=1;spacewait=2;} else {z=4;zx=-1000;zy=0;objput();flg=12;count=100;}
 }

 void complay1()
 {
  for (i=0;i<=8;i++) {cpp[i]=0;}
  for (i=0;i<=15;i++)
  {
   if (cp[i+teban*16]>=2 && p[i]>=2)
   {
    a=(int)(cp[i+teban*16]/2);cpp[a]=cpp[a]+1;
   }
  }
  cardnum=-1;tgn=-1;
  for (i=1;i<=8;i++)
  {
   if (cpp[i]>=2) {tgn=i;break;}
  }
  if (tgn>=1)
  {
   for (i=0;i<=15;i++)
   {
    if (cp[i+teban*16]==tgn*2 || cp[i+teban*16]==tgn*2+1) {cardnum=i;break;}
   }
  }
  if (cardnum<0)
  {
   for (i=0;i<=2;i++)
   {
    r=Random.Range(0,16);
    if (p[r]<2 || cp[r+teban*16]>=2) {i=0;}
    else {cardnum=r;break;}
   }
  }
  num1=cardnum;flg=2;count=0;
 }

 void complay2()
 {
  cardnum=-1;
  for (i=0;i<=15;i++)
  {
   a=(int)(p[num1]/2);a1=cp[i+teban*16];
   if ((a1==a*2 || a1==a*2+1) && p[i]>=2 && i!=num1)
   {
    cardnum=i;break;
   }
  }
  if (cardnum<0)
  {
   for (i=0;i<1000;i++)
   {
    r=Random.Range(0,16);
    if (p[r]>=2 && cp[r+teban*16]<2 && r!=num1) {cardnum=r;break;}
   }
  }
  if (cardnum<0)
  {
   for (i=0;i<=2;i++)
   {
    r=Random.Range(0,16);
    if (p[r]<2 || r==num1) {i=0;} else {cardnum=r;break;}
   }
  }
  num2=cardnum;flg=6;count=0;
 }

 void commemory()
 {
  for (i=0;i<=1;i++)
  {
   if (pl[i]>0)
   {
    a=pl[i]*20+10;
    r=Random.Range(0,100);
    if (r0)
   {
    a=5-pl[i];
    for (j=0;j=503 && ax<=777 && ay>=224 && ay<=498)
  {
   if ((ax-503)%70<=64 && (ay-224)%70<=64)
   {
    x=(int)(ax-503)/70;y=(int)(ay-224)/70;
    selecard((3-y)*4+x);px=x;py=3-y;
   }
  }
 }

 void selecard(int n)
 {
  if (p[n]>=2 && n!=num1)
  {
   if (mai==0) {mai=1;num1=n;flg=2;count=0;} else {mai=2;num2=n;flg=6;count=0;}
  }
 }

 void hantei()
 {
  atari=0;
  a1=(int)(p[num1]/2);a2=(int)(p[num2]/2);
  if (a1==a2)
  {
   score[teban]=score[teban]+1;scoreput();
   atari=1;bgm2.Play();
  }
  else {bgm3.Play();}
 }

 void scoreput()
 {
  for (i=154;i<=177;i++) {z=i;zx=-1000;zy=0;objput();}
  if (score[0]<10) {a1=10;a2=score[0];} else {a1=11;a2=score[0]-10;}
  z=a1+154;zx=59;zy=-190;objput();z=a2+154;zx=70;objput();
  if (score[1]<10) {a1=10;a2=score[1];} else {a1=11;a2=score[1]-10;}
  z=a1+166;zx=59;zy=-214;objput();z=a2+166;zx=70;objput();
 }

 void titleput()
 {
  alldel();yax=0;ycount=0;z=9;zx=0;zy=0;objput();z=33;zx=yax*104-140;zy=60;objput();
  z=1;zx=-140;zy=60-pl[0]*20;objput();z=2;zx=-36;zy=60-pl[1]*20;objput();z=3;zx=68;zy=60-level*20;objput();
  bgmstop();spacewait=2;flg=0;count=0;
 }

 void OnGUI()
 {
  GUI.Label (new Rect (800,230,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();bgm5.Stop();
 }

 void alldel()
 {
  z=0;zx=-2000;zy=0;objput();
  for (i=1;i<=36;i++) {z=i;zx=-1000;zy=0;objput();}
  for (i=100;i<=177;i++) {z=i;zx=-1000;zy=0;objput();}
  for (i=100;i<=133;i++) {obj[i].transform.rotation=Quaternion.identity;}
  for (i=0;i<=3;i++)
  {
   for (j=0;j<=3;j++) {obj[i*4+j+116].transform.Rotate(0,0,i*90);}
  }
 }
}