Skip to content

Commit

Permalink
Initial commit. Computer can calculate best move (depth set to 3), bu…
Browse files Browse the repository at this point in the history
…t only from set-up positions.
  • Loading branch information
AmirZur committed Apr 11, 2018
1 parent 74eb73f commit 13fe444
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 30 deletions.
6 changes: 6 additions & 0 deletions MerelsRules.ConsoleApp/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
58 changes: 58 additions & 0 deletions MerelsRules.ConsoleApp/MerelsRules.ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C71875CC-2ED7-46AB-B115-F310E08E5263}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>MerelsRules.ConsoleApp</RootNamespace>
<AssemblyName>MerelsRules.ConsoleApp</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MerelsRules\MerelsRules.csproj">
<Project>{5667b2df-d76c-4f31-809e-569597600193}</Project>
<Name>MerelsRules</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
92 changes: 92 additions & 0 deletions MerelsRules.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;

namespace MerelsRules.ConsoleApp
{
public class Program
{
static void Main(string[] args)
{
gm = GameManager.GetInstance();
//for(int i = 0; i < 6; i++)
//{
// Console.WriteLine(PrintBoard());
// GameManager.Piece p = i % 2 == 0 ? GameManager.Piece.O : GameManager.Piece.X;
// Console.WriteLine("Where do you want to place a " + p.ToString() + "? ");
// int loc = int.Parse(Console.ReadLine());
// gm.Board[loc] = p;
// gm.PieceLocations[((i % 2) * 3) + (i / 2)] = loc;
//}
int[] locations = new[] { 0, 1, 2, 3, 7, 8 };
for (int i = 0; i < 6; i++)
{
GameManager.Piece p = i % 2 == 0 ? GameManager.Piece.O : GameManager.Piece.X;
int loc = locations[i];
gm.Board[loc] = p;
gm.PieceLocations[((i % 2) * 3) + (i / 2)] = loc;
}

Console.WriteLine("\nBeginning Game\n");

while(true)
{
Console.WriteLine(PrintBoard());
Console.WriteLine("What piece do you want to move (0-9)? ");
int initial = int.Parse(Console.ReadLine());
Console.WriteLine("Where do you want to move the piece (0-9)? ");
int destination = int.Parse(Console.ReadLine());
Console.WriteLine();
//player
gm.MakeMove(initial, destination);
Console.WriteLine("Your move:");
Console.WriteLine(PrintBoard() + "\n");
if (gm.CheckGameEnd())
{
Console.WriteLine("You won!");
break;
}
//computer
Tuple<int, int> compMove = gm.MakeMove(initial, destination);
Console.WriteLine("The computer moved the piece on " + compMove.Item1 + " to " + compMove.Item2);
Console.WriteLine(PrintBoard());
if (gm.CheckGameEnd())
{
Console.WriteLine("Computer won.");
break;
}
}

Console.ReadLine();
}

private static GameManager gm;


private static string PrintBoard()
{
string result = "";
result += PrintRow(SubArray(gm.Board, 0, 3)) + "\n";
result += "|\\|/|" + "\n";
result += PrintRow(SubArray(gm.Board, 3, 3)) + "\n";
result += "|/|\\|" + "\n";
result += PrintRow(SubArray(gm.Board, 6, 3));
return result;
}

private static string PrintRow(GameManager.Piece[] row)
{
string result = (row[0] == GameManager.Piece.Empty ? " " : row[0].ToString());
for(int i = 1; i < row.Length; i++)
{
result += "-" + (row[i]==GameManager.Piece.Empty?" ":row[i].ToString());
}
return result;
}

private static T[] SubArray<T>(T[] data, int index, int length)
{
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
}
}
36 changes: 36 additions & 0 deletions MerelsRules.ConsoleApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MerelsRules.ConsoleApp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MerelsRules.ConsoleApp")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c71875cc-2ed7-46ab-b115-f310e08e5263")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
6 changes: 6 additions & 0 deletions MerelsRules.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.27428.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MerelsRules", "MerelsRules\MerelsRules.csproj", "{5667B2DF-D76C-4F31-809E-569597600193}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MerelsRules.ConsoleApp", "MerelsRules.ConsoleApp\MerelsRules.ConsoleApp.csproj", "{C71875CC-2ED7-46AB-B115-F310E08E5263}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{5667B2DF-D76C-4F31-809E-569597600193}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5667B2DF-D76C-4F31-809E-569597600193}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5667B2DF-D76C-4F31-809E-569597600193}.Release|Any CPU.Build.0 = Release|Any CPU
{C71875CC-2ED7-46AB-B115-F310E08E5263}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C71875CC-2ED7-46AB-B115-F310E08E5263}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C71875CC-2ED7-46AB-B115-F310E08E5263}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C71875CC-2ED7-46AB-B115-F310E08E5263}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 0 additions & 12 deletions MerelsRules/Class1.cs

This file was deleted.

Loading

0 comments on commit 13fe444

Please sign in to comment.