Public Group

# c# game

## Recommended Posts

awesome

##### Share on other sites

I got this example of the Knuth Suffle Algorithm from the book: Unity in Action: Multiplatform game development in C# - 2nd Edition. You can find this example in "Chapter 5. Building a Memory Card Game".

Helpers.cs

using System;

public class Helpers
{
// Implementation of the Knuth shuffle algorithm
public static int[] SuffleArray(int[] numbers)
{
int[] newArray = numbers.Clone() as int[];
var rnd = new Random();

for (int i = 0; i < newArray.Length; i++)
{
int tmp = newArray[i];
int r = rnd.Next(i, newArray.Length - 1);
newArray[i] = newArray[r];
newArray[r] = tmp;
}

return newArray;
}
}

I study TDD and BDD with these books:

I wrote the unit test using NUnit Framework:

HelpersTests.cs

using NUnit.Framework;

namespace ShuffleArray_UnitTests
{
[TestFixture]
public class HelpersTests
{
[Test]
public void ShuffleArray_ActualAreNotEqualToInput_ReturnsShuffledArray()
{
// Arrange
int[] inputArray = new int[] { 1, 2, 3, 4, 5 };

// Act
int[] actualArray = Helpers.SuffleArray(inputArray);

// Assert
Assert.AreNotEqual(inputArray, actualArray);
}
}
}

##### Share on other sites

The benefit of the other solutions is that they can shuffle a list of *anything*.

Your unit test does not really test that the cards are well shuffled  I think that would be quite difficult to test. For instance your test would pass the array 5,4,3,2,1.

##### Share on other sites

Yes, it is difficult to write more useful tests for random thing. But I think this test adds some executable specifications

• the method returns something with the same length (not null, not empty array, not with different length)
• the method returns something different than inputArray
1 hour ago, duke_meister said:

The benefit of the other solutions is that they can shuffle a list of *anything*.

Yes, but maybe he will open the book "Unity in Action" and will study some ideas for card games. I think he will use Unity in the future. But some ideas can be useful for WinForms games, maybe. Or maybe he will get the book and will study Unity after my message. Maybe not and he just will ignore my message. It will be good for me too because I prected in English a little and I wrote the unit test - it is good for me too. And I know where to find prototype of a card game if I will make the card game in Unity. Your solution is very good. I like it.

Edited by 8Observer8

##### Share on other sites

I take no credit for any of those solutions. A good programmer knows when to write code and when to 'steal' it

##### Share on other sites

well I have decided to make a snake game because it is easier then blackjack

##### Share on other sites

here is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

int x = 0, y = 0;

private void timer1_Tick(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
if (x >= 340)
{
x = 340;
}
x += 5;
greenBrush.Dispose();
g.Dispose();
}        }

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

int x = 0, y = 0;

private void timer1_Tick(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.Left)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
if (x >= 340)
{
x = 340;
}
x += 5;
greenBrush.Dispose();
g.Dispose();
}        }

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

}
}
}

##### Share on other sites

I got my snake game to animate but I want it to move in the direction of the mouse. here is my updated code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340, 280, 100, 10);
Rectangle rect_black = new Rectangle(240, 280, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

int x = 0, y = 0;

private void timer1_Tick(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
if (x >= 340)
{
x = 340;
}
x += 5;
greenBrush.Dispose();
g.Dispose();

if (MouseButtons == MouseButtons.Left)
{

}
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

}
}
}

##### Share on other sites

I am trying to get my snake to follow the mouse pointer. here is my updated code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340, 280, 100, 10);
Rectangle rect_black = new Rectangle(240, 280, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
greenBrush.Dispose();
g.Dispose();
}

int x = 0, y = 0;

private void timer1_Tick(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
g.FillRectangle(greenBrush, rect_green);
g.FillRectangle(blackBrush, rect_black);
if (x >= 340)
{
x = 340;
}
x += 5;
greenBrush.Dispose();
g.Dispose();

if (MouseButtons == MouseButtons.Left)
{
Graphics g_two = this.CreateGraphics();
SolidBrush greenBrush_two = new SolidBrush(Color.Green);
SolidBrush blackBrush_two = new SolidBrush(Color.Black);
Rectangle rect_green_two = new Rectangle(340 + x, 280 + y, 10, 100);
Rectangle rect_black_two = new Rectangle(340 + x, 180 + y, 10, 100);
g_two.FillRectangle(greenBrush_two, rect_green_two);
g_two.FillRectangle(blackBrush_two, rect_black_two);
y += 5;
greenBrush_two.Dispose();
g_two.Dispose();
}
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{

}
}
}

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 13
• 9
• 11
• 15
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!