using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace wuziqi
{
    class MainMethod
    {
        #region 变量
        private int i,j,k;

        /// <summary>
        /// shifouxiazi[i,j]=2表示此处无子,shifouxiazi[i,j]=1表示此处为黑子,shifouxiazi[i,j]=0表示此处为白子
        /// </summary>
        public int[,] shifouxiazi = new int[15, 15];

        /// <summary>
        /// 白棋获胜表
        /// </summary>
        public  bool[, , ] tableplayer1=new bool[15, 15, 572];

        /// <summary>
        /// 黑棋获胜表
        /// </summary>
        public  bool[, , ] tableplayer2=new bool[15, 15, 572];

        /// <summary>
        /// 统计赢家赢的次数
        /// </summary>
        public  int count = 0;

        /// <summary>
        /// 回合数
        /// </summary>
        public static int huihecount = 0;
        #endregion

        #region  方法

        /// <summary>
        /// 计算玩家分数
        /// </summary>
        /// <param name="player1lines">player1lines[x，y]，player2lines[x，y]加分算法中[x，y]点分数总和</param>
        /// <param name="player2lines">player1lines[x，y]，player2lines[x，y]加分算法中[x，y]点分数总和</param>
        /// <param name="shifouxiazi">shifouxiazi[i,j]=2表示此处无子,shifouxiazi[i,j]=1表示此处为黑子,shifouxiazi[i,j]=0表示此处为白子</param>
        /// <param name="tableplayer1">白棋获胜表</param>
        /// <param name="tableplayer2">黑棋获胜表</param>
        /// <param name="ifwin">获胜判断参数，ifwin[0，i]表白棋，ifwin[1，i]表黑棋</param>
        public void GetScoreMethod(int[,] player1lines, int[,] player2lines, int[,] shifouxiazi, bool[, ,] tableplayer1, bool[, ,] tableplayer2, int[,] ifwin)
        {
            for (  i = 0; i <= 14; i++)
                for ( j = 0; j <= 14; j++)
                {
                    player1lines[i, j] = 0;
                    //总分数清零
                    if (shifouxiazi[i, j] == 2)
                        for (k = 0; k < 572; k++)
                            if (tableplayer1[i, j, k])
                            {
                                //该位置可能取胜（为自方棋子）
                                switch (ifwin[0, k])
                                {
                                    //白棋取连子情况
                                    case 1:
                                        player1lines[i, j] += 30;   //形成1连子时,加30分
                                        break;
                                    case 2:
                                        player1lines[i, j] += 120;   //形成2连子时,加120分
                                        break;
                                    case 3:
                                        player1lines[i, j] += 300;  //形成3连子时,加300分
                                        break;
                                    case 4:
                                        player1lines[i, j] += 720; //形成4连子时,加720分
                                        break;
                                }
                            }
                }

            for (i = 0; i <= 14; i++)
                for (j = 0; j <= 14; j++)
                {
                    player2lines[i, j] = 0;
                    //总分数清零
                    if (shifouxiazi[i, j] == 2)
                        for (k = 0; k < 572; k++)
                            if (tableplayer2[i, j, k])
                            {
                                //该位置可能取胜（为自方棋子） 
                                switch (ifwin[1, k])
                                {
                                    //黑棋取连子情况
                                    case 1:
                                        player2lines[i, j] += 30;  //形成1连子时,加35分
                                        break;
                                    case 2:
                                        player2lines[i, j] += 120;  //形成2连子时,加135分
                                        break;
                                    case 3:
                                        player2lines[i, j] += 300;  //形成3连子时,加350分
                                        break;
                                    case 4:
                                        player2lines[i, j] += 720;  //形成4连子时,加800分
                                        break;
                                }
                            }
                }

        }

        /// <summary>
        /// 初始化棋盘棋子
        /// </summary>
        public void RefreshQP( )
        {
            //把棋盘所有位置置为空（未下子）
            for (i = 0; i < 15; i++)
                for (j = 0; j < 11; j++)
                {
                    //横连的所有可能5连子情况
                    for (k = 0; k < 5; k++)
                    {
                        tableplayer1[j + k, i, count]  = true;
                        tableplayer2[j + k, i, count] = true;
                    }
                    count++;
                }
            for (i = 0; i < 15; i++)
                for (j = 0; j < 11; j++)
                {
                    //竖连的所有可能5连子情况
                    for (k = 0; k < 5; k++)
                    {
                        tableplayer1[i, j + k, count] = true;
                        tableplayer2[i, j + k, count] = true;
                    }
                    count++;
                }
            for (i = 0; i < 11; i++)
                for (j = 0; j < 11; j++)
                {
                    //左上至右下所有斜连5子情况
                    for (k = 0; k < 5; k++)
                    {
                        tableplayer1[j + k, i + k, count] = true;
                        tableplayer2[j + k, i + k, count] = true;
                    }
                    count++;
                }
            for (i = 0; i < 11; i++)
                for (j = 14; j >= 4; j--)
                {
                    //右上至左下所有斜连5子情况
                    for (k = 0; k < 5; k++)
                    {
                        tableplayer1[j - k, i + k, count] = true;
                        tableplayer2[j - k, i + k, count] = true;
                    }
                    count++;
                }
        }


       /// <summary>
        /// 获取回合数以及先手 
       /// </summary>
       /// <param name="playername">先手的名称</param>
        /// <param name="player1">白棋是否有玩的权利;true表示有;false表示无</param>
       /// <param name="player2">黑棋是否有玩的权利;true表示有;false表示无</param>
        /// <param name="huihe">玩家玩的总次数</param>
        /// <returns>第一个数表示玩家玩的总次数;第二个数表示回合次数;第三个数表示先手的名称</returns>
        public  string  [] GetCountNum(string playername,bool player1,bool player2,int huihe)
        {
            string[] news = new string[3];
            switch (playername)
            {
                case "黑棋":
                    break;
                case "白棋":
                    break;
                case "待定":
                    if (player1)
                    {
                        playername = "白棋";
                    }
                    else if (player2)
                    {
                        playername = "黑棋";

                    }
                    break;
            }
            huihe++;
            if (huihe % 2 == 0)
                huihecount = huihe / 2;
            else
                huihecount = (huihe + 1) / 2;
            news[0] = huihe.ToString();
            news[1] = huihecount.ToString();
            news[2] = playername;
            return news;
        }

        /// <summary>
        /// 算分数
        /// </summary>
        public  void GetScore(int[,] player1lines,int [,]player2lines,ref int score1,ref int score2)
        {
            score1 = 0;
            score2 = 0;
            for (int ii = 0; ii <= 14; ii++)
            {
                for (int jj = 0; jj <= 14; jj++)
                {
                    score1 += player1lines[ii, jj];
                    score2 += player2lines[ii, jj];
                }
            }
        }

        /// <summary>
        /// 获取赢家的分数
        /// </summary>
        /// <returns></returns>
        public  string GetWinScore(int score1,int score2)
        {
            string winscore = "";
            if (score1 > score2)
                winscore = score1.ToString();
            else if (score1 < score2)
                winscore = score2.ToString();
            else winscore = score1.ToString();
            return winscore;
        }

        /// <summary>
        /// 显示玩家信息
        /// </summary>
        /// <param name="type">选择的游戏类型</param>
        /// <param name="name1">白棋玩家的名称</param>
        /// <param name="name2">黑棋玩家的名称</param>
        /// <returns></returns>
        public string[] DisPlayPlayer(int type, string name1, string name2)
        {
            string[] name = new string[2];
            switch (type)
            {
                case 0:
                    name[0] = "电脑";
                    name[1] = "玩家";
                    break;
                case 1:
                    name[0] = "玩家1";
                    name[1] = "玩家2";
                    break;
                case 2:
                    name[0] = name1;
                    name[1] = name2;
                    break;

            }
            return name;
        }

        #endregion
    }
}
