Tic tac toe vs计算机课

时间:2015-05-31 21:14:48

标签: c# winforms tic-tac-toe

我正在写一个tic tac toe游戏。我的逻辑是在课堂上,这就是我遇到困难的原因。我的董事会是一个矩阵。首先,当我点击一个按钮时我放X,然后计算机选择一个块放在哪里。在类中我保存了矩阵元素的值,但我不知道如何在winform上显示它。这是我的代码的一部分..

public string ToggleNextLetter()
{
        if (!changedLetterX)
        {
            changedLetterX = true;
            return this.nextLetter = "X";
        }

        changedLetterX = false;
        MoveComputer();
        return this.nextLetter = "O";
}

//从表格

中保留X的位置
public string[,] Matrix(int row, int col, string value)
{
        this.field[row, col] = value;

        return this.field;
    }

public string[,] MoveComputer()
    {

        //priority 1: get tick tac toe
        //priority 2: block x tic tac toe
        //priority 3: go for corner space
        //priority 4: pick open space

        string move;

        move = LookForWinOrBlock("O"); //look for win
        if(move == null)
        {
            move = LookForWinOrBlock("X"); //look for block
            if (move == null)
            {
                move = LookForCorner();
                if (move == null)
                {
                    move = LookForOpenSpace();
                }
            }
        }

        return move;
}

private void btn_click(object sender, EventArgs e)
{
        Button btn = (Button)sender;
        int btnRow = Convert.ToInt32(btn.Tag) / 10;
        int btnCol = Convert.ToInt32(btn.Tag) % 10;
        btn.Text = game.ToggleNextLetter();
        game.Matrix(btnRow, btnCol, btn.Text);
        btn.Enabled = false;
        //btn.Text = game.ToggleNextLetter();

        if (game.HasWinner())
        {
            foreach (var control in Controls)
            {
                try
                {
                    Button b = (Button)control;
                    b.Enabled = false;
                }
                catch { }
            }
            MessageBox.Show("The winner is " + btn.Text);
        }
    }

2 个答案:

答案 0 :(得分:0)

尝试使用DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();

            //initiailze the datatable
            dt.Columns.Add("Col 1", typeof(string));
            dt.Columns.Add("Col 2", typeof(string));
            dt.Columns.Add("Col 3", typeof(string));

            //add three rows
            dt.Rows.Add();
            dt.Rows.Add();
            dt.Rows.Add();

            dataGridView1.DataSource = dt;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //examples of filling table
            dt.Rows[0][0] = "X";
            dt.Rows[0][1] = "O";
            dt.Rows[0][2] = "X";
            dt.Rows[1][0] = "O";
            dt.Rows[1][1] = "X";
            dt.Rows[1][2] = "O";
            dt.Rows[2][0] = "X";
            dt.Rows[2][1] = "O";
            dt.Rows[2][2] = "X";

        }
    }
}
​

答案 1 :(得分:0)

只需分开你的功能

public string[,] MoveComputer() {
    // just add this line
    MakePlay(btnRow, btnCol, btnText);

    return move;
} 

private void btn_click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    int btnRow = Convert.ToInt32(btn.Tag) / 10;
    int btnCol = Convert.ToInt32(btn.Tag) % 10;
    btn.Text = game.ToggleNextLetter();
    MakePlay((btnRow, btnCol, btn.Text);
}

private void MakePlay(int btnRow, int btnCol, string btnText) {
    game.Matrix(btnRow, btnCol, btn.Text);
    btn.Enabled = false;
    //btn.Text = game.ToggleNextLetter();

    if (game.HasWinner())
    {
        foreach (var control in Controls)
        {
            try
            {
                Button b = (Button)control;
                b.Enabled = false;
            }
            catch { }
        }
        MessageBox.Show("The winner is " + btn.Text);
    }
}
相关问题