//create button for a pawn private DraughtsButton Create_pawn(bool king, int id, int x, int y) { DraughtsButton pawn = new DraughtsButton(); pawn.id = id; pawn.Name = string.Format("{0}-{1}", x, y); pawn.x = x; pawn.y = y; pawn.Click += new EventHandler(this.MoveHandler); if (king) { switch (id % 3) { case 0: pawn.Image = Properties.Resources.field; break; case 1: pawn.Image = Properties.Resources.white_king; break; case 2: pawn.Image = Properties.Resources.black_king; break; } } else { switch (id % 3) { case 0: pawn.Image = Properties.Resources.field; break; case 1: pawn.Image = Properties.Resources.white_pawn; break; case 2: pawn.Image = Properties.Resources.black_pawn; break; } } pawn.Size = new System.Drawing.Size(square_size, square_size); pawn.Location = new System.Drawing.Point(x * square_size, y * square_size); pawn.TabStop = false; pawn.FlatStyle = FlatStyle.Flat; pawn.FlatAppearance.BorderSize = 0; return(pawn); }
//handle continuation of the move after jump from squares list void MoveHandler(Queue <int> move_template, int x, int y) { DraughtsButton highlighted = (DraughtsButton)this.Controls.Find(string.Format("{0}-{1}", x, y), false)[0]; highlighted.FlatStyle = FlatStyle.Flat; highlighted.FlatAppearance.BorderColor = Color.DarkGreen; highlighted.FlatAppearance.BorderSize = 2; DraughtsButton field = (DraughtsButton)this.Controls.Find(string.Format("{0}-{1}", move_template.Dequeue(), move_template.Dequeue()), false)[0]; int result = state.move(highlighted.x, highlighted.y, field.x, field.y); switch (result) { //0,1 - incorrect move, no action neccessary, so no case case 2: //remove captured pawn and end turn winCheck(); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; highlighted.Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; phase = 0; //change_active(); ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; break; case 3: //further captures possible winCheck(); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } highlighted.Image = Properties.Resources.field; ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; MoveHandler(move_template, field.x, field.y); phase = 2; break; } }
//handle first pawn selection, then move from move squares list public void MoveHandler(Queue <int> move_template) { int result = 0; string x = move_template.Dequeue().ToString(); string y = move_template.Dequeue().ToString(); DraughtsButton highlighted = (DraughtsButton)this.Controls.Find(string.Format("{0}-{1}", x, y), false)[0]; highlighted.FlatStyle = FlatStyle.Flat; highlighted.FlatAppearance.BorderColor = Color.DarkGreen; highlighted.FlatAppearance.BorderSize = 2; DraughtsButton field = (DraughtsButton)this.Controls.Find(string.Format("{0}-{1}", move_template.Dequeue(), move_template.Dequeue()), false)[0]; if (state.get_square(field.x, field.y) == 0) { result = state.move(highlighted.x, highlighted.y, field.x, field.y); switch (result) { case 1: //end turn; if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; highlighted.Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; phase = 0; return; case 2: //remove captured pawn and end turn winCheck(); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; highlighted.Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; phase = 0; return; case 3: //further captures possible winCheck(); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } highlighted.Image = Properties.Resources.field; ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; MoveHandler(move_template, field.x, field.y); phase = 0; break; } } }
//handle first pawn selection, then move from mouseclicks void MoveHandler(object sender, EventArgs e) { int result = 0; DraughtsButton field = (DraughtsButton)sender; switch (phase) { case 0: //choose the pawn if (state.get_square(field.x, field.y) == state.active_side) { field.FlatStyle = FlatStyle.Flat; field.FlatAppearance.BorderColor = Color.DarkGreen; field.FlatAppearance.BorderSize = 2; highlighted = field; phase = 1; move_list.Enqueue(field.x); move_list.Enqueue(field.y); } break; case 1: //make the move or change the pawn if (state.get_square(field.x, field.y) == state.active_side) //change the pawn { highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; field.FlatStyle = FlatStyle.Flat; field.FlatAppearance.BorderColor = Color.DarkGreen; field.FlatAppearance.BorderSize = 2; highlighted = field; move_list.Dequeue(); move_list.Dequeue(); move_list.Enqueue(field.x); move_list.Enqueue(field.y); } else { if (state.get_square(field.x, field.y) == 0) { result = state.move(highlighted.x, highlighted.y, field.x, field.y); switch (result) { //0 - incorrect move, no action neccessary, so no case case 1: //end turn; move_list.Enqueue(field.x); move_list.Enqueue(field.y); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; highlighted.Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; phase = 0; change_active(); break; case 2: //remove captured pawn and end turn jump = true; move_list.Enqueue(field.x); move_list.Enqueue(field.y); winCheck(); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; highlighted.Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; phase = 0; change_active(); break; case 3: //further captures possible jump = true; winCheck(); move_list.Enqueue(field.x); move_list.Enqueue(field.y); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; field.FlatAppearance.BorderColor = Color.DarkGreen; field.FlatAppearance.BorderSize = 2; highlighted.Image = Properties.Resources.field; ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; highlighted = field; phase = 2; break; } } } break; case 2: //move continuation after a jump result = state.move(highlighted.x, highlighted.y, field.x, field.y); switch (result) { //0,1 - incorrect move, no action neccessary, so no case case 2: //remove captured pawn and end turn move_list.Enqueue(field.x); move_list.Enqueue(field.y); winCheck(); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; highlighted.Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; phase = 0; ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; change_active(); break; case 3: //further captures possible move_list.Enqueue(field.x); move_list.Enqueue(field.y); winCheck(); if (state.check_promotion(field.x, field.y)) { if (state.active_side == 1) { field.Image = Properties.Resources.white_king; } else { field.Image = Properties.Resources.black_king; } } else { field.Image = highlighted.Image; } field.id = highlighted.id; field.FlatAppearance.BorderColor = Color.DarkGreen; field.FlatAppearance.BorderSize = 2; highlighted.Image = Properties.Resources.field; ((DraughtsButton)this.Controls.Find(state.to_kill, false)[0]).Image = Properties.Resources.field; highlighted.id = 0; highlighted.FlatAppearance.BorderSize = 0; highlighted.FlatStyle = System.Windows.Forms.FlatStyle.Flat; highlighted = field; phase = 2; break; } break; } }