有AI的重力四子棋

这里展示了一个有AI的重力四子棋的C#程序。 AI采用alpha beta剪枝的方法。


界面代码MainWindow.xaml:

<Window x:Class="ConnectFour.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ConnectFour" Height="650" Width="500">
    <Grid x:Name="MainGrid" SizeChanged="ConnectFourForm_SizeChanged"  >
        <Button x:Name="btnHumanStart" Content="Human first" HorizontalAlignment="Left" Height="35" Margin="10,5,0,0" VerticalAlignment="Top" Width="100" Click="btn_Human_Click"/>
        <Button x:Name="btnProgramStart" Content="Program first" HorizontalAlignment="Right" Height="35" Margin="0,5,10,0" VerticalAlignment="Top" Width="100" Click="btn_Program_First"/>
        <TextBox x:Name="txbSearchDepth" HorizontalAlignment="Center" Height="20" Margin="0,15,0,0" TextWrapping="Wrap" Text="Enter search depth here" VerticalAlignment="Top" Width="195"/>
        <Canvas x:Name="chessBoard" Margin="10,50,10,50" PreviewMouseLeftButtonDown="ChessBorad_MouseLeftButtonDown" PreviewMouseRightButtonDown="ChessBorad_MouseRightButtonDown" />
        <TextBox x:Name="txbMessage" Height="25" Margin="10,585,5,10" TextWrapping="Wrap" Text="Search nodes number and pruning nodes number" VerticalAlignment="Top"/>

    </Grid>
</Window>


逻辑代码ManWindow.xaml.cs:

/*
 * This program is a gravity connect four game. AI uses the alpha beta pruning search.
 * */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace ConnectFour
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const int column = 4;           // The column of chess board
        private const int row = 5;              // The row of chess board
        private const bool HUMAN = true;        // Human turn
        private const bool PROGRAM = false;     // Program turn

        private bool player = HUMAN;

        private double width;
        private double height;
        private double gridWidth;
        private double gridHeight;

        private int maxDepth = 10;
        private bool canClick = true;

        private Position board = new Position();

        private int nodeNumber = 0;
        private int maxCut = 0;
        private int minCut = 0;

        public MainWindow()
        {
            InitializeComponent();
            AddHandler(FrameworkElement.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(ChessBorad_MouseLeftButtonDown), true);
            AddHandler(FrameworkElement.PreviewMouseRightButtonDownEvent, new MouseButtonEventHandler(ChessBorad_MouseRightButtonDown), true);
        }

        private void DrawChessBoard()
        {
            chessBoard.Children.Clear();

            width = chessBoard.ActualWidth;
            height = chessBoard.ActualHeight;
            gridWidth = width * 0.9 / column;
            gridHeight = height * 0.9 / row;

            for (int i = 0; i <= row; i++)
            {
                DrawingLine(width * 0.05, height * 0.05 + i * gridHeight, width * 0.95, height * 0.05 + i * gridHeight);
            }

            for (int i = 0; i <= column; i++)
            {
                DrawingLine(width * 0.05 + i * gridWidth, height * 0.05, width * 0.05 + i * gridWidth, height * 0.95);
            }

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    if (board.chess[i, j] == Position.HUMAN_CHESS)
                        DrawingEllipse(gridWidth * 0.9, gridHeight * 0.9, width * 0.05 + j * gridWidth + 0.5 * gridWidth, height * 0.05 + i * gridHeight + 0.5 * gridHeight, HUMAN);
                    else if (board.chess[i, j] == Position.PROGRAM_CHESS)
                        DrawingEllipse(gridWidth * 0.9, gridHeight * 0.9, width * 0.05 + j * gridWidth + 0.5 * gridWidth, height * 0.05 + i * gridHeight + 0.5 * gridHeight, PROGRAM);
                }
            }
        }

        protected void DrawingLine(double x1, double y1, double x2, double y2)
        {
            Line myLine = new Line();
            myLine.Stroke = System.Windows.Media.Brushes.Black;
            myLine.X1 = x1;
            myLine.Y1 = y1;
            myLine.X2 = x2;
            myLine.Y2 = y2;
            myLine.StrokeThickness = 1;

            chessBoard.Children.Add(myLine);
        }

        protected void DrawingEllipse(double widthEllipse, double heightEllipse, double centerX, double centerY, bool currentPlayer)
        {
            Ellipse myEllipse = new Ellipse();
            if (currentPlayer) myEllipse.Stroke = System.Windows.Media.Brushes.DarkBlue;
            else myEllipse.Stroke = System.Windows.Media.Brushes.Red;

            myEllipse.Width = widthEllipse;
            myEllipse.Height = heightEllipse;
            myEllipse.StrokeThickness = 2;
            myEllipse.Fill = myEllipse.Stroke;

            myEllipse.Margin = new Thickness(centerX - widthEllipse / 2, centerY - heightEllipse / 2, 0, 0);

            chessBoard.Children.Add(myEllipse);
        }

        private bool isInChessBoundary(double x, double y)
        {
            return x >= chessBoard.ActualWidth * 0.05 && x <= chessBoard.ActualWidth * 0.95 && y >= chessBoard.ActualHeight * 0.05 && y <= chessBoard.ActualHeight * 0.95;
        }

        private void ConnectFourForm_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DrawChessBoard();
        }

        private void ChessBorad_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!canClick) return;
            Point mousePosition = e.GetPosition(chessBoard);
            double x = mousePosition.X;
            double y = mousePosition.Y;
            if (!isInChessBoundary(x, y)) return;
            int columnIndex = (int)((x - width * 0.05) / gridWidth);
            bool canNext = mouseLeftClickedInColumn(columnIndex);
            DrawChessBoard();
            testEndOfGame();
            if (canNext)
            {
                player = PROGRAM;
                programTurn();
                testEndOfGame();
            }
            e.Handled = true;
        }

        private void ChessBorad_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!canClick) r
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值