diff --git a/TTTConsole/Program.cs b/TTTConsole/Program.cs index 20130c9..9e0f9b3 100644 --- a/TTTConsole/Program.cs +++ b/TTTConsole/Program.cs @@ -32,6 +32,7 @@ static string[] PLAYER_STR = new string[] { "", "先手", "後手" }; static string[] PLAYER_MARK = new string[] { " ", "○", "×" }; static string[] names; + static int[] thinkers; static bool showNumber = false; /// @@ -49,7 +50,6 @@ // プラグイン名の取得 System.Text.StringBuilder sb = new System.Text.StringBuilder(256); names = new string[2]; - //var names = new List(); GetNameH(sb, sb.Capacity); names[0] = sb.ToString(); GetNameR(sb, sb.Capacity); @@ -57,7 +57,7 @@ // プレイヤーの選択 Console.WriteLine("プレイヤーの選択"); - var thinkers = new int[ttt.PLAYERS]; + thinkers = new int[ttt.PLAYERS]; for (var pl=0; pl< ttt.PLAYERS; pl++) { for (var i = 0; i < names.Length; i++) { Console.WriteLine($"({i + 1}) {names[i]}"); @@ -67,6 +67,7 @@ if (!int.TryParse(input, out thinkers[pl])) return; if (thinkers[pl] < 1 || thinkers[pl] > names.Length) return; + --thinkers[pl]; } // 数字表示の選択 @@ -81,11 +82,11 @@ Console.Write($"{PlayerStr(ttt.Player)} の番"); switch (thinkers[(int)ttt.Player - 1]) { - case 1: + case 0: Console.WriteLine(""); ttt.Set(MyTurnH(ttt.GetBoard())); break; - case 2: + case 1: ttt.Set(MyTurnR(ttt.GetBoard())); if (!IsHumanR()) Console.WriteLine($" --> {ttt.LastSet + 1}"); break; @@ -125,12 +126,13 @@ static void ShowBoard(TicTacToe ttt) { Console.WriteLine(new string('~', 6) + $" ターン{ttt.Turn} " + new string('~', 6)); + var flip = ttt.Player == PLAYER.Second ? -1 : 1; for (var row = 0; row < ttt.BOARD_ROWS; row++) { Console.Write(" "); for (var col = 0; col < ttt.BOARD_COLS; col++) { var pos = row * ttt.BOARD_COLS + col; - var pIdx = ttt.Board[pos] > 0 ? PLAYER.First : - ttt.Board[pos] < 0 ? PLAYER.Second : PLAYER.None; + var pIdx = ttt.Board[pos] * flip > 0 ? PLAYER.First : + ttt.Board[pos] * flip < 0 ? PLAYER.Second : PLAYER.None; Console.Write(PLAYER_MARK[(int)pIdx]); if (showNumber) Console.Write(ttt.Board[pos] == 0 ? " " : string.Format("{0,2}", Math.Abs(ttt.Board[pos]))); @@ -150,7 +152,7 @@ /// プレイヤー /// 表示文字列 static string PlayerStr(PLAYER p) { - return $"{PLAYER_STR[(int)p]}({names[(int)p - 1]})"; + return $"{PLAYER_STR[(int)p]}({names[thinkers[(int)p - 1]]})"; } } } diff --git a/TTTConsole/TicTacToe.cs b/TTTConsole/TicTacToe.cs index 9626bf3..c49d2bf 100644 --- a/TTTConsole/TicTacToe.cs +++ b/TTTConsole/TicTacToe.cs @@ -23,7 +23,7 @@ public readonly int PLAYERS = 2; // プレイヤー数 public readonly int TURN_LIMIT; // ターン数上限(このターン数に至ると引き分け) public readonly int NONE = 0; // 空のマス - public readonly int[] SIGN; // プレイヤーの符号 + //public readonly int[] SIGN; // プレイヤーの符号 public readonly int[,] LINES; // 並べる線の情報 public int[] Board { get; private set; } // 盤 0:空 正:先手駒 負:後手駒 @@ -41,7 +41,7 @@ public TicTacToe(int turnLimit = 100) { TURN_LIMIT = turnLimit; Board = new int[BOARD_SIZE]; - SIGN = new int[] { 0, 1, -1 }; + //SIGN = new int[] { 0, 1, -1 }; LINES = new int[8, 3]{ { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } }; _board = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int)) * BOARD_SIZE); @@ -86,19 +86,16 @@ } // 盤の更新 - Board[pos] = Turn * SIGN[(int)Player]; if (Turn > REMAIN_PIECES) { - var remove = (Turn - REMAIN_PIECES) * SIGN[(int)Player]; - for (var i = 0; i < BOARD_SIZE; i++) { - if (Board[i] == remove) Board[i] = NONE; - } + for (var i = 0; i < BOARD_SIZE; i++) if (Board[i] > 0) --Board[i]; + Board[pos] = REMAIN_PIECES; + } else { + Board[pos] = Turn; } // 勝利判定 for (var i = 0; i < LINES.GetLength(0); i++) { - if (Board[LINES[i, 0]] * SIGN[(int)Player] > 0 && - Board[LINES[i, 1]] * SIGN[(int)Player] > 0 && - Board[LINES[i, 2]] * SIGN[(int)Player] > 0) { + if (Board[LINES[i, 0]] > 0 && Board[LINES[i, 1]] > 0 && Board[LINES[i, 2]] > 0) { Winner = Player; Judge = JUDGE.WIN; return; @@ -115,10 +112,20 @@ return; } } + FlipBoard(); return; } /// + /// 盤の値を反転 + /// + private void FlipBoard() { + for (var i = 0; i < BOARD_SIZE; i++) { + Board[i] = -Board[i]; + } + } + + /// /// アンマネージドメモリの盤データを取得 /// ///