任何人都可以给我c#console应用程序中的shuffle游戏的代码片段 [英] Can any one give me the code snippet for the shuffle game in c# console application

查看:116
本文介绍了任何人都可以给我c#console应用程序中的shuffle游戏的代码片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随机播放规则

随机播放按照以下规则播放:



n这是一款单人游戏。



n当游戏开始时,应用程序会以最少的击键次数显示完成游戏的玩家的名字。然后,它在3×3网格中以随机顺序显示从0到8的数字,如下图所示。





随机游戏界面



n游戏的目标是按升序排列数字,如下列安排所示。

b / b








1 2 3



4 5 6



7 8 0



目标安排网格中的数字



n零表示空置空间,因此它应该是最后一个。



n数字可以在网格中向上,向下,向左或向右移动。



n数字可以通过与空置空间交换来移动。



n一次只能移动一个号码。



n只有可以与空置空间交换相同行或列中空白区域附近的数字。



n一个数字不能对角移动。



n游戏没有时间限制。但是,应该计算完成游戏所需的按键次数。



设计规格



游戏的设计应符合以下规格:



n这是一款单人游戏。



n完成游戏没有时间限制。



n每次开始新游戏时,屏幕上最初出现的数字应该随机播放。 $>


n零表示一个空位。



n玩家可以交换一个与零相邻的数字。这个数字应该与零在同一行或列中。



n空位,零,可以向左,向右,向上或向下移动。



n玩家可以通过按下以下键将数字与零交换:



l L或l:交换0与左边的数字。



l R或r:用右边的数字交换0。



l U或u:用上面的数字交换0。



l D或d:用下面的数字交换0。



n在按升序排列所有数字后,玩家将按键盘上的E或e结束游戏。



n当玩家按下E时或者,程序将检查玩家是否正确排列了数字并相应地显示消息。



n应计算完成游戏的击键次数。如果计数小于记录计数,则应要求玩家输入他/她的姓名。播放器的名称和击键次数应作为新记录保存在文件中。当玩家开始新游戏时,此名称和击键次数应出现在屏幕上。









好​​朋友,如果你这么想,那么我会亲自试试....感谢你的回复和不错的网站,你们非常乐于助人......

解决方案

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间随机播放
{
class 节目
{
int [,] CorrectMatch = {{ 1 2 3 },{ 4 5 6 },{ 7 8 9 }};
int [,] Temp = {{ 0 0 0 },{ 0 0 0 },{ 0 0 0 }};
int [] elements = { 0 1 2 3 4 5 6 7 8 };
int [,] ShuffledElements = {{ 0 0 0 },{ 0 0 0 },{ 0 0 0 }};
Random rnd = new Random();
public void 随机播放()
{

int [] RandomArray = elements.OrderBy(x = > rnd.Next())。ToArray( );
int g = 0 ;
for int i = 0 ; i < 3 ; i ++)
{
for int j = 0 ; j < 3 ; j ++)
{
ShuffledElements [i,j] = RandomArray [g ]。
g ++;

}
}
.Display();
}
public void 显示()
{

int j = 0 ;
Console.Write( \ n \ nn \ n);
while (j < 3
{
Console.Write( \t\t\ t \t);
for int i = 0 ; i < 3 ; i ++)
{
Console.Write (ShuffledElements [j,i] + );

}
Console.WriteLine( \ n );
j ++;
}
.GamePlay();
}
public void GamePlay()
{
Console.WriteLine( 1.L或l:用左边的数字交换0。\ n2.R或r:用右边的数字交换0。\ n3.U或u:用上面的数字交换0。\ n4.D或d:用下面的数字交换0。\ n5。按E检查如果你的谜题得到解决。);
Console.Write( 输入您的选择);
string choice = Console.ReadLine();
switch (选择)
{
case R
for int i = 0 ; i < 3 ; i ++)
{
for int j = 0 ; j< 3; j ++)
{
if (ShuffledElements [i,j] == 0 && j < 2
{
Temp [i,j] = ShuffledElements [i,j];
ShuffledElements [i,j] = ShuffledElements [i,j + 1 ];
ShuffledElements [i,j + 1 ] = Temp [i,j];
}
}
}
.Display();
break ;
case L
for int i = 0 ; i < 3 ; i ++)
{
for int j = 0 ; j < 3 ; j ++)
{
if (ShuffledElements [i,j] == 0
{
Temp [i,j] = ShuffledElements [i ,j];
ShuffledElements [i,j] = ShuffledElements [i,j - 1 ];
ShuffledElements [i,j - 1 ] = Temp [i,j];
}
}
}
.Display();
break ;
case U
for int i = 0 ; i < 3 ; i ++)
{
int j = 0 ;
while (j< 3)
{
if (ShuffledElements [i,j] == 0
{
Temp [i,j] = ShuffledElements [i,j];
ShuffledElements [i,j] = ShuffledElements [i - 1 ,j];
ShuffledElements [i - 1 ,j] = Temp [i,j];
}
j ++;
}
}
.Display();
break ;
case D
for int i = 0 ; i < 2 ; i ++)
{
的类=code-keyword>( int j = 0 ; j< 3; j ++)
{
if (ShuffledElements [i,j] == 0
{
Temp [i,j] = ShuffledElements [i,j];
ShuffledElements [i,j] = ShuffledElements [i + 1 ,j];
ShuffledElements [i + 1 ,j] = Temp [i,j];
}
}
}
.Display();

break ;
case E
this .CheckWin();
break ;
默认
Console.Clear();
Console.WriteLine
\ n \ nn \ nn \\ t \ tt请按有效输入);
this .Display();
break ;

}
}
public void CheckWin( )
{
Console.Clear();
if (ShuffledElements == CorrectMatch)
{

Console.WriteLine( \ n \ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\跨度>);
}
else
{
Console.WriteLine( \ n \ n \ n \\ n \ n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
this .Display();
}
}

静态 void Main( string [] args)
{
Program p = new Program();
p.Shuffle();
p.GamePlay();
Console.ReadLine();
}
}
}


不,我们不会。因为这不是这个网站实际工作的方式。

这里不是一个代码订购网站,你可以来发布你的要求,并期望有人为你提供一个完整的现成解决方案。 />


请参阅:

CodeProject质量保证常见问题解答 [ ^ ]

您尝试了什么? [ ^ ]

Rules of Shuffle
Shuffle is played as per the following rules:

n It is a single-player game.

n When the game starts, the application displays the name of the player who has completed the game in the minimum number of keystrokes. Then, it displays the numbers from zero to eight in random order in a 3×3 grid, as shown in the following figure.


Interface of the Shuffle Game

n The objective of the game is to arrange the numbers in ascending order, as shown in the following arrangement.





1 2 3

4 5 6

7 8 0

Targeted Arrangement of Numbers in the Grid

n Zero indicates vacant space and therefore, it should come at the last.

n Numbers can be moved up, down, left, or right within the grid.

n The numbers can be moved by swapping them with the vacant space.

n Only one number can be moved at a time.

n Only the numbers adjacent to the vacant space in the same row or column can be swapped with the vacant space.

n A number cannot be moved diagonally.

n There is no time limit for the game. However, the number of key presses required to complete the game should be counted.

Design Specifications

The design of the game should be as per the following specifications:

n It is a single-player game.

n There is no time limit for completing the game.

n The numbers that initially appear on the screen should be shuffled each time a new game is started.

n Zero indicates a vacant place.

n The player can swap a number that is adjacent to zero. This number should be in the same row or column as zero.

n The vacant place, zero, can be moved left, right, up, or down.

n A player can swap a number with zero by pressing the following keys:

l L or l: Swaps 0 with the number on its left.

l R or r: Swaps 0 with the number on its right.

l U or u: Swaps 0 with the number above.

l D or d: Swaps 0 with the number below.

n After arranging all the numbers in ascending order, the player will press E or e on the keyboard to end the game.

n When the player presses E or e, the program will check whether the player has arranged the numbers correctly and display a message accordingly.

n The number of keystrokes to complete the game should be counted. If the count is less than the record count, the player should be asked to enter his/her name. The name of the player and the number of keystrokes should be saved in a file as the new record. This name and the number of keystrokes should appear on the screen when a player starts a new game.




okay friends if you think so then i will try this by my self.... thanks for you reply and nice site you have very helpful...

解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shuffle
{
    class Program
    {
        int[,] CorrectMatch = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int[,] Temp = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
        int[] elements = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        int[,] ShuffledElements = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
        Random rnd = new Random();
        public void Shuffle()
        {

            int[] RandomArray = elements.OrderBy(x => rnd.Next()).ToArray();
            int g = 0;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    ShuffledElements[i, j] = RandomArray[g];
                    g++;

                }
            }
            this.Display();
        }
        public void Display()
        {

                        int j = 0;
                        Console.Write("\n\n\n");
            while (j < 3)
            {
                Console.Write("\t\t\t\t");
                for (int i = 0; i < 3; i++)
                {
                    Console.Write(ShuffledElements[j, i] + "  ");

                }
                Console.WriteLine("\n");
                j++;
            }
            this.GamePlay();
        }
        public void GamePlay()
        {
            Console.WriteLine("1. L or l: Swaps 0 with the number on its left.\n2. R or r: Swaps 0 with the number on its right.\n3. U or u: Swaps 0 with the number above.\n4. D or d: Swaps 0 with the number below.\n5. Press E to check if your puzzle is solved.");
            Console.Write("Enter your choice");
            string choice = Console.ReadLine();
            switch (choice)
            {
                case "R":
                    for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j <3; j++)
                        {
                            if (ShuffledElements[i, j] == 0 && j < 2)
                            {
                                Temp[i, j] = ShuffledElements[i, j];
                                ShuffledElements[i, j] = ShuffledElements[i, j + 1];
                                ShuffledElements[i, j + 1] = Temp[i, j];
                            }
                        }
                    }
                    this.Display();
                    break;
                case "L":
                    for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            if (ShuffledElements[i, j] == 0)
                            {
                                Temp[i, j] = ShuffledElements[i, j];
                                ShuffledElements[i, j] = ShuffledElements[i, j - 1];
                                ShuffledElements[i, j - 1] = Temp[i, j];
                            }
                        }
                    }
                    this.Display();
                    break;
                case "U":
                    for (int i = 0; i < 3; i++)
                    {
                        int j = 0;
                        while(j<3)
                        {
                            if (ShuffledElements[i, j] == 0)
                            {
                                Temp[i, j] = ShuffledElements[i, j];
                                ShuffledElements[i, j] = ShuffledElements[i - 1, j];
                                ShuffledElements[i - 1, j] = Temp[i, j];
                            }
                            j++;
                        }
                    }
                    this.Display();
                    break;
                case "D":
                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = 0; j <3 ; j++)
                        {
                            if (ShuffledElements[i, j] == 0)
                            {
                                Temp[i, j] = ShuffledElements[i, j];
                                ShuffledElements[i, j] = ShuffledElements[i + 1, j];
                                ShuffledElements[i + 1, j] = Temp[i, j];
                            }
                        }
                    }
                    this.Display();

                    break;
                case "E":
                    this.CheckWin();
                    break;
                default:
                    Console.Clear();
                    Console.WriteLine
                        ("\n\n\n\n\t\t\tPlease Press Valid input");
                    this.Display();
                    break;

            }
        }
            public void CheckWin()
            {
                Console.Clear();
                if (ShuffledElements == CorrectMatch)
                {

                    Console.WriteLine("\n\n\n\n\t\t\tCongratulations You Solved it!!!!!!");
                }
                else
                {
                    Console.WriteLine("\n\n\n\n\t\t\tStill Not Matched Correctly");
                    this.Display();
                }
            }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Shuffle();
            p.GamePlay();
            Console.ReadLine();
        }
    }
}


No, we won't. Because this is not how this site works actually.
Here is not a code-ordering site where you can come and post your requirements, and expect someone to deliver you a full ready-made solution.

Please see:
CodeProject QA FAQ[^]
What have you tried?[^]


这篇关于任何人都可以给我c#console应用程序中的shuffle游戏的代码片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆