Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using Chess.Data;
- using Type = Chess.Data.Type;
- namespace Chess.Players.Algorithms
- {
- public class WeightedPlayer : Player, IPlayer
- {
- //Simple robot player class which chooses the highest weighted move
- private readonly Hashtable _hashtable = new Hashtable
- {
- {Type.King, 900},
- {Type.Queen, 90},
- {Type.Rook, 50},
- {Type.Bishop, 30},
- {Type.Knight, 30},
- {Type.Pawn, 10}
- };
- public WeightedPlayer(Colours colour) : base(colour) { }
- private List<KeyValuePair<Pos, KeyValuePair<int, Pos>>> GetValidWeightedMoves(Board board, Pos pos)
- {
- List<KeyValuePair<Pos, KeyValuePair<int, Pos>>> output = new List<KeyValuePair<Pos, KeyValuePair<int, Pos>>>();
- for (int x = 0; x < board.GetCols(); x++)
- for (int y = 0; y < board.GetRows(); y++)
- if (board.PieceAt(pos).ValidMove(board, pos, new Pos(x, y)))
- {
- if (board.PieceAt(new Pos(x, y)) != null)
- output.Add(new KeyValuePair<Pos, KeyValuePair<int, Pos>>(pos,
- new KeyValuePair<int, Pos>((int) _hashtable[board.PieceAt(new Pos(x, y)).GetType()],
- new Pos(x, y))));
- else
- output.Add(new KeyValuePair<Pos, KeyValuePair<int, Pos>>(pos,
- new KeyValuePair<int, Pos>(0, new Pos(x, y))));
- }
- return output;
- }
- public MovePieceData MovePiece(Board board)
- {
- List<KeyValuePair<Pos, KeyValuePair<int, Pos>>> validMoves = new List<KeyValuePair<Pos, KeyValuePair<int, Pos>>>();
- //Loops through every cell in the board, and adds all the valid movements for each piece to a list
- for (int x = 0; x < board.GetCols(); x++)
- for (int y = 0; y < board.GetRows(); y++)
- if (board.PieceAt(new Pos(x, y)) != null && board.PieceAt(new Pos(x, y)).GetColour() == _colour)
- validMoves.AddRange(GetValidWeightedMoves(board, new Pos(x, y)));
- //Sort list of movements by weight
- validMoves = validMoves.OrderBy(o => o.Value.Key).Reverse().ToList();
- //Fetch positional information from highest weight movement
- List<Pos> movement = new List<Pos> {validMoves[0].Key, validMoves[0].Value.Value};
- return base.MovePiece(board, movement[0], movement[1]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement