pikau
Accessing individual words in user input (Console.Readline())
I am looking to access specific words that the user inputs. For example, i have to first assess whether the first word inputted is a valid command (e.g. "add", "help" "exit" etc.) and then see if the word following that is a valid constraint for that command. How might i go about this? Here is my current code:
namespace A2
{
// Define a section to hold all the info that prompts the user to provide responses upon first opening the project.
public class UserInput
{
static void Main()
{
// First we must print a statement that can be anything to welcome the user to the macchine thing
// Write out custom message to the UI
Console.WriteLine($"Welcome to the Tom's Obstacle Avoidance System!");
// Write out the valid commands
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
// List other options
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");
// While loop that keeps running while true (while the use hasnt yet entered exit) that checks
// the validity/input of the user.
while (true)
{
// Prompt the user to enter a command.
Console.WriteLine($"Enter command:");
// Read the users input
string UserInput = Console.ReadLine();
// If the user says exit, then we terminate the program
// Check if it is exit first to avoid unnessary iterating of the valid commands list if it is "exit"
if (UserInput == "exit")
{
// Terminate
break;
}
// If the user enters something else other than exit, then we can check if it is a valid command
if (!Commands.ValidCommands.Contains(UserInput))
{
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
// Prompting the help message
Console.WriteLine($"Type 'help' to see a list of commands.");
}
if (UserInput == "help")
{
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");
}
if (UserInput == "add")
{
switch (UserInput)
{
case "gaurd":
Console.WriteLine($"Chosen Gaurd");
break;
case "fence":
Console.WriteLine($"Chosen Fence");
break;
case "sensor":
Console.WriteLine($"Chosen Sensor");
break;
case "camera":
Console.WriteLine($"Chosen Camera");
break;
}
}
else
Console.WriteLine($"You need to specify an obstacle type.");
if (!Obstacles.ValidObstacles.Contains(UserInput))
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
}
}
}
}
namespace A2
{
// Define a section to hold all the info that prompts the user to provide responses upon first opening the project.
public class UserInput
{
static void Main()
{
// First we must print a statement that can be anything to welcome the user to the macchine thing
// Write out custom message to the UI
Console.WriteLine($"Welcome to the Tom's Obstacle Avoidance System!");
// Write out the valid commands
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
// List other options
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");
// While loop that keeps running while true (while the use hasnt yet entered exit) that checks
// the validity/input of the user.
while (true)
{
// Prompt the user to enter a command.
Console.WriteLine($"Enter command:");
// Read the users input
string UserInput = Console.ReadLine();
// If the user says exit, then we terminate the program
// Check if it is exit first to avoid unnessary iterating of the valid commands list if it is "exit"
if (UserInput == "exit")
{
// Terminate
break;
}
// If the user enters something else other than exit, then we can check if it is a valid command
if (!Commands.ValidCommands.Contains(UserInput))
{
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
// Prompting the help message
Console.WriteLine($"Type 'help' to see a list of commands.");
}
if (UserInput == "help")
{
Console.WriteLine($"Valid commands are:");
Commands.ListCommands();
Console.WriteLine($"help: displays this help message");
Console.WriteLine($"exit: closes this program");
}
if (UserInput == "add")
{
switch (UserInput)
{
case "gaurd":
Console.WriteLine($"Chosen Gaurd");
break;
case "fence":
Console.WriteLine($"Chosen Fence");
break;
case "sensor":
Console.WriteLine($"Chosen Sensor");
break;
case "camera":
Console.WriteLine($"Chosen Camera");
break;
}
}
else
Console.WriteLine($"You need to specify an obstacle type.");
if (!Obstacles.ValidObstacles.Contains(UserInput))
// Inform the user that their input is invalid, returning their input back to them.
Console.WriteLine("Invalid option: {0}", UserInput);
}
}
}
}
1 replies
Methods that check strings issues
Hey, im currently trying to pass a few strings of a 2D array through methods in order to find a winner of this game. Basically, no matter what, it always returns as no winner, rather than X or O. The relevant code in classes is below:
Board.cs
public string GetRow(int row)
// We need to convert the int row values into string values. (0 - 2)
// Will use the same loop for row and column, basically you define the row/column string as empty,
// then we pass this through a loop that iterates between the three rows/columns between -1 and 3 (0 < 3)
// and this append the value (column or row) via the += operator to each individual string.
{
string rowString = "";
for (int i = 0; i < 3; i++)
{
rowString += board[row, i];
}
return rowString;
}
/// <summary>
/// Get the string representation of a column of the board, with 0 being the left column and 2 being the right column
/// </summary>
/// <param name="column">The column number</param>
/// <returns>The string representation of a column of the board</returns>
public string GetColumn(int column)
{
string columnString = "";
for (int i = 0; i < 3; i++)
{
columnString += board[column, i];
}
return columnString;
}
/// <summary>
/// Get the string representation of a diagonal of the board
/// </summary>
/// <param name="topLeftToBottomRight">Whether the diagonal is from top left to bottom right</param>
/// <returns>The string representation of a diagonal of the board</returns>
/// Only difference here is that I have to use the topLeftToBottomRight in an if or else statement becuase
/// theres only 2 possible ways diagonals can go.
public string GetDiagonal(bool topLeftToBottomRight)
{
string diagonalString = "";
if (topLeftToBottomRight)
{
for (int i = 0; i < board.GetLength(0); i++)
{
diagonalString += board[i, i];
}
}
else
{
for (int i=0; i < board.GetLength(0); i++)
{
diagonalString += board[i, board.GetLength(1) -1 -i];
}
}
return diagonalString;
}
/// <summary>
/// Check if a string is homogeneous, i.e. all characters in the string are the same
/// </summary>
/// <param name="line">The string to check for homogeneity</param>
public static bool IsHomogeneous(string line)
{
if (line == "XXX" || line == "OOO") return true;
else return false;
}
}
}
public string GetRow(int row)
// We need to convert the int row values into string values. (0 - 2)
// Will use the same loop for row and column, basically you define the row/column string as empty,
// then we pass this through a loop that iterates between the three rows/columns between -1 and 3 (0 < 3)
// and this append the value (column or row) via the += operator to each individual string.
{
string rowString = "";
for (int i = 0; i < 3; i++)
{
rowString += board[row, i];
}
return rowString;
}
/// <summary>
/// Get the string representation of a column of the board, with 0 being the left column and 2 being the right column
/// </summary>
/// <param name="column">The column number</param>
/// <returns>The string representation of a column of the board</returns>
public string GetColumn(int column)
{
string columnString = "";
for (int i = 0; i < 3; i++)
{
columnString += board[column, i];
}
return columnString;
}
/// <summary>
/// Get the string representation of a diagonal of the board
/// </summary>
/// <param name="topLeftToBottomRight">Whether the diagonal is from top left to bottom right</param>
/// <returns>The string representation of a diagonal of the board</returns>
/// Only difference here is that I have to use the topLeftToBottomRight in an if or else statement becuase
/// theres only 2 possible ways diagonals can go.
public string GetDiagonal(bool topLeftToBottomRight)
{
string diagonalString = "";
if (topLeftToBottomRight)
{
for (int i = 0; i < board.GetLength(0); i++)
{
diagonalString += board[i, i];
}
}
else
{
for (int i=0; i < board.GetLength(0); i++)
{
diagonalString += board[i, board.GetLength(1) -1 -i];
}
}
return diagonalString;
}
/// <summary>
/// Check if a string is homogeneous, i.e. all characters in the string are the same
/// </summary>
/// <param name="line">The string to check for homogeneity</param>
public static bool IsHomogeneous(string line)
{
if (line == "XXX" || line == "OOO") return true;
else return false;
}
}
}
30 replies
Defining and checking a 2D string array
Hey.
I am creating a tic tac toe board game with 3 rows and 3 columns (a 2D array). When I input, for example far more than 3 characters for a row into the console, it always returns that the board is valid. My isValid method is not working evidently. Im not sure if it is becuase of improper intiialisation of the board class, game class, or the method itself. Please let me know!
Board.cs relevant code:
Game.cs relevant code:
Relevant program.cs code: (cannot change this)
Just looking for a bit of guidance!
class Board
{
private char[,] board;
/// <summary>
/// Get whether the board is valid, i.e. the board is a 3x3 board with only 'X', 'O' and '.' characters
/// and the number of 'X' is equal to or one more than the number of 'O'
/// </summary>
/// <returns>True if the board is valid, false otherwise</returns>
///
// Need to also make sure that the number of o is not 1 less than x as x goes first.
public bool IsValid()
{
// CHeck if both the rows and column of the board are in 3x3
if (board.GetLength(0) != 3)
return false;
else if (board.GetLength(1) != 3)
return false;
else return true;
}
/// <summary>
/// Constructor of the Board class. Fill the 2D board of characters using the lines array of strings.
/// </summary>
/// <param name="lines">The array of strings to fill the 2D board of characters</param>
public Board(string[] lines)
{
// Constructor that creates a 3x3 array of characters (char)
board = new char[3, 3];
}
class Board
{
private char[,] board;
/// <summary>
/// Get whether the board is valid, i.e. the board is a 3x3 board with only 'X', 'O' and '.' characters
/// and the number of 'X' is equal to or one more than the number of 'O'
/// </summary>
/// <returns>True if the board is valid, false otherwise</returns>
///
// Need to also make sure that the number of o is not 1 less than x as x goes first.
public bool IsValid()
{
// CHeck if both the rows and column of the board are in 3x3
if (board.GetLength(0) != 3)
return false;
else if (board.GetLength(1) != 3)
return false;
else return true;
}
/// <summary>
/// Constructor of the Board class. Fill the 2D board of characters using the lines array of strings.
/// </summary>
/// <param name="lines">The array of strings to fill the 2D board of characters</param>
public Board(string[] lines)
{
// Constructor that creates a 3x3 array of characters (char)
board = new char[3, 3];
}
public class Game
{
// Board field
internal Board Board;
// Constructor that takes in 3 strings that represents the board (this string is defined in program)
public Game(string[] lines)
{
Board = new Board(lines);
}
public class Game
{
// Board field
internal Board Board;
// Constructor that takes in 3 strings that represents the board (this string is defined in program)
public Game(string[] lines)
{
Board = new Board(lines);
}
Game game = new Game(lines);
if (game.Board.IsValid())
{
Console.WriteLine($"Valid as hell");
Console.WriteLine(game.HasWinner() ? $"The winner is: {game.GetWinner()}!" : "There is no winner!");
}
else
{
Console.WriteLine("The board is invalid!");
}
Console.WriteLine("===========================");
Game game = new Game(lines);
if (game.Board.IsValid())
{
Console.WriteLine($"Valid as hell");
Console.WriteLine(game.HasWinner() ? $"The winner is: {game.GetWinner()}!" : "There is no winner!");
}
else
{
Console.WriteLine("The board is invalid!");
}
Console.WriteLine("===========================");
428 replies