✅ Changing variables based on player input
I need help for a tic tac toe console game.
146 Replies
few improvements for discord, you can format your text to code using backticks, have a look at $codegif
and my next question would be, what have you tried so far?
i cant think of anything
let's say I type
1
what should happenfirstSlot will change to "X"
okay, and how do you change the value of
firstSlot
firstSlot = "X";
good
so now that the value is updated, how will you display that change in the console?
running this again
very good
now, how do you check in C# if my input was in fact
1
and not 2
?exactly
i get that you can do "if input 1, first slot = X" and so on for every input but isnt there a faster way?
don't worry about faster or better just yet, try to get to the point where your code works first
okay
hold on
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
aight
Hello! if you're coding tic-tac-toe, it's better to use an array of cells rather than individual variables
example:
then you can do something like this:
do not forget to check if your cell in range of your array
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
$paste
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!
oh boy, you really should learn about loops and methods 🙂
also,
Thread.Sleep(999999999);
...
who hurt you as a child?oh sorry, just update your post
I'll have a look
update my post?
i dont know how to stop the scripts
oh nvm I thought I was in #chat here
ok
well it works, its just not optimized
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
yeah, but is methods = fuctions?
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
yeah
you could also simplify the logic of deciding which player's turn it currently is
do you know what this is for?
with boolean variables?
yes
this is to make the randomizer for the computer placement avoid putting stuff on the other player
oh sorry I missed that
i dont know how to do lists or arrays, thats why its such a inefficient way
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.
ugh so complicated
int[][] jaggedArray = new int[3][];
what does this even mean aaaa
let's take an example from that article
what do you think this means:
it makes space for the integers? idk
ok maybe we should start with regular arrays
this is how an array is declared in C#:
ok
i have questions
yes
why is there int typed in two places?
how can a array be a int
ah, the
[]
s mean that it's an array that contains ints
so it's not an integer itselfok 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?
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 itspecify it twice?
so I could also have said:
ok
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
ok
so for example
i get that
ok, quick knowledge check then
oh no
what if I did:
What would the console print if I did:
number
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 stringyes
i know about the types
okay good
char bool string int float double decimal
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 numbershould i always type "var" array?
no, but most people do
cause it's shorter
why wouldnt i?
its shorter, faster to type
because the code would work just the same
regardless whether you use
var
or int[]
wouldnt var be a little bit slower cause the compiler does extra work?
that's a valid question but unfortunately I don't know enough about how the language works behind-the-scenes to answer that
ok
ok i get it :D
good, so what you've done there is to create an array, which holds 5 places for integers
great
what would happen if after this code i typed ?
would the array just gain a slot?
it would overwrite the 5-place array with a new array with 6 places
ok what if the other one had integers in it?
would it clear all
they would all be forgotten yes
ok
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
ok
quick question
uhu
this would be an error right?
yup
ok
so how do i modify the array?
you can do it with indexes, like this:
why the zero in the parameter?
and if you then want to get the first value of the array, you can read it like this:
the
0
means the index or the position inside the array of which you change or get the valuezero is the first array?
not quite, the zeroth position is the position of the first number in the array
i meant that
it's a convention in programming to start at 0
ok
so how would you get the last element from the
numbers
array I just made?this array?
yup
numbers[4];
very good
xd thank you
I think you're ready to move on to jagged arrays now, let's have another look at them:
what do you think that means
uhm
makes a int array of 3 possible integers with the first value being 4?
not quite, let's take a step back: what does
int[2]
do again?oh right sets the first element to 2
right?
no it doesnt
what am i saying
no, remember that I'm doing
new
this timeEnvironment.Exit()
or better yet, return from main - this will be doable when you break this up into methodskindly read the room
makes a array with 3 slots
not quite, when you initialise the array, the number you give with it is the number of places
I was replying to a direct reply. take your attitude elsewhere
what you are referring to is the index 2, which would get the third item from the list, but only when it already exists
here's an article with more information about arrays: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays
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.
what does it do?
it creates an array that can contain two numbers
oh
okay
sorry for being so bad at this
that's okay, you're still learning
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.
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
this link?
yup
k
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 whateverright
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
okay that sounds good
its also what i wanted to do but i didnt know how
the board displaying method was good?
yeah, you can keep it with the string
could i make a simple unity game after 1 month?
I don't know much about game development, but a month is probably enough to make something small yes
ok
but remember that Unity is more than just C#, you'll have to learn how to work with the program as well
yeah
everything is so hard
have you done the $helloworld tutorials by any chance?
Written interactive course https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/
Videos https://dotnet.microsoft.com/learn/videos
some of the microsoft learn
i did a 4h course
try those tutorials I linked, the first one is interactive
interactive how?
open the link and you'll understand
ok, should i close this thread now?
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
ok thanks for all the help and links! :D
good luck, have fun!
i will check out the stuff u sent @hypersensitive internet denizen i saved them
you can close it by typing "\close" and choosingthe option that pops up
\close
didnt work lll
whoops one sec