• Log in or sign up for an account here: https://www.sythe.org/login

TicTacToe.cs

Noam

Apostle of the Setting Sun

$50 USD Donor New
Competition Winner
Joined
Jul 27, 2011
Sythe Gold
0
Messages
2,993
Location
(0,0)
Two Factor Authentication User
Gohan has AIDS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication8
{
class Program
{
static void Main (string[] args)
{
Console.WriteLine ("--- Tic Tac Toe ---");
Console.Write ("Enter Board Size: ");
int boardSize = int.Parse (Console.ReadLine ());
int[,] board = new int[boardSize + 1, boardSize + 1];
int rows, header, columns, turns = 0, maxTurns = boardSize * boardSize;
Console.WriteLine ();
for (turns = 0; turns < maxTurns; turns++) {
Console.Clear ();
if (boardSize > 1 && boardSize < 20) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Write (" ");
for (header = 1; header <= boardSize; header++) {

Console.Write ("{0,-4}", header);
}
Console.WriteLine ();
Console.WriteLine ();
for (rows = 1; rows <= boardSize; rows++) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Write ("{0,-4}", rows);
Console.ForegroundColor = ConsoleColor.White;
for (columns = 1; columns <= boardSize; columns++) {
if (board[rows - 1, columns - 1] == 0) {
Console.Write ("? ");
}
if (board[rows - 1, columns - 1] == 1) {
Console.ForegroundColor = ConsoleColor.Green;
Console.Write ("X ");
Console.ForegroundColor = ConsoleColor.White;
}
if (board[rows - 1, columns - 1] == 2) {
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write ("O ");
Console.ForegroundColor = ConsoleColor.White;
}
Console.ForegroundColor = ConsoleColor.White;
}
Console.WriteLine ();
Console.WriteLine ();
}
} else {
Console.WriteLine ("We Can't Make That Board");
break;

}
int inputRow = -1, inputColumn = -1;
if (turns % 2 == 0) {
Console.WriteLine ("X's Turn");
} else {
Console.WriteLine ("O's Turn");
}
Console.WriteLine ("Type the row and column of the tile you want to select");
while (1 == 1) {
inputRow = -1;
inputColumn = -1;
while (inputRow < 0 || inputRow >= boardSize) {
Console.Write ("Enter Row: ");
inputRow = int.Parse (Console.ReadLine ()) - 1;
}
while (inputColumn < 0 || inputColumn >= boardSize) {
Console.Write ("Enter Column: ");
inputColumn = int.Parse (Console.ReadLine ()) - 1;
}
Console.WriteLine ();
if (board[inputRow, inputColumn] == 0) {
board[inputRow, inputColumn] = (turns % 2) + 1;
break;
} else {
Console.WriteLine ("That space is taken");
}
}

for (columns = 1; columns < boardSize; columns++) {
if (board[inputRow, 0] != board[inputRow, columns]) {
break;
}
}
if (rows == boardSize) {
if (turns % 2 == 0) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine ("Win for X - Row");
} else {
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine ("Win for O - Row");
}
Console.ForegroundColor = ConsoleColor.White;
break;
}
for (rows = 1; rows < boardSize; rows++) {
if (board[0, inputColumn] != board[rows, inputColumn]) {
break;
}
}
if (rows == boardSize) {
if (turns % 2 == 0) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine ("Win for X - Column");
} else {
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine ("Win for O - Column");
}
Console.ForegroundColor = ConsoleColor.White;
break;
}
if (inputColumn == inputRow) {
for (rows = 1; rows <= boardSize; rows++) {
if (board[0, 0] != board[rows, rows]) {
break;
}
}
if (rows == boardSize) {
if (turns % 2 == 0) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine ("Win for X - Main Diagonal");
} else {
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine ("Win for O - Main Diagonal");
}
Console.ForegroundColor = ConsoleColor.White;
break;
}
}
if (inputColumn + inputRow + 2 == board.GetLength (1)) {
for (rows = 1; rows < board.GetLength (1); rows++) {
if (board[0, board.GetLength (1) - 2] != board[rows - 1, board.GetLength (1) - rows - 1]) {
break;
}
}
if (rows == board.GetLength (1)) {
if (turns % 2 == 0) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine ("Win for X - Secondary Diagonal");
} else {
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine ("Win for O - Secondary Diagonal");
}
Console.ForegroundColor = ConsoleColor.White;
break;
}
}
}

if (turns == maxTurns) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine ("Tie");
Console.ForegroundColor = ConsoleColor.White;
}

}
}
}
 
Runescape Gamblingrunescape gambling
Back
Top