简体   繁体   English

C#XNA对布尔数组TicTacToe感到困惑

[英]c# XNA confused about boolean arrays TicTacToe

I'm making TicTac Toe with Xna to get a firmer grip on c# programming, but i can't figure out how to solve this problem: i checkif there is a winning patern in a boolean array for player one and two and if so the game has to stop, but it stops too if there is a row of both x's and o's. 我正在用Xna制作TicTac Toe,以更牢固地掌握c#编程,但是我不知道该如何解决此问题:我检查是否存在布尔数组中的获胜者格局,其中一号和二号玩家,如果是,则游戏必须停止,但是如果同时存在x和o的行,它也将停止。 however, this doesn't aply when the pattern is vertical. 但是,当图案为垂直时,这并不适用。 The:code(I'm using a few texture2d's one is a cross one is an "o" , two are mouse forms and one is the background ::代码(我使用的是Texture2d的一些,一个是十字架,一个是“ o”,两个是鼠标形式,一个是背景

      using System;
   using System.Collections.Generic;
     using System.Linq;
        using Microsoft.Xna.Framework;
     using Microsoft.Xna.Framework.Audio;
   using Microsoft.Xna.Framework.Content;
     using Microsoft.Xna.Framework.GamerServices;
        using Microsoft.Xna.Framework.Graphics;
      using Microsoft.Xna.Framework.Input;
     using Microsoft.Xna.Framework.Media;

     namespace TicTacToe
 {
   public class Game1 : Microsoft.Xna.Framework.Game
    {
    GraphicsDeviceManager graphics;
    static int x, y;
    SpriteBatch spriteBatch;
    static bool ifinitialized = false;
    static bool player,winOne,winTwo;
    Texture2D TTTS, TTTX, TTTR, mousex, mouseo;
    Texture2D[,] whatDraw;
    SpriteFont font;
    Rectangle[,] select;
    Vector2[,] position;
    MouseState mouse = new MouseState() ;
    MouseState prevMouse = new MouseState();
    Rectangle mouseRect ;

    static bool[,] playerOne, playerTwo, total;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    protected override void Initialize()
    {
        mouse = Mouse.GetState();
        playerOne = playerTwo = total  = new bool[3, 3] {                         {false,false,false }, 
            { false, false, false }, { false, false, false } };
        select = new Rectangle[3, 3];
        position = new Vector2[3, 3];
        whatDraw = new Texture2D[3, 3];
        position[0, 0] = new Vector2(0, 0);
        position[1, 0] = new Vector2(162, 0);
        position[2, 0] = new Vector2(322, 0);
        position[0, 1] = new Vector2(0, 162);
        position[1, 1] = new Vector2(162, 162);
        position[2, 1] = new Vector2(322, 162);
        position[0, 2] = new Vector2(0, 322);
        position[1, 2] = new Vector2(162, 322);
        position[2, 2] = new Vector2(322, 322);

        x = 0;
        y = 0;
        this.IsMouseVisible = false;
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        TTTS = Content.Load<Texture2D>("TTTScheme");
        TTTR = Content.Load<Texture2D>("TTTR");
        TTTX = Content.Load<Texture2D>("TTTX");
        font = Content.Load<SpriteFont>("Font");
        mouseo  = Content.Load<Texture2D>("Mouse0;");
        mousex = Content.Load<Texture2D>("mouseX;");

        while (y<3)
        {
            while (x<3)
            {
                whatDraw[x, y] = Content.Load<Texture2D>("empty");
                x++;
            }
            x = 0;
            y++;
        }
        y = 0;
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetStat(PlayerIndex.One).Buttons.Back==                ButtonState.Pressed)
            this.Exit();
        if (!ifinitialized)
        {
            select[0, 0] = new Rectangle(0, 0,TTTX.Width,TTTX.Height);
            select[1, 0] = new Rectangle(162, 0, TTTX.Width, TTTX .Height);
            select[2, 0] = new Rectangle(322, 0, TTTX .Width, TTTX .Height);
            select[0, 1] = new Rectangle(0, 162, TTTX .Width, TTTX .Height);
            select[1, 1] = new Rectangle(162, 162, TTTX .Width,  
    TTTX .Height);
            select[2, 1] = new Rectangle(322, 162, TTTX.Width,   
  TTTX .Height);
            select[0, 2] = new Rectangle(0, 322, TTTX.Width, TTTX .Height);
            select[1, 2] = new Rectangle(162, 322, TTTX .Width, 
 TTTX .Height);
            select[2, 2] = new Rectangle(322, 322, TTTX .Width, 


    TTTX .Height);
            ifinitialized = true;
        }
        prevMouse = mouse;
        mouse = Mouse.GetState();
        if (!winOne && !winTwo)
        {
            while (y < 3)
            {
                while (x < 3)
                {

                    if (select[x, y].Contains(new Point(mouse.X, mouse.Y))
 &&
                         mouse.LeftButton == ButtonState.Pressed &&
                         prevMouse.LeftButton == 
ButtonState.Released&&       (total[x, y] == false))
                    {
                            total[x, y] = true;

                            if (player == false)
                            {
                                playerOne[x,y] = true;
                                whatDraw[x,y] = TTTX;
                                player = true;
                            IfWona();
                        }
                            else if (player == true)
                            {
                                playerTwo[x, y] = true;
                                whatDraw[x,y] = TTTR;
                                player = false;
                            IfWonb();
                        }


                    }
                    x++;
                }
                x = 0;
                y++;
            }
            y = 0;

        }
        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);
        spriteBatch.Begin();
        spriteBatch.Draw(TTTS, new Vector2(0, 0), Color.White);
        while(y<3)
        {
            while (x < 3)
            {

                spriteBatch.Draw(whatDraw[x, y], position[x, y], Color.White);
                x++;
            }
            x = 0;
            y++;
        }
        y = 0;

        if(player==true)
        spriteBatch.Draw(mouseo, new Vector2 (mouse.X-(mouseo .Width /2),
            mouse.Y-(mouseo .Height/2)), Color.White);
        if(player== false)
            spriteBatch.Draw(mousex, new Vector2(mouse.X - (mousex.Width / 2),
                mouse.Y - (mousex.Height / 2)), Color.White);
        spriteBatch.End();
        base.Draw(gameTime);
    }
    static void IfWona()
    {
        if ((playerOne[0, 0] && playerOne[1, 0] && playerOne[2, 0]) || (playerOne[0, 1] && playerOne[1, 1] && playerOne[2, 1]) ||
            (playerOne[0, 2] && playerOne[1, 2] && playerOne[2, 2]) || (playerOne[0, 0] && playerOne[1, 0] && playerOne[2, 0]) ||
            (playerOne[0, 1] && playerOne[1, 1] && playerOne[2, 1]) || (playerOne[0, 2] && playerOne[1, 2] && playerOne[2, 2]) ||
            (playerOne[0, 0] && playerOne[1, 1] && playerOne[2, 2]) || (playerOne[0, 2] && playerOne[1, 1] && playerOne[2, 0]))
        {
            winOne = true;
        }
    }
        static void IfWonb()
            {
            if ((playerTwo[0, 0] && playerTwo[1, 0] && playerTwo[2, 0]) || (playerTwo[0, 1] && playerTwo[1, 1] && playerTwo[2, 1]) ||
                (playerTwo[0, 2] && playerTwo[1, 2] && playerTwo[2, 2]) || (playerTwo[0, 0] && playerTwo[1, 0] && playerTwo[2, 0]) ||
                (playerTwo[0, 1] && playerTwo[1, 1] && playerTwo[2, 1]) || (playerTwo[0, 2] && playerTwo[1, 2] && playerTwo[2, 2]) ||
                (playerTwo[0, 0] && playerTwo[1, 1] && playerTwo[2, 2]) || (playerTwo[0, 2] && playerTwo[1, 1] && playerTwo[2, 0]))
            {
                winTwo = true;

            }
        }
    }
}

thanks already a young programmer 已经感谢年轻的程序员

When I reformat your code, it looks like some of the win conditions are repeated. 当我重新格式化您的代码时,看起来有些重复了获胜条件。

    (playerOne[0, 0] && playerOne[1, 0] && playerOne[2, 0]) ||  // here
    (playerOne[0, 1] && playerOne[1, 1] && playerOne[2, 1]) ||
    (playerOne[0, 2] && playerOne[1, 2] && playerOne[2, 2]) || 
    (playerOne[0, 0] && playerOne[1, 0] && playerOne[2, 0]) ||  // and here
    (playerOne[0, 1] && playerOne[1, 1] && playerOne[2, 1]) || 
    (playerOne[0, 2] && playerOne[1, 2] && playerOne[2, 2]) ||
    (playerOne[0, 0] && playerOne[1, 1] && playerOne[2, 2]) || 
    (playerOne[0, 2] && playerOne[1, 1] && playerOne[2, 0]))

Take a look for other repeated win conditions as well. 看看其他重复的获胜条件。

Welcome to the wide world of programming, LC Masters! 欢迎来到编程世界,LC Masters!

The code you have to check for a winner will work. 您必须检查获胜者的代码将起作用。 The issue you are having in your code is that there is currently no distinction between playerOne and playerTwo . 您的代码中存在的问题是, playerOneplayerTwo当前没有区别。 While it may look like you have created two separate boolean arrays for these variables, you are making a mistake on this line: 虽然看起来您已经为这些变量创建了两个单独的布尔数组,但您在此行上犯了一个错误:

 playerOne = playerTwo = total = new bool[3, 3] { {false,false,false }, { false, false, false }, { false, false, false } }; 

The problem with this line of code is that you are assigning playerOne to the exact same boolean array as playerTwo . 与这行代码的问题是,你要分配playerOne完全相同的布尔数组作为playerTwo After that line of code executes, playerOne and playerTwo become aliases of the exact same data in memory (same goes for total ), meaning, they are two different names for the same boolean array. 执行该行代码后, playerOneplayerTwo成为内存中完全相同数据的别名( total相同),这意味着它们是同一布尔数组的两个不同名称。 This would be like having a nickname that a lot of people call you by. 这就像有很多人打给您的昵称一样。 Your name might be "Thomas" and some friends might call you "TJ", but either way, "Thomas" and "TJ" are names that refer to the exact same person; 您的名字可能是“ Thomas”,有些朋友可能会称呼您为“ TJ”,但无论哪种方式,“ Thomas”和“ TJ”都是指完全相同的人。 you! 您! Because of this problem, any move that either player performs will be seen as being done by both players simultaneously. 由于这个问题,任何一个玩家执行的任何动作都将被视为同时由两个玩家执行。

To fix your problem, you will need to separate the assignment for playerOne and playerTwo (and I would do the same for total), as shown in the following code: 要解决您的问题,您需要将playerOneplayerTwo的分配分开(我对总数进行相同的操作),如以下代码所示:

playerOne = new bool[3, 3];
playerTwo = new bool[3, 3];

Now playerOne and playerTwo each have their own boolean array. 现在playerOneplayerTwo每个都有自己的布尔数组。 After applying this code, you may need to make additional changes in your code to account for these two players having their own boolean arrays (for example, you don't want to allow two players to play at the same location). 应用此代码后,您可能需要在代码中进行其他更改,以说明这两个具有自己的布尔数组的玩家(例如,您不想允许两个玩家在同一位置玩)。

As a final point, notice that when creating playerOne and playerTwo in the code above, I did not explicitly assign the values of the array to false , because it's not necessary. 最后一点,请注意,在上面的代码中创建playerOneplayerTwo时,由于没有必要,我没有将数组的值显式分配给false When you create a boolean array, the values of the array will default to false, as shown in the table provided here . 当你创建一个布尔数组,数组的值将默认为false,如图中所提供的表格这里

Again, this solution may not fix everything with your program, but it will put you closer toward completing your project. 同样,此解决方案可能无法解决您程序中的所有问题,但它将使您更加接近完成项目。 Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM