Skip to content

Commit

Permalink
Fixed MiniMax (changed so that Player takes MinIndex)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmirZur committed Apr 19, 2018
1 parent 69b58d1 commit efb1db0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
18 changes: 9 additions & 9 deletions MerelsRules.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static void Main(string[] args)

private static GameManager gm;


private static string PrintBoard()
{
string result = "";
Expand Down Expand Up @@ -59,7 +58,7 @@ public static void PlayPlayerFirst()
Console.WriteLine(PrintBoard());
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Where do you want to place your piece?");
Console.WriteLine("Where do you want to place your piece (0-9)?");
int loc = int.Parse(Console.ReadLine());
Console.WriteLine();
//player placement
Expand All @@ -69,7 +68,6 @@ public static void PlayPlayerFirst()
if (gm.CheckGameEnd())
{
Console.WriteLine("You won!");
Console.ReadLine();
return;
}
//computer placement
Expand All @@ -78,7 +76,7 @@ public static void PlayPlayerFirst()
Console.WriteLine(PrintBoard());
if (gm.CheckGameEnd())
{
Console.ReadLine();
Console.WriteLine("Computer won.");
return;
}
}
Expand All @@ -101,7 +99,7 @@ public static void PlayPlayerFirst()
if (gm.CheckGameEnd())
{
Console.WriteLine("You won!");
break;
return;
}
//computer move
Tuple<int, int> compMove = gm.MakeMove(initial, destination);
Expand All @@ -110,7 +108,7 @@ public static void PlayPlayerFirst()
if (gm.CheckGameEnd())
{
Console.WriteLine("Computer won.");
break;
return;
}
}
}
Expand Down Expand Up @@ -143,14 +141,14 @@ public static void PlayComputerFirst()
Console.WriteLine(PrintBoard());
if (gm.CheckGameEnd())
{
Console.WriteLine("Computer won.");
Console.ReadLine();
return;
}
}
Console.WriteLine("Where do you want to place your piece?");
int location = int.Parse(Console.ReadLine());
Console.WriteLine();
//player placement
gm.MakeMove(-1, location);
Console.WriteLine("Your placement:");
Console.WriteLine(PrintBoard());
Expand All @@ -171,7 +169,8 @@ public static void PlayComputerFirst()
if (gm.CheckGameEnd())
{
Console.WriteLine("Computer won.");
break;
Console.ReadLine();
return;
}
//player move
Console.WriteLine("What piece do you want to move (0-9)? ");
Expand All @@ -185,7 +184,8 @@ public static void PlayComputerFirst()
if (gm.CheckGameEnd())
{
Console.WriteLine("You won!");
break;
Console.ReadLine();
return;
}
}
}
Expand Down
38 changes: 18 additions & 20 deletions MerelsRules/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public void SetPlayer(Piece piece)
Computer = SwitchPiece(Player);
}

public bool CheckGameEnd()
{
return CheckGameEnd(Board);
}

public Tuple<int, int> MakeMove(int moveInitial, int moveDestination)
{
if (CurrentTurn == Player)
Expand Down Expand Up @@ -109,7 +114,7 @@ private Tuple<int, int> MiniMax(Tuple<Piece[], int[]> boardAndLocations, Piece p

int[] pieceLocations = new int[inputPieceLocations.Length];
Array.Copy(inputPieceLocations, pieceLocations, inputPieceLocations.Length);

if (CheckScore(board, Computer) != 0)
return new Tuple<int, int>(CheckScore(board, Computer), depth);
else if (depth >= MaxDepth) return new Tuple<int, int>(0, depth);
Expand Down Expand Up @@ -151,18 +156,13 @@ private Tuple<int, int> MiniMax(Tuple<Piece[], int[]> boardAndLocations, Piece p
}
else
{
int MinScoreIndex = GetMaxScoreIndex(scores);
int MinScoreIndex = GetMinScoreIndex(scores);
_choiceInitial = moves[MinScoreIndex].Item1;
_choiceDestination = moves[MinScoreIndex].Item2;
return scores[MinScoreIndex];
}
}

public bool CheckGameEnd()
{
return CheckGameEnd(Board);
}

private static List<int> GetMoves(Piece[] board, Piece player, int location)
{
Debug.Assert(location >= 0);
Expand All @@ -189,8 +189,8 @@ private static List<int> GetMoves(Piece[] board, Piece player, int location)
private static int GetMaxScoreIndex(List<Tuple<int, int>> scores)
{
int maxInd = 0;
int max = 0;
for (int i = 0; i < scores.Count; i++)
int max = scores[0].Item1 - scores[0].Item2;
for (int i = 1; i < scores.Count; i++)
{
if (scores[i].Item1 - scores[i].Item2 > max)
{
Expand All @@ -204,13 +204,13 @@ private static int GetMaxScoreIndex(List<Tuple<int, int>> scores)
private static int GetMinScoreIndex(List<Tuple<int, int>> scores)
{
int minInd = 0;
int min = 0;
for (int i = 0; i < scores.Count; i++)
int min = scores[0].Item1 + scores[0].Item2;
for (int i = 1; i < scores.Count; i++)
{
if (scores[i].Item1 - scores[i].Item2 < min)
if (scores[i].Item1 + scores[i].Item2 < min)
{
minInd = i;
min = scores[i].Item1 - scores[i].Item2;
min = scores[i].Item1 + scores[i].Item2;
}
}
return minInd;
Expand Down Expand Up @@ -271,26 +271,24 @@ private static Tuple<Piece[], int[]> MakeBoardMove(Piece[] board, int[] pieceLoc

private static Tuple<Piece[], int[]> MakeBoardPlacement(Piece[] board, int[] pieceLocations, Piece piece, int location)
{
//changes location of piece in board
//sets location of piece in board
Piece[] newBoard = new Piece[board.Length];
Array.Copy(board, newBoard, board.Length);
newBoard[location] = piece;

//changes location of piece in list of piece locations
//sets location of piece in list of piece locations
int[] newPieceLocations = new int[pieceLocations.Length];
Array.Copy(pieceLocations, newPieceLocations, pieceLocations.Length);
int index = ((int)piece - 1) * 3;
newPieceLocations[Array.IndexOf(newPieceLocations, -1, index)] = location;
newPieceLocations[Array.IndexOf(newPieceLocations, -1, index, 3)] = location;

return new Tuple<Piece[], int[]>(newBoard, newPieceLocations);
}

private static bool CheckPlacementDone(int[] pieceLocations, Piece player)
{
int[] newPieceLocations = new int[pieceLocations.Length];
int index = ((int)player - 1) * 3;
Array.Copy(pieceLocations, index, newPieceLocations, 0, 3);
return Array.IndexOf(newPieceLocations, -1) < 0;
return Array.IndexOf(pieceLocations, -1, ((int)player - 1) * 3, 3) < 0;
}

}
}

0 comments on commit efb1db0

Please sign in to comment.