tic tac toe【三連棋】

后跳發表於2024-12-03

三連棋-控制檯原始碼

  1   2 
  3 char[,] nums;
  4 int player;
  5 
  6 Console.BackgroundColor= ConsoleColor.Blue;
  7 do
  8 {
  9     // 初始化棋盤和玩家
 10     InitializeGame(); 
 11     do
 12     {
 13         Console.Clear();
 14         Display(); 
 15         Console.Write($"{player}號玩家輸入:"); 
 16         try
 17         {
 18             string str = Console.ReadLine();
 19             int num = int.Parse(str);
 20             CheckNum(num); // 檢查數字
 21             ChangeNum(player, num); // 更新棋盤
 22 
 23             char symbol = player == 1 ? '0' : 'X';
 24 
 25             // 判斷輸贏
 26             if (CheckWin(nums, symbol))
 27             {
 28                 Console.Clear();
 29                 Display();
 30                 Console.WriteLine($"{player}號玩家獲勝!");
 31                 break;
 32             }
 33 
 34             // 判斷平局
 35             if (CheckDraw(nums))
 36             {
 37                 Console.Clear();
 38                 Display();
 39                 Console.WriteLine("遊戲平局!");
 40                 break;
 41             }
 42 
 43             // 切換玩家
 44             player = (player == 1) ? 2 : 1;
 45         }
 46         catch (Exception e)
 47         {
 48             Console.WriteLine(e.Message);
 49             Console.WriteLine("按任意鍵重新輸入...");
 50             Console.ReadKey();
 51         }
 52     } while (true);
 53 
 54     // 遊戲結束,詢問是否重新開始
 55     Console.WriteLine("是否重新開始遊戲?(Y/N)");
 56     string restart = Console.ReadLine().Trim().ToUpper();
 57     if (restart != "Y")
 58     {
 59         Console.WriteLine("遊戲結束,再見!");
 60         break;
 61     }
 62 } while (true);
 63 
 64 // 初始化遊戲狀態
 65 void InitializeGame()
 66 {
 67     nums = new char[3, 3]
 68     {
 69         {'1','2','3' },
 70         {'4','5','6' },
 71         {'7','8','9' }
 72     };
 73     player = 1; // 設定初始玩家編號
 74 }
 75 
 76 void ChangeNum(int player, int num)
 77 {
 78     char symbol = player == 1 ? '0' : 'X';
 79     switch (num)
 80     {
 81         case 1: nums[0, 0] = symbol; break;
 82         case 2: nums[0, 1] = symbol; break;
 83         case 3: nums[0, 2] = symbol; break;
 84         case 4: nums[1, 0] = symbol; break;
 85         case 5: nums[1, 1] = symbol; break;
 86         case 6: nums[1, 2] = symbol; break;
 87         case 7: nums[2, 0] = symbol; break;
 88         case 8: nums[2, 1] = symbol; break;
 89         case 9: nums[2, 2] = symbol; break;
 90         default: throw new Exception("無效輸入");
 91     }
 92 }
 93 
 94 void CheckNum(int num)
 95 {
 96     if (num < 1 || num > 9)
 97         throw new Exception("輸入數字錯誤,請輸入(1-9)");
 98 
 99     int row = (num - 1) / 3;
100     int col = (num - 1) % 3;
101 
102     if (nums[row, col] == 'X' || nums[row, col] == '0')
103         throw new Exception($"數字{num}已使用");
104 }
105 
106 bool CheckWin(char[,] board, char symbol)
107 {
108     for (int i = 0; i < 3; i++)
109     {
110         if (board[i, 0] == symbol && board[i, 1] == symbol && board[i, 2] == symbol)
111             return true;
112     }
113 
114     for (int i = 0; i < 3; i++)
115     {
116         if (board[0, i] == symbol && board[1, i] == symbol && board[2, i] == symbol)
117             return true;
118     }
119 
120     if (board[0, 0] == symbol && board[1, 1] == symbol && board[2, 2] == symbol)
121         return true;
122 
123     if (board[0, 2] == symbol && board[1, 1] == symbol && board[2, 0] == symbol)
124         return true;
125 
126     return false;
127 }
128 
129 bool CheckDraw(char[,] board)
130 {
131     foreach (var cell in board)
132     {
133         if (cell != 'X' && cell != '0')
134             return false;
135     }
136     return true;
137 }
138 
139 void Display()
140 {
141     Console.WriteLine($" {nums[0, 0]} | {nums[0, 1]} | {nums[0, 2]} ");
142     Console.WriteLine("___|___|___");
143     Console.WriteLine($" {nums[1, 0]} | {nums[1, 1]} | {nums[1, 2]} ");
144     Console.WriteLine("___|___|___");
145     Console.WriteLine($" {nums[2, 0]} | {nums[2, 1]} | {nums[2, 2]} ");
146 }

相關文章