记忆游戏无法正常工作 [英] Memory game doesnt work properly

查看:80
本文介绍了记忆游戏无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在研究c#

的一个小项目,这是一个带有卡片的记忆游戏,其中玩家在棋盘上选择x和y两次,游戏翻转卡,如果它们是相同的,它将保持翻转,否则,覆盖。

首先游戏创建一个二维数组并用随机字符填充它,默认为'' ,问题是游戏并不总是用字母填充数组(我猜)而且有些卡片什么都没有,'',董事会成员:

recently I've been working on a small project in c#
its a memory game with "Cards" in which the player chooses x and y on the board twice and the game "flips" the cards, if they are the same, it will stay "flipped", else, "covered".
first the game creates a two-dimension array and fills it with random chars, the default is ' ', the problem is that the game doesn't always fills the array with letter (I guess) and some cards are just nothing, ' ', the board class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MemoryCardYesodotProject
    {
        public class MemoryBoard
        {
    
            MemoryCard[,] board;//maarah do meimadi
            public MemoryBoard(int x, int y)
            {
    
                board = new MemoryCard[x, y];
                InitiateMemoryBoard();
    
                while (true)
                {
                    try
                    {
    
                        break;
                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine("Youve entered invalid input");
                    }
                }
                board = new MemoryCard[x, y];
            }
    
            private void InitiateMemoryBoard()
            {
                for (int i = 0; i < board.GetLength(0); i++)
                {
                    for (int j = 0; j < board.GetLength(1); j++)
                    {
                        board[i, j] = new MemoryCard(i, j, ' ');
                    }
                }
            }
    
            private bool IsExistInBoard(MemoryCard[,] arr, char tav)
            {
                //check if the value of tav exists in the list of used characters
                // return true if the selected charcter is in use.
                for (int i = 0; i < board.GetLength(0); i++)
                {
                    for (int j = 0; j < board.GetLength(1); j++)
                    {
                        if (board[i, j].GetShape() == tav)
                            return false;
                    }
                }
                return true;
            }
    
    
            public void BuildMemoryBoard()
            {
                // call InitiateMemoryBoard()
                // select randomly characters from A-z.
                // Verify that each character used once (pair)
                int c = 85;
                bool flag = false;
                while (flag == false)
                {
                    Random r = new Random();
                    c = r.Next(65, 90);
                    c = (char)c;
                    //int x = board.GetLength(0);
                    flag = IsExistInBoard(board, (char)c);
                }
                // Select randomly location on board for each pair (make sure that the place in not in use
                // Repeat doing it for all pairs\
                int counter = 0;
                while (counter < 2)
                {
                    Random r = new Random();
                    int i = r.Next(0, board.GetLength(0));
                    int j = r.Next(0, board.GetLength(1));
                    if (this.board[i, j].GetShape() == ' ')
                    {
                        this.board[i, j].SetShape((char)c);
                        counter++;
                    }
                }
            }
    
            public void DrawBoard()
            {
                //Console.Clear();
                // if the memory card detected show the shape otherwise show '#'
                for (int i = 0; i < board.GetLength(0); i++)
                {
                    for (int j = 0; j < board.GetLength(1); j++)
                    {
                        if (this.board[i, j].IsDetected() == false)
                            Console.Write("# ");
                        else
                            Console.Write((this.board[i, j].GetShape()).ToString() + ' ');
                    }
                    Console.WriteLine();
                }
            }
    
            public void DrawBoardWithGuess(int x1, int y1, int x2, int y2)
            {
                //Console.Clear();
                // if the memory card detected or the location in equal to one of the two selected cards
                // show the shape otherwise show '#'
                this.board[x1, y1].Detected(true);
                this.board[x2, y2].Detected(true);
                DrawBoard();
                this.board[x1, y1].Detected(false);
                this.board[x2, y2].Detected(false);
            }
            public void PlayMemoryGame()
            {
                // while not all memory cards detected
                // call DrawBoard
                // ask from the player position of two cards
                // verify that the input is valid (handle failure)
                // if the two selected cards are equal, new pair detected (mark as detected)
                // call DrawBoardWithGuess
                // Console.WriteLine("Press any key to continue");
                //Console.ReadKey();
                // all memory cards detected print you win.
    
                InitiateMemoryBoard();
                BuildMemoryBoard();
                int counter = 0;
                while (counter != board.GetLength(0) * board.GetLength(1) / 2)
                {
                    DrawBoard();
                    Console.WriteLine("enter position of first card (without a space or a comma, first the y and then the x)");
                    int card1 = int.Parse(Console.ReadLine());
                    int y1 = card1 % 10;
                    y1--;
                    int x1 = card1 / 10;
                    x1--;
                    Console.WriteLine("enter position of second card (without a space or a comma, first the y and then the x)");
                    int card2 = int.Parse(Console.ReadLine());
                    int y2 = card2 % 10;
                    y2--;
                    int x2 = card2 / 10;
                    x2--;
                    DrawBoardWithGuess(x1, y1, x2, y2);
                    if ((this.board[x1, y1].GetShape()) == (this.board[x2, y2].GetShape()))
                    {
                        this.board[x1, y1].Detected(true);
                        this.board[x2, y2].Detected(true);
                        counter++;
                    }
                }
                Console.WriteLine("YOU WIN!");
            }
        }
    }

the card class: 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MemoryCardYesodotProject
    {
        public class MemoryCard
        {
            private int x;
            private int y;
            private char shape;
            private bool detected;
    
            public MemoryCard(int x, int y, char c)
            {
                this.x = x;
                this.y = y;
                this.shape = c;
                this.detected = false;
            }
    
            public int GetX() { return this.x; }
            public int GetY() { return this.y; }    
            public char GetShape() { return this.shape; }
    
            public void SetX(int x) { this.x = x; }
            public void SetY(int y) { this.y = y; }
            public void SetShape(char s) { this.shape = s; }
    
            public bool IsDetected() { return this.detected; }
            public void Detected(bool d) { this.detected = d; }
    
            public void DisplayMemoryCard() { Console.Write(this.shape + " "); }
            public void DisplayHiddenCard() { Console.Write("# "); }
    
            public override string ToString()
            {
                string str;
                if (this.detected)
                    str = "detected";
                else
                    str = "not detected";
                return "(x=" + this.x + " y=" + this.y + "): shapr: " + this.shape + " " + str;
            }
        }
    }





我尝试了什么:



我试过自己填充数组,但是没关系,程序应该自己填充



What I have tried:

ive tried populating the array by myself, it worked but it doesn't matter, the program should populate by itself

推荐答案

这是你的代码,只是把它倾倒在我们身上并说它不起作用没有人帮助。

所以,这将取决于你。

在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。每一行单步检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
This is your code, and just dumping it us and saying it doesn't work helps nobody.
So, it's going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


当你不明白你的代码时正在做或为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


我不确定我在那里见过什么......

你的电路板应该有多少元素?

我认为你有(例如)一个10行8列的电路板。

所以你的电路板也应该这样的尺寸。

按顺序,你应该以这种方式遍历你的数组 - 我将采用你的方法例如:

I'm not quiet sure what I've seen there ...
How many elements your board should have ?
I think you have (for example) a board with 10 rows and 8 columns.
So your board-arry should also dimensioned like this.
In order of this you should iterate in this way through your array - I will take your method "" for example :
private bool IsExistInBoard(MemoryCard[,] arr, char tav)
            {
                //check if the value of tav exists in the list of used characters
                // return true if the selected charcter is in use.
                for (int i = 0; i < rows_Count; i++)
                {
                    for (int j = 0; j < columns_count; j++)
                    {
                        if (board[i, j].GetShape() == tav)
                            return false;
                    }
                }
                return true;
            }





所以......你要做的是:

- memeorize how很好你的数组的尺寸和使用这些变量,并采取这个迭代你的数组的尺寸 - 在你想要使用它的每个方法...



So ... what you have to do is :
- memeorize how great your Array is dimensioned and work with this variables and take this to iterate through the dimesions of your Array - in each method where you want to use it ...


这篇关于记忆游戏无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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