public
Created

Checks the outcome of a tic tac toe game.

  • Download Gist
Tic Tac Toe checker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Tic_Tac_Toe_Checker
{
class Program
{
static void Main(string[] args) //testing method
{
int[,] board1 = { { 1, 2, 0 }, { 0, 2, 0 }, { 0, 1, 0 } };
int[,] board2 = { { 1, 2, 0 }, { 0, 1, 2 }, { 0, 2, 1 } };
int[,] board3 = { { 1, 2, 1 }, { 2, 2, 2 }, { 1, 1, 0 } };
int[,] board4 = { { 1, 2, 1 }, { 1, 2, 1 }, { 2, 1, 2 } };
Console.WriteLine(TTTcheck(board1)); //. should print "0"
Console.WriteLine(TTTcheck(board2)); //. should print "1"
Console.WriteLine(TTTcheck(board3)); //. should print "2"
Console.WriteLine(TTTcheck(board4)); //. should print "-1"
}
 
static int TTTcheck(int[,] board)
{
int result = -1;
if ((board[0, 0] == board[0, 1]) && (board[0, 0] == board[0, 2]))
{
if (board[0, 0] == 1) return 1;
if (board[0, 0] == 2) return 2;
}
if ((board[1, 0] == board[1, 1]) && (board[1, 0] == board[1, 2]))
{
if (board[1, 0] == 1) return 1;
if (board[1, 0] == 2) return 2;
}
if ((board[2, 0] == board[2, 1]) && (board[2, 0] == board[2, 2]))
{
if (board[2, 0] == 1) return 1;
if (board[2, 0] == 2) return 2;
}
if ((board[0, 0] == board[1, 0]) && (board[0, 0] == board[2, 0]))
{
if (board[0, 0] == 1) return 1;
if (board[0, 0] == 2) return 2;
}
if ((board[0, 1] == board[1, 1]) && (board[0, 1] == board[2, 1]))
{
if (board[0, 1] == 1) return 1;
if (board[0, 1] == 2) return 2;
}
if ((board[0, 2] == board[1, 2]) && (board[0, 2] == board[2, 2]))
{
if (board[0, 2] == 1) return 1;
if (board[0, 2] == 2) return 2;
}
if ((board[0, 0] == board[1, 1]) && (board[0, 0] == board[2, 2]))
{
if (board[0, 0] == 1) return 1;
if (board[0, 0] == 2) return 2;
}
if ((board[0, 2] == board[1, 1]) && (board[0, 2] == board[2, 0]))
{
if (board[0, 2] == 1) return 1;
if (board[0, 2] == 2) return 2;
}
 
for (int i = 0, x = 0; (i < 3) & (x < 3); i++)
{
if ((board[i, 0] == 0) || (board[i, 1] == 0) || (board[i, 2] == 0))
return 0;
 
}
 
return result;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.