Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
ok so here's my new and improved question i need to make the piece move from 9,9 backwards to 9,8 and so on using a random number generator (1,6) to simulate a dice moving the piece up the board

here is my code so far

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

namespace SnakesNLadders_MS4403
{
    class Program
    {
        static char[,] CH_AR_Board = new char[10, 10];
        static string player1;
        static string player2;

        static void Main(string[] args)
        {

            Introduction();
            DisplayBoard();
            Console.ReadKey();
        }

        static void DisplayBoard()
        {
            //display column numbers for board
            for (int i = 0; i < CH_AR_Board.GetLength(1); i++)
            {
                //CH_AR_Board.GetLength(0) gives the size of the second dimension of the array
                if(i == 0)
                {
                    //at the beginning i need to add a bit more space than usual
                    Console.Write("   " + i + " ");
                }
                else
                {
                    //display value of i with spaces
                    Console.Write(" " + i + " ");
                }
            }
            //skip a line
            Console.WriteLine();

            //nested for loop to iterate around 2D array
            // outer for loop deals with rows (so i)
            // inner for loop deals with columns (so j)

            for (int i = 0; i < CH_AR_Board.GetLength(0); i++)
            {
                //CH_AR_Board.GetLength(0) gives the size of the first dimension of the Array
                //Display row numbers for board
                Console.Write(i + " ");
                for(int j = 0; j < CH_AR_Board.GetLength(1); j++)
                {
                    //display value in board space using counters as indexes
                    Console.Write("[" + CH_AR_Board[i, j] + "]");
                }
                Console.WriteLine();
            }
        }

        static void Introduction()
        {
            Console.Title = "Snakes And Ladders";
            Console.WriteLine("Welcome to Snakes And Ladders, A Simple Game\n");
            Console.WriteLine("Press Any Key To Continue");
            Console.ReadLine();
            Console.Clear();

            Console.WriteLine("Player 1 Enter Your Name ");
            player1 = Console.ReadLine();
            Console.WriteLine("Payer 2 Enter Your Name ");
            player2 = Console.ReadLine();
            Console.Clear();
        }
    }
}



thank you

What I have tried:

i have tried
C#
static void PlacePiece(int row, int col, string player)
        {
            if (player == "Player1")
            {
                CH_AR_Board[row, col] = Convert.ToChar(1);
            }
            else
            {
                CH_AR_Board[row, col] = Convert.ToChar(5);
            }

        }


it places the piece but i have no idea how u=i would use the random generator to move it
Posted
Updated 30-Mar-16 18:03pm
v5
Comments
Matt T Heffron 30-Mar-16 15:11pm    
Instead of saying you "tried a variety of things", show us the code for them (at least some of them). Then we can better see how to point you in the right direction.

Since this looks like homework, you are not likely to get working code, but maybe some hints.
Sergey Alexandrovich Kryukov 30-Mar-16 15:15pm    
I would say, this is a wrong way of posing a problem. You reduce the problem to a particular notion, a game loop, but the real question should be on the general game design. And yes, the main loop should be the key of it. You should not try to stick it into the existing design, but need to put it in the core design, that's it. But if you do it, it's unclear what you may need help with...
—SA

1 solution

Please see the comments to the comments to the question, by Matt T Heffron and myself. Essentially, this is the answer.

The skeleton of your code should be something like
C#
//...

static void Main() {
   Game game = new Game();
   Game.Introduction();
   Game.DisplayBoard();
   while (!Game.Finished) {
      Game.Step();
      // ...
   }    
}
Here, you can see one suggestion: use OOP, define some non-static class Game; don't try to develop all in one class. This is not required, but would make your work more comfortable for you. The main loop is usually also should be in the same game class; I've shown it separately just to show the look of the mail loop.

Some more moments:

Don't use Console.ReadKey(); use Console.ReadKey(true) instead. This is all the console input you would need. You won't really need Console.WriteLine or Console.Read* at all (except, perhaps, introduction). You will soon see it yourself, so it's better to understand it from the very beginning. You will need output to a location of screen, so you will need Console members Cursor* and Window*, and some more. Please see: Console Class (System)[^].

You will have to experiment with those members to achieve pseudo-graphical output. But that said: even a simple board game on a console doesn't look serious. It's very likely that the GUI application would be easier (and of course look and behave much better), especially with WPF and its Canvas class. However, if you want to practice with console, it could be an interesting exercise.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900