private void ListboxMove_SelectedIndexChanged(object sender, EventArgs e) { if (ListboxMove.SelectedIndex < 0) { return; } textBoxComment.Text = CommentList[ListboxMove.SelectedIndex]; if (ListboxMove.SelectedIndex > FENStep) { for (int i = FENStep; i < ListboxMove.SelectedIndex; i++) { pos.MakeMove(MoveList[i]); } } else { for (int i = FENStep; i > ListboxMove.SelectedIndex; i--) { pos.UnmakeMove(); } } FENStep = ListboxMove.SelectedIndex; if (FENStep > 0) { ptLastFrom = POSITION.UI_Coord2XY(MoveList[ListboxMove.SelectedIndex - 1].sqSrc, bFlipped); ptLastTo = POSITION.UI_Coord2XY(MoveList[ListboxMove.SelectedIndex - 1].sqDst, bFlipped); } PanelBoard.Refresh(); }
public MainForm() { InitializeComponent(); pos = new POSITION(); MoveList = new List <MOVE>(); CommentList = new List <string>(); soundPlayer = new SoundPlayer(); string path = Application.StartupPath; path = Path.GetDirectoryName(path); path = Path.GetDirectoryName(path); App_szPath = Path.Combine(path, "Resources"); engine = new POSITION(); }
void WriteMap2Csv(int[,] map, string csvPath) { using (FileStream fs = new FileStream(csvPath.Trim(), FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.Default)) { sw.AutoFlush = false; for (int y = 0; y < 10; y++) { for (int x = 0; x < 9; x++) { int sq = POSITION.UI_XY2Coord(x, y); sw.Write("{0} | {1},", map[0, sq], map[1, sq]); } sw.WriteLine(); } sw.Flush(); } } }
public override string ToString() { return(POSITION.MOVE2ICCS(sqSrc, sqDst)); }
bool ReadPGN(string filename, int maxHeight) { POSITION pos = new POSITION(); PgnFileStruct pgn = pos.ReadPgnFile(filename); if (pgn.StartFEN != POSITION.cszStartFen) { Console.WriteLine("非开局或全局谱"); return(false); } int result; switch (pgn.Result) { case "0-1": result = -1; break; case "1-0": result = 1; break; case "1/2-1/2": result = 0; break; default: return(false); } pos.FromFEN(pgn.StartFEN); int height = 0; foreach (MOVE mv in pgn.MoveList) { pos.MakeMove(mv, false); ulong key = pos.Key; if (Book.TryGetValue(key, out BookEntry entry)) { switch (result) { case 1: entry.win++; Debug.Assert(entry.win < ushort.MaxValue); break; case 0: entry.draw++; Debug.Assert(entry.loss < ushort.MaxValue); break; case -1: entry.loss++; Debug.Assert(entry.draw < ushort.MaxValue); break; default: Debug.Fail("Unknown result"); break; } Book[key] = entry; } else { ulong mirror_key = pos.CalculateZobrist(true); if (Book.TryGetValue(mirror_key, out entry)) { switch (result) { case 1: entry.win++; Debug.Assert(entry.win < ushort.MaxValue); break; case 0: entry.draw++; Debug.Assert(entry.loss < ushort.MaxValue); break; case -1: entry.loss++; Debug.Assert(entry.draw < ushort.MaxValue); break; default: Debug.Fail("Unknown result"); break; } Book[mirror_key] = entry; } else { entry = new BookEntry(); switch (result) { case 1: entry.win = 1; break; case 0: entry.draw = 1; break; case -1: entry.loss = 1; break; default: Debug.Fail("Unknown result"); return(false); } Book.Add(key, entry); } } height++; if (height > maxHeight) { return(true); } } return(true); }
void MakeMove(int sqFrom, int sqTo) { Graphics g = PanelBoard.CreateGraphics(); if (pos.IsLegalMove(sqFrom, sqTo)) { int pcCaptured = pos.pcSquares[sqTo]; MOVE step = new MOVE(sqFrom, sqTo, pos.pcSquares[sqFrom], pcCaptured); pos.MakeMove(step); if (pos.CheckedBy(1 - pos.sdPlayer) > 0) { PlaySound("ILLEGAL"); pos.UnmakeMove(); return; } engine.MakeMove(step); MoveList.Add(step); CommentList.Add(textBoxComment.Text); if (FENStep > 0) { //擦除上一步的起始和结束位置选择框 DrawBoard(ptLastFrom, g); DrawBoard(ptLastTo, g); DrawPiece(ptLastTo, pcLast, g); } ptLastFrom = POSITION.UI_Coord2XY(sqFrom, bFlipped); ptLastTo = POSITION.UI_Coord2XY(sqTo, bFlipped); pcLast = cnPieceImages[step.pcSrc]; //擦除原来的位置 DrawBoard(ptLastFrom, g); DrawSelection(ptLastFrom, g); //移动到新位置 DrawSelection(ptLastTo, g); DrawPiece(ptLastTo, pcLast, g); bSelected = false; FENStep++; string label = step.ToString(); if (FENStep % 2 == 1) { label = ((FENStep / 2 + 1).ToString() + "." + label); } label = label.PadLeft(8); ListboxMove.Items.Add(label); if (pos.pcSquares[sqTo] > 0) { PlaySound("CAPTURE"); } else { PlaySound("MOVE"); } if (pos.IsMate()) {//直接吃王或者绝杀 if (pos.sdPlayer == 1 && MenuAIBlack.Checked && !MenuAIRed.Checked || pos.sdPlayer == 0 && MenuAIRed.Checked && !MenuAIBlack.Checked) { PlaySound("WIN"); } else if (pos.sdPlayer == 1 && !MenuAIBlack.Checked && MenuAIRed.Checked || pos.sdPlayer == 0 && !MenuAIRed.Checked && MenuAIBlack.Checked) { PlaySound("LOSS"); } if (pos.sdPlayer == 0) { MessageBox.Show("黑方胜!"); } else { MessageBox.Show("红方胜!"); } App_inGame = false; } } }
private async void PanelBoard_MouseClick(object sender, MouseEventArgs e) { if (!App_inGame) { return; } int x = e.X / gridSize; int y = e.Y / gridSize; if (x < 0 || x > 9 || y < 0 || y > 10) { return; } int piece; if (bFlipped) { piece = pos.UI_GetFlippedPiece(x, y); } else { piece = pos.UI_GetPiece(x, y); } Graphics g = PanelBoard.CreateGraphics(); if (bSelected) { if (POSITION.SIDE(piece) == pos.sdPlayer && ptSelected != new Point(x, y)) { //又选择别的己方子 DrawBoard(ptSelected, g); DrawPiece(ptSelected, pcSelected, g); ptSelected = new Point(x, y); pcSelected = cnPieceImages[piece]; DrawSelection(ptSelected, g); PlaySound("CLICK"); } else { int sqFrom, sqTo; if (bFlipped) { sqFrom = POSITION.UI_XY2Coord(8 - ptSelected.X, 9 - ptSelected.Y); sqTo = POSITION.UI_XY2Coord(8 - x, 9 - y); } else { sqFrom = POSITION.UI_XY2Coord(ptSelected.X, ptSelected.Y); sqTo = POSITION.UI_XY2Coord(x, y); } MakeMove(sqFrom, sqTo); await GoAsync(); } } else if (POSITION.SIDE(piece) == pos.sdPlayer) { if (pos.sdPlayer == 0 && !MenuAIRed.Checked || pos.sdPlayer == 1 && !MenuAIBlack.Checked) { ptSelected = new Point(x, y); pcSelected = cnPieceImages[piece]; DrawSelection(ptSelected, g); bSelected = true; } } }