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;
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)
{
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()
{
int c = 85;
bool flag = false;
while (flag == false)
{
Random r = new Random();
c = r.Next(65, 90);
c = (char)c;
flag = IsExistInBoard(board, (char)c);
}
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()
{
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)
{
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()
{
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