At the moment, it is simply massive. I already reduced the size by about a hundred lines but it still seems way too big... Would appreciate some help :)
using System;
class Program
{
static int turns = 1;
static int player = 2;
static char[] board = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
static char playerSignature = 'X';
private static void Introduction()
{
Console.WriteLine("This is a simple TicTacToe game. Enter y if you have played before and n if you are new to this.");
string input1 = " ";
while (input1 != "y" || input1 != "n")
{
input1 = Console.ReadLine();
switch (input1)
{
case "y":
Console.WriteLine("Alright, let's get started, you are X, your friend is O.");
DrawBoard(board);
break;
case "n":
Console.WriteLine("TicTacToeRules:");
Console.WriteLine("1. The game is played on a grid that's 3 squares by 3 squares.");
Console.WriteLine("2. You are X, your friend is O. Players take turns putting their marks in empty squares.");
Console.WriteLine("3. The first player to get 3 of her marks in a row (up, down, across, or diagonally) is the winner.");
Console.WriteLine("4. When all 9 squares are full, the game is over.");
Console.WriteLine("If you have read the rules, press any key to continue.");
Console.ReadKey();
Console.Clear();
DrawBoard(board);
break;
default:
Console.WriteLine("Please enter y/n.");
break;
}
}
}
private static void DrawBoard(char[] board)
{
string row = "| {0} | {1} | {2} |";
string sep = "|___|___|___|";
Console.WriteLine(" ___ ___ ___ ");
Console.WriteLine(row, board[0], board[1], board[2]);
Console.WriteLine(sep);
Console.WriteLine(row, board[3], board[4], board[5]);
Console.WriteLine(sep);
Console.WriteLine(row, board[6], board[7], board[8]);
Console.WriteLine(sep);
}
private static void Reset()
{
char[] reset = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
board = reset;
DrawBoard(board);
}
private static void Draw()
{
Console.WriteLine("It's a draw!\n" +
"Press any key to play again.");
Console.ReadKey();
Reset();
}
public static void Main()
{
Introduction();
while (true)
{
bool isrow = false;
bool iscol = false;
int row = 0;
int col = 0;
while (!isrow)
{
Console.WriteLine("Choose a row (1-3): ");
try
{
row = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Please enter a number between 1 and 3.");
}
if (row == 1 || row == 2 || row == 3)
{
isrow = true;
}
else
{
Console.WriteLine("\nInvalid row!");
}
}
while (!iscol)
{
Console.WriteLine("Choose a column (1-3): ");
try
{
col = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Please enter a number between 1 and 3.");
}
if (col == 1 || col == 2 || col == 3)
{
iscol = true;
}
else
{
Console.WriteLine("\nInvalid column!");
}
}
int[] input = { row, col };
if (player == 2)
{
player = 1;
XorO(player, input);
}
else
{
player = 2;
XorO(player, input);
}
DrawBoard(board);
Win();
turns++;
if (turns == 10 && (board[0] == playerSignature && board[1] == playerSignature && board[2] == playerSignature && board[3] == playerSignature &&
board[4] == playerSignature && board[5] == playerSignature && board[6] == playerSignature && board[7] == playerSignature && board[8] == playerSignature))
{
Draw();
}
}
}
private static void XorO(int player, int[] input)
{
playerSignature = (player == 1) ? 'X' : 'O';//if player ==1 dann ps = 'X', else 'O'
int index = ((input[0] - 1) * 3) + (input[1] - 1);//3 - 1 = 2 , 2*3 = 6 + 2 = 8 für 3 3 | compact layout
if (board[index] == ' ')
{
board[index] = playerSignature;
}
else
{
Console.WriteLine("That spot is already taken! Try again!");
}
}
private static void Win()
{
if ((board[6] == playerSignature && board[4] == playerSignature && board[2] == playerSignature ||
board[0] == playerSignature && board[4] == playerSignature && board[8] == playerSignature) ||
(board[0] == playerSignature && board[1] == playerSignature && board[2] == playerSignature ||
board[3] == playerSignature && board[4] == playerSignature && board[5] == playerSignature ||
board[6] == playerSignature && board[7] == playerSignature && board[8] == playerSignature) ||
(board[0] == playerSignature && board[3] == playerSignature && board[6] == playerSignature ||
board[1] == playerSignature && board[4] == playerSignature && board[7] == playerSignature ||
board[2] == playerSignature && board[5] == playerSignature && board[8] == playerSignature))
{
if (player == 1)
{
Console.WriteLine("Congratulations Player 1, you win!\n");
Console.WriteLine("Play again? y/n.");
Playagain();
}
else if (player == 2)
{
Console.WriteLine("Congratulations Player 2, you win!\n");
Console.WriteLine("Play again y/n?");
Playagain();
}
}
}
private static void Playagain()
{
string input2 = " ";
input2 = Console.ReadLine();
if (input2 == "y")
{
Reset();
Console.Clear();
}
else if (input2 == "n")
{
Console.Clear();
Console.WriteLine("Close the window to exit the game.");
Console.ReadKey();
}
else
{
Console.WriteLine("Please enter y/n.");
}
}
}
Introduction
method, the conditioninput1 != "y" || input1 != "n"
is always true the game never start \$\endgroup\$if(input1== "n" || input1 == "y") { break; }
\$\endgroup\$input1 != "y" || input1 != "n"
Since it's impossible forinput1
to be equal to both "y" and "n" at the same time, this means that at least one of these is always true; and when you use||
and at least one value istrue
, then the result is alwaystrue
. It helps to work this out on paper. \$\endgroup\$break
is a band aid on a bad logical construct. It is better to fix the bad logical construct as opposed to relying on the band aid. Even if it works, it's very unreadable and needlessly complicates your code. \$\endgroup\$