using System;
using System.Collections.Generic;
using System.Linq;

namespace どう書く_org_マルバツゲーム {
    class Program {
        static void Main(string[] args) {
            int maruWin = 0;
            int batuWin = 0;
            int draw = 0;
            IPlayerBase maru = new Player() { Name = "○" };
            IPlayerBase batu = new Player() { Name = "×" };

            for(int i = 0; i < 10000; i++) {
                Game game = new Game(maru, batu);
                switch(game.Start()) {
                case "○":
                    maruWin++;
                    break;
                case "×":
                    batuWin++;
                    break;
                case "draw":
                    draw++;
                    break;
                }
            }

            Console.WriteLine("○ WIN : " + maruWin.ToString());
            Console.WriteLine("× WIN : " + batuWin.ToString());
            Console.WriteLine("  DRAW : " + draw.ToString());

            Console.ReadLine();
        }
    }

    class Game {
        private IPlayerBase _maru;
        private IPlayerBase _batu;

        public string[] Board = new string[] {
            "□□□",
            "□□□",
            "□□□" };

        public Game(IPlayerBase maru, IPlayerBase batu) {
            _maru = maru;
            _maru.Game = this;
            _batu = batu;
            _batu.Game = this;
        }

        public string Start() {
            string jadge = "";
            while(true) {
                _maru.Play();
                jadge = Judge(_maru.Name);
                if(jadge != "") {
                    break;
                }
                _batu.Play();
                jadge = Judge(_batu.Name);
                if(jadge != "") {
                    break;
                }
            }

            return jadge;
        }

        //勝者を判定
        private string Judge(string name) {
            string name3 = name + name + name;
            //横
            foreach(string str in Board) {
                if(str == name3) {
                    return name;
                }
            }

            //縦
            for(int x = 0; x < 3; x++) {
                string str = "";
                for(int y = 0; y < 3; y++) {
                    str += Board[y][x];
                }
                if(str == name3) {
                    return name;
                }
            }

            //斜め
            string tmp = Board[0][0].ToString() + Board[1][1].ToString() + Board[2][2].ToString();
            if(tmp == name3) {
                return name;
            }
            tmp = Board[0][2].ToString() + Board[1][1].ToString() + Board[2][0].ToString();
            if(tmp == name3) {
                return name;
            }

            //引き分け
            if(!(Board[0] + Board[1] + Board[2]).Contains('□')) {
                return "draw";
            }
            return "";
        }
    }

    class Player :IPlayerBase {
        #region IPlayerBase メンバ

        public Game Game { get; set; } //参加しているゲーム

        public string Name { get; set; } //○か×か

        private Random Rnd = new Random();
        //一手打つ
        public void Play() {
            List<Location> cells = new List<Location>(); //置けるマス

            //置けるマスを列挙
            for(int y = 0; y < 3; y++) {
                for(int x = 0; x < 3; x++) {
                    if(Game.Board[y][x] == '□') {
                        Location location = new Location();
                        location.x = x;
                        location.y = y;

                        cells.Add(location);
                    }
                }
            }

            //置けるマスが無ければ何もしない
            if(cells.Count == 0) return;

            //乱数で置くマスを決定
            int index = Rnd.Next(cells.Count);
            Location cell = cells[index];

            //石を置く
            Game.Board[cell.y] = Game.Board[cell.y].Remove(cell.x, 1).Insert(cell.x, Name);
        }
        #endregion
    }

    //マスの位置
    struct Location {
        public int x;
        public int y;
    }

    //プレーヤーが実装するインターフェース
    //これを実装したプレーヤーに差し替え可
    interface IPlayerBase {
        Game Game { set; get; }
        string Name { set; get; }
        void Play();
    }
}