using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class black_white : MonoBehaviour {
//四个锚点位置,用于计算棋子落点
public GameObject LeftTop;
public GameObject RightTop;
public GameObject LeftBottom;
public GameObject RightBottom;
//主摄像机
public Camera cam;
//锚点在屏幕上的映射位置
Vector3 LTPos;
Vector3 RTPos;
Vector3 LBPos;
Vector3 RBPos;
Vector3 PointPos;//当前点选的位置
float gridWidth = 1; //棋盘网格宽度
float gridHeight = 1; //棋盘网格高度
float minGridDis; //网格宽和高中较小的一个
Vector2[,] chessPos; //存储棋盘上所有可以落子的位置
int[,] chessState; //存储棋盘位置上的落子状态
enum turn { black=1, white=-1 } ;
turn chessTurn; //落子顺序
public Texture2D white; //白棋子
public Texture2D black; //黑棋子
public Texture2D blackWin; //白子获胜提示图
public Texture2D whiteWin; //黑子获胜提示图
public GameObject Tips;
int winner = 0; //获胜方,1为黑子,-1为白子
bool isPlaying = true; //是否处于对弈状态
// Use this for initialization
void Start () {
chessPos = new Vector2[14, 14];
chessState = new int[14, 14];
chessTurn = turn.black;
//计算锚点位置
LTPos = cam.WorldToScreenPoint(LeftTop.transform.position);
RTPos = cam.WorldToScreenPoint(RightTop.transform.position);
LBPos = cam.WorldToScreenPoint(LeftBottom.transform.position);
RBPos = cam.WorldToScreenPoint(RightBottom.transform.position);
//计算网格宽度
gridWidth = (RTPos.x - LTPos.x) / 14;
gridHeight = (LTPos.y - LBPos.y) / 14;
minGridDis = gridWidth < gridHeight ? gridWidth : gridHeight;
//计算落子点位置
for (int i = 0; i < 14; i++)
{
for (int j = 0; j < 14; j++)
{
chessPos[i, j] = new Vector2((LBPos.x + gridWidth * i + gridWidth / 2), (LBPos.y + gridHeight * j + gridHeight / 2));
}
}
ReSet();
}
void ReSet()
{
for (int i = 0; i < 14; i++)
{
for (int j = 0; j < 14; j++)
{
chessState[i, j] = 0;
}
}
isPlaying = true;
chessTurn = turn.black;
winner = 0;
chessState[6, 6] = 1;
chessState[7, 7] = 1;
chessState[6, 7] = -1;
chessState[7, 6] = -1;
Tips.gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
//检测鼠标输入并确定落子状态
if (isPlaying && Input.GetMouseButtonDown(0))
{
PointPos = Input.mousePosition;
int curr_x = 0, curr_y = 0;
for (int i = 0; i < 14; i++)
{
for (int j = 0; j < 14; j++)
{
//找到最接近鼠标点击位置的落子点,如果空则落子
if (Dis(PointPos, chessPos[i, j]) < minGridDis/2 && chessState[i, j] == 0)
{
//根据下棋顺序确定落子颜色
chessState[i, j] = chessTurn == turn.black ? 1 : -1;
curr_x = i;
curr_y = j;
result(curr_x, curr_y, chessTurn);
//落子成功,更换下棋顺序
chessTurn = chessTurn == turn.black ? turn.white : turn.black;
}
}
}
int re = isWin();
//调用判断函数,确定是否有获胜方
if (re == 1)
{
Debug.Log("黑棋胜");
winner = 1;
isPlaying = false;
}
else if (re == -1)
{
Debug.Log("白棋胜");
winner = -1;
isPlaying = false;
}
}
//按下空格重新开始游戏
if (Input.GetKeyDown(KeyCode.Space))
{
ReSet();
}
}
float Dis(Vector3 mPos, Vector2 gridPos)
{
return Mathf.Sqrt(Mathf.Pow(mPos.x - gridPos.x, 2) + Mathf.Pow(mPos.y - gridPos.y, 2));
}
void OnGUI()
{
//绘制棋子
for (int i = 0; i < 14; i++)
{
for (int j = 0; j < 14; j++)
{
if (chessState[i, j] == 1)
{
GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth/2 , Screen.height - chessPos[i, j].y - gridHeight/2 , gridWidth, gridHeight), black);
}
if (chessState[i, j] == -1)
{
GUI.DrawTexture(new Rect(chessPos[i, j].x - gridWidth/2 , Screen.height - chessPos[i, j].y - gridHeight/2 , gridWidth, gridHeight), white);
}
}
}
if ((int)chessTurn == 1)
{
GUI.DrawTexture(new Rect(0, Screen.height * 0.5f, gridWidth, gridHeight), black);
}
else
{
GUI.DrawTexture(new Rect(0, Screen.height * 0.5f, gridWidth, gridHeight), white);
}
//根据获胜状态,弹出相应的胜利图片
if (winner == 1)
{
GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), blackWin);
Tips.gameObject.SetActive(true);
}
else if (winner == -1)
{
GUI.DrawTexture(new Rect(Screen.width * 0.25f, Screen.height * 0.25f, Screen.width * 0.5f, Screen.height * 0.25f), whiteWin);
Tips.gameObject.SetActive(true);
}
}
int isWin()
{
int black_count = 0, white_count = 0;
bool is_null = false;
foreach (int a in chessState)
{
if (a == 0)
{
is_null = true;
}
else if (a == 1)
{
black_count++;
}
else if (a == -1)
{
white_count++;
}
}
if (!is_null)
{
return black_count > white_count ? 1 : -1;
}
else
{
if (black_count == 0 || white_count == 0)
{
return black_count > white_count ? 1 : -1;
}
else
{
return 2;
}
}
}
void result(int x, int y, turn curr_turn)
{
//黑白子当前数量
int Black_num = 0, White_num = 0;
int serial_right = 0;
int serial_left = 0;
int serial_top = 0;
int serial_bottom = 0;
int serial_obliqueRightTop = 0;
int serial_obliqueLeftBottom = 0;
int serial_obliqueLeftTop = 0;
int serial_obliqueRightBottom = 0;
//判断右边
if (x + 1 <= 13 && chessState[x + 1, y] != 0)
{
List chess_list = new List();
for (int i = x + 1; i < 14; i++)
{
if (chessState[i, y] != 0)
{
serial_right++;
chess_list.Add(chessState[i, y]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_right = 0;
}
else
{
int curr_index = 0;
for (int l = x; l <= x + serial_right; l++)
{
if (chessState[l, y] == (int)curr_turn && l - x >= 2)
{
if (l - x > curr_index)
{
curr_index = l - x;
}
}
}
serial_right = curr_index;
}
for (int i = x; i < x + serial_right; i++)
{
chessState[i, y] = (int)curr_turn;
}
}
//判断左边
if (x - 1 >= 0 && chessState[x - 1, y] != 0)
{
List chess_list = new List();
for (int i = x - 1; i >= 0; i--)
{
if (chessState[i, y] != 0)
{
serial_left++;
chess_list.Add(chessState[i, y]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_left = 0;
}
else
{
int curr_index = 0;
for (int l = x; l >= x - serial_left; l--)
{
if (chessState[l, y] == (int)curr_turn && x - l >= 2)
{
if (x - l > curr_index)
{
curr_index = x - l;
}
}
}
serial_left = curr_index;
}
for (int i = x; i > x - serial_left; i--)
{
chessState[i, y] = (int)curr_turn;
}
}
//判断上
if (y + 1 <= 13 && chessState[x, y + 1] != 0)
{
List chess_list = new List();
for (int i = y + 1; i < 14; i++)
{
if (chessState[x, i] != 0)
{
serial_top++;
chess_list.Add(chessState[x, i]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_top = 0;
}
else
{
int curr_index = 0;
for (int l = y; l <= y + serial_top; l++)
{
if (chessState[x, l] == (int)curr_turn && l - y >= 2)
{
if (l - y > curr_index)
{
curr_index = l - y;
}
}
}
serial_top = curr_index;
}
for (int i = y; i < y + serial_top; i++)
{
chessState[x, i] = (int)curr_turn;
}
}
//判断下
if (y - 1 >= 0 && chessState[x, y - 1] != 0)
{
List chess_list = new List();
for (int i = y - 1; i >= 0; i--)
{
if (chessState[x, i] != 0)
{
serial_bottom++;
chess_list.Add(chessState[x, i]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_bottom = 0;
}
else
{
int curr_index = 0;
for (int l = y; l >= y - serial_bottom; l--)
{
if (chessState[x, l] == (int)curr_turn && y - l >= 2)
{
if (y - l > curr_index)
{
curr_index = y - l;
}
}
}
serial_bottom = curr_index;
}
for (int i = y; i > y - serial_bottom; i--)
{
chessState[x, i] = (int)curr_turn;
}
}
//斜右上线/
if (x + 1 <= 13 && y + 1 <= 13 && chessState[x + 1, y + 1] != 0)
{
List chess_list = new List();
for (int i = 1; (i + x < 14) && (i + y < 14); i++)
{
if (chessState[x + i, y + i] != 0)
{
serial_obliqueRightTop++;
chess_list.Add(chessState[x + i, y + i]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_obliqueRightTop = 0;
}
else
{
int curr_index = 0;
for (int l = 0; l <= serial_obliqueRightTop; l++)
{
if (chessState[x + l, y + l] == (int)curr_turn && l >= 2)
{
if (l > curr_index)
{
curr_index = l;
}
}
}
serial_obliqueRightTop = curr_index;
}
for (int i = 0; i < serial_obliqueRightTop; i++)
{
chessState[x + i, y + i] = (int)curr_turn;
}
}
//斜左下线/
if (x - 1 >= 0 && y - 1 >= 0 && chessState[x - 1, y - 1] != 0)
{
List chess_list = new List();
for (int i = 1; (x - i >= 0) && (y - i >= 0); i++)
{
if (chessState[x - i, y - i] != 0)
{
serial_obliqueLeftBottom++;
chess_list.Add(chessState[x - i, y - i]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_obliqueLeftBottom = 0;
}
else
{
int curr_index = 0;
for (int l = 0; l <= serial_obliqueLeftBottom; l++)
{
if (chessState[x - l, y - l] == (int)curr_turn && l >= 2)
{
if (l > curr_index)
{
curr_index = l;
}
}
}
serial_obliqueLeftBottom = curr_index;
}
for (int i = 0; i < serial_obliqueLeftBottom; i++)
{
chessState[x - i, y - i] = (int)curr_turn;
}
}
//斜左上线
if (x - 1 >= 0 && y + 1 < 14 && chessState[x - 1, y + 1] != 0)
{
List chess_list = new List();
for (int i = 1; (x - i >= 0) && (y + i < 14); i++)
{
if (chessState[x - i, y + i] != 0)
{
serial_obliqueLeftTop++;
chess_list.Add(chessState[x - i, y + i]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_obliqueLeftTop = 0;
}
else
{
int curr_index = 0;
for (int l = 0; l <= serial_obliqueLeftTop; l++)
{
if (chessState[x - l, y + l] == (int)curr_turn && l >= 2)
{
if (l > curr_index)
{
curr_index = l;
}
}
}
serial_obliqueLeftTop = curr_index;
}
for (int i = 0; i < serial_obliqueLeftTop; i++)
{
chessState[x - i, y + i] = (int)curr_turn;
}
}
//斜右下线
if (x + 1 < 14 && y - 1 >= 0 && chessState[x + 1, y - 1] != 0)
{
List chess_list = new List();
for (int i = 1; (x + i < 14) && (y - i >= 0); i++)
{
if (chessState[x + i, y - i] != 0)
{
serial_obliqueRightBottom++;
chess_list.Add(chessState[x + i, y - i]);
}
else
{
break;
}
}
if (!chess_list.Contains((int)curr_turn))
{
serial_obliqueRightBottom = 0;
}
else
{
int curr_index = 0;
for (int l = 0; l <= serial_obliqueRightBottom; l++)
{
if (chessState[x + l, y - l] == (int)curr_turn && l >= 2)
{
if (l > curr_index)
{
curr_index = l;
}
}
}
serial_obliqueRightBottom = curr_index;
}
for (int i = 0; i < serial_obliqueRightBottom; i++)
{
chessState[x + i, y - i] = (int)curr_turn;
}
}
}
public void close()
{
Application.Quit();
}
}
主要算法是先判断落子处的某个方向是否旁边有棋子,如果没有就可以跳出,如果有就继续统计其连续有多少棋子,再在其连续的棋子中判断有没有和当前落子类型一样的棋子,没有就跳出,有就继续判断离当前落子处最远的一颗同类型的棋子,中间只要大于一颗棋子的距离,就可以把这些该变色的棋子统计出来