Skip to content

Instantly share code, notes, and snippets.

@kthompson
Forked from anonymous/GameOfFifteen.cs
Last active August 29, 2015 14:06
Show Gist options
  • Save kthompson/9de261f71ca8226ae80c to your computer and use it in GitHub Desktop.
Save kthompson/9de261f71ca8226ae80c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GameOfFifteen
{
class Program
{
static void Main(string[] args)
{
var game = new Game();
Scramble(game, 100);
while (true)
{
Console.Clear();
PrintGrid(game);
Console.WriteLine();
Console.WriteLine("Enter your move(or q to quit): ");
var key = Console.ReadKey();
switch (key.Key)
{
case ConsoleKey.LeftArrow:
game.MoveBy(-1, 0);
break;
case ConsoleKey.RightArrow:
game.MoveBy(1, 0);
break;
case ConsoleKey.UpArrow:
game.MoveBy(0, -1);
break;
case ConsoleKey.DownArrow:
game.MoveBy(0, 1);
break;
default:
if (key.KeyChar == 'q')
return;
break;
}
if (game.Won())
{
Console.Clear();
PrintGrid(game);
Console.WriteLine();
Console.WriteLine("You won!!");
Console.WriteLine();
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
return;
}
}
}
private static void Scramble(Game game, int count)
{
var random = new Random();
var directions = new[]
{
new Position{X = 0, Y = 1},
new Position{X = 0, Y = -1},
new Position{X = 1, Y = 0},
new Position{X = -1, Y = 0},
};
while (count > 0)
{
count--;
var d = directions[random.Next(4)];
game.MoveBy(d.X, d.Y);
}
}
private static void PrintGrid(Game game)
{
for (int x = 0; x < game.Width; x++)
{
for (int y = 0; y < game.Height; y++)
{
var n = game[x, y].ToString();
if (n == "-1") n = "";
Console.Write("| {0} ", n.PadLeft(2));
}
Console.WriteLine("|");
}
}
}
class Position
{
public int X { get; set; }
public int Y { get; set; }
}
class Game
{
public int[,] Grid { get; set; }
public Position CurrentPosition { get; private set; }
private int _numTiles;
public Game()
{
this.Grid = new[,]
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, -1}
};
_numTiles = Width*Height;
UpdateCurrentPosition();
}
public bool MoveBy(int x, int y)
{
var current = this.CurrentPosition;
var newPos = new Position {X = current.X + x, Y = current.Y + y};
if (!CanMoveTo(newPos))
return false;
var source = this[current];
var dest = this[newPos];
this[current] = dest;
this[newPos] = source;
this.CurrentPosition = newPos;
return true;
}
public int this[Position p]
{
get { return this.Grid[p.Y, p.X]; }
set { this.Grid[p.Y, p.X] = value; }
}
public int this[int x, int y]
{
get { return this.Grid[x, y]; }
set { this.Grid[x, y] = value; }
}
public bool Won()
{
var counter = 1;
foreach (var n in IterateCells())
{
if (n == -1 && counter == _numTiles)
return true;
if (n != counter)
return false;
counter++;
}
return false;
}
public int Height
{
get { return this.Grid.GetUpperBound(1) + 1; }
}
public int Width
{
get { return this.Grid.GetUpperBound(0) + 1; }
}
private void UpdateCurrentPosition()
{
for (var x = 0; x < Width; x++)
for (var y = 0; y < Height; y++)
if (this[x, y] == -1)
this.CurrentPosition = new Position {X = x, Y = y};
}
private bool CanMoveTo(Position pos)
{
return pos.X < Width && pos.Y < Height &&
pos.X >= 0 && pos.Y >= 0;
}
private IEnumerable<int> IterateCells()
{
for (var x = 0; x < Width; x++)
for (var y = 0; y < Height; y++)
yield return this[x, y];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment