Перевод программы с С++ на С# - C#
Формулировка задачи:
Вы не могли бы мне помочь перевести программу с C++ на C#. Заранее благодарен
#include <iostream> #include <iomanip> #include <cstdlib> //////////////////////////////////////////////////////////////////////////////// const int SZ = 4; int board[SZ][SZ]; /* _____________ |1 |2 |3 |4 | |5 |6 |7 |8 | |9 |10|11|12| |13|14|15|0 | ------------- */ void cleanBoard(); bool checkForSolution(); bool checkBoard(); void showBoard(); void changeBoard(); void fillBoard(); void mainMenu(); void startGame(); //////////////////////////////////////////////////////////////////////////////// int main() { mainMenu(); return 0; } //////////////////////////////////////////////////////////////////////////////// void mainMenu() { system("cls"); std::cout << "\t15 P U ZZ L E" << std::endl; std::cout << "\tEnter 1 for NEW GAME!\n" << "\tEnter 2 for EXIT.\n" << "\tEnter 3 for HELP.\n"; bool y = 0; int choice; while (!y) { std::cout << ">"; std::cin >> choice; if (choice < 1 || choice > 3) std::cout << "Incorrect input! Try again!" << std::endl; else y = 1; } switch (choice) { case 1: startGame(); break; case 2: exit(0); case 3: std::cout << "\nType the number of slice you want\n" << "to move. If you want to show main\n" << "menu type 0. Good luck!\n" << std::endl; system("pause"); startGame(); break; } } //////////////////////////////////////////////////////////////////////////////// void startGame() { do { cleanBoard(); fillBoard(); } while (!checkForSolution()); int turns = 0; while (!checkBoard()) { showBoard(); changeBoard(); turns++; } std::cout << "You made it in " << turns << " turns!" << std::endl; system("pause"); } //////////////////////////////////////////////////////////////////////////////// void cleanBoard() { for (int i = 0; i < SZ; i++) for (int j = 0; j < SZ; j++) board[i][j] = 0; } //////////////////////////////////////////////////////////////////////////////// bool checkForSolution() { int solution[SZ*SZ] = {0}; int index = 0; for (int i = 0; i < SZ; i++) for (int j = 0; j < SZ; j++, index++) solution[index] = board[i][j]; int inv = 0; for (int i = 0; i < SZ*SZ; i++) if (solution[i]) for (int j = 0; j < i; j++) if (solution[j] > solution[i]) ++inv; for (int i = 0; i < SZ*SZ; i++) if (solution[i] == 0) inv += 1 + i / 4; return (inv & 1) ? 0 : 1; } //////////////////////////////////////////////////////////////////////////////// bool checkBoard() { int count = 1; for (int i = 0; i < SZ; i++) for (int j = 0; j < SZ; j++, count++) { if (i == SZ-1 && j == SZ-1 && board[i][j] == 0) break; if (board[i][j] != count) return 0; } return 1; } //////////////////////////////////////////////////////////////////////////////// void showBoard() { system("cls"); std::cout << "15 P U ZZ L E" << std::endl; for (int i = 0; i < SZ; i++) { std::cout << "-------------" << std::endl; std::cout << "|"; for (int j = 0; j < SZ; j++) { std::cout << std::left << std::setw(2) << board[i][j] << "|"; } std::cout << std::endl; } std::cout << "-------------" << std::endl; } //////////////////////////////////////////////////////////////////////////////// void changeBoard() { bool y = 0; while (!y) { int turn = 0; do { std::cout << ">"; std::cin >> turn; } while (turn < 0 || turn > 15); if (turn == 0) { mainMenu(); return; } int getx = 0; int gety = 0; for (int i = 0; i < SZ; i++) for (int j = 0; j < SZ; j++) if (board[i][j] == turn) { getx = i; gety = j; } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; for (int i = 0; i < 4; i++) { int newx = getx + dx[i]; int newy = gety + dy[i]; if (newx < 0 || newx > SZ-1 || newy < 0 || newy > SZ-1) continue; if (board[newx][newy] == 0) { board[newx][newy] = turn; board[getx][gety] = 0; y = 1; break; } } if (!y) std::cout << "Incorrect turn! Try again!" << std::endl; } } //////////////////////////////////////////////////////////////////////////////// void fillBoard() { bool already[15] = {0}; int count = 1; for (int i = 0; i < SZ; i++) for (int j = 0; j < SZ; j++, count++) { if (i == SZ-1 && j == SZ-1) break; while (true) { int randNum = rand()%15; if(!already[randNum]) { already[randNum] = 1; board[i][j] = randNum+1; break; } } } }
Решение задачи: «Перевод программы с С++ на С#»
textual
Листинг программы
using System; namespace ConsoleApplication1 { internal class Program { private const int Sz = 4; private static int[,] _board; private static void Main() { _board = new int[Sz, Sz]; //Initialize board MainMenu(); } private static void MainMenu() { var y = false; var choice = 0; Console.Clear(); Console.WriteLine("\t15 P U ZZ L E"); Console.WriteLine("Enter 1 for NEW GAME!"); Console.WriteLine("Enter 2 for EXIT!"); Console.WriteLine("Enter 3 for HELP!"); while (!y) { Console.WriteLine(">"); choice = Convert.ToInt32(Console.ReadLine()); if (choice < 1 || choice > 3) Console.WriteLine("Incorrect input! Try again!"); else y = true; } switch (choice) { case 1: StartGame(); break; case 2: Environment.Exit(0); break; case 3: Console.WriteLine("Type the number of slice you want"); Console.WriteLine("to move. If you want to show main"); Console.WriteLine("menu type 0. Good luck!"); Console.ReadKey(); StartGame(); break; } } private static void StartGame() { do { CleanBoard(); FillBoard(); } while (!CheckForSolution()); var turns = 0; while (!CheckBoard()) { ShowBoard(); ChangeBoard(); turns++; } Console.WriteLine("You made it in " + turns + " turns"); Console.ReadKey(); } private static void CleanBoard() { for (var i = 0; i < Sz; i++) for (var j = 0; j < Sz; j++) _board[i, j] = 0; } private static bool CheckForSolution() { var solution = new int[Sz*Sz]; var index = 0; for (var i = 0; i < Sz; i++) for (var j = 0; j < Sz; j++, index++) solution[index] = _board[i, j]; var inv = 0; for (var i = 0; i < Sz*Sz; i++) if (solution[i] > 0) for (var j = 0; j < i; j++) if (solution[j] > solution[i]) ++inv; for (var i = 0; i < Sz*Sz; i++) if (solution[i] == 0) inv += 1 + i/4; return (inv & 1) == 0; } private static bool CheckBoard() { var count = 1; for (var i = 0; i < Sz; i++) for (var j = 0; j < Sz; j++, count++) { if (i == Sz - 1 && j == Sz - 1 && _board[i, j] == 0) break; if (_board[i, j] != count) return false; } return true; } private static void ShowBoard() { Console.Clear(); Console.WriteLine("15 P U ZZ L E"); Console.WriteLine("-------------"); for (var i = 0; i < Sz; i++) { Console.Write("|"); for (var j = 0; j < Sz; j++) { Console.Write(_board[i, j] + " | "); } Console.WriteLine(); } Console.WriteLine("-------------"); } private static void ChangeBoard() { var y = false; int[] dx = {0, 0, 1, -1}; int[] dy = {1, -1, 0, 0}; var getx = 0; var gety = 0; while (!y) { int turn; do { Console.WriteLine(">"); turn = Convert.ToInt32(Console.ReadLine()); } while (turn < 0 || turn > 15); if (turn == 0) { MainMenu(); return; } for (var i = 0; i < Sz; i++) for (var j = 0; j < Sz; j++) if (_board[i, j] == turn) { getx = i; gety = j; } for (var i = 0; i < 4; i++) { var newx = getx + dx[i]; var newy = gety + dy[i]; if (newx < 0 || newx > Sz - 1 || newy < 0 || newy > Sz - 1) continue; if (_board[newx, newy] == 0) { _board[newx, newy] = turn; _board[getx, gety] = 0; y = true; break; } } if (!y) Console.WriteLine("Incorrect turn! Try again!"); } } private static void FillBoard() { var already = new bool[15]; var count = 1; for (var i = 0; i < Sz; i++) for (var j = 0; j < Sz; j++, count++) { if (i == Sz - 1 && j == Sz - 1) break; while (true) { var randNum = new Random().Next(15); if (!already[randNum]) { already[randNum] = true; _board[i, j] = randNum + 1; break; } } } } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д