Making a simple AI for a battleship program in C# - c#

I've just recently started learning to code C# to hopefully get a job someday. I'm trying to get 3 randomly generated points in a 5x5 grid. For some reason when I try to run it it just auto crashes. For testing purposes I added a Console.WriteLine and Console.ReadKey to try and see the output but it still auto closed immediately. Is there any reason why this shouldn't be working? Thanks for any help :D
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
}
public void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
}

your method is not called. that is why it auto closes.
nothing is done in your program.
class Program
{
static void Main(string[] args)
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
now the output is:
complete code:
class Program
{
static void Main(string[] args)
{
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
Console.WriteLine(AiB[0]);
Console.ReadKey();
}
}
This is how i found out that the method was not used:
Before:
After:

As already called out above you are missing the CompB();from the main method .
One more I just looked into the for loop in line 18 it starts from in the i=1 but you are doing
System.Console.WriteLine(AiB[0]);
you should try out something like
System.Console.WriteLine(AiB[1]);
static void Main(string[] args)
{`enter code here`
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
System.Console.WriteLine(AiB[1]);
System.Console.ReadKey();
}

You need to call your method CompB() from the main method. Then you have to write out all the values int the array.
class Program
{
static void Main(string[] args)
{
CompB();
}
public static void CompB()
{
int[] AiB = new int[6];
for (int i = 1; i < 3; i++)
{
Random rnd = new Random();
int AiR = rnd.Next(0, 26);
AiB[i] = AiR;
}
//Write all values:
for (int i = 0; i < 6; i++)
{
System.Console.WriteLine("Value of {0}: {1}", i, AiB[i]);
}
System.Console.ReadKey();
}
}

Related

How to store instances of an integer in an array in a separate variable in C#?

Goal
A C# program that picks a random number from 1 to 6 and stores it
Question
Which way to store: the entire sequence or an array of 6 elements with increments to the n-1 index for every hit of n?
Code
using System;
namespace DiceProbabilityCalc
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] instanceCount = new int[1000];
for (int i = 0; i < 999; i++)
{
int num = rnd.Next(1, 7);
}
}
}
}
Thanks to #PeterSmith and #Martheen for solving the question in the comments: The array instanceCount can be used to store the number of instances instead of the entire sequence. This is the resulting code.
using System;
namespace DiceProbabilityCalc
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int[] instanceCount = new int[6];
for (int i = 0; i < 999; i++)
{
int num = rnd.Next(1, 7);
instanceCount[num - 1]++;
}
for (int j = 0; j < instanceCount.Length; j++)
{
Console.WriteLine(instanceCount[j]);
}
}
}
}

VB.net C# number and letter generator

I been having issues creating a number and letter generator, it should look like this: 9WJLNN8MNDVJCFLQJ4W93YH6ZM:ZWN6QV9ZXG9YCMWAXXWP492DS9
26 letters and numbers randomly colon and same thing after colon, but I keep getting errors but heres my code from what I got so far
Right now I cant even get the numbers and letters together to make it work, im just so confused on what to do. If anyone can help me out that would be amazing. I've been working on this for a few days now.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
namespace Testing23891721983712983981
{
class Program
{
static void Main(string[] args)
{
{
Random rand = new Random();
int[] numbers = new int[4];
for (int i = 0; i < 4; i++)
{
numbers[i] = rand.Next(1000, 10000);
}
string prefix = string.Join("-", numbers);
for (int i = 0; i < 100; i++)
{
int threeDigits = rand.Next(100, 1000);
RandomGenerator generator = new RandomGenerator();
string str = generator.RandomString(26, false);
Console.WriteLine(threeDigits, str);
Console.ReadKey();
}
}
}
public class RandomGenerator
{
// Generate a random number between two numbers
public int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
// Generate a random string with a given size
public string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
// Generate a random password
public string RandomPassword()
{
StringBuilder builder = new StringBuilder();
builder.Append(RandomString(4, true));
builder.Append(RandomNumber(1000, 9999));
builder.Append(RandomString(2, false));
return builder.ToString();
}
}
}
}
use Guid class to generate random numbers with string and then use substring on it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
{
Random rand = new Random();
int[] numbers = new int[4];
for (int i = 0; i < 4; i++)
{
numbers[i] = rand.Next(1000, 10000);
}
string prefix = string.Join("-", numbers);
string strguid = "";
for (int i = 0; i < 2; i++)
{
Guid guid = Guid.NewGuid();
if (strguid != "")
{
strguid = strguid + ":" + guid.ToString().Replace("-", "").Substring(0, 26).ToUpper();
}
else
{
strguid = guid.ToString().Replace("-", "").Substring(0, 26).ToUpper();
}
}
Console.WriteLine(strguid);
Console.ReadKey();
}
}
}
}

C# about return integer array

When I write C# code return the integer array,why it just output 3? it cannot random number.Please help me, thanks!
static void Main(string[] args)
{
Console.WriteLine(3);
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
}
return array;
}
static void Main(string[] args)
{
var output = RandomArray(10);
for (int i = 0; i < output.Length; i++)
{
Console.WriteLine(output[i]);
}
Console.WriteLine();
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
}
return array;
}
In output variable, you will get array. You can use it further.
You can do this.
The problem is you were just writing 3 to the console
private static Random rand = new Random();
public static void Main(string[] args)
{
foreach(var item in RandomArray(3))
Console.WriteLine(item);
}
static int[] RandomArray(int items)
{
int[] array = new int[items];
for (int i = 0; i < items; i++)
array[i] = rand.Next(100, 200);
return array;
}
Demo Here
Random Class
Console.WriteLine Method
Main is the entry point in a C# application. Main is called by the common language runtime (CLR) at the start of your program's execution. In other words, Main is called automatically. RandomArray is also a method. However, it is not an Entry point and therefore it will not be called automatically. You have to call RandomArray from main in order for the method to execute.
Right now your program is effectively just:
static void Main(string[] args)
{
Console.WriteLine(3);
}
The rest of the code is just sitting around doing nothing.
In order to fix this problem you need to call RandomArray from Main
static void Main(string[] args)
{
Console.WriteLine(3);
RandomArray(5);
}
And if you also want to output more data to the console then you can do something like:
static int[] RandomArray(int items)
{
int[] array = new int[items];
Random rand = new Random();
for (int i = 0; i < items; i++)
{
array[i] = rand.Next(100, 200);
Console.WriteLine($"{array[i]} inserted into array");
}
return array;
}
Presumably you also want to do something with the return value of RandomArray. Modify Main to assign the return value to a variable. Then you can do something with the result:
static void Main(string[] args)
{
Console.WriteLine(3);
int[] result = RandomArray(5);
foreach (var element in result)
{
Console.WriteLine(element);
}
}

The name 'myNumber' does not exist in the current context"

I'm sure this has a very simple solution but I'm new to C# and I just can't seem to get rid of this error message.
I'm trying to generate a random number between 1 and 100 inclusive in my code.
namespace FirstName_A1
{
class FromTwoDtoOneD
{
static void Main1()
{
int[,] twoDArray = new int[10,12];
int[] oneDArray = new int[10*12];
FillTwoDimArray(twoDArray);
DisplayTwoDimArray(twoDArray);
StoreValues(twoDArray, oneDArray);
DisplayOneDimArray(oneDArray);
}
static void FillTwoDimArray(int[,] twoDArray) //method to fill twoD array with random numbers
{
Random myRandom = new Random(); //Random class is under System namespace
myNumber = myRandom.Next(1, 101); //generates number between 1-100 inclusive
for(int a=0;a<10;a++)
{
for(int b=0;b<12;b++)
{
twoDArray[a, b] = myNumber;
}
}
}
static void DisplayTwoDimArray(int[,] twoDArray)
{
Console.WriteLine("Two-Dimensional Array");
for(int a=0;a<10;a++)
{
for(int b=0;b<12;b++)
{
Console.Write(twoDArray[a, b] + " ");
}
Console.WriteLine();
}
}
static void StoreValues(int[,] twoDArray, int[ ] oneDArray)
{
int c = 0;
for(int a=0;a<10;a++)
{
for(int b=0;b<12;b++)
{
oneDArray[c] = twoDArray[a, b];
c = c + 1;
}
}
}
static void DisplayOneDimArray(int[] oneDArray)
{
Console.WriteLine("One Dimensional Array");
for(int a=0;a<10;a++)
{
Console.WriteLine(oneDArray[a]);
}
}
}
}
You haven't declared the variable myNumber in the FillTwoDimArray method. I'm declaring with var myNumber = myRandom.Next(...);
static void FillTwoDimArray(int[,] twoDArray) //method to fill twoD array with random numbers
{
Random myRandom = new Random(); //Random class is under System namespace
var myNumber = myRandom.Next(1, 101); //generates number between 1-100 inclusive
for(int a=0;a<10;a++)
{
for(int b=0;b<12;b++)
{
twoDArray[a, b] = myNumber;
}
}
}

Cannot call my method, what have I forgotten c# Skill:NOVICE

Getting back into the swing of things after focusing on other assignments.
could someone tell me why I get an error for this.
static void Main(string[] args)
{
Console.WriteLine("% unique numbers");
Random rnd = new Random();
for (int count = 0; count <= 5; count++)
{
DiceR(); // ------ Error here -----
}
}
public void DiceR()
{
Random rnd = new Random();
Console.WriteLine(rnd.Next(1, 6));
}
}
make it static
static public void DiceR()
{
Random rnd = new Random();
Console.WriteLine(rnd.Next(1, 6));
}

Categories

Resources