Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MiniMax
- {
- class GameBoard
- {
- public const int SIZE = 6;
- public const int EMPTY = 0;
- public const int WHITE = 1;
- public const int BLUE = 2;
- public int[,] gameboard = new int[SIZE, SIZE];
- public GameBoard()
- {
- for (int i=0;i<6;i++)
- {
- for (int j = 0; j < 6; j++)
- {
- gameboard[i, j] = EMPTY;
- }
- }
- }
- public void setPawn(AImove mv)
- {
- int x = mv.x;
- int y = mv.y;
- gameboard[x, y] = mv.color;
- }
- }
- //class AImove to save all parameters of move
- public class AImove
- {
- public AImove(int xx, int yy, int colorr)
- {
- x = xx;
- y = yy;
- color = colorr;
- }
- public AImove(int scr){
- score = scr;}
- public AImove() { }
- public int x;
- public int y;
- public int color;
- public int score;
- }
- class MiniMax
- {
- public enum side { ORDER, CHAOS };
- List<AImove> moves = new List<AImove>();
- //get best move for computer
- public AImove getBestMove(int[,] board, side aiside)
- {
- AImove plus10 = new AImove(10);
- AImove minus10 = new AImove(-10);
- AImove neutral = new AImove(0);
- //base cases for order
- if (aiside == side.ORDER)
- {
- if (doesOrderWin(board)== true)
- { return plus10; }
- else if (doesOrderWin(board) == false && nextMovePossible(board) == false)
- { return minus10; }
- else if (doesOrderWin(board) == false) return neutral;
- }
- //base cases for chaos
- if (aiside == side.ORDER)
- {
- if (doesOrderWin(board) == true)
- { return minus10; }
- else if (doesOrderWin(board) == false && nextMovePossible(board) == false)
- { return plus10; }
- else if (doesOrderWin(board) == false) return neutral;
- }
- //check recursive every possible grid
- for (int y = 0; y < 6; y++)
- {
- for (int x = 0; x < 6; x++)
- {
- if (board[x, y] == GameBoard.EMPTY)
- {
- AImove move = new AImove();
- move.x = x;
- move.y = y;
- //check for two different colors
- for (int clr = GameBoard.WHITE; clr <= GameBoard.BLUE; clr++)
- {
- move.color = clr;
- board[x, y] = clr;
- if (aiside == side.ORDER)
- {
- move.score = getBestMove(board, side.CHAOS).score;
- }
- else
- move.score = getBestMove(board, side.ORDER).score;
- moves.Add(move);
- board[x, y] = GameBoard.EMPTY;
- }
- }
- }
- }
- //pick the best move
- AImove bestMove = new AImove();
- if (aiside == side.ORDER)
- {
- int bestScore = -10000000;
- foreach (AImove mv in moves)
- {
- if (mv.score > bestScore)
- {
- bestMove = mv;
- bestScore = mv.score;
- }
- }
- }
- else
- {
- int bestScore = 10000000;
- foreach (AImove mv in moves)
- {
- if (mv.score < bestScore)
- {
- bestMove = mv;
- bestScore = mv.score;
- }
- }
- }
- return bestMove;
- }
- /////////////////////////////////////////////////////////////////////////////////////
- public bool doesOrderWin(int[,] gmbrd)
- {
- //check columns
- for (int x = 0; x < 6; x++)
- {
- if (theSamePawns(gmbrd, x, 0, 0, 1))
- return true;
- }
- for (int x = 0; x < 6; x++)
- {
- if (theSamePawns(gmbrd, x, 1, 0, 1))
- return true;
- }
- //check rows
- for (int y = 0; y <= 5; y++)
- {
- if (theSamePawns(gmbrd, 0, y, 1, 0))
- return true;
- }
- for (int y = 0; y <= 5; y++)
- {
- if (theSamePawns(gmbrd, 1, y, 1, 0))
- return true;
- }
- //check diagonal left to right
- if (theSamePawns(gmbrd, 0, 1, 1, 1))
- return true;
- if (theSamePawns(gmbrd, 0, 0, 1, 1))
- return true;
- if (theSamePawns(gmbrd, 1, 1, 1, 1))
- return true;
- if (theSamePawns(gmbrd, 1, 0, 1, 1))
- return true;
- //check diagonal right to left
- if (theSamePawns(gmbrd, 4, 0, -1, 1))
- return true;
- if (theSamePawns(gmbrd, 5, 0, -1, 1))
- return true;
- if (theSamePawns(gmbrd, 4, 1, -1, 1))
- return true;
- if (theSamePawns(gmbrd, 5, 1, -1, 1))
- return true;
- return false;
- }
- //check 5 grids in row
- public bool theSamePawns(int[,] board, int startX, int startY, int dx, int dy)
- {
- if (board[startX, startY] == GameBoard.EMPTY)
- return false;
- for (int i = 0; i < 5; i++)
- {
- int x = startX + (dx * i);
- int y = startY + (dy * i);
- if (board[x, y] != board[startX, startY])
- return false;
- }
- return true;
- }
- public bool nextMovePossible(int[,] board)
- {
- foreach (int grid in board)
- {
- if (grid == GameBoard.EMPTY)
- return true;
- }
- return false;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- class Program
- {
- static void Main(string[] args)
- {
- GameBoard gBoard = new GameBoard();
- MiniMax ai = new MiniMax();
- MiniMax.side computerside = MiniMax.side.ORDER;
- AImove bestaimove = new AImove();
- //fill random grids
- AImove ex00white = new AImove(0, 0, GameBoard.WHITE);
- gBoard.setPawn(ex00white);
- AImove ex13blue = new AImove(1, 3, GameBoard.BLUE);
- gBoard.setPawn(ex13blue);
- AImove ex14blue = new AImove(1, 4, GameBoard.BLUE);
- gBoard.setPawn(ex14blue);
- AImove ex15blue = new AImove(1, 5, GameBoard.BLUE);
- gBoard.setPawn(ex15blue);
- AImove ex10blue = new AImove(1, 0, GameBoard.BLUE);
- gBoard.setPawn(ex10blue);
- AImove ex03white = new AImove(0, 3, GameBoard.WHITE);
- gBoard.setPawn(ex03white);
- //find and show best move
- bestaimove = ai.getBestMove(gBoard.gameboard, computerside);
- Console.WriteLine("Best move is: {0} in x: {1} y: {2}", bestaimove.color, bestaimove.x, bestaimove.y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement