解答・コメントを送る方法

コメントを送るには2つの方法があります。
  • 匿名でコメントを書く
    ログインせずにコメントを書くことができます。 名前は「匿名」となります。
  • アカウントを作成してコメントを書く
    アカウントを作成すると、記名での投稿ができます。 また、プロフィールページが作成され、 簡単なプロフィールや 統計情報が表示されるようになります。
どちらの場合も投稿後の修正・削除はできないので、 投稿前によくご確認下さい。

投稿ボタンを押す前に以下の文章を確認してください

  • 当サイトへの投稿は クリエイティブ・コモンズ・ライセンス BY(表示)および、その解釈に同意するものとみなされます。各ページには下のようにライセンス表示が行われます。
    Creative Commons License このサイトの内容は、 クリエイティブ・コモンズ・ライセンスの下でライセンスされています。 [詳細]
  • あなたの投稿したコード・コメント・トピックが再利用・添削されることを望まない場合は、投稿をお控えください。
  • 自分が書いていない、ウェブサイトや書籍などからの無断コピーは著作権の侵害です。著作権者の了解を得るか、自分で0から書いてください。
  • 著作権の侵害、名誉毀損、など投稿内容に問題がある場合、削除することがあります。
  • これらのことにあなたはあらかじめ同意したものとみなされます。

Post comment

Post a comment to the following challenge: マルバツゲーム (Nested Flatten)

As a reply to the following comment: あにす: IPlayerBaseインターフェースを...(#6192) [show]

[hide]
IPlayerBaseインターフェースを実装していればプレーヤーになれます。
勝敗判定の部分をもっとエレガントに書きたいですね。

結果
○ WIN : 5837
× WIN : 2846
  DRAW : 1317

○ WIN : 5863
× WIN : 2924
  DRAW : 1213

○ WIN : 5811
× WIN : 2895
  DRAW : 1294
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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();
    }
}


コメント本文
形式 [?]
コード
言語

タグ
半角スペースで区切って複数のタグを入力できます。
参考ページタイトル

参考ページURL
利用規約を読んで同意する必要があります。
by guest

Index

Feed

Other

Link

Pathtraq

loading...