Jump to content
  • Advertisement
phil67rpg

c# game

Recommended Posts

Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.Threading.Tasks;
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.Threading.Tasks;
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 this post


Link to post
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.Threading.Tasks;
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!