Created
December 6, 2012 22:03
-
-
Save ackjake/4228921 to your computer and use it in GitHub Desktop.
Checks the outcome of a tic tac toe game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment