void Start() { m_param = GetComponent <BoardParam>(); m_param.Destination = Destination.None; m_boardStop = GetComponent <StopBoard>(); m_boardMove = GetComponent <MoveBoard>(); m_boardPreparation = GetComponent <BoardPreparation>(); m_turn = FindObjectOfType <TurnManager>(); }
private void compMove() { //snapshot and worker square[8, 8] arrays board brd = new board(); Array.Copy(game.squares, brd.snapshot, game.squares.Length); Array.Copy(game.squares, brd.worker, game.squares.Length); //blank moveBoard MoveBoard[,] movboard = new MoveBoard[8, 8]; //blank panels Panel[,] pans = new Panel[8, 8]; //Piece being evaled Piece currEval = new Piece(); //dictionary that stores points that each piece has in algorithm Dictionary <Piece, int> hits = new Dictionary <Piece, int>(); //dictionary that temporarily stores the max points a Piece has, used for deciding which move a piece will make Dictionary <Piece, int> tempHits = new Dictionary <Piece, int>(); //dictionary that temporarily stores the max points a Piece has, used for deciding best move to make after initial piece move Dictionary <Piece, int> tempHits_2 = new Dictionary <Piece, int>(); //populate dictionary foreach (Piece pce in game.opponentPieces) { if (pce.alive) { hits.Add(pce, 0); tempHits.Add(pce, 0); } } //how far to look ahead, based on difficulty int limiter = 0; if (game.difficulty == "easy") { limiter = 20; } else if (game.difficulty == "medium") { limiter = 30; } else { limiter = 40; } foreach (Piece pce in game.opponentPieces) { /*Here we start by making every piece have a shot at moving first, the one that allows the best future moves, * will be selected and moved. I know it's primitive, but we're not trying to outdo HAL! */ Array.Copy(brd.snapshot, brd.worker, brd.snapshot.Length); Array.Clear(movboard, 0, movboard.Length); currEval = pce; bool isCheck = false; if (pce.alive) { //first move showValidMoves(pce, brd.worker, ref movboard, ref pans); //now cycle through each potetial move of each potential piece we could move for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (movboard[x, y].moveable) { //now move to each movable slot move(pce, x, y, false, ref brd.worker, out isCheck); if (!isCheck) { //add points to piece since it was able to be moved, helps AI break check situation hits[pce] += 10; //now we dig deeper for (int a = 0; a < limiter; a++) { //run through pieces, the one that has most move options is chosen //________________________________________________________________ // NOTE : IN FUTURE, OPTIMIZE THIS SECTION FOR BETTER CRITERIA! foreach (Piece nextPce in game.opponentPieces) { showValidMoves(nextPce, brd.worker, ref movboard, ref pans); for (int p = 0; p < 8; p++) { for (int q = 0; q < 8; q++) { if (movboard[p, q].moveable) { if (brd.worker[p, q].name != null) { hits[pce] += 40; tempHits_2[nextPce] += 40; } hits[pce] += 10; tempHits_2[nextPce] += 10; } } } } //make move Piece holdr = new Piece(); int max = 0; foreach (KeyValuePair <Piece, int> temp in tempHits_2) { if (temp.Value > max) { holdr = temp.Key; max = temp.Value; } } //continue here, we made first move, now we found most profitable next move //finding moves that have holdr above average could be a better way of identing profitability //challenge : pick where holdr (most profitable piece moves to) foreach (Piece pice in game.opponentPieces) { if (pice.name == holdr.name) { //move most profitable second piece } } } } } } } } } }
private bool check(square[,] squares) { Piece king = new Piece(); Panel[,] pan = new Panel[8, 8]; Array.Copy(game.renderer, pan, game.renderer.Length); MoveBoard[,] movboard = new MoveBoard[8, 8]; Array.Copy(game.MovBoard, movboard, game.MovBoard.Length); //work here if (userTurn) { for (int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { if (game.userColor == Color.White) { if (squares[a, b].name == "wking") { king.x = a; king.y = b; king.type = PTypes.king; break; } } else { if (squares[a, b].name == "bking") { king.x = a; king.y = b; king.type = PTypes.king; break; } } } } //is check? foreach (Piece pce in game.opponentPieces) { if (pce.alive) { userTurn = false; showValidMoves(pce, squares, ref movboard, ref pan); userTurn = true; if (movboard[king.x, king.y].moveable) { return(true); } } } } else { for (int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { if (game.userColor != Color.White) { if (squares[a, b].name == "wking") { king.x = a; king.y = b; king.type = PTypes.king; break; } } else { if (squares[a, b].name == "bking") { king.x = a; king.y = b; king.type = PTypes.king; break; } } } } foreach (Piece pce in game.userPieces) { if (pce.alive) { userTurn = true; showValidMoves(pce, squares, ref movboard, ref pan); userTurn = false; if (movboard[king.x, king.y].moveable) { return(true); } } } } return(false); }
private bool checkMate(square[,] squares) { //TODO //Fix issue where in multiplayer, pieces switch from alive to dead // **Should be fixed, keep testing //More checkMate() optimizations might be required square[,] nsqrs = new square[8, 8]; Array.Copy(squares, nsqrs, squares.Length); MoveBoard[,] movboard = new MoveBoard[8, 8]; MoveBoard[,] nmovbrd = new MoveBoard[8, 8]; Panel[,] pan = new Panel[8, 8]; Array.Copy(game.renderer, pan, game.renderer.Length); Piece[] oppPieces = new Piece[16]; Array.Copy(game.opponentPieces, oppPieces, game.opponentPieces.Length); //define both kings Piece bking = new Piece(); Piece wking = new Piece(); for (int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { if (squares[a, b].name == "bking") { bking.name = "bking"; bking.type = PTypes.king; bking.x = a; bking.y = b; } else if (squares[a, b].name == "wking") { wking.name = "wking"; wking.type = PTypes.king; wking.x = a; wking.y = b; } } } bool inCheck = false; if (userTurn) { foreach (Piece pice in game.userPieces) { showValidMoves(pice, nsqrs, ref movboard, ref pan); Array.Copy(movboard, nmovbrd, movboard.Length); Array.Copy(game.opponentPieces, oppPieces, game.opponentPieces.Length); //reset background color for (int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { if (a % 2 == 0) { game.renderer[a, b].BackColor = (b % 2 == 0) ? Color.White : Color.Gray; } else { game.renderer[a, b].BackColor = (b % 2 == 0) ? Color.Gray : Color.White; } } } inCheck = false; for (int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { Array.Copy(nmovbrd, movboard, movboard.Length); if (movboard[a, b].moveable) { move(pice, a, b, true, ref nsqrs, out inCheck); Array.Copy(oppPieces, game.opponentPieces, game.opponentPieces.Length); if (inCheck) { inCheck = true; } else { //reset background color for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (x % 2 == 0) { game.renderer[x, y].BackColor = (y % 2 == 0) ? Color.White : Color.Gray; } else { game.renderer[x, y].BackColor = (y % 2 == 0) ? Color.Gray : Color.White; } } } Array.Copy(oppPieces, game.opponentPieces, game.opponentPieces.Length); return(false); } } } } } } else { foreach (Piece pice in game.opponentPieces) { showValidMoves(pice, nsqrs, ref movboard, ref pan); inCheck = false; for (int a = 0; a < 8; a++) { for (int b = 0; b < 8; b++) { if (movboard[a, b].moveable) { move(bking, a, b, true, ref nsqrs, out inCheck); Array.Copy(oppPieces, game.opponentPieces, game.opponentPieces.Length); if (inCheck) { inCheck = true; } else { //reset background color for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (x % 2 == 0) { game.renderer[x, y].BackColor = (y % 2 == 0) ? Color.White : Color.Gray; } else { game.renderer[x, y].BackColor = (y % 2 == 0) ? Color.Gray : Color.White; } } } Array.Copy(oppPieces, game.opponentPieces, game.opponentPieces.Length); return(false); } } } } } //reset background color for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { if (x % 2 == 0) { game.renderer[x, y].BackColor = (y % 2 == 0) ? Color.White : Color.Gray; } else { game.renderer[x, y].BackColor = (y % 2 == 0) ? Color.Gray : Color.White; } } } } Array.Copy(oppPieces, game.opponentPieces, game.opponentPieces.Length); if (inCheck) { return(true); } else { return(false); } }