- I need to be able to move my token.
- The token starts in the square 1.
- If the token is on squearedsquare 1 and is moved by 3 then the token is on square 4.
- A player can win the game.
- The first in reachwho reaches the square 100 is the winner.
- You can't over-square the board. If you do it your position will be remind toremain the previous onesame. (97 + 4 = 97)
- The moves are based inon a roll dice between 1 and 6.
- The player rollrolls the dice, then movemoves the token.
Keeping all this in mind as I did this.
using System;
using System.Collections.Generic;
namespace Snakes_and_ladders
{
class CBoard
{
private int[] board;
List<CPlayer> players = new List<CPlayer>();
public int[] Board { get => board; }
public CBoard()
{
// Se crea unA tablero100-cell deboard 100is celdascreated porby defectodefault.
board = new int[100];
Array.Clear(board, 0, board.Length);
}
/*
*/ FuncionFunction parato crearcreate Ladders yand Snakes.Se
changes the value
* Se cambia el// valorof delthe array enin elthe index [i-1] siendobeing i lathe key del diccionario.of the
*// eldictionary.the valorvalue quethat seis guardasaved enin esethat index correspondecorresponds
al valor del tablero donde se
// to the value of the board *where moverathe elplayer jugadormoves enin casocase deof
caer en dicho // falling into said Index.
* //
*// Ex: Key => 2, ValorValue => 10 implica queimplies existethat unathere escalerais quea valadder desdethat
la celda
// goes from *cell 1 a lato celdacell 9 del tablero.
of the */board.
private void createSnakesOrLadders(Dictionary<int, int> dataDict)
{
foreach (KeyValuePair<int, int> data in dataDict)
{
board[data.Key - 1] = data.Value - 1;
}
}
/* Sobrecarga/ deDefault constructor por defectooverload
*// Creates Creaan unA boardx deL AxLboard +by añadiradding escalerasladders yand serpientessnakes.
*/
public CBoard(int altura, int largo,
Dictionary<int, int> ladders = null, Dictionary<int, int> snakes = null)
{
// ComoAt minimoa esminimum, necesarioa un2x2 tableroboard deis 2x2necessary.
if (altura < 2 || largo < 2)
throw new Exception("La"The alturaheight yand largolength necesitaneed serto albe menosat mayorleast agreater than 1.");
// TamañoInitial inicialsize delof numerothe denumber of ladders yand snakes enon elthe tableroboard.
int ladderSize = 0;
int snakesSize = 0;
// SiIf elthe tableroboard nois esnot null, guardamoswe elsave numerothe realactual denumber of ladders yand snakes.
if (!(ladders is null))
ladderSize = ladders.Count;
if (!(snakes is null))
snakesSize = snakes.Count;
// CreamosWe elcreate tablerothe board, conwith valoresvalues aset to 0.
board = new int[altura * largo];
Array.Clear(board, 0, board.Length);
// Si elIf tamañothe total delsize numeroof dethe number of ladders yand snakes es menoris queless lathan mitadhalf delthe tableroboard
// Se crean los ladders yand snakes enare elcreated tableroon the board. SinoIf not, sethe lanzaexception lais excepcionthrown.
if ((ladderSize * 2) + (snakesSize * 2) / 2 < board.Length)
{
if (!(ladders is null))
createSnakesOrLadders(ladders);
if (!(snakes is null))
createSnakesOrLadders(snakes);
}
else
{
throw new Exception("La suma"The total desum of Snakes yand Ladders no puede superarcannot elexceed 50% delof tablerothe board.");
}
}
}
}
using System;
using System.Threading;
namespace Snakes_and_ladders
{
class CPlayer
{
int[] board;
private int position = 0;
private string nickName = null;
private int diceResult = 0;
private bool winner = false;
public int Position { get => position + 1; }
public int DiceResult { get => diceResult; }
public string NickName { get => nickName; }
public bool Winner { get => winner; }
public CPlayer(string nickName, CBoard board) {
this.nickName = nickName;
this.board = board.Board;
}
public void Roll()
{
// Se espereWait 30 milisegundos para cambiarmilliseconds lato semillachange delthe random seed.
Random rnd = new Random();
Thread.Sleep(30);
diceResult = rnd.Next(1, 7);
}
public void Move()
{
// Move the player N dice cells.
if (position + diceResult < board.Length)
if (board[position + diceResult] == 0)
position = position + diceResult;
else
position = board[diceResult + position];
if (position == board.Length - 1)
winner = true;
}
}
}