-
-
Save dhristoskov/ceac23131b701e88ccb1 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections.Generic; | |
using System.Threading; | |
using System.Globalization; | |
namespace TicTacToev2 | |
{ | |
class TicTacToev2 | |
{ | |
static List<char> board = new List<char>() { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; | |
static List<string> players = new List<string>() { "нулев", "първи", "втори" }; | |
static int player = 1; | |
static int userInput; | |
static string winGame = String.Empty; | |
private static string GameWinCheck() | |
{ | |
if (board[1] == board[2] && board[2] == board[3]) | |
{ | |
return "win"; | |
} | |
else if (board[4] == board[5] && board[5] == board[6]) | |
{ | |
return "win"; | |
} | |
else if (board[7] == board[8] && board[8] == board[9]) | |
{ | |
return "win"; | |
} | |
else if (board[1] == board[4] && board[4] == board[7]) | |
{ | |
return "win"; | |
} | |
else if (board[2] == board[5] && board[5] == board[8]) | |
{ | |
return "win"; | |
} | |
else if (board[3] == board[6] && board[6] == board[9]) | |
{ | |
return "win"; | |
} | |
else if (board[1] == board[5] && board[5] == board[9]) | |
{ | |
return "win"; | |
} | |
else if (board[3] == board[5] && board[5] == board[7]) | |
{ | |
return "win"; | |
} | |
else if (board[1] != '1' && board[2] != '2' && board[3] != '3' && board[4] != '4' && board[5] != '5' | |
&& board[6] != '6' && board[7] != '7' && board[8] != '8' && board[9] != '9') | |
{ | |
return "draw"; | |
} | |
else | |
{ | |
return String.Empty; | |
} | |
} | |
private static void GameBoard() | |
{ | |
ConsoleColor oldColor = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine(); | |
Console.WriteLine("\t | | "); | |
Console.WriteLine("\t {0} | {1} | {2}", board[1], board[2], board[3]); | |
Console.WriteLine("\t _____|_____|_____ "); | |
Console.WriteLine("\t | | "); | |
Console.WriteLine("\t {0} | {1} | {2}", board[4], board[5], board[6]); | |
Console.WriteLine("\t _____|_____|_____ "); | |
Console.WriteLine("\t | | "); | |
Console.WriteLine("\t {0} | {1} | {2}", board[7], board[8], board[9]); | |
Console.WriteLine("\t | | "); | |
Console.ForegroundColor = oldColor; | |
} | |
static void Main(string[] args) | |
{ | |
CultureInfo bg = new CultureInfo("bg-BG"); | |
Thread.CurrentThread.CurrentCulture = bg; | |
do | |
{ | |
ConsoleColor oldColor = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Magenta; | |
Console.Clear(); | |
Console.WriteLine(" Първи играч маркира с 'Х' \n Втори играч маркира с 'О'"); | |
Console.WriteLine(); | |
if (player == 1) | |
{ | |
Console.WriteLine("\tНа ред е първи играч:"); | |
Console.Beep(); | |
} | |
else if (player == 2) | |
{ | |
Console.WriteLine("\tНа ред е втори играч:"); | |
Console.Beep(); | |
} | |
Console.WriteLine(); | |
GameBoard(); | |
Console.WriteLine(); | |
Console.WriteLine("Направете избор от свободните полета:"); | |
Console.Write("\t\t"); | |
try | |
{ | |
userInput = int.Parse(Console.ReadLine()); | |
if (board[userInput] != 'X' && board[userInput] != 'O') | |
{ | |
if (player == 1) | |
{ | |
board[userInput] = 'X'; | |
player = 2; | |
} | |
else if (player == 2) | |
{ | |
board[userInput] = 'O'; | |
player = 1; | |
} | |
} | |
else | |
{ | |
Console.WriteLine("Избраното от вас поле вече е маркирано"); | |
Console.WriteLine("Моля изберете ново свободно поле"); | |
Thread.Sleep(3000); | |
} | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("Избраното от вас число не е част от играта"); | |
Console.WriteLine("Моля изберете ново число от 1 до 9"); | |
Thread.Sleep(3000); | |
} | |
Console.ForegroundColor = oldColor; | |
winGame = GameWinCheck(); | |
} while (winGame != "win" && winGame != "draw"); | |
Console.Clear(); | |
GameBoard(); | |
ConsoleColor oldColor_2 = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Magenta; | |
Console.WriteLine(" \tGAME OVER!!!"); | |
if (winGame == "win") | |
{ | |
Console.WriteLine("Победител е {0} играч, Честито!!!", players[player % 2 + 1]); | |
Console.Beep(); | |
} | |
else if (winGame == "draw") | |
{ | |
Console.WriteLine("\tИграта е равна!!!"); | |
Console.Beep(); | |
} | |
Console.ForegroundColor = oldColor_2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment