C
C#2y ago
rapid

✅ Changing variables based on player input

I need help for a tic tac toe console game.
146 Replies
rapid
rapidOP2y ago
Random placement = new Random();
string firstSlot = "1";
string secondSlot = "2";
string thirdSlot = "3";
string fourthSlot = "4";
string fifthSlot = "5";
string sixthSlot = "6";
string seventhSlot = "7";
string eighthSlot = "8";
string ninthSlot = "9";
string turn = "Your turn, choose a slot!";

Console.WriteLine($"TIC TAC TOE!\t({turn})\n" +
$"\n{firstSlot}\t{secondSlot}\t{thirdSlot}" +
$"\n{fourthSlot}\t{fifthSlot}\t{sixthSlot}" +
$"\n{seventhSlot}\t{eighthSlot}\t{eighthSlot}\n");

string input = Console.ReadLine();

// What I need to do:
// If player inputs 1, firstSlot will change to X.
// If player inputs 2, secondSlot will change to X.
// And so on...

turn = "Computer's turn, please wait!";
Thread.Sleep( 2000 );
Random placement = new Random();
string firstSlot = "1";
string secondSlot = "2";
string thirdSlot = "3";
string fourthSlot = "4";
string fifthSlot = "5";
string sixthSlot = "6";
string seventhSlot = "7";
string eighthSlot = "8";
string ninthSlot = "9";
string turn = "Your turn, choose a slot!";

Console.WriteLine($"TIC TAC TOE!\t({turn})\n" +
$"\n{firstSlot}\t{secondSlot}\t{thirdSlot}" +
$"\n{fourthSlot}\t{fifthSlot}\t{sixthSlot}" +
$"\n{seventhSlot}\t{eighthSlot}\t{eighthSlot}\n");

string input = Console.ReadLine();

// What I need to do:
// If player inputs 1, firstSlot will change to X.
// If player inputs 2, secondSlot will change to X.
// And so on...

turn = "Computer's turn, please wait!";
Thread.Sleep( 2000 );
JansthcirlU
JansthcirlU2y ago
few improvements for discord, you can format your text to code using backticks, have a look at $codegif
JansthcirlU
JansthcirlU2y ago
and my next question would be, what have you tried so far?
rapid
rapidOP2y ago
i cant think of anything
JansthcirlU
JansthcirlU2y ago
let's say I type 1 what should happen
rapid
rapidOP2y ago
firstSlot will change to "X"
JansthcirlU
JansthcirlU2y ago
okay, and how do you change the value of firstSlot
rapid
rapidOP2y ago
firstSlot = "X";
JansthcirlU
JansthcirlU2y ago
good so now that the value is updated, how will you display that change in the console?
rapid
rapidOP2y ago
running this again
Console.WriteLine($"TIC TAC TOE!\t({turn})\n" +
$"\n{firstSlot}\t{secondSlot}\t{thirdSlot}" +
$"\n{fourthSlot}\t{fifthSlot}\t{sixthSlot}" +
$"\n{seventhSlot}\t{eighthSlot}\t{eighthSlot}\n");
Console.WriteLine($"TIC TAC TOE!\t({turn})\n" +
$"\n{firstSlot}\t{secondSlot}\t{thirdSlot}" +
$"\n{fourthSlot}\t{fifthSlot}\t{sixthSlot}" +
$"\n{seventhSlot}\t{eighthSlot}\t{eighthSlot}\n");
JansthcirlU
JansthcirlU2y ago
very good now, how do you check in C# if my input was in fact 1 and not 2?
rapid
rapidOP2y ago
if (input == "1")
if (input == "1")
JansthcirlU
JansthcirlU2y ago
exactly
rapid
rapidOP2y ago
i get that you can do "if input 1, first slot = X" and so on for every input but isnt there a faster way?
JansthcirlU
JansthcirlU2y ago
don't worry about faster or better just yet, try to get to the point where your code works first
rapid
rapidOP2y ago
okay hold on
JansthcirlU
JansthcirlU2y ago
no worries, take your time brb, just ping me when you got something, I may not respond instantly but there are plenty of other people who can help you out so don't worry
rapid
rapidOP2y ago
aight
pppoe252110
pppoe2521102y ago
Hello! if you're coding tic-tac-toe, it's better to use an array of cells rather than individual variables example:
string[] cells = new string[]{"1","2","3","4","5","6","7","8","9"};
string[] cells = new string[]{"1","2","3","4","5","6","7","8","9"};
then you can do something like this:
if(int.TryParse(Console.ReadLine(), out int cell)
{

Console.WriteLine($"Wow! your cell is {cell} and value in your cell is{cells[cell]}");
}
if(int.TryParse(Console.ReadLine(), out int cell)
{

Console.WriteLine($"Wow! your cell is {cell} and value in your cell is{cells[cell]}");
}
do not forget to check if your cell in range of your array
rapid
rapidOP2y ago
good to know! i will learn arrays in the future i wish i saw this earlier tho because i made tic tac toe, kind of it took 1000 lines for some reason and it took 3 hours @hypersensitive internet denizen i got it how do i send it its too long
Pobiega
Pobiega2y ago
$paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Pobiega
Pobiega2y ago
oh boy, you really should learn about loops and methods 🙂 also, Thread.Sleep(999999999);... who hurt you as a child?
JansthcirlU
JansthcirlU2y ago
oh sorry, just update your post I'll have a look
rapid
rapidOP2y ago
update my post? i dont know how to stop the scripts
JansthcirlU
JansthcirlU2y ago
oh nvm I thought I was in #chat here
rapid
rapidOP2y ago
ok well it works, its just not optimized
JansthcirlU
JansthcirlU2y ago
true, but at least it's something you can start improving now first of all, you repeat a lot of things, which is fine because there are only so many actions you can take, but imagine if you had a 100x100 tic tac toe board, then you would be doing a whole lot more work so you'd want to simplify that a bit
rapid
rapidOP2y ago
yeah, but is methods = fuctions?
JansthcirlU
JansthcirlU2y ago
I wouldn't worry about the difference just yet, they're conceptually a bit different but you're thinking in the right direction to put repeated things inside a function
rapid
rapidOP2y ago
yeah
JansthcirlU
JansthcirlU2y ago
you could also simplify the logic of deciding which player's turn it currently is
rapid
rapidOP2y ago
do you know what this is for?
rapid
rapidOP2y ago
with boolean variables?
JansthcirlU
JansthcirlU2y ago
yes
rapid
rapidOP2y ago
this is to make the randomizer for the computer placement avoid putting stuff on the other player
rapid
rapidOP2y ago
JansthcirlU
JansthcirlU2y ago
oh sorry I missed that
rapid
rapidOP2y ago
i dont know how to do lists or arrays, thats why its such a inefficient way
JansthcirlU
JansthcirlU2y ago
using a list could work, but there's actually a better data structure for this purpose: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays
Jagged Arrays - C# Programming Guide
A jagged array in C# is an array whose elements are arrays of different sizes. Learn how to declare, initialize, and access jagged arrays.
rapid
rapidOP2y ago
ugh so complicated int[][] jaggedArray = new int[3][]; what does this even mean aaaa
JansthcirlU
JansthcirlU2y ago
let's take an example from that article what do you think this means:
int[][] jaggedArray3 =
{
new int[] { 1, 3, 5, 7, 9 },
new int[] { 0, 2, 4, 6 },
new int[] { 11, 22 }
};
int[][] jaggedArray3 =
{
new int[] { 1, 3, 5, 7, 9 },
new int[] { 0, 2, 4, 6 },
new int[] { 11, 22 }
};
rapid
rapidOP2y ago
it makes space for the integers? idk
JansthcirlU
JansthcirlU2y ago
ok maybe we should start with regular arrays this is how an array is declared in C#:
int[] array = new int[5]; // Creates empty array with five spots to fill in with numbers
int[] array = new int[5]; // Creates empty array with five spots to fill in with numbers
rapid
rapidOP2y ago
ok i have questions
JansthcirlU
JansthcirlU2y ago
yes
rapid
rapidOP2y ago
why is there int typed in two places? how can a array be a int
JansthcirlU
JansthcirlU2y ago
ah, the []s mean that it's an array that contains ints so it's not an integer itself
rapid
rapidOP2y ago
ok so first it says int[] array. i get that its making an array of integers but then after "new", it says integers again. why is it saying int again even though its already an array for ints?
JansthcirlU
JansthcirlU2y ago
ah, that has to do with types in C#, I assume you've only worked with things like int and string but they are special cases where you can directly assign a value to them but for arrays you have to specify the type of array it is in order to use it
rapid
rapidOP2y ago
specify it twice?
JansthcirlU
JansthcirlU2y ago
so I could also have said:
var array = new int[5]; // same as above
var array = new int[5]; // same as above
rapid
rapidOP2y ago
ok
JansthcirlU
JansthcirlU2y ago
the thing on the left is a variable and the thing on the right is the value you give to that variable, and since C# uses types, the thing on the left and the thing on the right both have to have the same type
rapid
rapidOP2y ago
ok
JansthcirlU
JansthcirlU2y ago
so for example
int number = "number"; // does not work
int number = "number"; // does not work
rapid
rapidOP2y ago
i get that
JansthcirlU
JansthcirlU2y ago
ok, quick knowledge check then
rapid
rapidOP2y ago
oh no
JansthcirlU
JansthcirlU2y ago
what if I did:
var number = "number";
var number = "number";
What would the console print if I did:
Console.WriteLine(number);
Console.WriteLine(number);
rapid
rapidOP2y ago
number
JansthcirlU
JansthcirlU2y ago
correct so var is a special keyword that looks at the value (in this case the value is "number") and it figures out that it's a string
rapid
rapidOP2y ago
yes i know about the types
JansthcirlU
JansthcirlU2y ago
okay good
rapid
rapidOP2y ago
char bool string int float double decimal
JansthcirlU
JansthcirlU2y ago
so for the array it's the same concept, you can say var array but to declare it you must specify the type of array and that could be char[] or bool[] or int[] or whatever really and then you also have to specify the initial length of the array, which is why you have to give it a number
rapid
rapidOP2y ago
should i always type "var" array?
JansthcirlU
JansthcirlU2y ago
no, but most people do cause it's shorter
rapid
rapidOP2y ago
why wouldnt i? its shorter, faster to type
JansthcirlU
JansthcirlU2y ago
because the code would work just the same regardless whether you use var or int[]
rapid
rapidOP2y ago
wouldnt var be a little bit slower cause the compiler does extra work?
JansthcirlU
JansthcirlU2y ago
that's a valid question but unfortunately I don't know enough about how the language works behind-the-scenes to answer that
rapid
rapidOP2y ago
ok
int[] array = new int[5];
int[] array = new int[5];
ok i get it :D
JansthcirlU
JansthcirlU2y ago
good, so what you've done there is to create an array, which holds 5 places for integers
rapid
rapidOP2y ago
great what would happen if after this code i typed ?
array = new int[6];
array = new int[6];
would the array just gain a slot?
JansthcirlU
JansthcirlU2y ago
it would overwrite the 5-place array with a new array with 6 places
rapid
rapidOP2y ago
ok what if the other one had integers in it? would it clear all
JansthcirlU
JansthcirlU2y ago
they would all be forgotten yes
rapid
rapidOP2y ago
ok
JansthcirlU
JansthcirlU2y ago
what's important to know is that you can't change the length of an array, once you've given it a length, it stays that length however, you can change the values inside the array
rapid
rapidOP2y ago
ok quick question
JansthcirlU
JansthcirlU2y ago
uhu
rapid
rapidOP2y ago
this would be an error right?
var array = new int[6];
array = new string[6];
var array = new int[6];
array = new string[6];
JansthcirlU
JansthcirlU2y ago
yup
rapid
rapidOP2y ago
ok so how do i modify the array?
JansthcirlU
JansthcirlU2y ago
you can do it with indexes, like this:
var numbers = new int[5];
numbers[0] = 5; // Set the first element to 5, indexes are zero based
var numbers = new int[5];
numbers[0] = 5; // Set the first element to 5, indexes are zero based
rapid
rapidOP2y ago
why the zero in the parameter?
JansthcirlU
JansthcirlU2y ago
and if you then want to get the first value of the array, you can read it like this:
var firstNumber = numbers[0]; // Gives back the value 5
var firstNumber = numbers[0]; // Gives back the value 5
the 0 means the index or the position inside the array of which you change or get the value
rapid
rapidOP2y ago
zero is the first array?
JansthcirlU
JansthcirlU2y ago
not quite, the zeroth position is the position of the first number in the array
rapid
rapidOP2y ago
i meant that
JansthcirlU
JansthcirlU2y ago
it's a convention in programming to start at 0
rapid
rapidOP2y ago
ok
JansthcirlU
JansthcirlU2y ago
so how would you get the last element from the numbers array I just made?
rapid
rapidOP2y ago
this array?
JansthcirlU
JansthcirlU2y ago
yup
rapid
rapidOP2y ago
numbers[4];
JansthcirlU
JansthcirlU2y ago
very good
rapid
rapidOP2y ago
xd thank you
JansthcirlU
JansthcirlU2y ago
I think you're ready to move on to jagged arrays now, let's have another look at them:
var twoByFourNumbers = new int[2][4];
var twoByFourNumbers = new int[2][4];
what do you think that means
rapid
rapidOP2y ago
uhm makes a int array of 3 possible integers with the first value being 4?
JansthcirlU
JansthcirlU2y ago
not quite, let's take a step back: what does int[2] do again?
rapid
rapidOP2y ago
oh right sets the first element to 2 right? no it doesnt what am i saying
JansthcirlU
JansthcirlU2y ago
no, remember that I'm doing new this time
Pobiega
Pobiega2y ago
Environment.Exit() or better yet, return from main - this will be doable when you break this up into methods
JansthcirlU
JansthcirlU2y ago
kindly read the room
rapid
rapidOP2y ago
makes a array with 3 slots
JansthcirlU
JansthcirlU2y ago
not quite, when you initialise the array, the number you give with it is the number of places
Pobiega
Pobiega2y ago
I was replying to a direct reply. take your attitude elsewhere
JansthcirlU
JansthcirlU2y ago
what you are referring to is the index 2, which would get the third item from the list, but only when it already exists
JansthcirlU
JansthcirlU2y ago
Single-Dimensional Arrays - C# Programming Guide
Create a single-dimensional array in C# using the new operator specifying the array element type and the number of elements.
rapid
rapidOP2y ago
what does it do?
JansthcirlU
JansthcirlU2y ago
it creates an array that can contain two numbers
rapid
rapidOP2y ago
oh okay sorry for being so bad at this
JansthcirlU
JansthcirlU2y ago
that's okay, you're still learning
ZacharyPatten
ZacharyPatten2y ago
I'm just going to throw this out there... arrays don't really help you much for a game like tic-tac-toe. It is probably just as easy to make it without arrays as it is to make it with arrays. If you just want to code tic-tac-toe then you can do without them, but arrays are an essential data type to understand, so you will have to learn them at some point.
JansthcirlU
JansthcirlU2y ago
true @rapid, ZP is right, maybe don't worry about jagged arrays yet, but look into that article on the Microsoft docs about arrays so you can learn a bit about them let's instead focus on a different aspect, deciding whose turn it is
rapid
rapidOP2y ago
this link?
JansthcirlU
JansthcirlU2y ago
yup
rapid
rapidOP2y ago
k
JansthcirlU
JansthcirlU2y ago
there's still a few things we can improve, but I don't want to overwhelm you with information so I'll try to summarize it: * create a function to assign "X" or "O" to the board * create a big while loop in which everything happens (kind of like a game loop) * figure out a way to decide whose turn it is (player 1 or player 2) the first is something you've brought up yourself, which is always a good instinct to have in programming the second is a bit more abstract, but a game loop is like when you play a video game and the screen refreshes to update your actions i.e. shooting bullets or moving around or whatever
rapid
rapidOP2y ago
right
JansthcirlU
JansthcirlU2y ago
you can put the tic-tac-toe logic inside a loop to repeat all the necessary actions (1) decide whose turn it is (2) see what spot the player chose (3) put the right symbol at that spot (4) display the new board (5) see if either player has made a winning move don't worry about knowing exactly how to do all of those things just yet, but keep in mind that that will probably be the general structure of your code it's gonna take time to learn the concepts and to understand them so don't feel bad that you don't understand all the things we've talked about today
rapid
rapidOP2y ago
okay that sounds good its also what i wanted to do but i didnt know how the board displaying method was good?
JansthcirlU
JansthcirlU2y ago
yeah, you can keep it with the string
rapid
rapidOP2y ago
could i make a simple unity game after 1 month?
JansthcirlU
JansthcirlU2y ago
I don't know much about game development, but a month is probably enough to make something small yes
rapid
rapidOP2y ago
ok
JansthcirlU
JansthcirlU2y ago
but remember that Unity is more than just C#, you'll have to learn how to work with the program as well
rapid
rapidOP2y ago
yeah everything is so hard
JansthcirlU
JansthcirlU2y ago
have you done the $helloworld tutorials by any chance?
rapid
rapidOP2y ago
some of the microsoft learn i did a 4h course
JansthcirlU
JansthcirlU2y ago
try those tutorials I linked, the first one is interactive
rapid
rapidOP2y ago
interactive how?
JansthcirlU
JansthcirlU2y ago
open the link and you'll understand
rapid
rapidOP2y ago
ok, should i close this thread now?
ZacharyPatten
ZacharyPatten2y ago
if you no longer need help with a topic then yeah closing it is helpful so other users won't try to read everything and catch up in attempts to help you can always open a new post to ask more questions posts will automatically go stale after a period of time too
rapid
rapidOP2y ago
ok thanks for all the help and links! :D
JansthcirlU
JansthcirlU2y ago
good luck, have fun!
rapid
rapidOP2y ago
i will check out the stuff u sent @hypersensitive internet denizen i saved them
ZacharyPatten
ZacharyPatten2y ago
you can close it by typing "\close" and choosingthe option that pops up
rapid
rapidOP2y ago
\close didnt work lll
ZacharyPatten
ZacharyPatten2y ago
whoops one sec
Want results from more Discord servers?
Add your server