Skip to content

Instantly share code, notes, and snippets.

@kashwaa
Created March 29, 2014 17:00
Show Gist options
  • Save kashwaa/9858029 to your computer and use it in GitHub Desktop.
Save kashwaa/9858029 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Rotate
{
class Program
{
static void Main(string[] args)
{
var lines = File.ReadAllLines("data.txt");
int currentline=1;
for (int i = 0; i < Convert.ToInt32(lines[0]); i++)
{
int size = Convert.ToInt32(lines[currentline].Split(' ')[0]);
int k = Convert.ToInt32(lines[currentline].Split(' ')[1]);
currentline+=1;
List<string> game = new List<string>();
for (int o = 0; o < size; o++)
{
game.Add(lines[currentline + o]);
}
var table = load(game.ToArray());
table = Rotate(table);
table = applyGravity(table);
Console.WriteLine("Case #{0}: {1}",i+1,win(k,table).ToString());
currentline += size;
}
}
public enum Winner
{
Red,Blue,Both,Neither
}
public static char[,] load(string[] data)
{
char[,] table=new char[data.Length,data.Length];
for (int i = 0; i < data.Length; i++)
{
for (int o = 0; o < data.Length; o++)
{
table[i,o] = data[i][o];
}
}
return table;
}
public static Winner win(int k,char[,] table)
{
Winner winner=Winner.Neither;
for (int i = 0; i < table.GetLength(0); i++)
{
for (int o = 0; o < table.GetLength(0); o++)
{
if (table[i, o] != '.')
{
if (winner!=Winner.Red&&winner!=Winner.Both)
{
if (winH(k, table, 'R', i, o) || winV(k, table, 'R', i, o) || winD1(k, table, 'R', i, o) || winD2(k, table, 'R', i, o))
{
if (winner == Winner.Blue) winner = Winner.Both; else winner = Winner.Red;
}
}
if (winner != Winner.Blue && winner != Winner.Both)
{
if (winH(k, table, 'B', i, o) || winV(k, table, 'B', i, o) || winD1(k, table, 'B', i, o) || winD2(k, table, 'B', i, o))
{
if (winner == Winner.Red) winner = Winner.Both; else winner = Winner.Blue;
}
}
}
}
}
return winner;
}
public static bool winV(int k, char[,] table, char player, int x, int y)
{
bool winner = true;
for (int i = 0; i < k; i++)
{
try
{
if (table[x + i, y] != player)
{
winner = false;
}
}
catch (Exception)
{
return false;
}
}
return winner;
}
public static bool winH(int k, char[,] table, char player, int x, int y)
{
bool winner = true;
for (int i = 0; i < k; i++)
{
try
{
if (table[x, y+i] != player)
{
winner = false;
}
}
catch (Exception)
{
return false;
}
}
return winner;
}
public static bool winD1(int k, char[,] table, char player, int x, int y)
{
bool winner = true;
for (int i = 0; i < k; i++)
{
try
{
if (table[x+i, y + i] != player)
{
winner = false;
}
}
catch (Exception)
{
return false;
}
}
return winner;
}
public static bool winD2(int k, char[,] table, char player, int x, int y)
{
bool winner = true;
for (int i = 0; i < k; i++)
{
try
{
if (table[x + i, y - i] != player)
{
winner = false;
}
}
catch (Exception)
{
return false;
}
}
return winner;
}
public static char[,] Rotate(char[,] board)
{
char[,] newboard = new char[board.GetLength(0), board.GetLength(0)];
for (int x = 0; x < board.GetLength(0); x++)
{
for (int y = 0; y < board.GetLength(0); y++)
{
newboard[x, y] = board[board.GetLength(0) - y - 1, x];
}
}
return newboard;
}
public static char[,] applyGravity(char[,] board)
{
StringBuilder buffer = new StringBuilder();
for (int y = 0; y < board.GetLength(0); y++)
{
for (int x = 0; x < board.GetLength(0); x++)
{
buffer.Append(board[x, y]);
}
StringBuilder bufferA = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i]!='.')
{
bufferA.Append(buffer[i]);
}
}
int rep = bufferA.Length;
for (int i = 0; i < board.GetLength(0)-rep; i++)
{
bufferA.Insert(0,'.' );
}
for (int x = 0; x < board.GetLength(0); x++)
{
board[x, y]=bufferA[x];
}
buffer = new StringBuilder();
}
return board;
}
public static void printBoard(char[,] board)
{
for (int x = 0; x < board.GetLength(0); x++)
{
for (int y = 0; y < board.GetLength(0); y++)
{
Console.Write(board[x,y]);
}
Console.WriteLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment