/// <summary> /// Funkce nastavujici hraci figurku na kralovnu, pokud dosla na druhy konec hraci desky /// </summary> /// <param name="fw"></param> private void setQueen(Fairway fw) { // zjistime X-ovou souradnici pole int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; // podle toho, o jakeho se jedna hrace switch (fw.Player) { case status.blackPlayer: if (x == GlobalVariables.size - 1) { fw.Queen = true; fw.Image = Resources.queen_black; } break; case status.whitePlayer: if (x == 0) { fw.Queen = true; fw.Image = Resources.queen_white; } break; } }
private void genQueMoves(Gameboard gb, Fairway fw) { status playerOnMove = gb.PlayingWhite ? status.whitePlayer : status.blackPlayer; List <string> moves = fw.Moves; int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; int temp = 0; for (int i = 0; i < 8; i++) { for (int j = 1; x + j * xs[i] < GlobalVariables.size && x + j * xs[i] >= 0 && y + j * ys[i] >= 0 && y + j * ys[i] < GlobalVariables.size && gb.Board[x + j * xs[i], y + j * ys[i]].Player == status.free; j++) { if (gb.Board[x + j * xs[i], y + j * ys[i]].Player == 0) { string move = (char)(y + j * ys[i] + 'A') + "" + (x + j * xs[i] + 1); moves.Add(move); } temp = j + 1; } fw.StoneMove = moves.Count > 0 ? true : false; genQueJumpTree(gb, fw); } }
/// <summary> /// Funkce generujici pole hracich poli, na ktere jde tahnout /// </summary> /// <param name="gb">Instrance tridy hraci deska</param> /// <param name="fw">Instance tridy hraci pole</param> /// <returns>Vraci List hracich poli, na ktere je mozno figurkou tahnout</returns> private List <Fairway> generateDests(Gameboard gb, Fairway fw) { var dest = new List <Fairway>(); var temp = new List <String>(); if (fw.Jump) { for (int i = 0; i < fw.Dests.Count; i++) { foreach (var jump in fw.Dests) { string[] jumps = jump.Split('-'); temp.Add(jumps[0]); } } } else { temp = fw.Moves; } foreach (var fair in gb.Board) { if (temp.Contains(fair.Name)) { dest.Add(fair); } } return(dest); }
private void FriskaDama_Load(object sender, EventArgs e) { Fairway fw; char letter = 'A'; addDescriptions(); for (int i = 0; i < GlobalVariables.size; i++) { for (int j = 0; j < GlobalVariables.size; j++) { fw = new Fairway(); fw.Size = new Size(FAIRWAY_WIDTH, FAIRWAY_HEIGHT); fw.Enabled = false; fw.Location = new Point(FAIRWAY_SHIFT + (i * FAIRWAY_HEIGHT), 2 * FAIRWAY_SHIFT + (j * FAIRWAY_WIDTH)); fw.BackgroundImage = ((i + j) % 2 == 1) ? Resources.board_dark : Resources.board_light; gb.Board[j, i] = fw; Controls.Add(fw); } letter = (char)(letter + 1); } gb.Winner = status.free; gb.mainDialog = this; statusBarLabel.Text = "Pro spuštění nové hry nebo načtení uložené hry stiskněte Hra v levém horním rohu"; }
private Fairway randomTo(Gameboard gb, Fairway fw) { List <string> destinations = gb.possibleMoves(fw); int position = random.Next(0, destinations.Count); string name = destinations[position]; return(gb.getFigure(name)); }
public void addHistory(Fairway from, Fairway to) { string typ = from.Jump ? "skok" : "tah"; string hrac = PlayingWhite ? "Bily hrac: " : "Cerny hrac: "; string input = string.Format("{0} {1} -> {2} ({3})", hrac, from.Name, to.Name, typ); addMissedHistory(input); }
private void randomAI(Gameboard gb) { Fairway from = randomFrom(gb); Fairway to = randomTo(gb, from); gb.addHistory(from, to); rules.makeMove(from, to, gb, true); }
/// <summary> /// Funkce uvolnujici hraci pole /// </summary> /// <param name="fw">Hraci pole, ktere ma byt uvolneno</param> private void setFree(Fairway fw) { fw.Dests = new List <string>(); fw.Overs = new List <string>(); fw.Player = status.free; fw.Queen = false; fw.Jump = false; fw.StoneMove = false; fw.Image = null; }
public Gameboard() { Board = new Fairway[GlobalVariables.size,GlobalVariables.size]; PlayingWhite = true; WhitePlayer = 0; BlackPlayer = 0; ShowMoveHelp = true; From = null; Winner = status.free; }
public Gameboard() { Board = new Fairway[GlobalVariables.size, GlobalVariables.size]; PlayingWhite = true; WhitePlayer = 0; BlackPlayer = 0; ShowMoveHelp = true; From = null; Winner = status.free; }
private void genWalMoves(Gameboard gb, Fairway fw) { if (!gb.PlayingWhite) { walkersMoves(gb, fw, 1, -1); walkersMoves(gb, fw, 1, 1); } else { walkersMoves(gb, fw, -1, -1); walkersMoves(gb, fw, -1, 1); } }
public Fairway getFigureInBetween(Fairway f, Fairway t) { var result = new Fairway(); for (int i = 0; i < f.Dests.Count; i++) { if (f.Dests[i].Equals(t.Name)) { return(getFigure(f.Overs[i])); } } return(new Fairway()); }
/// <summary> /// Funkce reprezentujici provedeni tahu /// </summary> /// <param name="from">Instance tridy hraci pole - odkud se tahne</param> /// <param name="to">Instance tridy hraci pole - kam se tahne</param> private void makeMove(Fairway from, Fairway to) { to.Jump = from.Jump; to.Player = from.Player; to.Queen = from.Queen; to.Image = from.Image; to.Dests = new List <string>(); to.Overs = new List <string>(); from.Moves = new List <string>(); to.Moves = new List <string>(); setFree(from); setQueen(to); }
private void genJumps(Gameboard gb, Fairway fw) { int x, y; TreeNode root = fw.Queen ? genQueJumpTree(gb, fw) : genWalJumpTree(gb, fw); fw.Ranks = new List <int>(); fw.Dests = new List <string>(); fw.Overs = new List <string>(); List <string> routes = root.allRoutes(root); foreach (var route in routes) { var r = new List <string>(route.Split('-')); r.RemoveAll(c => c.Equals("")); int rank = 0; string dest = ""; string over = ""; for (int i = 0; i < r.Count; i += 2) { x = Convert.ToInt32(r[i].Substring(1)) - 1; y = Convert.ToInt32(r[i][0]) - 'A'; rank += gb.Board[x, y].Queen ? 3 : 2; if (over == "") { over = r[i]; } else { over = over + "-" + r[i]; } if (dest == "") { dest = r[i + 1]; } else { dest = dest + "-" + r[i + 1]; } } if (rank > 0 && dest != "" && over != "") { fw.Ranks.Add(rank); fw.Dests.Add(dest); fw.Overs.Add(over); } } fw.Jump = fw.Ranks.Count > 0 ? true : false; }
private void fw_2ndClick(object sender, EventArgs e) { var form = Form.ActiveForm as Friska_dama; BackgroundWorker bw = form.bw; var fwClicked = (Fairway)sender; List <Fairway> playersFigures = rules.choosableFigures(this); List <string> posMoves = possibleMoves(From); if (fwClicked == From) { foreach (var fw in Board) { fw.Click -= fw_2ndClick; fw.Click += fw_1stClick; } if (ShowMoveHelp) { colorUp(); } From = null; form.statusBarLabel.Text = "Vyberte figurku, kterou chcete táhnout"; } else if (fwClicked != From && playersFigures.Contains(fwClicked)) { From = fwClicked; fairwayColorUp(this, From); } else if (posMoves.Contains(fwClicked.Name)) { addHistory(From, fwClicked); rules.makeMove(From, fwClicked, this, false); Winner = rules.checkWinner(this); from = null; foreach (var fw in Board) { fw.Click -= fw_2ndClick; fw.Click += fw_1stClick; } if (ShowMoveHelp) { colorUp(); } form.Activate(); form.FriskaDama_Activated(sender, e); } }
public Fairway Clone() { var result = new Fairway(); result.player = Player; result.Queen = Queen; result.Jump = Jump; result.StoneMove = StoneMove; result.Moves = Moves; result.Name = Name; result.Dests = Dests; result.Overs = Overs; result.Ranks = Ranks; return(result); }
private void walkersMoves(Gameboard gb, Fairway fw, int dx, int dy) { int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; if (x + dx >= 0 && x + dx < GlobalVariables.size && y + dy >= 0 && y + dy < GlobalVariables.size && gb.Board[x + dx, y + dy].Player == 0) { List <String> moves = fw.Moves; moves.Add((char)(y + dy + 'A') + "" + (x + dx + 1)); fw.StoneMove = moves.Count > 0 ? true : false; fw.Moves = moves; } fw.StoneMove = fw.Moves.Count > 0 ? true : false; }
/// <summary> /// Funkce kontrolujici, zda v danem smeru je za figurkou volno /// </summary> /// <param name="gb">Instance tridy hraci deska</param> /// <param name="fw">Instance tridy hraci pole reprezentujici odkud kontrolujeme</param> /// <param name="dx">X smer kterym kontrolujeme</param> /// <param name="dy">Y smer kterym kontrolujeme</param> /// <returns>True, pokud je za figurou v danem smeru volno, jinak false</returns> private bool checkPossibleJump(Gameboard gb, Fairway fw, int dx, int dy) { status playerOnMove = gb.PlayingWhite ? status.whitePlayer : status.blackPlayer; int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; if (x + 2 * dx < GlobalVariables.size && x + 2 * dx >= 0 && y + 2 * dy < GlobalVariables.size && y + 2 * dy >= 0 && (int)gb.Board[x + dx, y + dy].Player == -(int)playerOnMove && gb.Board[x + 2 * dx, y + 2 * dy].Player == 0) { return(true); } else { return(false); } }
// funkce obarvujici figurky, jemiz je mozne tahnout public void colorUp() { List <Fairway> figures = rules.choosableFigures(this); for (int i = 0; i < GlobalVariables.size; i++) { for (int j = 0; j < GlobalVariables.size; j++) { Fairway chosen = Board[i, j]; if (figures.Contains(chosen) && ShowMoveHelp) { chosen.BackgroundImage = Resources.board_dark_active_figure; } else { chosen.BackgroundImage = ((i + j) % 2 == 1) ? Resources.board_dark : Resources.board_light; } } } }
private List <Fairway> queAfterJumpDests(Gameboard gb, Fairway fw, int dx, int dy) { var result = new List <Fairway>(); int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; for (int j = 0; x + dx < GlobalVariables.size && x + dx >= 0 && y + dy < GlobalVariables.size && y + dy >= 0 && (gb.Board[x + dx, y + dy].Player == status.free); j++) { result.Add(gb.Board[x + dx, y + dy]); x += dx; y += dy; } return(result); }
public List <string> possibleMoves(Fairway fw) { var possibleMoves = new List <string>(); if (fw.Dests.Count > 0) { for (int i = 0; i < fw.Dests.Count; i++) { string[] dests = fw.Dests[i].Split('-'); if (!possibleMoves.Contains(dests[0])) { possibleMoves.Add(dests[0]); } } } else { possibleMoves = fw.Moves; } return(possibleMoves); }
// funkce obarvujici mozne tahy figurkou private void fairwayColorUp(Gameboard gb, Fairway fw) { List <string> moves = fw.Moves; for (int i = 0; i < GlobalVariables.size; i++) { for (int j = 0; j < GlobalVariables.size; j++) { Fairway chosen = gb.Board[i, j]; if (moves.Contains(chosen.Name) && gb.ShowMoveHelp) { chosen.BackgroundImage = Resources.board_dark_active; } else { chosen.BackgroundImage = ((i + j) % 2 == 1) ? Resources.board_dark : Resources.board_light; } } } fw.BackgroundImage = Resources.board_dark_active_figure; }
private Gameboard MiniMaxAI(Gameboard gb, int depth) { List <Fairway> bMove = bestMove(gb, depth); string f = bMove[0].Name; string t = bMove[1].Name; var from = new Fairway(); var to = new Fairway(); foreach (var fw in gb.Board) { from = fw.Name == f ? fw : from; to = fw.Name == t ? fw : to; } rules.generateMoves(gb, from); gb.addHistory(from, to); rules.makeMove(from, to, gb, true); return(gb); }
private TreeNode genWalJumpTree(Gameboard gb, Fairway fw) { var root = new TreeNode(); int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; for (int i = 0; i < 8; i++) { if (checkPossibleJump(gb, fw, xs[i], ys[i])) { Gameboard potomek = gb.gameboardCopy(); Fairway from = potomek.Board[x, y]; Fairway over = potomek.Board[x + xs[i], y + ys[i]]; Fairway to = potomek.Board[x + 2 * xs[i], y + 2 * ys[i]]; TreeNode jump; bool bQ = from.Queen; makeJump(from, over, to); bool aQ = to.Queen; if (bQ == aQ) { jump = genWalJumpTree(potomek, to); } else { jump = new TreeNode(); } jump.over = over.Name; jump.dest = to.Name; root.childs.Add(jump); jump.rank = over.Queen ? 3 : 2; } } return(root); }
private Gameboard MiniMaxAI(Gameboard gb, int depth) { List<Fairway> bMove = bestMove(gb, depth); string f = bMove[0].Name; string t = bMove[1].Name; var from = new Fairway(); var to = new Fairway(); foreach (var fw in gb.Board) { from = fw.Name == f ? fw : from; to = fw.Name == t ? fw : to; } rules.generateMoves(gb, from); gb.addHistory(from, to); rules.makeMove(from, to, gb, true); return gb; }
private void FriskaDama_Load(object sender, EventArgs e) { ur.mainDialog = this; Fairway fw; char letter = 'A'; addDescriptions(); for (int i = 0; i < GlobalVariables.size; i++) { for (int j = 0; j < GlobalVariables.size; j++) { fw = new Fairway(); fw.Size = new Size(FAIRWAY_WIDTH, FAIRWAY_HEIGHT); fw.Enabled = false; fw.Location = new Point(FAIRWAY_SHIFT + (i * FAIRWAY_HEIGHT), 2 * FAIRWAY_SHIFT + (j * FAIRWAY_WIDTH)); fw.BackgroundImage = ((i + j) % 2 == 1) ? Properties.Resources.board_dark : Properties.Resources.board_light; gb.Board[j, i] = fw; Controls.Add(fw); } letter = (char)((int)letter + 1); } this.gb.Winner = status.free; this.gb.mainDialog = this; statusBarLabel.Text = "Pro spuštění nové hry nebo načtení uložené hry stiskněte Hra v levém horním rohu"; }
private void fw_2ndClick(object sender, EventArgs e) { var form = Form.ActiveForm as Friska_dama; BackgroundWorker bw = form.bw; var fwClicked = (Fairway) sender; List<Fairway> playersFigures = rules.choosableFigures(this); List<string> posMoves = possibleMoves(From); if (fwClicked == From) { foreach (var fw in Board) { fw.Click -= fw_2ndClick; fw.Click += fw_1stClick; } if (ShowMoveHelp) colorUp(); From = null; form.statusBarLabel.Text = "Vyberte figurku, kterou chcete táhnout"; } else if (fwClicked != From && playersFigures.Contains(fwClicked)) { From = fwClicked; fairwayColorUp(this, From); } else if (posMoves.Contains(fwClicked.Name)) { addHistory(From, fwClicked); rules.makeMove(From, fwClicked, this, false); Winner = rules.checkWinner(this); from = null; foreach (var fw in Board) { fw.Click -= fw_2ndClick; fw.Click += fw_1stClick; } if (ShowMoveHelp) colorUp(); form.Activate(); form.FriskaDama_Activated(sender, e); } }
public List<string> possibleMoves(Fairway fw) { var possibleMoves = new List<string>(); if (fw.Dests.Count > 0) { for (int i = 0; i < fw.Dests.Count; i++) { string[] dests = fw.Dests[i].Split('-'); if (!possibleMoves.Contains(dests[0])) possibleMoves.Add(dests[0]); } } else { possibleMoves = fw.Moves; } return possibleMoves; }
public Fairway getFigureInBetween(Fairway f, Fairway t) { var result = new Fairway(); for (int i = 0; i < f.Dests.Count; i++) { if (f.Dests[i].Equals(t.Name)) return getFigure(f.Overs[i]); } return new Fairway(); }
private void genQueMoves(Gameboard gb, Fairway fw) { status playerOnMove = gb.PlayingWhite ? status.whitePlayer : status.blackPlayer; List<string> moves = fw.Moves; int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; int temp = 0; for (int i = 0; i < 8; i++) { for (int j = 1; x + j*xs[i] < GlobalVariables.size && x + j*xs[i] >= 0 && y + j*ys[i] >= 0 && y + j*ys[i] < GlobalVariables.size && gb.Board[x + j*xs[i], y + j*ys[i]].Player == status.free; j++) { if (gb.Board[x + j*xs[i], y + j*ys[i]].Player == 0) { string move = (char) (y + j*ys[i] + 'A') + "" + (x + j*xs[i] + 1); moves.Add(move); } temp = j + 1; } fw.StoneMove = moves.Count > 0 ? true : false; genQueJumpTree(gb, fw); } }
private List<Fairway> queAfterJumpDests(Gameboard gb, Fairway fw, int dx, int dy) { var result = new List<Fairway>(); int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; for (int j = 0; x + dx < GlobalVariables.size && x + dx >= 0 && y + dy < GlobalVariables.size && y + dy >= 0 && (gb.Board[x + dx, y + dy].Player == status.free); j++) { result.Add(gb.Board[x + dx, y + dy]); x += dx; y += dy; } return result; }
public void generateMoves(Gameboard gb, Fairway fw) { genJumps(gb, fw); var newDests = new List <string>(); var newOvers = new List <string>(); if (fw.Jump) { int bestRank = fw.Ranks[0]; int rank = fw.Ranks[0]; fw.Moves = new List <string>(); for (int i = 0; i < fw.Ranks.Count; i++) { rank = fw.Ranks[i]; string[] d = fw.Dests[i].Split('-'); string[] o = fw.Overs[i].Split('-'); if (rank == bestRank) { if (!newDests.Contains(d[0])) { newDests.Add(d[0]); newOvers.Add(o[0]); } if (!fw.Moves.Contains(d[0])) { fw.Moves.Add(d[0]); } } else if (rank > bestRank) { bestRank = rank; fw.Moves = new List <string>(); newDests = new List <string>(); newOvers = new List <string>(); if (!newDests.Contains(d[0])) { newDests.Add(d[0]); } if (!newOvers.Contains(o[0])) { newOvers.Add(o[0]); } if (!fw.Moves.Contains(d[0])) { fw.Moves.Add(d[0]); } } } } else if (fw.Queen) { genQueMoves(gb, fw); } else { genWalMoves(gb, fw); } fw.Overs = newOvers; fw.Dests = newDests; }
public void generateMoves(Gameboard gb, Fairway fw) { genJumps(gb, fw); var newDests = new List<string>(); var newOvers = new List<string>(); if (fw.Jump) { int bestRank = fw.Ranks[0]; int rank = fw.Ranks[0]; fw.Moves = new List<string>(); for (int i = 0; i < fw.Ranks.Count; i++) { rank = fw.Ranks[i]; string[] d = fw.Dests[i].Split('-'); string[] o = fw.Overs[i].Split('-'); if (rank == bestRank) { if (!newDests.Contains(d[0])) { newDests.Add(d[0]); newOvers.Add(o[0]); } if (!fw.Moves.Contains(d[0])) fw.Moves.Add(d[0]); } else if (rank > bestRank) { bestRank = rank; fw.Moves = new List<string>(); newDests = new List<string>(); newOvers = new List<string>(); if (!newDests.Contains(d[0])) newDests.Add(d[0]); if (!newOvers.Contains(o[0])) newOvers.Add(o[0]); if (!fw.Moves.Contains(d[0])) fw.Moves.Add(d[0]); } } } else if (fw.Queen) genQueMoves(gb, fw); else genWalMoves(gb, fw); fw.Overs = newOvers; fw.Dests = newDests; }
private TreeNode genQueJumpTree(Gameboard gb, Fairway fw) { var root = new TreeNode(); var moves = new List <string>(); int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; status playerOnMove = gb.PlayingWhite ? status.whitePlayer : status.blackPlayer; for (int i = 0; i < 8; i++) { bool jump = false; for (int j = 1; x + j * xs[i] < GlobalVariables.size && x + j * xs[i] >= 0 && y + j * ys[i] < GlobalVariables.size && y + j * ys[i] >= 0; j++) { // pokud je vedle volno, pridej do moves if (gb.Board[x + j * xs[i], y + j * ys[i]].Player == status.free && !jump) { string move = (char)(y + j * ys[i] + 'A') + "" + (x + j * xs[i] + 1); } // pokud je vedle tvoje vlastni figurka, prerus else if (gb.Board[x + j * xs[i], y + j * ys[i]].Player == playerOnMove) { break; } else if ( x + (j + 1) * xs[i] < GlobalVariables.size && x + (j + 1) * xs[i] >= 0 && y + (j + 1) * ys[i] >= 0 && y + (j + 1) * ys[i] < GlobalVariables.size && (int)gb.Board[x + j * xs[i], y + j * ys[i]].Player == -(int)playerOnMove && gb.Board[x + (j + 1) * xs[i], y + (j + 1) * ys[i]].Player == status.free) { jump = true; Gameboard potomek = gb.gameboardCopy(); Fairway from = potomek.Board[x, y]; Fairway over = potomek.Board[x + j * xs[i], y + j * ys[i]]; Fairway to = potomek.Board[x + (j + 1) * xs[i], y + (j + 1) * ys[i]]; makeJump(from, over, to); List <Fairway> destinies = queAfterJumpDests(potomek, to, xs[i], ys[i]); destinies.Insert(0, to); foreach (var des in destinies) { TreeNode node = genQueJumpTree(potomek, des); node.over = over.Name; node.dest = des.Name; node.rank = over.Queen ? 3 : 2; root.childs.Add(node); } } else { break; } } } return(root); }
public TreeNode(Fairway over, Fairway to) { dest = to.Name; this.over = over.Name; childs = new List <TreeNode>(); }
/// <summary> /// Funkce uvolnujici hraci pole /// </summary> /// <param name="fw">Hraci pole, ktere ma byt uvolneno</param> private void setFree(Fairway fw) { fw.Dests = new List<string>(); fw.Overs = new List<string>(); fw.Player = status.free; fw.Queen = false; fw.Jump = false; fw.StoneMove = false; fw.Image = null; }
public Fairway Clone() { var result = new Fairway(); result.player = Player; result.Queen = Queen; result.Jump = Jump; result.StoneMove = StoneMove; result.Moves = Moves; result.Name = Name; result.Dests = Dests; result.Overs = Overs; result.Ranks = Ranks; return result; }
public void makeMove(Fairway from, Fairway to, Gameboard gb, bool minimax) { if (!from.Moves.Contains(to.Name) && !minimax) { throw new Exception("Neplatný tah"); } bool changePlayer = true; if (from.Jump) { gb.MovesWithoutJump = 0; Fairway over = gb.getFigureInBetween(from, to); bool bQ = from.Queen; makeJump(from, over, to); bool aQ = to.Queen; if (to.Jump && bQ == aQ) { foreach (var fw in gb.Board) { fw.Jump = false; fw.StoneMove = false; fw.Moves = new List <string>(); fw.Ranks = new List <int>(); fw.Dests = new List <string>(); fw.Overs = new List <string>(); } generateMoves(gb, to); gb.Winner = checkWinner(gb); if (to.Jump) { changePlayer = false; } else { changePlayer = true; } } if (bQ == aQ) { generateMoves(gb, to); } } else { gb.MovesWithoutJump += 1; makeMove(from, to); setQueen(to); foreach (var fw in gb.Board) { fw.Jump = false; fw.StoneMove = false; fw.Moves = new List <string>(); fw.Ranks = new List <int>(); fw.Dests = new List <string>(); fw.Overs = new List <string>(); } } if (changePlayer) { gb.PlayingWhite = !gb.PlayingWhite; generateMoves(gb); gb.Winner = checkWinner(gb); } }
/// <summary> /// Funkce reprezentujici provedeni tahu /// </summary> /// <param name="from">Instance tridy hraci pole - odkud se tahne</param> /// <param name="to">Instance tridy hraci pole - kam se tahne</param> private void makeMove(Fairway from, Fairway to) { to.Jump = from.Jump; to.Player = from.Player; to.Queen = from.Queen; to.Image = from.Image; to.Dests = new List<string>(); to.Overs = new List<string>(); from.Moves = new List<string>(); to.Moves = new List<string>(); setFree(from); setQueen(to); }
private TreeNode genQueJumpTree(Gameboard gb, Fairway fw) { var root = new TreeNode(); var moves = new List<string>(); int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; status playerOnMove = gb.PlayingWhite ? status.whitePlayer : status.blackPlayer; for (int i = 0; i < 8; i++) { bool jump = false; for (int j = 1; x + j*xs[i] < GlobalVariables.size && x + j*xs[i] >= 0 && y + j*ys[i] < GlobalVariables.size && y + j*ys[i] >= 0; j++) { // pokud je vedle volno, pridej do moves if (gb.Board[x + j*xs[i], y + j*ys[i]].Player == status.free && !jump) { string move = (char) (y + j*ys[i] + 'A') + "" + (x + j*xs[i] + 1); } // pokud je vedle tvoje vlastni figurka, prerus else if (gb.Board[x + j*xs[i], y + j*ys[i]].Player == playerOnMove) { break; } else if ( x + (j + 1)*xs[i] < GlobalVariables.size && x + (j + 1)*xs[i] >= 0 && y + (j + 1)*ys[i] >= 0 && y + (j + 1)*ys[i] < GlobalVariables.size && (int) gb.Board[x + j*xs[i], y + j*ys[i]].Player == -(int) playerOnMove && gb.Board[x + (j + 1)*xs[i], y + (j + 1)*ys[i]].Player == status.free) { jump = true; Gameboard potomek = gb.gameboardCopy(); Fairway from = potomek.Board[x, y]; Fairway over = potomek.Board[x + j*xs[i], y + j*ys[i]]; Fairway to = potomek.Board[x + (j + 1)*xs[i], y + (j + 1)*ys[i]]; makeJump(from, over, to); List<Fairway> destinies = queAfterJumpDests(potomek, to, xs[i], ys[i]); destinies.Insert(0, to); foreach (var des in destinies) { TreeNode node = genQueJumpTree(potomek, des); node.over = over.Name; node.dest = des.Name; node.rank = over.Queen ? 3 : 2; root.childs.Add(node); } } else { break; } } } return root; }
/// <summary> /// Funkce realizujici skok /// </summary> /// <param name="from">pole ze ktereho se tahne</param> /// <param name="over">pole pres ktere se tahne</param> /// <param name="to">pole kam se tahne</param> private void makeJump(Fairway from, Fairway over, Fairway to) { makeMove(from, to); setFree(over); setQueen(to); }
// funkce obarvujici mozne tahy figurkou private void fairwayColorUp(Gameboard gb, Fairway fw) { List<string> moves = fw.Moves; for (int i = 0; i < GlobalVariables.size; i++) { for (int j = 0; j < GlobalVariables.size; j++) { Fairway chosen = gb.Board[i, j]; if (moves.Contains(chosen.Name) && gb.ShowMoveHelp) chosen.BackgroundImage = Resources.board_dark_active; else chosen.BackgroundImage = ((i + j)%2 == 1) ? Resources.board_dark : Resources.board_light; } } fw.BackgroundImage = Resources.board_dark_active_figure; }
private void genJumps(Gameboard gb, Fairway fw) { int x, y; TreeNode root = fw.Queen ? genQueJumpTree(gb, fw) : genWalJumpTree(gb, fw); fw.Ranks = new List<int>(); fw.Dests = new List<string>(); fw.Overs = new List<string>(); List<string> routes = root.allRoutes(root); foreach (var route in routes) { var r = new List<string>(route.Split('-')); r.RemoveAll(c => c.Equals("")); int rank = 0; string dest = ""; string over = ""; for (int i = 0; i < r.Count; i += 2) { x = Convert.ToInt32(r[i].Substring(1)) - 1; y = Convert.ToInt32(r[i][0]) - 'A'; rank += gb.Board[x, y].Queen ? 3 : 2; if (over == "") over = r[i]; else over = over + "-" + r[i]; if (dest == "") dest = r[i + 1]; else dest = dest + "-" + r[i + 1]; } if (rank > 0 && dest != "" && over != "") { fw.Ranks.Add(rank); fw.Dests.Add(dest); fw.Overs.Add(over); } } fw.Jump = fw.Ranks.Count > 0 ? true : false; }
public TreeNode(Fairway over, Fairway to) { this.dest = to.Name; this.over = over.Name; childs = new List<TreeNode>(); }
public void makeMove(Fairway from, Fairway to, Gameboard gb, bool minimax) { if (!from.Moves.Contains(to.Name) && !minimax) throw new Exception("Neplatný tah"); bool changePlayer = true; if (from.Jump) { gb.MovesWithoutJump = 0; Fairway over = gb.getFigureInBetween(from, to); bool bQ = from.Queen; makeJump(from, over, to); bool aQ = to.Queen; if (to.Jump && bQ == aQ) { foreach (var fw in gb.Board) { fw.Jump = false; fw.StoneMove = false; fw.Moves = new List<string>(); fw.Ranks = new List<int>(); fw.Dests = new List<string>(); fw.Overs = new List<string>(); } generateMoves(gb, to); gb.Winner = checkWinner(gb); if (to.Jump) changePlayer = false; else changePlayer = true; } if (bQ == aQ) { generateMoves(gb, to); } } else { gb.MovesWithoutJump += 1; makeMove(from, to); setQueen(to); foreach (var fw in gb.Board) { fw.Jump = false; fw.StoneMove = false; fw.Moves = new List<string>(); fw.Ranks = new List<int>(); fw.Dests = new List<string>(); fw.Overs = new List<string>(); } } if (changePlayer) { gb.PlayingWhite = !gb.PlayingWhite; generateMoves(gb); gb.Winner = checkWinner(gb); } }
/// <summary> /// Funkce generujici pole hracich poli, na ktere jde tahnout /// </summary> /// <param name="gb">Instrance tridy hraci deska</param> /// <param name="fw">Instance tridy hraci pole</param> /// <returns>Vraci List hracich poli, na ktere je mozno figurkou tahnout</returns> private List<Fairway> generateDests(Gameboard gb, Fairway fw) { var dest = new List<Fairway>(); var temp = new List<String>(); if (fw.Jump) { for (int i = 0; i < fw.Dests.Count; i++) { foreach (var jump in fw.Dests) { string[] jumps = jump.Split('-'); temp.Add(jumps[0]); } } } else temp = fw.Moves; foreach (var fair in gb.Board) { if (temp.Contains(fair.Name)) dest.Add(fair); } return dest; }
private Fairway randomTo(Gameboard gb, Fairway fw) { List<string> destinations = gb.possibleMoves(fw); int position = random.Next(0, destinations.Count); string name = destinations[position]; return gb.getFigure(name); }
private void walkersMoves(Gameboard gb, Fairway fw, int dx, int dy) { int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; if (x + dx >= 0 && x + dx < GlobalVariables.size && y + dy >= 0 && y + dy < GlobalVariables.size && gb.Board[x + dx, y + dy].Player == 0) { List<String> moves = fw.Moves; moves.Add((char) (y + dy + 'A') + "" + (x + dx + 1)); fw.StoneMove = moves.Count > 0 ? true : false; fw.Moves = moves; } fw.StoneMove = fw.Moves.Count > 0 ? true : false; }
/// <summary> /// Funkce kontrolujici, zda v danem smeru je za figurkou volno /// </summary> /// <param name="gb">Instance tridy hraci deska</param> /// <param name="fw">Instance tridy hraci pole reprezentujici odkud kontrolujeme</param> /// <param name="dx">X smer kterym kontrolujeme</param> /// <param name="dy">Y smer kterym kontrolujeme</param> /// <returns>True, pokud je za figurou v danem smeru volno, jinak false</returns> private bool checkPossibleJump(Gameboard gb, Fairway fw, int dx, int dy) { status playerOnMove = gb.PlayingWhite ? status.whitePlayer : status.blackPlayer; int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; if (x + 2*dx < GlobalVariables.size && x + 2*dx >= 0 && y + 2*dy < GlobalVariables.size && y + 2*dy >= 0 && (int) gb.Board[x + dx, y + dy].Player == -(int) playerOnMove && gb.Board[x + 2*dx, y + 2*dy].Player == 0) return true; else return false; }
private TreeNode genWalJumpTree(Gameboard gb, Fairway fw) { var root = new TreeNode(); int x = Convert.ToInt32(fw.Name.Substring(1)) - 1; int y = Convert.ToInt32(fw.Name[0]) - 'A'; for (int i = 0; i < 8; i++) { if (checkPossibleJump(gb, fw, xs[i], ys[i])) { Gameboard potomek = gb.gameboardCopy(); Fairway from = potomek.Board[x, y]; Fairway over = potomek.Board[x + xs[i], y + ys[i]]; Fairway to = potomek.Board[x + 2*xs[i], y + 2*ys[i]]; TreeNode jump; bool bQ = from.Queen; makeJump(from, over, to); bool aQ = to.Queen; if (bQ == aQ) jump = genWalJumpTree(potomek, to); else jump = new TreeNode(); jump.over = over.Name; jump.dest = to.Name; root.childs.Add(jump); jump.rank = over.Queen ? 3 : 2; } } return root; }
public Fairway Clone() { Fairway result = new Fairway(); result.player = this.Player; result.Queen = this.Queen; result.Jump = this.Jump; result.StoneMove = this.StoneMove; result.Moves = this.Moves; result.Name = this.Name; result.Dests = this.Dests; result.Overs = this.Overs; result.Ranks = this.Ranks; return result; }