Last active
February 13, 2025 07:03
-
-
Save pashagray/57b71ba379624cf5f7c1373e18e893aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
// Смотрю есть горячий спор по именованию констант. Майкрософт рекомендует | |
// использовать PascalCase, но в этом случае можно путать с методами и property. | |
// Коммьюнити, судя по интернетам, предпочитает использовать UPPER_CASE. Мне кстати | |
// такой подход тоже нравится. Сразу видно, что это константа. | |
private const int BOARD_ROWS = 3; | |
private const int BOARD_COLUMNS = 4; | |
private const int MAX_ATTEMPTS = 6; | |
private const int WIN_CONDITION = 3; | |
private const char TRUE_SYMBOL = 'X'; | |
private const char FALSE_SYMBOL = 'O'; | |
static void Main(string[] args) | |
{ | |
bool[] board = new bool[BOARD_ROWS * BOARD_COLUMNS]; | |
for (int i = 0; i < board.Length; i++) | |
{ | |
board[i] = false; | |
} | |
int currentAttempt = 0; | |
int currentWins = 0; | |
Random random = new Random(); | |
while (true) | |
{ | |
if (currentWins == WIN_CONDITION) | |
{ | |
Console.WriteLine("Вы выиграли игру!"); | |
break; | |
} | |
if (currentAttempt == MAX_ATTEMPTS) | |
{ | |
Console.WriteLine("Вы проиграли игру. Попытки закончились!"); | |
break; | |
} | |
Console.WriteLine($"Попытка: {currentAttempt} / {MAX_ATTEMPTS}"); | |
Console.WriteLine($"Победы: {currentWins} / {WIN_CONDITION}"); | |
Console.WriteLine("Текущее состояние поля: "); | |
for (int i = 0; i < board.Length; i++) | |
{ | |
if (i % BOARD_COLUMNS == 0) | |
{ | |
Console.WriteLine(); | |
} | |
if (board[i] == true) | |
{ | |
Console.Write(TRUE_SYMBOL); | |
} | |
else | |
{ | |
Console.Write(FALSE_SYMBOL); | |
} | |
} | |
Console.WriteLine("\n"); | |
// Составляем список свободных (false) индексов | |
int freeCount = 0; | |
for (int i = 0; i < board.Length; i++) | |
{ | |
if (board[i] == false) | |
{ | |
freeCount++; | |
} | |
} | |
if (freeCount == 0) | |
{ | |
Console.WriteLine("Все клетки заполнены. Игра завершена!"); | |
break; | |
} | |
int[] freeIndexes = new int[freeCount]; | |
int indexCounter = 0; | |
for (int i = 0; i < board.Length; i++) | |
{ | |
if (board[i] == false) | |
{ | |
freeIndexes[indexCounter] = i; | |
indexCounter++; | |
} | |
} | |
int randomIndex = freeIndexes[random.Next(0, freeIndexes.Length)]; | |
board[randomIndex] = true; | |
int row = randomIndex / BOARD_COLUMNS; | |
int col = randomIndex % BOARD_COLUMNS; | |
Console.WriteLine("Введите номер строки (1-" + BOARD_ROWS + "): "); | |
int guessRow; | |
while (!int.TryParse(Console.ReadLine(), out guessRow) || guessRow < 1 || guessRow > BOARD_ROWS) | |
{ | |
Console.WriteLine($"Некорректный ввод. Введите число от 1 до {BOARD_ROWS}."); | |
} | |
Console.WriteLine(); | |
Console.WriteLine("Введите номер столбца (1-" + BOARD_COLUMNS + "): "); | |
int guessColumn; | |
while (!int.TryParse(Console.ReadLine(), out guessColumn) || guessColumn < 1 || guessColumn > BOARD_COLUMNS) | |
{ | |
Console.WriteLine($"Некорректный ввод. Введите число от 1 до {BOARD_COLUMNS}."); | |
} | |
Console.WriteLine(); | |
currentAttempt++; | |
bool isWinMatch = (guessRow == row + 1 && guessColumn == col + 1); | |
if (isWinMatch) | |
{ | |
currentWins++; | |
Console.WriteLine("Вы угадали верно!\n"); | |
} | |
else | |
{ | |
Console.WriteLine("Увы, вы не угадали. Попробуйте снова.\n"); | |
} | |
} | |
Console.WriteLine("Игра окончена. Нажмите любую клавишу для выхода..."); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string commandRestart = "restart"; | |
const string commandPrintResult = "="; | |
const string commandExit = "exit"; | |
int[] currentNumbers = new int[0]; | |
Console.BackgroundColor = ConsoleColor.Blue; | |
Console.ForegroundColor = ConsoleColor.White; | |
Console.WriteLine("This program counts sum all numbers entered"); | |
Console.BackgroundColor = ConsoleColor.Black; | |
Console.WriteLine(); | |
Console.WriteLine("Enter first number or command:"); | |
Console.BackgroundColor = ConsoleColor.DarkBlue; | |
Console.WriteLine("Command list"); | |
Console.WriteLine($" {commandRestart} - Reset numbers"); | |
Console.WriteLine($" {commandPrintResult} - Print result"); | |
Console.WriteLine($" {commandExit} - Exit"); | |
Console.BackgroundColor = ConsoleColor.Black; | |
Console.ForegroundColor = ConsoleColor.Gray; | |
Console.WriteLine(); | |
Console.WriteLine("Enter first number or command:"); | |
while(true) | |
{ | |
string commandOrNumber = Console.ReadLine() ?? string.Empty; | |
if (int.TryParse(commandOrNumber, out int number)) | |
{ | |
Array.Resize(ref currentNumbers, currentNumbers.Length + 1); | |
currentNumbers[currentNumbers.Length - 1] = number; | |
Console.WriteLine($"Number {number} added"); | |
Console.WriteLine($"Total numbers: {currentNumbers.Length}"); | |
} | |
else | |
{ | |
string command = commandOrNumber.ToLower(); | |
switch (command) | |
{ | |
case commandRestart: | |
Console.WriteLine("Numbers have been flushed"); | |
currentNumbers = new int[0]; | |
break; | |
case commandPrintResult: | |
if (currentNumbers.Length == 0) | |
{ | |
Console.WriteLine("No numbers entered"); | |
} | |
else | |
{ | |
int sum = 0; | |
foreach (int n in currentNumbers) | |
{ | |
sum += n; | |
} | |
Console.WriteLine($"Result: {sum}"); | |
} | |
break; | |
case commandExit: | |
Console.WriteLine("Exiting..."); | |
return; | |
default: | |
Console.WriteLine("Unknown command"); | |
break; | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const int maxArrayLength = 100; | |
const int minNumberInArray = 10; | |
const int maxNumberInArray = 20; | |
var random = new Random(); | |
var length = random.Next(1, maxArrayLength + 1); | |
var array = new int[length]; | |
for (var i = 0; i < length; i++) | |
{ | |
array[i] = random.Next(minNumberInArray, maxNumberInArray + 1); | |
} | |
Console.WriteLine("Array: " + string.Join(", ", array)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int[][] arr = new int[][] | |
{ | |
new int[] { 8, 2, 3, 1, 1 }, | |
new int[] { 4, 12, 6, 2 }, | |
new int[] { 7, 8, 9 }, | |
new int[] { 2, 1, 10, 1}, | |
}; | |
int largerstNumber = 0; | |
int[] largerstNumberPosition = new int[2]; | |
for(int i = 0; i < arr.Length; i++) | |
{ | |
for(int j = 0; j < arr[i].Length; j++) | |
{ | |
if(arr[i][j] > largerstNumber) | |
{ | |
largerstNumber = arr[i][j]; | |
largerstNumberPosition[0] = i; | |
largerstNumberPosition[1] = j; | |
} | |
} | |
} | |
Console.WriteLine($"Largest number is {largerstNumber} at position {largerstNumberPosition[0]},{largerstNumberPosition[1]}"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string commandHead = "head"; | |
const string commandTail = "tail"; | |
const string commandExit = "exit"; | |
Console.BackgroundColor = ConsoleColor.White; | |
Console.ForegroundColor = ConsoleColor.Black; | |
// Commands list | |
Console.WriteLine("Heads to Tails"); | |
Console.WriteLine("Are you lucky?"); | |
Console.WriteLine("Commands: "); | |
Console.WriteLine("head - Heads"); | |
Console.WriteLine("tail - Tails"); | |
Console.WriteLine("exit - Exit"); | |
Console.BackgroundColor = ConsoleColor.Black; | |
Console.ForegroundColor = ConsoleColor.White; | |
Console.WriteLine(); | |
Console.WriteLine("Enter a command: "); | |
var command = ""; | |
do | |
{ | |
command = Console.ReadLine(); | |
var randomResult = new Random().Next(0, 2); | |
if (command == commandHead && randomResult == 0) | |
{ | |
Console.WriteLine("You win!"); | |
} | |
else if (command == commandTail && randomResult == 1) | |
{ | |
Console.WriteLine("You win!"); | |
} | |
else | |
{ | |
Console.WriteLine("You lose!"); | |
} | |
} | |
while (command != commandExit); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment