Skip to content

Commit

Permalink
completed puzzleState: implementation of StateBase for FifteenPuzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth committed Jul 8, 2017
1 parent bd20393 commit fdb34da
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 6 deletions.
6 changes: 6 additions & 0 deletions FifteenPuzzle/FifteenPuzzle.csproj
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Search\Search.csproj">
<Project>{B081B079-214A-4C6B-842B-83663FD8CEA9}</Project>
<Name>Search</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 4 additions & 2 deletions FifteenPuzzle/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;

namespace FifteenPuzzle
{ public class Program
{
public class Program
{
public static void Main(string[] args)
{
try {
if(args.Length == 0)
{
RunAllTestCases();
} else if (args.Length == 2)
}
else if (args.Length == 2)
{
RunSingleSearch();
}
Expand Down
224 changes: 220 additions & 4 deletions FifteenPuzzle/PuzzleState.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,228 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Search;

namespace FifteenPuzzle
{
class PuzzleState
public struct PuzzleSpace
{
public int Row { get; private set; }
public int Col { get; private set; }

public PuzzleSpace(int row, int col)
{
Row = row;
Col = col;
}
}

public class PuzzleState : StateBase
{
private Dictionary<int, PuzzleSpace> _valueLookup;

public int[,] Board { get; private set; }

public PuzzleState(int[,] board, Dictionary<int, PuzzleSpace> valueLookup)
{
Board = board;
_valueLookup = valueLookup;
}

public PuzzleState(string stateAsString)
{
// example stateAsString:
// ((1 5 3 7) (4 9 2 11) (8 13 10 14) (12 15 0 6) (3 2))

Board = new int[4,4];
string[] parts = stateAsString.Split(new[] { ") (" }, StringSplitOptions.None);

for (int i = 0; i < 4; i++)
{
string part = parts[i].TrimStart(new[] { '(', ' ' }).TrimEnd(new[] { ')', ' ' });
string[] nums = part.Split();

for (int j = 0; j < 4; j++)
{
int num = int.Parse(nums[j]);
Board[i, j] = num;
_valueLookup[num] = new PuzzleSpace(i, j);

}
}
}

#region equality

public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals(obj as PuzzleState);
}

public override bool Equals(StateBase other)
{
return this.GetHashCode().Equals(other.GetHashCode());
}

#endregion

public override int GetHashCode()
{
int hashCode = 13;

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
hashCode = (hashCode * 397) ^ Board[i, j] ^ (i + j);
}
}

return hashCode;
}

public PuzzleSpace GetSpace(int num)
{
return _valueLookup[num];
}

public PuzzleState Clone()
{
Dictionary<int, PuzzleSpace> valueLookup = new Dictionary<int, PuzzleSpace>(16);
int[,] newBoard = new int[4, 4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int num = Board[i, j];
newBoard[i, j] = num;
valueLookup[num] = new PuzzleSpace(i, j);
}
}

return new PuzzleState(newBoard, valueLookup);
}

#region Actions

public PuzzleState Up()
{

PuzzleSpace oldVoidLocation = GetSpace(0);

if (oldVoidLocation.Row == 0)
{
return null;
}

PuzzleState newState = Clone();
PuzzleSpace newVoidLocation = new PuzzleSpace(oldVoidLocation.Row - 1, oldVoidLocation.Col);

int valToBeSwapped = newState.Board[newVoidLocation.Row, newVoidLocation.Col];
newState.Board[newVoidLocation.Row, newVoidLocation.Col] = 0;
newState.Board[oldVoidLocation.Row, oldVoidLocation.Col] = valToBeSwapped;
newState._valueLookup[0] = newVoidLocation;
newState._valueLookup[valToBeSwapped] = oldVoidLocation;
return newState;
}

public PuzzleState Down()
{
PuzzleSpace oldVoidLocation = GetSpace(0);

if (oldVoidLocation.Row == 3)
{
return null;
}

PuzzleState newState = Clone();
PuzzleSpace newVoidLocation = new PuzzleSpace(oldVoidLocation.Row + 1, oldVoidLocation.Col);

int valToBeSwapped = newState.Board[newVoidLocation.Row, newVoidLocation.Col];
newState.Board[newVoidLocation.Row, newVoidLocation.Col] = 0;
newState.Board[oldVoidLocation.Row, oldVoidLocation.Col] = valToBeSwapped;
newState._valueLookup[0] = newVoidLocation;
newState._valueLookup[valToBeSwapped] = oldVoidLocation;
return newState;

}

public PuzzleState Left()
{
PuzzleSpace oldVoidLocation = GetSpace(0);

if (oldVoidLocation.Col == 0)
{
return null;
}

PuzzleState newState = Clone();

PuzzleSpace newVoidLocation = new PuzzleSpace(oldVoidLocation.Row, oldVoidLocation.Col - 1);

int valToBeSwapped = newState.Board[newVoidLocation.Row, newVoidLocation.Col];
newState.Board[newVoidLocation.Row, newVoidLocation.Col] = 0;
newState.Board[oldVoidLocation.Row, oldVoidLocation.Col] = valToBeSwapped;
newState._valueLookup[0] = newVoidLocation;
newState._valueLookup[valToBeSwapped] = oldVoidLocation;
return newState;

}

public PuzzleState Right()
{
PuzzleSpace oldVoidLocation = GetSpace(0);

if (oldVoidLocation.Col == 3)
{
return null;
}

PuzzleState newState = Clone();
PuzzleSpace newVoidLocation = new PuzzleSpace(oldVoidLocation.Row, oldVoidLocation.Col + 1);

int valToBeSwapped = newState.Board[newVoidLocation.Row, newVoidLocation.Col];
newState.Board[newVoidLocation.Row, newVoidLocation.Col] = 0;
newState.Board[oldVoidLocation.Row, oldVoidLocation.Col] = valToBeSwapped;
newState._valueLookup[0] = newVoidLocation;
newState._valueLookup[valToBeSwapped] = oldVoidLocation;
return newState;

}

#endregion

public override IDictionary<string, StateBase> Successors()
{
Dictionary<string, StateBase> dict = new Dictionary<string, StateBase>();
var u = Up();
var d = Down();
var l = Left();
var r = Right();

if (u != null)
{
dict["U"] = u;
}

if (d != null)
{
dict["D"] = d;
}

if (l != null)
{
dict["L"] = l;
}

if (r != null)
{
dict["R"] = r;
}

return dict;
}
}
}

0 comments on commit fdb34da

Please sign in to comment.