Advertisement
b0b3kpl

MiniMax for OC

Nov 5th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MiniMax
  8. {
  9.     class GameBoard
  10.     {
  11.         public const int SIZE = 6;
  12.         public const int EMPTY = 0;
  13.         public const int WHITE = 1;
  14.         public const int BLUE = 2;
  15.         public int[,] gameboard = new int[SIZE, SIZE];
  16.  
  17.         public GameBoard()
  18.         {
  19.             for (int i=0;i<6;i++)
  20.             {
  21.                 for (int j = 0; j < 6; j++)
  22.                 {
  23.                     gameboard[i, j] = EMPTY;
  24.                 }
  25.             }
  26.          }
  27.  
  28.         public void setPawn(AImove mv)
  29.         {
  30.             int x = mv.x;
  31.             int y = mv.y;
  32.             gameboard[x, y] = mv.color;
  33.         }
  34.     }
  35.  
  36.     //class AImove to save all parameters of move
  37.     public class AImove
  38.     {
  39.         public AImove(int xx, int yy, int colorr)
  40.         {
  41.             x = xx;
  42.             y = yy;
  43.             color = colorr;
  44.         }
  45.         public AImove(int scr){
  46.             score = scr;}
  47.         public AImove() { }
  48.  
  49.         public int x;
  50.         public int y;
  51.         public int color;
  52.         public int score;
  53.     }
  54.     class MiniMax
  55.     {
  56.         public enum side { ORDER, CHAOS };
  57.         List<AImove> moves = new List<AImove>();
  58.  
  59.         //get best move for computer
  60.         public AImove getBestMove(int[,] board, side aiside)
  61.         {
  62.             AImove plus10 = new AImove(10);
  63.             AImove minus10 = new AImove(-10);
  64.             AImove neutral = new AImove(0);
  65.  
  66.             //base cases for order
  67.             if (aiside == side.ORDER)
  68.             {
  69.                 if (doesOrderWin(board)== true)
  70.                 { return plus10; }
  71.                 else if (doesOrderWin(board) == false && nextMovePossible(board) == false)
  72.                 { return minus10; }
  73.                 else if (doesOrderWin(board) == false) return neutral;
  74.             }
  75.             //base cases for chaos
  76.             if (aiside == side.ORDER)
  77.             {
  78.                 if (doesOrderWin(board) == true)
  79.                 { return minus10; }
  80.                 else if (doesOrderWin(board) == false && nextMovePossible(board) == false)
  81.                 { return plus10; }
  82.                 else if (doesOrderWin(board) == false) return neutral;
  83.             }
  84.             //check recursive every possible grid
  85.             for (int y = 0; y < 6; y++)
  86.             {
  87.                 for (int x = 0; x < 6; x++)
  88.                 {
  89.                     if (board[x, y] == GameBoard.EMPTY)
  90.                     {
  91.                         AImove move = new AImove();
  92.                         move.x = x;
  93.                         move.y = y;
  94.  
  95.                         //check for two different colors
  96.                         for (int clr = GameBoard.WHITE; clr <= GameBoard.BLUE; clr++)
  97.                         {
  98.                             move.color = clr;
  99.                             board[x, y] = clr;
  100.  
  101.                             if (aiside == side.ORDER)
  102.                             {
  103.                                 move.score = getBestMove(board, side.CHAOS).score;
  104.                             }
  105.                             else
  106.                                 move.score = getBestMove(board, side.ORDER).score;
  107.  
  108.                             moves.Add(move);
  109.                             board[x, y] = GameBoard.EMPTY;
  110.                         }
  111.  
  112.                     }
  113.                 }
  114.             }
  115.  
  116.             //pick the best move
  117.             AImove bestMove = new AImove();
  118.             if (aiside == side.ORDER)
  119.             {
  120.                 int bestScore = -10000000;
  121.                 foreach (AImove mv in moves)
  122.                 {
  123.                     if (mv.score > bestScore)
  124.                     {
  125.                         bestMove = mv;
  126.                         bestScore = mv.score;
  127.                     }
  128.                 }
  129.             }
  130.  
  131.             else
  132.             {
  133.                 int bestScore = 10000000;
  134.                 foreach (AImove mv in moves)
  135.                 {
  136.                     if (mv.score < bestScore)
  137.                     {
  138.                         bestMove = mv;
  139.                         bestScore = mv.score;
  140.                     }
  141.                 }
  142.             }
  143.  
  144.             return bestMove;
  145.         }
  146.  
  147.         /////////////////////////////////////////////////////////////////////////////////////
  148.  
  149.         public bool doesOrderWin(int[,] gmbrd)
  150.         {
  151.             //check columns
  152.             for (int x = 0; x < 6; x++)
  153.             {
  154.                 if (theSamePawns(gmbrd, x, 0, 0, 1))
  155.                     return true;
  156.             }
  157.             for (int x = 0; x < 6; x++)
  158.             {
  159.                 if (theSamePawns(gmbrd, x, 1, 0, 1))
  160.                     return true;
  161.             }
  162.  
  163.             //check rows
  164.             for (int y = 0; y <= 5; y++)
  165.             {
  166.                 if (theSamePawns(gmbrd, 0, y, 1, 0))
  167.                     return true;
  168.             }
  169.             for (int y = 0; y <= 5; y++)
  170.             {
  171.                 if (theSamePawns(gmbrd, 1, y, 1, 0))
  172.                     return true;
  173.             }
  174.  
  175.             //check diagonal left to right
  176.             if (theSamePawns(gmbrd, 0, 1, 1, 1))
  177.                 return true;
  178.             if (theSamePawns(gmbrd, 0, 0, 1, 1))
  179.                 return true;
  180.             if (theSamePawns(gmbrd, 1, 1, 1, 1))
  181.                 return true;
  182.             if (theSamePawns(gmbrd, 1, 0, 1, 1))
  183.                 return true;
  184.  
  185.             //check diagonal right to left
  186.             if (theSamePawns(gmbrd, 4, 0, -1, 1))
  187.                 return true;
  188.             if (theSamePawns(gmbrd, 5, 0, -1, 1))
  189.                 return true;
  190.             if (theSamePawns(gmbrd, 4, 1, -1, 1))
  191.                 return true;
  192.             if (theSamePawns(gmbrd, 5, 1, -1, 1))
  193.                 return true;
  194.  
  195.             return false;
  196.         }
  197.  
  198.         //check 5 grids in row
  199.         public bool theSamePawns(int[,] board, int startX, int startY, int dx, int dy)
  200.         {
  201.             if (board[startX, startY] == GameBoard.EMPTY)
  202.                 return false;
  203.             for (int i = 0; i < 5; i++)
  204.             {
  205.                 int x = startX + (dx * i);
  206.                 int y = startY + (dy * i);
  207.  
  208.                 if (board[x, y] != board[startX, startY])
  209.                     return false;
  210.             }
  211.             return true;
  212.         }
  213.  
  214.         public bool nextMovePossible(int[,] board)
  215.         {
  216.             foreach (int grid in board)
  217.             {
  218.                 if (grid == GameBoard.EMPTY)
  219.                     return true;
  220.             }
  221.             return false;
  222.         }
  223.     }
  224.  
  225.     /////////////////////////////////////////////////////////////////////////////
  226.  
  227.     class Program
  228.     {
  229.         static void Main(string[] args)
  230.         {
  231.             GameBoard gBoard = new GameBoard();
  232.             MiniMax ai = new MiniMax();
  233.             MiniMax.side computerside = MiniMax.side.ORDER;
  234.             AImove bestaimove = new AImove();
  235.  
  236.             //fill random grids
  237.             AImove ex00white = new AImove(0, 0, GameBoard.WHITE);
  238.             gBoard.setPawn(ex00white);
  239.             AImove ex13blue = new AImove(1, 3, GameBoard.BLUE);
  240.             gBoard.setPawn(ex13blue);
  241.             AImove ex14blue = new AImove(1, 4, GameBoard.BLUE);
  242.             gBoard.setPawn(ex14blue);
  243.             AImove ex15blue = new AImove(1, 5, GameBoard.BLUE);
  244.             gBoard.setPawn(ex15blue);
  245.             AImove ex10blue = new AImove(1, 0, GameBoard.BLUE);
  246.             gBoard.setPawn(ex10blue);
  247.             AImove ex03white = new AImove(0, 3, GameBoard.WHITE);
  248.             gBoard.setPawn(ex03white);
  249.  
  250.             //find and show best move
  251.             bestaimove = ai.getBestMove(gBoard.gameboard, computerside);
  252.  
  253.             Console.WriteLine("Best move is: {0} in x: {1} y: {2}", bestaimove.color, bestaimove.x, bestaimove.y);
  254.         }
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement