Skip to content

Instantly share code, notes, and snippets.

@rdeioris
Created November 26, 2018 16:07
Show Gist options
  • Select an option

  • Save rdeioris/d3a178564db9e361bfaf6db07f8cc90c to your computer and use it in GitHub Desktop.

Select an option

Save rdeioris/d3a178564db9e361bfaf6db07f8cc90c to your computer and use it in GitHub Desktop.
Memory
using System;
using Aiv.Draw;
namespace Memory1B
{
struct Game
{
public int Turns;
public Window Window;
public Card[] Cards;
public Random Random;
public int CardWidth;
public int CardHeight;
public int Columns;
public int Rows;
}
struct Card
{
public Color Color;
public bool IsCovered;
public bool IsWon;
}
struct Color
{
public byte R;
public byte G;
public byte B;
}
class Program
{
static Card CreateCard(Color color)
{
Card card = new Card();
card.Color = color;
card.IsCovered = true;
return card;
}
static Color CreateColor(byte r, byte g, byte b)
{
Color color = new Color();
color.R = r;
color.G = g;
color.B = b;
return color;
}
static void PutPixel(Window window, int x, int y, Color color)
{
if (x < 0 || y < 0 || x >= window.width || y >= window.height)
{
return;
}
int index = (y * window.width + x) * 3;
window.bitmap[index] = color.R;
window.bitmap[index + 1] = color.G;
window.bitmap[index + 2] = color.B;
}
static void DrawHorizontalLine(Window window, int x, int y, int width, Color color)
{
for (int i = x; i < x + width; i++)
{
PutPixel(window, i, y, color);
}
}
static void DrawFillRectangle(Window window, int x, int y, int width, int height, Color color)
{
for (int i = y; i < y + height; i++)
{
DrawHorizontalLine(window, x, i, width, color);
}
}
static void DrawCard(ref Game game, int index)
{
Card card = game.Cards[index];
Color cardColor = CreateColor(100, 100, 100);
if (!card.IsCovered || card.IsWon)
{
cardColor = card.Color;
}
int x = (index % game.Columns) * game.CardWidth;
int y = (index / game.Columns) * game.CardHeight;
DrawFillRectangle(game.Window, x, y, game.CardWidth, game.CardHeight, cardColor);
}
static void DrawCards(ref Game game)
{
for (int i = 0; i < game.Cards.Length; i++)
{
DrawCard(ref game, i);
}
}
static void CardsShuffle(ref Game game)
{
for (int i = 0; i < game.Cards.Length; i++)
{
int randomIndex = game.Random.Next(i, game.Cards.Length);
Card cardCopy = game.Cards[i];
game.Cards[i] = game.Cards[randomIndex];
game.Cards[randomIndex] = cardCopy;
}
}
static void CheckClick(ref Game game)
{
if (!game.Window.mouseLeft)
{
return;
}
int cellX = game.Window.mouseX / game.CardWidth;
int cellY = game.Window.mouseY / game.CardHeight;
int index = cellY * game.Columns + cellX;
game.Cards[index].IsCovered = false;
}
static bool ColorCompare(Color colorA, Color colorB)
{
if (colorA.R != colorB.R)
return false;
if (colorA.G != colorB.G)
return false;
if (colorA.B != colorB.B)
return false;
return true;
}
static bool CardCompare(ref Game game, int cardA)
{
for (int i = 0; i < game.Cards.Length; i++)
{
if (i == cardA)
continue;
if (game.Cards[i].IsWon)
continue;
if (game.Cards[i].IsCovered)
continue;
// NOTE: the first card met, is the second one
return ColorCompare(game.Cards[cardA].Color, game.Cards[i].Color);
}
return false;
}
static void Main(string[] args)
{
Color red = CreateColor(255, 0, 0);
Color blue = CreateColor(0, 0, 255);
Color green = CreateColor(0, 255, 0);
Color yellow = CreateColor(255, 255, 0);
Game memory = new Game();
memory.Cards = new Card[8];
memory.Random = new Random();
memory.Columns = 4;
memory.Rows = 2;
memory.CardWidth = 200;
memory.CardHeight = 400;
memory.Cards[0] = CreateCard(red);
memory.Cards[1] = CreateCard(red);
memory.Cards[2] = CreateCard(green);
memory.Cards[3] = CreateCard(green);
memory.Cards[4] = CreateCard(blue);
memory.Cards[5] = CreateCard(blue);
memory.Cards[6] = CreateCard(yellow);
memory.Cards[7] = CreateCard(yellow);
CardsShuffle(ref memory);
for (int i = 0; i < memory.Cards.Length; i++)
{
Color cardColor = memory.Cards[i].Color;
Console.WriteLine("R: {0} G: {1} B: {2}", cardColor.R, cardColor.G, cardColor.B);
}
//Console.ReadLine();
memory.Window = new Window(memory.CardWidth * memory.Columns, memory.CardHeight * memory.Rows, "Memory", PixelFormat.RGB);
while (memory.Window.opened)
{
// if the game is paused, CheckClick() must be skipped
CheckClick(ref memory);
DrawCards(ref memory);
memory.Window.Blit();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment