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

本文关键字:TicTacToe 数组 XNA 布尔 | 更新日期: 2023-09-27 18:31:44

我正在用Xna制作TicTac Toe,以更牢固地掌握c#编程,但是我不知道如何解决这个问题: 我检查玩家一和二的布尔数组中是否有获胜的父亲,如果是这样,游戏必须停止,但如果同时有一行 x 和 o,游戏也会停止。 然而, 当模式是垂直的时,这并不适用。The:code(我正在使用一些 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;
            }
        }
    }
}

已经谢谢了一个年轻的程序员

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

当我重新格式化您的代码时,看起来一些获胜条件被重复了。

    (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]))

也看看其他重复的获胜条件。

欢迎来到广阔的编程世界,LC大师!

您必须检查获胜者的代码将起作用。您在代码中遇到的问题是当前playerOneplayerTwo之间没有区别。虽然看起来您已经为这些变量创建了两个单独的布尔数组,但您在以下行上犯了一个错误:

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

这行代码的问题在于您将playerOne分配给与playerTwo完全相同的布尔数组。在该行代码执行后,playerOneplayerTwo成为内存中完全相同数据的别名(total也是如此),这意味着它们是同一布尔数组的两个不同名称。这就像有一个很多人称呼你的昵称。你的名字可能是"托马斯",有些朋友可能会叫你"T.J.",但无论哪种方式,"托马斯"和"T.J."都是指同一个人的名字;你!由于这个问题,任何一名玩家执行的任何动作都将被视为由两名玩家同时完成。

要解决您的问题,您需要将 playerOneplayerTwo 的分配分开(我会对总数做同样的事情),如以下代码所示:

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

现在playerOneplayerTwo都有自己的布尔数组。应用此代码后,您可能需要在代码中进行其他更改,以考虑这两个玩家具有自己的布尔数组(例如,您不希望允许两个玩家在同一位置玩游戏)。

最后一点,请注意,在上面的代码中创建playerOneplayerTwo时,我没有显式将数组的值分配给false,因为这不是必需的。创建布尔数组时,数组的值将默认为 false,如此处提供的表所示。

同样,此解决方案可能无法解决程序的所有问题,但它将使您更接近完成项目。祝您编码愉快!