Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Chess.Data;
  5. using Type = Chess.Data.Type;
  6.  
  7. namespace Chess.Players.Algorithms
  8. {
  9.     public class WeightedPlayer : Player, IPlayer
  10.     {
  11.         //Simple robot player class which chooses the highest weighted move
  12.         private readonly Hashtable _hashtable = new Hashtable
  13.         {
  14.             {Type.King, 900},
  15.             {Type.Queen, 90},
  16.             {Type.Rook, 50},
  17.             {Type.Bishop, 30},
  18.             {Type.Knight, 30},
  19.             {Type.Pawn, 10}
  20.         };
  21.        
  22.         public WeightedPlayer(Colours colour) : base(colour) { }
  23.  
  24.         private List<KeyValuePair<Pos, KeyValuePair<int, Pos>>> GetValidWeightedMoves(Board board, Pos pos)
  25.         {
  26.             List<KeyValuePair<Pos, KeyValuePair<int, Pos>>> output = new List<KeyValuePair<Pos, KeyValuePair<int, Pos>>>();
  27.            
  28.             for (int x = 0; x < board.GetCols(); x++)
  29.                 for (int y = 0; y < board.GetRows(); y++)
  30.                     if (board.PieceAt(pos).ValidMove(board, pos, new Pos(x, y)))
  31.                     {
  32.                         if (board.PieceAt(new Pos(x, y)) != null)
  33.                             output.Add(new KeyValuePair<Pos, KeyValuePair<int, Pos>>(pos,
  34.                                 new KeyValuePair<int, Pos>((int) _hashtable[board.PieceAt(new Pos(x, y)).GetType()],
  35.                                     new Pos(x, y))));
  36.                         else
  37.                             output.Add(new KeyValuePair<Pos, KeyValuePair<int, Pos>>(pos,
  38.                                 new KeyValuePair<int, Pos>(0, new Pos(x, y))));
  39.                     }
  40.  
  41.             return output;
  42.         }
  43.  
  44.         public MovePieceData MovePiece(Board board)
  45.         {
  46.             List<KeyValuePair<Pos, KeyValuePair<int, Pos>>> validMoves = new List<KeyValuePair<Pos, KeyValuePair<int, Pos>>>();
  47.            
  48.             //Loops through every cell in the board, and adds all the valid movements for each piece to a list
  49.             for (int x = 0; x < board.GetCols(); x++)
  50.             for (int y = 0; y < board.GetRows(); y++)
  51.                 if (board.PieceAt(new Pos(x, y)) != null && board.PieceAt(new Pos(x, y)).GetColour() == _colour)
  52.                     validMoves.AddRange(GetValidWeightedMoves(board, new Pos(x, y)));
  53.  
  54.             //Sort list of movements by weight
  55.             validMoves = validMoves.OrderBy(o => o.Value.Key).Reverse().ToList();
  56.            
  57.             //Fetch positional information from highest weight movement
  58.             List<Pos> movement = new List<Pos> {validMoves[0].Key, validMoves[0].Value.Value};
  59.            
  60.             return base.MovePiece(board, movement[0], movement[1]);
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement