using UnityEngine;
using System.Collections;
public class Baduk : MonoBehaviour {
public GameObject Player_White;
public GameObject Player_Black;
public GameObject Play_Field;
public int map_size = 18;
int turn;//1==black | 2==white
int[,] map_status;
int[,] repeat_status;
GameObject[,] stone_status;
string turnname;
int index_x;
int index_z;
int b_cnt;
int w_cnt;
int clear_b;
int clear_w;
int clear_all;
string winner;
public enum GameMode{
Playing, GameOver
}
GameMode gamemode;
// Use this for initialization
void Start () {
map_status = new int[map_size,map_size];
repeat_status = new int[map_size,map_size];
stone_status = new GameObject[map_size, map_size];
Player_White.transform.position = new Vector3(9.0f, 0.5f, -11.0f);
Player_Black.transform.position = new Vector3(-9.0f, 0.5f, 11.0f);
turn = 1;//black
for(int i = 0; i < map_size; i++){
for(int j = 0; j < map_size; j++){
Instantiate(Play_Field, new Vector3(-8.5f+1.0f*j, 0.0f, 8.5f-1.0f*i), Quaternion.identity);
map_status[i, j] = 0;
repeat_status[i, j] = 0;
}//for
}//for
index_x = -1;
index_z = -1;
b_cnt = 0;
w_cnt = 0;
clear_b=0;
clear_w=0;
clear_all=0;
gamemode=GameMode.Playing;
winner=null;
}//Start
public void Restart(){
Player_White.transform.position = new Vector3(9.0f, 0.5f, -11.0f);
Player_Black.transform.position = new Vector3(-9.0f, 0.5f, 11.0f);
turn = 1;//black
for(int i = 0; i < map_size; i++){
for(int j = 0; j < map_size; j++){
Instantiate(Play_Field, new Vector3(-8.5f+1.0f*j, 0.0f, 8.5f-1.0f*i), Quaternion.identity);
map_status[i, j] = 0;
repeat_status[i, j] = 0;
DestroyObject(stone_status[i,j]);
}//for
}//for
index_x = -1;
index_z = -1;
b_cnt = 0;
w_cnt = 0;
clear_b=0;
clear_w=0;
clear_all=0;
gamemode=GameMode.Playing;
winner=null;
}//Restart
// Update is called once per frame
void Update () {
if(gamemode == GameMode.Playing){
if(turn == 1){
turnname = "Black";
} else if(turn == 2){
turnname = "White";
}
Touch_Map();
Clear_Check();
Game_ClearCheck();
}//if
if(Input.GetKeyDown(KeyCode.Space)){
Restart();
}//if
}//Update
public void Game_ClearCheck(){
clear_b = 0;
clear_w = 0;
clear_all = 0;
for(int i = 0; i < map_size; i++){
for(int j = 0; j < map_size; j++){
if(map_status[i,j] == 1){//count black
clear_b++;
}//if
if(map_status[i,j] == 2){//count white
clear_w++;
}//if
if(map_status[i,j] == 0){//count null
clear_all++;
}//if
}//for
}//for
if(gamemode == GameMode.Playing && clear_all == 0){
GameOver();
}
}//Game_ClearCheck
public void GameOver(){
gamemode = GameMode.GameOver;
if(clear_b > clear_w){
winner="Black";
} else if(clear_b < clear_w){
winner="White";
} else if(clear_b == clear_w){
winner="Draw";
}//if
}//GameOver
public void Clear_Check(){
for(int i = 0; i < map_size; i++){
for(int j = 0; j < map_size; j++){
if(repeat_status[i,j]==0){
if((i == 0 && j == 0) || (i == 0 && j == map_size-1) || (i == map_size-1 && j == 0) || (i == map_size-1 && j == map_size-1)){//corner
if(i == 0 && j == 0 && map_status[i,j]==1 && map_status[i+1,j]==2 && map_status[i,j+1]==2//top left
|| i == 0 && j == 0 && map_status[i,j]==2 && map_status[i+1,j]==1 && map_status[i,j+1]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
} else if(i == map_size-1 && j == 0 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i,j+1]==2//top right
|| i == map_size-1 && j == 0 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i,j+1]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
} else if(i == 0 && j == map_size-1 && map_status[i,j]==1 && map_status[i+1,j]==2 && map_status[i,j-1]==2//bottom left
|| i == 0 && j == map_size-1 && map_status[i,j]==2 && map_status[i+1,j]==1 && map_status[i,j-1]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
} else if(i == map_size-1 && j == map_size-1 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i,j-1]==2//bottom right
|| i == map_size-1 && j == map_size-1 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i,j-1]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
}//if
}
if(i == 0 || i == map_size-1 || j == 0 || j == map_size-1){//border
if(i == 0 && map_status[i,j]==1 && map_status[i,j-1]==2 && map_status[i,j+1]==2 && map_status[i+1,j]==2//left
|| i == 0 && map_status[i,j]==2 && map_status[i,j-1]==1 && map_status[i,j+1]==1 && map_status[i+1,j]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
} else if(i == map_size-1 && map_status[i,j]==1 && map_status[i,j-1]==2 && map_status[i,j+1]==2 && map_status[i-1,j]==2//right
|| i == map_size-1 && map_status[i,j]==2 && map_status[i,j-1]==1 && map_status[i,j+1]==1 && map_status[i-1,j]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
} else if(j == 0 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i+1,j]==2 && map_status[i,j+1]==2//top
|| j == 0 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i+1,j]==1 && map_status[i,j+1]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
} else if(j == map_size-1 && map_status[i,j]==1 && map_status[i-1,j]==2 && map_status[i+1,j]==2 && map_status[i,j-1]==2//bottom
|| j == map_size-1 && map_status[i,j]==2 && map_status[i-1,j]==1 && map_status[i+1,j]==1 && map_status[i,j-1]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
}//if
}
if(0 < i && i < map_size-1 && 0 < j && j < map_size-1 ){//inside
if(map_status[i,j]==1 && map_status[i+1,j]==2 && map_status[i,j+1]==2 && map_status[i-1,j]==2 && map_status[i,j-1]==2
|| map_status[i,j]==2 && map_status[i+1,j]==1 && map_status[i,j+1]==1 && map_status[i-1,j]==1 && map_status[i,j-1]==1){
Move_Stone(map_status[i,j], stone_status[i,j]);
map_status[i,j]=0;
repeat_status[i,j]=1;
}//if
}//if
}//repeat if
}//for
}//for
}//Clear_Check
public void Move_Stone(int turn, GameObject stone){
if(turn == 1){//black
stone.transform.position = new Vector3(5.0f-0.5f*b_cnt, 0.5f, -10.0f);
b_cnt++;
} else if(turn == 2){//white
stone.transform.position = new Vector3(-5.0f+0.5f*w_cnt, 0.5f, 10.0f);
w_cnt++;
}
}//Move_Stone
public bool Position_Check(int turn, float x, float z){
switch((int)(10*x)){
case -85 : index_x = 0; break;
case -75 : index_x = 1; break;
case -65 : index_x = 2; break;
case -55 : index_x = 3; break;
case -45 : index_x = 4; break;
case -35 : index_x = 5; break;
case -25 : index_x = 6; break;
case -15 : index_x = 7; break;
case -5 : index_x = 8; break;
case 5 : index_x = 9; break;
case 15 : index_x = 10; break;
case 25 : index_x = 11; break;
case 35 : index_x = 12; break;
case 45 : index_x = 13; break;
case 55 : index_x = 14; break;
case 65 : index_x = 15; break;
case 75 : index_x = 16; break;
case 85 : index_x = 17; break;
}
switch((int)(10*z)){
case 85 : index_z = 0; break;
case 75 : index_z = 1; break;
case 65 : index_z = 2; break;
case 55 : index_z = 3; break;
case 45 : index_z = 4; break;
case 35 : index_z = 5; break;
case 25 : index_z = 6; break;
case 15 : index_z = 7; break;
case 5 : index_z = 8; break;
case -5 : index_z = 9; break;
case -15 : index_z = 10; break;
case -25 : index_z = 11; break;
case -35 : index_z = 12; break;
case -45 : index_z = 13; break;
case -55 : index_z = 14; break;
case -65 : index_z = 15; break;
case -75 : index_z = 16; break;
case -85 : index_z = 17; break;
}
//print ("Index_X : "+index_x+", Index_Z : "+index_z);
bool result=false;
if(map_status[index_x, index_z] == 0){
result = true;
}
return result;
}//Position_Check
public void My_Turn(int turn, int index_x, int index_z){
map_status[index_x, index_z] = turn;
if(turn == 1){
stone_status[index_x, index_z] = (GameObject)Instantiate(Player_Black, new Vector3(-8.5f+1.0f*index_x, 0.5f, 8.5f-1.0f*index_z), Quaternion.identity);
} else if(turn == 2){
stone_status[index_x, index_z] = (GameObject)Instantiate(Player_White, new Vector3(-8.5f+1.0f*index_x, 0.5f, 8.5f-1.0f*index_z), Quaternion.identity);
}
}//My_Turn
public void Touch_Map(){
if(Input.GetMouseButtonDown(0)){
Vector3 point = Input.mousePosition;
Ray ray = Camera.main.ScreenPointToRay(point);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 10000.0f) == true){
if(Position_Check(turn, hit.transform.position.x, hit.transform.position.z) == true){// this empty...
My_Turn(turn, index_x, index_z);
if(turn == 1){
turn = 2;
} else if(turn == 2){
turn = 1;
}//turn if
} else{
print ("Wrong Play !!!");
}//Position_Check if
}//Physics if
}//click if
}//Touch_Map
void OnGUI(){
GUI.Label(new Rect(325, 175, 175, 25), "Current Turn : "+turnname);
string temppos="";
for(int i = 0; i < map_size; i++){
temppos="";
for(int j = 0; j < map_size; j++){
temppos += map_status[j, i]+"\t";
}
GUI.Label(new Rect(25, 100+10*i, 300, 25), temppos);
}
GUI.Label(new Rect(850, 175, 175, 25), "Black "+clear_b+" : "+clear_w+" White");
GUI.Label(new Rect(850, 200, 175, 25), "Winner : "+winner);
}//OnGUI
}//class