(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.
Non-nullable field 'board' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable. (CS8618)
Non-nullable field 'board' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable. (CS8618)
and for my code, all i have is this:
namespace Game
{
class TicTacToe
{
public int[] board; // < error here
}
}
namespace Game
{
class TicTacToe
{
public int[] board; // < error here
}
}
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
becquerel
becquerel5mo ago
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[]
Gokul
Gokul5mo ago
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
becquerel
becquerel5mo ago
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
it’s raining outside
how do you make a constructor then? because i saw, say for a class like Fruit that holds a name:
class Fruit
{
private string Name;

public Fruit(string fruitName)
{
this.Name = fruitName;
}
}
class Fruit
{
private string Name;

public Fruit(string fruitName)
{
this.Name = fruitName;
}
}
you do this as a constructor how do you access instance variables? like in python you would do
class Fruit:
def __init__(self, fruit_name: str) -> None:
self.name = fruit_name
class Fruit:
def __init__(self, fruit_name: str) -> None:
self.name = fruit_name
i know in C# you have the this keyword, but i dont know how you use it in methods
Gokul
Gokul5mo ago
like 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
becquerel
becquerel5mo ago
you can simply skip using the 'this' keyword 99% of the time in c# and reference the instance variable directly
Gokul
Gokul5mo ago
sample with list : class TicTacToe { public List<int> board; public TIcTacToe() {
this.board = new List<int>();
} } true
it’s raining outside
so
class TicTacToe
{
public int[] board;

public TicTacToe()
{
this.board = new int[9];
}
}
class TicTacToe
{
public int[] board;

public TicTacToe()
{
this.board = new int[9];
}
}
how do you slice an array then? or should i just hardcode winning lines :thumbsup:
Gokul
Gokul5mo ago
it can be done using index , if you know starting point and ending point of your subset
MODiX
MODiX5mo ago
Angius
REPL Result: Success
int[] arr = [ 1, 2, 3, 4, 5, 6, 7];
arr[2..4]
int[] arr = [ 1, 2, 3, 4, 5, 6, 7];
arr[2..4]
Result: int[]
[
3,
4
]
[
3,
4
]
Compile: 262.495ms | Execution: 46.842ms | React with ❌ to remove this embed.
Angius
Angius5mo ago
is how you slice
Gokul
Gokul5mo ago
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
it’s raining outside
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 easier
Gokul
Gokul5mo ago
for 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?
it’s raining outside
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
Gokul
Gokul5mo ago
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
it’s raining outside
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 question
it’s raining outside
i keep getting this
No description
Angius
Angius5mo ago
Means the TicTacToe class is probably not public
it’s raining outside
: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)
it’s raining outside
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.
Angius
Angius5mo ago
And what's the issue?
it’s raining outside
code: https://mystb.in/0439f2b2378dd292ff output when printing the board after moves 2323232:
1 2 3 4 5 6 7 # added in
v v v v v v v # added in
. . . . . 1 2
. . . . . 1 2
. . . . . . .
. 1 2 . . . .
. 1 2 . . . .
. 1 2 . . . .
1 2 3 4 5 6 7 # added in
v v v v v v v # added in
. . . . . 1 2
. . . . . 1 2
. . . . . . .
. 1 2 . . . .
. 1 2 . . . .
. 1 2 . . . .
this is how it's going through the board: https://mystb.in/1b40b9b78558f1edaf wait lemme try
long sq = 1;
sq <<= ...;
long sq = 1;
sq <<= ...;
HOLY FUCK IT WORKED @ZZZZZZZZZZZZZZZZZZZZZZZZZ fixed it. issue was i needed to make it a long before shifting
Angius
Angius5mo ago
:rubberduck:
it’s raining outside
hellooo back with more problems updated code: https://mystb.in/219816c511e078d363 for some reason, playing in column 7 (furthest to the right) this happens:
. . . . . . .
. . . . . . .
. 1 . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
Column: 7 | Heights: [0, 7, 14, 21, 28, 35, 43]
Bitboards: [1024, 0]
. . . . . . .
. . . . . . .
. 1 . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
Column: 7 | Heights: [0, 7, 14, 21, 28, 35, 43]
Bitboards: [1024, 0]
indexes btw are
6 13 20 27 34 41 48
---------------------
5 12 19 26 33 40 47
4 11 18 25 32 39 46
3 10 17 24 31 38 45
2 9 16 23 30 37 44
1 8 15 22 29 36 43
0 7 14 21 28 35 42
6 13 20 27 34 41 48
---------------------
5 12 19 26 33 40 47
4 11 18 25 32 39 46
3 10 17 24 31 38 45
2 9 16 23 30 37 44
1 8 15 22 29 36 43
0 7 14 21 28 35 42
just to keep it updated wait 42 - 32 = 10 fuck my life another overflow error :perage: fuck your static types this language is bullshit
Want results from more Discord servers?
Add your server