Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Posted
Updated 13-Mar-17 1:42am
Comments
Michael_Davies 11-Mar-17 16:59pm    
Looks like the loop in BuildMemoryBoard will exit the moment it finds a code has not been used already, whereas you want to exit the loop when every slot on the board has been filled. Use the debugger and watch what happens.

You pass the array as a parameter to IsExistInBoard, do not use it but use the global variable directly, which you are passing as an argument...

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!
 
Share this answer
 
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.
 
Share this answer
 
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 :
C#
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;
            }


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 ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900