需要帮助我的项目4连续 [英] Need help with my project 4 in a row

查看:56
本文介绍了需要帮助我的项目4连续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#Visual Studio中连续4场比赛。我有7行和49个冒号,命名为行ABCDEFG,在行A上它用A1-A7-B2-7等等。但是在获得胜利后,我获得一个完整的X胜利,如7 x = wins.i想获胜者4个符号后。我希望有人可以帮助我:)对不起我的解释和英语,请随便问一下,这是代码:



Im making a 4 in a row game in C# Visual Studio. I have 7 rows and 49 colons, Named the rows ABCDEFG, and on row A its liks A1-A7 - B2-7 etc. But im getting a winner after 1 row of full X wins like 7 x = wins.i want a winner after 4 symbols. im new at this hope somebody can help me :) sorry for my bad explaination and english, just feel free to ask, here is the code:

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

namespace _4PåRad
{
    public partial class Form1 : Form
    {
        bool turn = true; //true = x tur; false = Y tur
        int turn_count = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void beskrivelseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Laget Av Petar", "Fire på rad");
        }

        private void lukkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (turn)
                b.Text = "X";
            else
                b.Text = "O";

            turn = !turn;
            b.Enabled = false;

            checkforwinner();
        }

        private void checkforwinner()
        {
            bool there_is_a_winner = false;
            //Horisontol
            if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (A3.Text == A4.Text) && (A4.Text == A5.Text) && (A5.Text == A6.Text) && (A6.Text == A7.Text) && (!A1.Enabled))
                there_is_a_winner = true;
            else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (B3.Text == B4.Text) && (B4.Text == B5.Text) && (B5.Text == B6.Text) && (B6.Text == B7.Text) && (!B1.Enabled))
                there_is_a_winner = true;
            else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (C3.Text == C4.Text) && (C4.Text == C5.Text) && (C5.Text == C6.Text) && (C6.Text == C7.Text) && (!C1.Enabled))
                there_is_a_winner = true;
            else if ((D1.Text == D2.Text) && (D2.Text == D3.Text) && (D3.Text == D4.Text) && (D4.Text == D5.Text) && (D5.Text == D6.Text) && (D6.Text == D7.Text) && (!D1.Enabled))
                there_is_a_winner = true;
            else if ((E1.Text == E2.Text) && (E2.Text == E3.Text) && (E3.Text == E4.Text) && (E4.Text == E5.Text) && (E5.Text == E6.Text) && (E6.Text == E7.Text) && (!E1.Enabled))
                there_is_a_winner = true;
            else if ((F1.Text == F2.Text) && (F2.Text == F3.Text) && (F3.Text == F4.Text) && (F4.Text == F5.Text) && (F5.Text == F6.Text) && (F6.Text == F7.Text) && (!F1.Enabled))
                there_is_a_winner = true;
            else if ((G1.Text == G2.Text) && (G2.Text == G3.Text) && (G3.Text == G4.Text) && (G4.Text == G5.Text) && (G5.Text == G6.Text) && (G6.Text == G7.Text) && (!G1.Enabled))
                there_is_a_winner = true;


            if (there_is_a_winner)
            {
                String winner = "";
                if (turn)
                    winner = "O";
                else
                    winner = "X";

                MessageBox.Show(winner + " Wins!", "Yay!");
            }
        }
    }
}

推荐答案

好的,有几个那里的问题(它们是列而不是冒号 - 冒号完全是一个非常不同的东西:O)



我认为你的意思是你有7行乘7列,你已经将它们实现为Button对象,并将它们命名为A1,A2,...... B1,B2,......

首先要注意的是,这是一个真正的痛苦 - 因为你必须在检查时使用每个按钮的名称。



而不是在设计时添加所有按钮,将它们添加到运行时,改为使用数组:

Ok, there are a few problems there (and they are "columns" not "colons" - a "colon is a very different thing altogether :O )

What I assume you mean is that you have 7 rows by 7 columns, and you have implemented them as Button objects and named them A1, A2, ... B1, B2, ...
The first thing to notice is that that is a real pain to work with - because you have to use the names for each button when you check them.

Instead of adding all your buttons at design time, add them at run time, and use an array instead:
private Button[,] theBoard = new Button[7, 7];



然后,在你的表单构造函数中,填写董事会:


Then, in your form constructor, fill the board:

int y = 10;
for (int i = 0; i < 7; i++)
    {
    int x = 10;
    for (int j = 0; j < 7; j++)
        {
        Button b = new Button();
        b.Top = y;
        b.Left = x;
        b.Width = 20;
        b.Text = "";
        b.Click += new EventHandler(button_click);
        Controls.Add(b);
        x += 25;
        }
    y += 25;
    }

现在,您可以通过参考X,Y坐标来检查您的电路板 - 这意味着您可以编写一种方法来检查某个点的所有方向,并将其用于板上的每个坐标,而不是明确命名它们。有8个方向,每个方向是当前点加上或减去x和y偏移或1,0或-1的组合。这意味着你可以使用另一种方法来检查给定一个点和两个偏移的四个相同对象的匹配,然后调用8次。



试一试:它会比你的方式更容易,非常非常快!

Now, you can check your board by referring to X,Y coordinates - which means you can write a method for checking all directions from a point, and use that for each coordinate on the board instead of naming them explicitly. There are 8 directions, and each is a combination of the current point plus or minus an x and y offset or 1, 0, or -1. Which means you can use another method that checks for a match of four identical objects given a point and two offsets, then call that 8 times.

Give it a try: it's going to be a lot easier than your way, very, very quickly!


这篇关于需要帮助我的项目4连续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆