(one of many) why are my bitwise operations not working?
hello. i am a complete beginner. like i literally picked this language up yesterday because it resembled python in quite a few ways and the basics were really easy to cover. but honestly, this is something that i really cannot wrap my head around.
and for my code, all i have is this:
can someone explain this to me? my first project with C# is gonna be tictactoe, then connect 4 and hopefully a discord bot of some sort.
25 Replies
it isn't preventing you from having this here, it's warning you that it might error at runtime
because you are not setting it to a value
modern c# lets you/forces you to mark explicitly if a given thing can ever be null or not
you would do it in this situation by writing int[]? instead of int[]
Basically what it says is your int array is nullable, meaning it will hold null reference which could throw an exception if you try to do any operation without initialising it. One way to fix is to declare a constructor which gives the default value of int array or you could declare it nullable like public int[]? Board
you can prove something will never be null by giving it a value in the class's constructor, as the constructor is always called when the class is created
how do you make a constructor then?
because i saw, say for a class like
Fruit
that holds a name:
you do this as a constructor
how do you access instance variables? like in python you would do
i know in C# you have the this
keyword, but i dont know how you use it in methodslike this
class TicTacToe
{
public int[] board;
public TIcTacToe(int size)
{
this.board = new int[size];
} } or if you feel like you dont know what could be size of the array then you can use list for which you dont need to initialize the size
this.board = new int[size];
} } or if you feel like you dont know what could be size of the array then you can use list for which you dont need to initialize the size
you can simply skip using the 'this' keyword 99% of the time in c#
and reference the instance variable directly
sample with list :
class TicTacToe
{
public List<int> board;
public TIcTacToe()
{
this.board = new List<int>();
} } true
this.board = new List<int>();
} } true
so
how do you slice an array then? or should i just hardcode winning lines
:thumbsup:
it can be done using index , if you know starting point and ending point of your subset
Angius
REPL Result: Success
Result: int[]
Compile: 262.495ms | Execution: 46.842ms | React with ❌ to remove this embed.
is how you slice
there are various ways to slice an array, it would be helpfull if you explain how the slice needs to happen for your project requirement
is there a
step
argument for slicing?
for tictactoe, it would be nice to be able to slice the list into:
- rows
- columns
- diagonals (can be hardcoded)
makes it easierfor this you wont slice it , for a tictactoe project you would need 2D array to make the logic much simpler, and have a seperate function which will check all possible scenarios.
is it a 3x3 game tile or it can be nxn?
3x3. i dont see how you could make an NxN board to start with
hm ok makes sense. how do i make that in C#?
or could i just port a bitboard approach from one of my old python projects
I have seen few people try that with few weird rules😁
private static char[,] board = new char[3, 3];
This is how you declare the board array
And method to check if there is winner or not can be done like this
static bool CheckWinner()
{
// Check rows, columns, and diagonals
for (int i = 0; i < 3; i++)
{
if (board[i, 0] == currentPlayer && board[i, 1] == currentPlayer && board[i, 2] == currentPlayer) return true;
if (board[0, i] == currentPlayer && board[1, i] == currentPlayer && board[2, i] == currentPlayer) return true;
}
if (board[0, 0] == currentPlayer && board[1, 1] == currentPlayer && board[2, 2] == currentPlayer) return true;
if (board[0, 2] == currentPlayer && board[1, 1] == currentPlayer && board[2, 0] == currentPlayer) return true;
return false;
}
Hope from this you can fill in rest of the logic
wait what the fuck. you can do
array[x, y]
???
or is that pseudocode
oh also another question @Gokul - is there a faster way to test drive C# code than building it and running it? like can i just hit play on smth and have it do all the console work for me?
?close
._.
@ZZZZZZZZZZZZZZZZZZZZZZZZZ i have a questioni keep getting this
Means the
TicTacToe
class is probably not public
:LinusStare:
holy fuck that was it
:painman:
fuck copilot
@ZZZZZZZZZZZZZZZZZZZZZZZZZ more issues
i gave up on the tictactoe thing since i lost the copy that used bitboards
doing conenct four :thumbsup:
but i have a question about the number system
i come from python which has no (theoretical) limit to how big a number can be, since it divides the number into multiple
long
types if it goes over the cap (preventing an overflow error)but i have my bitboards arranged like this guide here
GitHub
BitboardC4/BitboardDesign.md at master · denkspuren/BitboardC4
The Design of Bitboards for use in Connect Four. Contribute to denkspuren/BitboardC4 development by creating an account on GitHub.
And what's the issue?
code: https://mystb.in/0439f2b2378dd292ff
output when printing the board after moves
2323232
:
this is how it's going through the board: https://mystb.in/1b40b9b78558f1edaf
wait
lemme try
HOLY FUCK IT WORKED
@ZZZZZZZZZZZZZZZZZZZZZZZZZ fixed it. issue was i needed to make it a long
before shifting:rubberduck:
hellooo
back with more problems
updated code: https://mystb.in/219816c511e078d363
for some reason, playing in column
7
(furthest to the right) this happens:
indexes btw are
just to keep it updated
wait
42 - 32 = 10
fuck my life another overflow error
:perage:
fuck your static types
this language is bullshit