C# Program: I have a grid system drawn in the console app and it asks the user t
ID: 3757084 • Letter: C
Question
C# Program:
I have a grid system drawn in the console app and it asks the user to pick 2 points on the grid and it draws an 'X' on the grid where the user selected. However, I am looking to have a line drawn as well from the corresponding 'X' 's. I don't want to use a form with system.drawing - is there any way I can do this?
Here is what I have so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
namespace program2
{
class Program
{
static void Main(string[] args)
{
//Print & Initialize Grid
char[,] grid = new char[6,6];
Initialize(grid);
Print(grid);
//Line
Console.Write("Enter first row selection: ");
int row1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter first column selection: ");
int col1 = Convert.ToInt32(Console.ReadLine());
grid[row1, col1] = 'X';
Console.Write("Enter second row selection: ");
int row2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second column selection: ");
int col2 = Convert.ToInt32(Console.ReadLine());
grid[row2, col2] = 'X';
Print(grid);
Console.ReadLine();
}
//Print the Grid
static void Print(char[,] grid)
{
Console.WriteLine(" | 1 | 2 | 3 | 4 | 5 |");
for (int row = 1; row < 6; row++)
{
Console.Write(row + "| ");
for (int col = 1; col < 6; col++)
{
Console.Write(grid[row, col]);
Console.Write(" | ");
}
Console.WriteLine();
}
}
//Grid Initialization
static void Initialize(char[,] grid)
{
for (int row = 1; row < 6; row++)
{
for (int col = 1; col < 6; col++)
{ grid[row, col] = ' '; }
}
}
//Draw Line
static void DrawLine(Pen, )
}
}
Explanation / Answer
using System;
using System.Threading;
namespace TIC_TAC_TOE
{
class Program
{
//making array and
//by default I am providing 0-9 where no use of zero
static char[] arr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
static int player = 1; //By default player 1 is set
static int choice; //This holds the choice at which position user want to mark
// The flag veriable checks who has won if it's value is 1 then some one has won the match if -1 then Match has Draw if 0 then match is still running
static int flag = 0;
static void Main(string[] args)
{
do
{
Console.Clear();// whenever loop will be again start then screen will be clear
Console.WriteLine("Player1:X and Player2:O");
Console.WriteLine(" ");
if (player % 2 == 0)//checking the chance of the player
{
Console.WriteLine("Player 2 Chance");
}
else
{
Console.WriteLine("Player 1 Chance");
}
Console.WriteLine(" ");
Board();// calling the board Function
choice = int.Parse(Console.ReadLine());//Taking users choice
// checking that position where user want to run is marked (with X or O) or not
if (arr[choice] != 'X' && arr[choice] != 'O')
{
if (player % 2 == 0) //if chance is of player 2 then mark O else mark X
{
arr[choice] = 'O';
player++;
}
else
{
arr[choice] = 'X';
player++;
}
}
else //If there is any possition where user want to run and that is already marked then show message and load board again
{
Console.WriteLine("Sorry the row {0} is already marked with {1}", choice, arr[choice]);
Console.WriteLine(" ");
Console.WriteLine("Please wait 2 second board is loading again.....");
Thread.Sleep(2000);
}
flag = CheckWin();// calling of check win
} while (flag != 1 && flag != -1);// This loof will be run until all cell of the grid is not marked with X and O or some player is not win
Console.Clear();// clearing the console
Board();// getting filled board again
if (flag == 1)// if flag value is 1 then some one has win or means who played marked last time which has win
{
Console.WriteLine("Player {0} has won", (player % 2) + 1);
}
else// if flag value is -1 the match will be draw and no one is winner
{
Console.WriteLine("Draw");
}
Console.ReadLine();
}
// Board method which creats board
private static void Board()
{
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[1], arr[2], arr[3]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[4], arr[5], arr[6]);
Console.WriteLine("_____|_____|_____ ");
Console.WriteLine(" | | ");
Console.WriteLine(" {0} | {1} | {2}", arr[7], arr[8], arr[9]);
Console.WriteLine(" | | ");
}
//Checking that any player has won or not
private static int CheckWin()
{
#region Horzontal Winning Condtion
//Winning Condition For First Row
if (arr[1] == arr[2] && arr[2] == arr[3])
{
return 1;
}
//Winning Condition For Second Row
else if (arr[4] == arr[5] && arr[5] == arr[6])
{
return 1;
}
//Winning Condition For Third Row
else if (arr[6] == arr[7] && arr[7] == arr[8])
{
return 1;
}
#endregion
#region vertical Winning Condtion
//Winning Condition For First Column
else if (arr[1] == arr[4] && arr[4] == arr[7])
{
return 1;
}
//Winning Condition For Second Column
else if (arr[2] == arr[5] && arr[5] == arr[8])
{
return 1;
}
//Winning Condition For Third Column
else if (arr[3] == arr[6] && arr[6] == arr[9])
{
return 1;
}
#endregion
#region Diagonal Winning Condition
else if (arr[1] == arr[5] && arr[5] == arr[9])
{
return 1;
}
else if (arr[3] == arr[5] && arr[5] == arr[7])
{
return 1;
}
#endregion
#region Checking For Draw
// If all the cells or values filled with X or O then any player has won the match
else if (arr[1] != '1' && arr[2] != '2' && arr[3] != '3' && arr[4] != '4' && arr[5] != '5' && arr[6] != '6' && arr[7] != '7' && arr[8] != '8' && arr[9] != '9')
{
return -1;
}
#endregion
else
{
return 0;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.