I am a beginner coder and was wondering how to improve my c# console code. It makes a tic-tac-toe game.
using System;
namespace TicTacToe
{
class Program
{
static string[] options = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; //stores the variables to change
static bool Playing = true; //stops the game once someone wins
static int turn = 0;
static void Main(string[] args)
{
Intro();
Board();
while (Playing) //is to stop the game once someone wins
{
if (turn%2 == 0)
{
Console.WriteLine("Player 1's turn");
}
else
{
Console.WriteLine("Player 2's turn");
}
int playerInput1;
Console.WriteLine("type in your responce player");
bool torf = int.TryParse(Console.ReadLine(), out playerInput1);
playerInput1--;
// makes sure the input is put in a valid value
if (torf && playerInput1 < 9 && playerInput1 > -1) //makes sure again the input is valid and continues
{
if (options[playerInput1] == "x" || options[playerInput1] == "o")
{
Console.WriteLine("Stop stealing othe people's place");
}
else
{
if (turn%2 == 0)
{
options[playerInput1] = "x";
turn++;
Board();
WinCondition();
Tie();
}
else
{
options[playerInput1] = "o";
turn++;
Board();
WinCondition();
Tie();
}
/*int playerInput2;
Console.WriteLine("o");
bool torf2 = int.TryParse(Console.ReadLine(), out playerInput2);
playerInput2--;
if (torf2 && playerInput2 < 9 && playerInput2 > -1)
{
if (options[playerInput2] == "x" || options[playerInput2] == "o")
{
Console.WriteLine("Stop stealing other people's space");
}
else
{
options[playerInput2] = "o";
Board();
WinCondition();
torf2 = false;
torf = false;
}
}*/
}
}
else
{
Console.WriteLine("Please input a valid expression");
}
}
}
public static void Board() // makes the board
{
Console.Clear();
Console.WriteLine(" | | ");
Console.WriteLine($" {options[0]} | {options[1]} | {options[2]}");
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine($" {options[3]} | {options[4]} | {options[5]}");
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine($" {options[6]} | {options[7]} | {options[8]}");
Console.WriteLine(" | | ");
}
public static void WinCondition()
{
if (options[0] == options[1] && options[1] == options[2])
{
Playing = false;
if(turn % 2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
else if (options[3] == options[4] && options[4] == options[5])
{
Playing = false;
if(turn % 2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
else if (options[6] == options[7] && options[7] == options[8])
{
Playing = false;
if(turn % 2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
// checks for horizontal wins
else if (options[0] == options[3] && options[3] == options[6])
{
Playing = false;
if(turn % 2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
else if (options[1] == options[4] && options[4] == options[7])
{
Playing = false;
if(turn % 2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
else if (options[2] == options[5] && options[5] == options[8])
{
Playing = false;
if(turn % 2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
// checks for vertical wins
else if (options[0] == options[4] && options[4] == options[8])
{
Playing = false;
if(turn % 2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
else if (options[2] == options[4] && options[4] == options[6])
{
Playing = false;
if (turn%2 == 0)
{
Console.WriteLine("Congrats on winning player 1, better luck next time player 2");
}
else
{
Console.WriteLine("Congrats on winning player 2, better luck next time player 1");
}
}
// checks for diagonal wins
}
public static void Intro()
{
Console.WriteLine("Welcome to\n");
Console.WriteLine(@"____________ ________ ________ ");
Console.WriteLine(@"___ __/__(_)______ ___ __/_____ _______ ___ __/__________ ");
Console.WriteLine(@"__ / __ /_ ___/________ / _ __ `/ ___/________ / _ __ \ _ \");
Console.WriteLine(@"_ / _ / / /__ _/_____/ / / /_/ // /__ _/_____/ / / /_/ / __/");
Console.WriteLine(@"/_/ /_/ \___/ /_/ \__,_/ \___/ /_/ \____/\___/ ");
Console.WriteLine("\n1.The game is played on a grid that's 3 squares by 3 squares.\n\n2.Player 1 is \"X\" and Player 2 is \"O\". Players take turns putting their marks in empty squares.\n\n3.The first player to get 3 of her marks in a row(horizontally, vertically or diagonally) is the winner.\n\n4.When all 9 squares are full, the game is over. If no player has 3 marks in a row, the game ends in a tie.\n\n5.You can put x or o in by typing the number you want to put it at");
Console.ReadKey(false);
Console.Clear();
}
public static void Tie()
{
if (options[0] != "1" && options[1]!= "2" && options[2] != "3" && options[3] != "4" && options[4] != "5" && options[5] != "6" && options[6] != "7" && options[7] != "8" && options[8] != "9")
{
Console.WriteLine("The game is a tie");
Playing = false;
}
}
}
}