Реализовать действие в крестиках ноликах - C#
Формулировка задачи:
помогите реализовать так чтобы он выдовал кто победитель(или ничья) и когда закончина игра. Заранее благодарю.
using System; namespace titactoe { class MainClass { public static char[,] titato = new char[3, 3] { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } }; public static void PrintTicTacToe() { Console.Clear(); Console.Write(" "); Console.BackgroundColor = ConsoleColor.White; for (int i = 0; i < titato.GetLength(0); i++) { Console.Write(" {0} ", i); } Console.WriteLine(); for (int i = 0; i < titato.GetLength(0); i++) { Console.BackgroundColor = ConsoleColor.White; Console.Write("{0}", i); for (int j = 0; j < titato.GetLength(1); j++) { Console.BackgroundColor = ConsoleColor.Black; if (titato[i, j] == 'x') { Console.ForegroundColor = ConsoleColor.Red; } else if (titato[i, j] == 'o') { Console.ForegroundColor = ConsoleColor.Green; } else { Console.ForegroundColor = ConsoleColor.Black; } Console.Write(" {0} ", titato[i, j]); } Console.WriteLine(); } Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Black; } public static void PushXO(int i, int j, char s) { titato[i, j] = s; } public static void Main(string[] args) { bool symbolX = true; char s = 'x'; int i = 0, j = 0; do { Console.WriteLine("TIC TAC TOE!"); PrintTicTacToe(); if (symbolX == true) { Console.WriteLine("Sisesta X koordinadid"); s = 'x'; symbolX = false; //kas jпѕѓпЅ¤rgmine on X } else { Console.WriteLine("Sisesta O koordinadid"); s = 'o'; symbolX = true; //kas jпѕѓпЅ¤rgmine on X } i = int.Parse(Console.ReadLine()); j = int.Parse(Console.ReadLine()); PushXO(i, j, s); Console.ReadLine(); PrintTicTacToe(); } while (true); } } }
Решение задачи: «Реализовать действие в крестиках ноликах»
textual
Листинг программы
using System; namespace titactoe { class MainClass { public static char[,] titato = new char[3, 3] { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } }; static bool CheckWin(char s) { if ((titato[0, 0] == titato[1, 1] & titato[1, 1] == titato[2, 2] & titato[0, 0]!=' ') || (titato[0, 1] == titato[0, 2] & titato[0, 2] == titato[0, 0] & titato[0, 1] != ' ') || (titato[1, 1] == titato[1, 2] & titato[1, 2] == titato[1, 0] & titato[1, 1] != ' ') || (titato[2, 1] == titato[2, 2] & titato[2, 2] == titato[2, 0] & titato[2, 1] != ' ') || (titato[1, 0] == titato[2, 0] & titato[2, 0] == titato[0, 0] & titato[1, 0] != ' ') || (titato[1, 1] == titato[2, 1] & titato[2, 1] == titato[0, 1] & titato[1, 1] != ' ') || (titato[1, 2] == titato[2, 2] & titato[2, 2] == titato[0, 2] & titato[1, 2] != ' ') || (titato[2, 0] == titato[1, 1] & titato[1, 1] == titato[0, 2] & titato[2, 0] != ' ')) { return true; } return false; } public static void PrintTicTacToe(char s) { Console.Clear(); Console.Write(" "); Console.BackgroundColor = ConsoleColor.White; for (int i = 0; i < titato.GetLength(0); i++) { Console.Write(" {0} ", i); } Console.WriteLine(); for (int i = 0; i < titato.GetLength(0); i++) { Console.BackgroundColor = ConsoleColor.White; Console.Write("{0}", i); for (int j = 0; j < titato.GetLength(1); j++) { Console.BackgroundColor = ConsoleColor.Black; if (titato[i, j] == 'x') { Console.ForegroundColor = ConsoleColor.Red; } else if (titato[i, j] == 'o') { Console.ForegroundColor = ConsoleColor.Green; } else { Console.ForegroundColor = ConsoleColor.Black; } Console.Write(" {0} ", titato[i, j]); } Console.WriteLine(); } Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Black; if (CheckWin(s)) { Console.WriteLine(s + " win!!!"); }; } public static void PushXO(int i, int j, char s) { titato[i, j] = s; } public static void Main(string[] args) { bool symbolX = true; char s = 'x'; int i = 0, j = 0; do { Console.WriteLine("TIC TAC TOE!"); PrintTicTacToe(s); if (symbolX == true) { Console.WriteLine("Sisesta X koordinadid"); s = 'x'; symbolX = false; //kas jпѕѓпЅ¤rgmine on X } else { Console.WriteLine("Sisesta O koordinadid"); s = 'o'; symbolX = true; //kas jпѕѓпЅ¤rgmine on X } i = int.Parse(Console.ReadLine()); j = int.Parse(Console.ReadLine()); PushXO(j, i, s); // Console.ReadLine(); PrintTicTacToe(s); } while (true); } } }