Othello Reversi game

I'm supposed to create an entire othello also called Reversi game from scratch in C# WPF application. I*m gonna be completely and utterly honest, i have no idea what to do and this assignment is way to hard for me. It's about 6+ months overdue and it's supposed to be done in groups of 3+ but the ones in my group just hopped of the course. I'm unironically desperate for some help and would really appreciate it if someone had some time during the day to help me wrap this up. I've done most of the stuff already but i need help with the main logic. I'm desperate.
377 Replies
TheRanger
TheRangerβ€’3d ago
what did you do so far?
Merineth πŸ‡ΈπŸ‡ͺ
I'll share it, one moment
Merineth πŸ‡ΈπŸ‡ͺ
GitHub
GitHub - aljomatrix/Assignment2Repository
Contribute to aljomatrix/Assignment2Repository development by creating an account on GitHub.
Merineth πŸ‡ΈπŸ‡ͺ
this is the repo, i think you should have access to it
Merineth πŸ‡ΈπŸ‡ͺ
I've created the essential windows, their corresponding popups
No description
No description
Merineth πŸ‡ΈπŸ‡ͺ
However all that remains is the logic part I still don't know how to set the initial board pieces for example
TheRanger
TheRangerβ€’3d ago
do u have the images of those pieces?
Merineth πŸ‡ΈπŸ‡ͺ
No. but i assume i can use some png photo online?
TheRanger
TheRangerβ€’3d ago
or just draw it on MS Paint or you could draw ellipses
Merineth πŸ‡ΈπŸ‡ͺ
Yeah i read about the ellipses but i don't know how to make them so i think png is easier for me?
TheRanger
TheRangerβ€’3d ago
there are tutorials on how to make ellipses in wpf
Merineth πŸ‡ΈπŸ‡ͺ
Could i use something like this perhaps?
TheRanger
TheRangerβ€’3d ago
sure
Merineth πŸ‡ΈπŸ‡ͺ
ok i have added them into my project
TheRanger
TheRangerβ€’3d ago
create Images Controls for your pieces and place them on your board or do u start with an empty board? im not familiar with the game
Merineth πŸ‡ΈπŸ‡ͺ
When i click new game, the prompts for the player names comes in. You inpuit those and then the game start when you hit ok That's when the initial pieces should be placed So in SetupGameDialog.xaml.cs
TheRanger
TheRangerβ€’3d ago
yeah
Merineth πŸ‡ΈπŸ‡ͺ
private void OKButton_Click(object sender, RoutedEventArgs e)
{
// Capture the player names and computer flag
Player1Name = Player1NameTextBox.Text;

// Check if Player 2 is a computer
IsPlayer2Computer = (bool)IsPlayer2ComputerCheckBox.IsChecked;

// If Player 2 is a computer, automatically set the name to "Computer"
if (IsPlayer2Computer)
{
Player2Name = "Computer";
}
else
{
// Otherwise, get Player 2's name from the TextBox
Player2Name = Player2NameTextBox.Text;
}

// Validation: Ensure both player names are provided (except Player 2 if it's a computer)
if (string.IsNullOrEmpty(Player1Name) || (string.IsNullOrEmpty(Player2Name) && !IsPlayer2Computer))
{
MessageBox.Show("Please enter names for both players.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

// Set the DialogResult to true and close the dialog if everything is valid
this.DialogResult = true;
this.Close();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
// Capture the player names and computer flag
Player1Name = Player1NameTextBox.Text;

// Check if Player 2 is a computer
IsPlayer2Computer = (bool)IsPlayer2ComputerCheckBox.IsChecked;

// If Player 2 is a computer, automatically set the name to "Computer"
if (IsPlayer2Computer)
{
Player2Name = "Computer";
}
else
{
// Otherwise, get Player 2's name from the TextBox
Player2Name = Player2NameTextBox.Text;
}

// Validation: Ensure both player names are provided (except Player 2 if it's a computer)
if (string.IsNullOrEmpty(Player1Name) || (string.IsNullOrEmpty(Player2Name) && !IsPlayer2Computer))
{
MessageBox.Show("Please enter names for both players.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

// Set the DialogResult to true and close the dialog if everything is valid
this.DialogResult = true;
this.Close();
}
Meaning this part It takes the two names from the players if it's an AI it's default set to "Computer" Maybe it's easier to set the board state before this? On the GameWindow.xaml.cs perhaps?
TheRanger
TheRangerβ€’3d ago
is the board form already open in the background when its asking for your name?
Merineth πŸ‡ΈπŸ‡ͺ
Yes i think so GameWindow.xml is the first window that starts when the program starts
TheRanger
TheRangerβ€’3d ago
i assume the form on the left is the game window?
Merineth πŸ‡ΈπŸ‡ͺ
Yes
TheRanger
TheRangerβ€’3d ago
i think its fine if it asks for your names after u create the board
Merineth πŸ‡ΈπŸ‡ͺ
I think so too
Merineth πŸ‡ΈπŸ‡ͺ
The gameboard has a grid which i made
No description
Merineth πŸ‡ΈπŸ‡ͺ
So the 4 pieces should initially go to D4, E4, D5, E5
TheRanger
TheRangerβ€’3d ago
yeah
Merineth πŸ‡ΈπŸ‡ͺ
public partial class GameWindow : Window
{
private GameManager gameManager;
public GameWindow()
{
InitializeComponent();
InitializeStartingPieces();
}
private void InitializeStartingPieces()
{
PlacePiece(3, 3, "white_piece.png");
PlacePiece(4, 4, "white_piece.png");
PlacePiece(3, 4, "black_piece.png");
PlacePiece(4, 3, "black_piece.png");
}

private void PlacePiece(int row, int col, string pieceImage)
{

}
public partial class GameWindow : Window
{
private GameManager gameManager;
public GameWindow()
{
InitializeComponent();
InitializeStartingPieces();
}
private void InitializeStartingPieces()
{
PlacePiece(3, 3, "white_piece.png");
PlacePiece(4, 4, "white_piece.png");
PlacePiece(3, 4, "black_piece.png");
PlacePiece(4, 3, "black_piece.png");
}

private void PlacePiece(int row, int col, string pieceImage)
{

}
Maybe something like this? I assume i have to call the method in GameWindow?
TheRanger
TheRangerβ€’3d ago
sure
Merineth πŸ‡ΈπŸ‡ͺ
Having a hard time placing them tho
TheRanger
TheRangerβ€’3d ago
how so? i think i see what u mean
Merineth πŸ‡ΈπŸ‡ͺ
I'm not familiar with how images work
TheRanger
TheRangerβ€’3d ago
u might want to create a method called CellToBoardPosition what is the size of your image's board?
Merineth πŸ‡ΈπŸ‡ͺ
You mean the entirety of the green board?
TheRanger
TheRangerβ€’3d ago
it seems to be 400,400 correct?
Merineth πŸ‡ΈπŸ‡ͺ
Yes
TheRanger
TheRangerβ€’3d ago
if you divide 400 by 8, since there are 8 rows and 8 columns
Merineth πŸ‡ΈπŸ‡ͺ
400x400 based of the GameGrid.xaml
TheRanger
TheRangerβ€’3d ago
a cell's size is 50x50
Merineth πŸ‡ΈπŸ‡ͺ
yeah
TheRanger
TheRangerβ€’3d ago
u could start by creating that method in your board class or CellToPixelPosition
Merineth πŸ‡ΈπŸ‡ͺ
ok
private void CellToPixelPosition()
{

}
private void CellToPixelPosition()
{

}
TheRanger
TheRangerβ€’3d ago
u can guess what it indicates it obviously need parameters
Merineth πŸ‡ΈπŸ‡ͺ
Convert the picture to 50x50?
TheRanger
TheRangerβ€’3d ago
ur board is 8x8 cells, right?
Merineth πŸ‡ΈπŸ‡ͺ
yes
TheRanger
TheRangerβ€’3d ago
its purpose is to give which position in the image this cell is
Merineth πŸ‡ΈπŸ‡ͺ
I'm not sure i understand
TheRanger
TheRangerβ€’3d ago
lets grab B2 B2 is 1,1 in cell dimensions, right?
Merineth πŸ‡ΈπŸ‡ͺ
Yes
TheRanger
TheRangerβ€’3d ago
ok in image dimension, where is it?
Merineth πŸ‡ΈπŸ‡ͺ
100x100?
TheRanger
TheRangerβ€’3d ago
thats the bottom right of it
Merineth πŸ‡ΈπŸ‡ͺ
75x75
TheRanger
TheRangerβ€’3d ago
thats its middle im not sure where the pivot of image control is in wpf but lets assume its the center, then yes it would be 75x75
Merineth πŸ‡ΈπŸ‡ͺ
Can't we place the images into 3.3 ? 3.3, 3.4, 4.3 and 4.4 Those are the grid positions for the 4 middle starting positions
TheRanger
TheRangerβ€’3d ago
ofc u can so ur parameters methods should be like this
public (int x,int y) CellToPixelPosition(int row, int col)
{

}
public (int x,int y) CellToPixelPosition(int row, int col)
{

}
u just need to do some math to convert for example 1,1 to 75,75
Merineth πŸ‡ΈπŸ‡ͺ
Oh ok
private void CellToPixelPosition(int row, int col)
{
int cellWidth = 50;
int cellHeight = 50;

int x = col * cellWidth;
int y = row * cellHeight;
}
private void CellToPixelPosition(int row, int col)
{
int cellWidth = 50;
int cellHeight = 50;

int x = col * cellWidth;
int y = row * cellHeight;
}
TheRanger
TheRangerβ€’3d ago
ah the pivot is top left by default mate ur cell is 50x50 not 75x75 if the pivot of the image is in the center u would add 25 after u multiply yeah but i guess we dont need that i assume someone created those classes and methods in ur projects and not you?
Merineth πŸ‡ΈπŸ‡ͺ
Yes My group partner who left most likely did GPT it
TheRanger
TheRangerβ€’3d ago
using GPT wont help you become a better programmer
Merineth πŸ‡ΈπŸ‡ͺ
I'm aware. But if i donn't finish this ASAP i'll be dropped from Uni and my life is over. So my priority is just finishing this game
TheRanger
TheRangerβ€’3d ago
tried to take C# courses online?
Merineth πŸ‡ΈπŸ‡ͺ
Yes but i don't have time
TheRanger
TheRangerβ€’3d ago
didnt they give u 6 months to make this game?
Merineth πŸ‡ΈπŸ‡ͺ
No It's 6 months overdue I'm already sitting 16 hours a day with other subjects
TheRanger
TheRangerβ€’3d ago
ah so the deadline was 6 months ago?
Merineth πŸ‡ΈπŸ‡ͺ
Yeah I've already passed the exam, but they wont mark the course as complete until this assignment is done And this assignments difficulty in proportion to the exam are vastly different
TheRanger
TheRangerβ€’3d ago
odd, must be a super hard uni that wants u to study 16 hours a day every day
Merineth πŸ‡ΈπŸ‡ͺ
And It's supposed to be done in groups of 3+ I mainly just need this finished so i can continue my studies
TheRanger
TheRangerβ€’3d ago
before the deadline were you working on it?
Merineth πŸ‡ΈπŸ‡ͺ
Yes We divided up the work between us I was meant to handle the windows but they abandoned it and i was left to do everything And i'm not good enough to do it all myself
TheRanger
TheRangerβ€’3d ago
so what are u going to do with this x and y?
Merineth πŸ‡ΈπŸ‡ͺ
return them i'd assume
TheRanger
TheRangerβ€’3d ago
correct
Merineth πŸ‡ΈπŸ‡ͺ
private (int x, int y) CellToPixelPosition(int row, int col)
{
int cellWidth = 50;
int cellHeight = 50;

int x = col * cellWidth;
int y = row * cellHeight;

return (x, y);
}
private (int x, int y) CellToPixelPosition(int row, int col)
{
int cellWidth = 50;
int cellHeight = 50;

int x = col * cellWidth;
int y = row * cellHeight;

return (x, y);
}
I think that's correct
TheRanger
TheRangerβ€’3d ago
yeah now use it in your PlacePiece method it might not be placed correctly but it will give u an idea on how it works
Merineth πŸ‡ΈπŸ‡ͺ
ok
private void PlacePiece(int row, int col, string pieceImage)
{

var (x, y) = CellToPixelPosition(row, col);

Image piece = new Image();
private void PlacePiece(int row, int col, string pieceImage)
{

var (x, y) = CellToPixelPosition(row, col);

Image piece = new Image();
we start like this?
TheRanger
TheRangerβ€’3d ago
yeah now do u know how to add the Image to the form and set its position?
Merineth πŸ‡ΈπŸ‡ͺ
To the form? As in the grid?
TheRanger
TheRangerβ€’3d ago
as in the form
Merineth πŸ‡ΈπŸ‡ͺ
No I don't know what a form is
TheRanger
TheRangerβ€’3d ago
the window
Merineth πŸ‡ΈπŸ‡ͺ
No i don't know how to do that it has something to do with my URL path?
TheRanger
TheRangerβ€’3d ago
Microsoft has documentations that has a documentation on each of Image's property and methods
Merineth πŸ‡ΈπŸ‡ͺ
What?
TheRanger
TheRangerβ€’3d ago
this might be tough to read, using a bit of GPT wont hurt in this case it has a property called Source you could use it to point to your image path
Merineth πŸ‡ΈπŸ‡ͺ
this one
TheRanger
TheRangerβ€’3d ago
yeah
Merineth πŸ‡ΈπŸ‡ͺ
imageSource.UriSource = new Uri("pack://application:,,,/Assignment2;component/Images/black_piece.png"); i tried this but it couldn't find the black_piece.png image
TheRanger
TheRangerβ€’3d ago
isnt it in your project folder? new Uri("black_piece.png"); should be enough
Merineth πŸ‡ΈπŸ‡ͺ
private void PlacePiece(int row, int col, string pieceImage)
{

var (x, y) = CellToPixelPosition(row, col);

Image piece = new Image();
piece.Source = new Uri("black_piece.png");


}
private void PlacePiece(int row, int col, string pieceImage)
{

var (x, y) = CellToPixelPosition(row, col);

Image piece = new Image();
piece.Source = new Uri("black_piece.png");


}
TheRanger
TheRangerβ€’3d ago
did it find it?
TheRanger
TheRangerβ€’3d ago
the document says its UriSource not Source
Merineth πŸ‡ΈπŸ‡ͺ
private void PlacePiece(int row, int col, string pieceImage) { var (x, y) = CellToPixelPosition(row, col); Image piece = new Image(); piece.UriSource = new Uri("black_piece.png"); } is that what you mean?
TheRanger
TheRangerβ€’3d ago
sure, did it work?
Merineth πŸ‡ΈπŸ‡ͺ
No
No description
TheRanger
TheRangerβ€’3d ago
ah wops it was the bitmap image, did u try reading the link u posted? it has an example
Merineth πŸ‡ΈπŸ‡ͺ
yes but it doesn't make much sense
TheRanger
TheRangerβ€’3d ago
what do u mean?
Merineth πŸ‡ΈπŸ‡ͺ
I don't understand it
TheRanger
TheRangerβ€’3d ago
ur basically creating a new bitmap image, pointing it to its path, then attach it to the Image control
Merineth πŸ‡ΈπŸ‡ͺ
I don't know what a bitmap is and i don't know what image control is
TheRanger
TheRangerβ€’3d ago
this one might be easier
Image imageControl = new Image
{
Source = new BitmapImage(new Uri("black_piece.png", UriKind.RelativeOrAbsolute)),
Width = 50,
Height = 50
};
Image imageControl = new Image
{
Source = new BitmapImage(new Uri("black_piece.png", UriKind.RelativeOrAbsolute)),
Width = 50,
Height = 50
};
a bitmap is an image like png, jpg the image control is Image piece = new Image(); the image that you will put into your board
Merineth πŸ‡ΈπŸ‡ͺ
uh
private void PlacePiece(int row, int col, string pieceImage)
{

var (x, y) = CellToPixelPosition(row, col);

Image piece = new Image();
Source = new BitmapImage(new Uri("black_piece.png", UriKind.RelativeOrAbsolute));


}
private void PlacePiece(int row, int col, string pieceImage)
{

var (x, y) = CellToPixelPosition(row, col);

Image piece = new Image();
Source = new BitmapImage(new Uri("black_piece.png", UriKind.RelativeOrAbsolute));


}
?
TheRanger
TheRangerβ€’3d ago
^
Merineth πŸ‡ΈπŸ‡ͺ
oh
private void PlacePiece(int row, int col, string pieceImage)
{

Image imageControl = new Image
{
Source = new BitmapImage(new Uri("black_piece.png", UriKind.RelativeOrAbsolute)),
Width = 50,
Height = 50
};
}
private void PlacePiece(int row, int col, string pieceImage)
{

Image imageControl = new Image
{
Source = new BitmapImage(new Uri("black_piece.png", UriKind.RelativeOrAbsolute)),
Width = 50,
Height = 50
};
}
TheRanger
TheRangerβ€’3d ago
using Source alone wont work, since you're not telling ur code which Image to apply this source to
Merineth πŸ‡ΈπŸ‡ͺ
It didn't crash but the pieces didn't show up
TheRanger
TheRangerβ€’3d ago
well yes, you need to add it to your board first BoardGrid.Children.Add(imageControl); might work since BoardGrid is the property of your board according to your code
Merineth πŸ‡ΈπŸ‡ͺ
I tried that earlier but Children doesn't exist
TheRanger
TheRangerβ€’3d ago
read what i said again
Merineth πŸ‡ΈπŸ‡ͺ
oh I don't have BoardGrid i think?
TheRanger
TheRangerβ€’3d ago
actually, your friend seems to have already initialized those :bigthonk: which file is this?
Merineth πŸ‡ΈπŸ‡ͺ
GameWindows.xaml.cs
TheRanger
TheRangerβ€’3d ago
well its defined in the GameGrid.xaml actually i think you should work with that file
Merineth πŸ‡ΈπŸ‡ͺ
ok
TheRanger
TheRangerβ€’3d ago
from what i see, ur friend already draw the elipses
Merineth πŸ‡ΈπŸ‡ͺ
i did it 99% of everything in the project is me over half a year ago
TheRanger
TheRangerβ€’3d ago
i assume u used chatgpt?
Merineth πŸ‡ΈπŸ‡ͺ
yes most likely
TheRanger
TheRangerβ€’3d ago
so technically, u did the elipse, and u dont know how it works?
Merineth πŸ‡ΈπŸ‡ͺ
I remeber dabbling with it because i couldn't get the images to work since that wasn't covered in the course I see i made all of them transparent
TheRanger
TheRangerβ€’3d ago
i see, what if you made them all black, would it work? in InitializeBoard() in GameGrid.xaml.cs
Merineth πŸ‡ΈπŸ‡ͺ
will try yes
Merineth πŸ‡ΈπŸ‡ͺ
changing the transparant to black it correctly fills in all the pieces on the board
TheRanger
TheRangerβ€’3d ago
then things got easier from here i wish i checked earlier, lol
Merineth πŸ‡ΈπŸ‡ͺ
so i guess all i have to do is make them transparant again and fill in the 4 correct ones in the middle?
TheRanger
TheRangerβ€’3d ago
make it transparent again
Merineth πŸ‡ΈπŸ‡ͺ
Yeah i'm terribly sorry Been working so long on this i completely forgot about it
TheRanger
TheRangerβ€’3d ago
i think ur project is pretty much finished, just needs small things to get added you should initialize your 4 pieces in your InitializeBoard method or wait
Merineth πŸ‡ΈπŸ‡ͺ
private void InitializeBoard()
{
// Clear any existing content (in case of reset)
BoardGrid.Children.Clear();

// Loop through each grid cell and create a tile
for (int row = 0; row < 8; row++)
{
for (int column = 0; column < 8; column++)
{
// Create the tile (Border)
var tile = new Border
{
Background = (row + column) % 2 == 0 ? Brushes.LightGreen : Brushes.DarkGreen, // Alternating green shades
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(1)
};

// Set row and column for the grid placement
Grid.SetRow(tile, row);
Grid.SetColumn(tile, column);

// Add the tile to the board grid
BoardGrid.Children.Add(tile);

// Optionally, add an Ellipse for pieces (will be added later in the UpdateBoard method)
if ((row == 3 && column == 3) || (row == 4 && column == 4)) // White pieces
{
var whitePiece = new Ellipse
{
Fill = Brushes.White, // White piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = whitePiece;
}
else if ((row == 3 && column == 4) || (row == 4 && column == 3)) // Black pieces
{
var blackPiece = new Ellipse
{
Fill = Brushes.Black, // Black piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = blackPiece;
}
}
}
}
private void InitializeBoard()
{
// Clear any existing content (in case of reset)
BoardGrid.Children.Clear();

// Loop through each grid cell and create a tile
for (int row = 0; row < 8; row++)
{
for (int column = 0; column < 8; column++)
{
// Create the tile (Border)
var tile = new Border
{
Background = (row + column) % 2 == 0 ? Brushes.LightGreen : Brushes.DarkGreen, // Alternating green shades
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(1)
};

// Set row and column for the grid placement
Grid.SetRow(tile, row);
Grid.SetColumn(tile, column);

// Add the tile to the board grid
BoardGrid.Children.Add(tile);

// Optionally, add an Ellipse for pieces (will be added later in the UpdateBoard method)
if ((row == 3 && column == 3) || (row == 4 && column == 4)) // White pieces
{
var whitePiece = new Ellipse
{
Fill = Brushes.White, // White piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = whitePiece;
}
else if ((row == 3 && column == 4) || (row == 4 && column == 3)) // Black pieces
{
var blackPiece = new Ellipse
{
Fill = Brushes.Black, // Black piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = blackPiece;
}
}
}
}
TheRanger
TheRangerβ€’3d ago
public GameGrid()
{
InitializeComponent();
InitializeBoard();
InitializePieces();
UpdateBoard();
}
public GameGrid()
{
InitializeComponent();
InitializeBoard();
InitializePieces();
UpdateBoard();
}
Merineth πŸ‡ΈπŸ‡ͺ
Oh so i should seperate them?
TheRanger
TheRangerβ€’3d ago
nice but the problem is the problem is that the GameBoard class does not have those pieces added
Merineth πŸ‡ΈπŸ‡ͺ
Hmm Oh i think i see what you mean GameGrid gets them added but GameBoard in GameWindow.xaml does not?
TheRanger
TheRangerβ€’3d ago
the GameBoard in GameBoard.cs it doesnt seem you initialized this instance of that class anywhere might be useful to initialize it in GameGrid.xaml.cs
Merineth πŸ‡ΈπŸ‡ͺ
Oh initialize a GameBoard Should i keep this?
TheRanger
TheRangerβ€’3d ago
No you need it to create the board in the window you have 2 boards, one for front end and one for back end the pieces in the back end board should control the pieces in the front end board GameBoard.cs is your back end board GameGrid.xaml is your front end board that is displayed to the user
Merineth πŸ‡ΈπŸ‡ͺ
oh i see so they should be linked basically?
TheRanger
TheRangerβ€’3d ago
UpdateBoard method pretty much links them
Merineth πŸ‡ΈπŸ‡ͺ
I see
TheRanger
TheRangerβ€’3d ago
public partial class GameGrid : UserControl
{
GameBoard _board;
public GameGrid()
{
_board = new GameBoard();
InitializeComponent();
InitializeBoard();
}
public partial class GameGrid : UserControl
{
GameBoard _board;
public GameGrid()
{
_board = new GameBoard();
InitializeComponent();
InitializeBoard();
}
add that field, to create the back end game board
Merineth πŸ‡ΈπŸ‡ͺ
oh Ok!
TheRanger
TheRangerβ€’3d ago
... when you define a variable in a method's scope, it means it only exists there no any other method can access it
Merineth πŸ‡ΈπŸ‡ͺ
Makes sense Ok so we now have an instance of GameBoard called _board
TheRanger
TheRangerβ€’3d ago
might be better to delete this
// Optionally, add an Ellipse for pieces (will be added later in the UpdateBoard method)
if ((row == 3 && column == 3) || (row == 4 && column == 4)) // White pieces
{
var whitePiece = new Ellipse
{
Fill = Brushes.White, // White piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = whitePiece;
}
else if ((row == 3 && column == 4) || (row == 4 && column == 3)) // Black pieces
{
var blackPiece = new Ellipse
{
Fill = Brushes.Black, // Black piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = blackPiece;
}
// Optionally, add an Ellipse for pieces (will be added later in the UpdateBoard method)
if ((row == 3 && column == 3) || (row == 4 && column == 4)) // White pieces
{
var whitePiece = new Ellipse
{
Fill = Brushes.White, // White piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = whitePiece;
}
else if ((row == 3 && column == 4) || (row == 4 && column == 3)) // Black pieces
{
var blackPiece = new Ellipse
{
Fill = Brushes.Black, // Black piece
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tile.Child = blackPiece;
}
cuz u know, when you call the UpdateBoard method, it will do it for you
Merineth πŸ‡ΈπŸ‡ͺ
Oh i see I went back tho
public partial class GameGrid : UserControl
{
GameBoard _board;
public GameGrid()
{
_board = new GameBoard();
InitializeComponent();
InitializeBoard();
}

private void InitializeBoard()
{
// Clear any existing content (in case of reset)
BoardGrid.Children.Clear();

// Loop through each grid cell and create a tile
for (int row = 0; row < 8; row++)
{
for (int column = 0; column < 8; column++)
{
// Create the tile (Border)
var tile = new Border
{
Background = (row + column) % 2 == 0 ? Brushes.LightGreen : Brushes.DarkGreen, // Alternating green shades
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(1)
};

// Optionally, add an Ellipse for pieces (will be added later in the UpdateBoard method)
tile.Child = new Ellipse
{
Fill = Brushes.Transparent, // Pieces will be added later
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};

// Set row and column for the grid placement
Grid.SetRow(tile, row);
Grid.SetColumn(tile, column);

// Add the tile to the board grid
BoardGrid.Children.Add(tile);
}
}

// Initialize the 4 starting ones with the right color

}
public partial class GameGrid : UserControl
{
GameBoard _board;
public GameGrid()
{
_board = new GameBoard();
InitializeComponent();
InitializeBoard();
}

private void InitializeBoard()
{
// Clear any existing content (in case of reset)
BoardGrid.Children.Clear();

// Loop through each grid cell and create a tile
for (int row = 0; row < 8; row++)
{
for (int column = 0; column < 8; column++)
{
// Create the tile (Border)
var tile = new Border
{
Background = (row + column) % 2 == 0 ? Brushes.LightGreen : Brushes.DarkGreen, // Alternating green shades
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(1)
};

// Optionally, add an Ellipse for pieces (will be added later in the UpdateBoard method)
tile.Child = new Ellipse
{
Fill = Brushes.Transparent, // Pieces will be added later
Width = 40,
Height = 40,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};

// Set row and column for the grid placement
Grid.SetRow(tile, row);
Grid.SetColumn(tile, column);

// Add the tile to the board grid
BoardGrid.Children.Add(tile);
}
}

// Initialize the 4 starting ones with the right color

}
This is what it looks like right now
TheRanger
TheRangerβ€’3d ago
u can call UpdateBoard() after InitializeBoard();
Merineth πŸ‡ΈπŸ‡ͺ
Right But UpdateBoard() currently expects to arguments
TheRanger
TheRangerβ€’3d ago
and do u know what argument is it?
Merineth πŸ‡ΈπŸ‡ͺ
Disk[] and Boardstate
TheRanger
TheRangerβ€’3d ago
so the boardstate, do u know where to find it?
Merineth πŸ‡ΈπŸ‡ͺ
Currently boardstate is only being used in my UpdateBoard method but my assumption is that it's _board?
TheRanger
TheRangerβ€’3d ago
yes, its stored in _board
Merineth πŸ‡ΈπŸ‡ͺ
Hmm UpdateBoard(_board.BoardState); i think ?
TheRanger
TheRangerβ€’3d ago
correct, try and see
Merineth πŸ‡ΈπŸ‡ͺ
Holy you are a genius
Merineth πŸ‡ΈπŸ‡ͺ
It did indeed update it correctly
TheRanger
TheRangerβ€’3d ago
to you maybe im a genius, but to actual seniors, no i dont think you have implemented mouse clicks yet
Merineth πŸ‡ΈπŸ‡ͺ
No sadly not
TheRanger
TheRangerβ€’3d ago
you should start by implementing a mouseclick event for the GameGrid.xaml
Merineth πŸ‡ΈπŸ‡ͺ
That makes sense, one moment Ok i think i have a basic idea. MouseDown="GameGrid_MouseDown"> I added this to use the function of mouse button in the xaml code Then i added this to my cs file
// Mouse Click event handler
private void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the position where the user clicked
var mousePosition = e.GetPosition(BoardGrid);
var column = (int)(mousePosition.X / 50); // Assuming each cell is 50px wide
var row = (int)(mousePosition.Y / 50); // Assuming each cell is 50px tall

// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
Disk currentPlayerDisk = Disk.Black; // Or whichever player is currently playing
if (_board.IsValidMove(row, column, currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
}
// Mouse Click event handler
private void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the position where the user clicked
var mousePosition = e.GetPosition(BoardGrid);
var column = (int)(mousePosition.X / 50); // Assuming each cell is 50px wide
var row = (int)(mousePosition.Y / 50); // Assuming each cell is 50px tall

// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
Disk currentPlayerDisk = Disk.Black; // Or whichever player is currently playing
if (_board.IsValidMove(row, column, currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
}
It does indeed work and i'm able to place them However it's not switching colour I'm still checking why that is the case
TheRanger
TheRangerβ€’3d ago
something in the GameBoard.cs then
Merineth πŸ‡ΈπŸ‡ͺ
I assume it's because it's not switching player in GameBoard?
TheRanger
TheRangerβ€’3d ago
debug and find out need a small break, brb
Merineth πŸ‡ΈπŸ‡ͺ
Disk currentPlayerDisk = Disk.Black; // Or whichever player is currently playing It's most likely this part I solved it by adding a method to swap disc colour method
private void TogglePlayerTurn()
{
// Toggle between Black and White
if (_currentPlayerDisk == Disk.Black)
{
_currentPlayerDisk = Disk.White;
}
else
{
_currentPlayerDisk = Disk.Black;
}
}
private void TogglePlayerTurn()
{
// Toggle between Black and White
if (_currentPlayerDisk == Disk.Black)
{
_currentPlayerDisk = Disk.White;
}
else
{
_currentPlayerDisk = Disk.Black;
}
}
TheRanger
TheRangerβ€’3d ago
cool
Merineth πŸ‡ΈπŸ‡ͺ
and i called this method inside the MouseDown event
private void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the position where the user clicked
var mousePosition = e.GetPosition(BoardGrid);
var column = (int)(mousePosition.X / 50); // Assuming each cell is 50px wide
var row = (int)(mousePosition.Y / 50); // Assuming each cell is 50px tall

// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, _currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);

// Toggle the current player for the next turn
TogglePlayerTurn();
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
}
private void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the position where the user clicked
var mousePosition = e.GetPosition(BoardGrid);
var column = (int)(mousePosition.X / 50); // Assuming each cell is 50px wide
var row = (int)(mousePosition.Y / 50); // Assuming each cell is 50px tall

// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, _currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);

// Toggle the current player for the next turn
TogglePlayerTurn();
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
}
And it does indeed work However it's not flipping the colors of the discs
TheRanger
TheRangerβ€’3d ago
flip the colors then in the Gameboard.cs
Merineth πŸ‡ΈπŸ‡ͺ
I think it's FlipDiskInDirection method? Yeah i see the problem
Merineth πŸ‡ΈπŸ‡ͺ
It doesn't enter the if or else if it went direction to else and returned void
Merineth πŸ‡ΈπŸ‡ͺ
Aah this is hell to debug. I don't understand. Is it trying to compare and int with White here? No wait itr's comparing Transparent with white
TheRanger
TheRangerβ€’3d ago
need to watch how reversi works, lol
Merineth πŸ‡ΈπŸ‡ͺ
hahah Yeah that was step 1 for me also, never heard of it until i started this course lol essentially i clicked C4 which should make D4 Black and place a black on C4
TheRanger
TheRangerβ€’3d ago
so basically when you click, it should find the closest black ellipse that intersects with a white ellipse?
Merineth πŸ‡ΈπŸ‡ͺ
Hmm When i click C4 it should compare all tiles from C4 to E4 and turn them black
TheRanger
TheRangerβ€’3d ago
looks simple tbh when u click c4 you should find a white piece next to it when you do you determine its direction in this case its right so ull keep iterating to the right, save the positions of the pieces you iterated to into a list, till you hit a black piece once you hit it, flip the pieces that are stored in the list
Merineth πŸ‡ΈπŸ‡ͺ
I think i did it
Merineth πŸ‡ΈπŸ‡ͺ
I just played a full game i think You're a genius the only problem now is that the prompt on who won or not didn't show It's a bit trickier than i thought
Merineth πŸ‡ΈπŸ‡ͺ
i might cry The absolute last thing i need to do is implement the AI part which is the hardest part as it includes threading which i'm absolutely novice at
TheRanger
TheRangerβ€’3d ago
u could run a method in the background asynchronously
TheRanger
TheRangerβ€’3d ago
Dot Net Tutorials
Dot Net Tutorials
Task in C#
In this article, I am going to discuss Task in C# with Examples. The Task data type in C# represents an asynchronous operation.
Merineth πŸ‡ΈπŸ‡ͺ
Hmm interesting I was thinking What if i just made it so when it's white turn, and the computer checkmark has been checked, then it will do the moves for it
Merineth πŸ‡ΈπŸ‡ͺ
this one specifically There is one implementation of it already tho
public override async Task<(int x, int y)> RequestMove(GameBoard board, List<(int x, int y)> validMoves)
{
//placeholder move
(int x, int y) selectedMove = (-1, -1);

Random random = new Random();
int index = random.Next(validMoves.Count);

selectedMove = validMoves[index];

//Sleep for 2 seconds
await Task.Delay(2000);

// Return the selected move
return selectedMove;

}
public override async Task<(int x, int y)> RequestMove(GameBoard board, List<(int x, int y)> validMoves)
{
//placeholder move
(int x, int y) selectedMove = (-1, -1);

Random random = new Random();
int index = random.Next(validMoves.Count);

selectedMove = validMoves[index];

//Sleep for 2 seconds
await Task.Delay(2000);

// Return the selected move
return selectedMove;

}
But if i'm being perfectly honest, i'm not sure if it's written correctly Considering it isn't working right now, i'd assume it's not
TheRanger
TheRangerβ€’3d ago
actually is threading important? the computer can think instantly
Merineth πŸ‡ΈπŸ‡ͺ
Sadly, i think it’s a requirement Will i require an energy drink to keep up with this part? haha so is threading just meant to delay the process of moving by the ai? I'm not sure if you are too familiar with vhdl, but is threading somewhat similar to a process from vhdl?
TheRanger
TheRangerβ€’3d ago
pretty much it would be great to create a class called AI for maintability and have multiple methods to execute in order
Merineth πŸ‡ΈπŸ‡ͺ
I see Could i potentially rename ComputerPlayer.cs to AI.cs maybe or do you recommend making a completely new file with AI
TheRanger
TheRangerβ€’3d ago
oh yeah i forgot it exists
Merineth πŸ‡ΈπŸ‡ͺ
πŸ˜” i'm so lost
TheRanger
TheRangerβ€’3d ago
what are u stuck with?
Merineth πŸ‡ΈπŸ‡ͺ
Well essentially
private void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the position where the user clicked
var mousePosition = e.GetPosition(BoardGrid);
var column = (int)(mousePosition.X / 50); // Assuming each cell is 50px wide
var row = (int)(mousePosition.Y / 50); // Assuming each cell is 50px tall

// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, _currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);

// Toggle the current player for the next turn
TogglePlayerTurn();
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
if (_board.GameOver())
{
InitializeWinnerDialog();
}

}
private void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
// Get the position where the user clicked
var mousePosition = e.GetPosition(BoardGrid);
var column = (int)(mousePosition.X / 50); // Assuming each cell is 50px wide
var row = (int)(mousePosition.Y / 50); // Assuming each cell is 50px tall

// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, _currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);

// Toggle the current player for the next turn
TogglePlayerTurn();
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
if (_board.GameOver())
{
InitializeWinnerDialog();
}

}
This part of the code is the one where you select a position on the board which is for the human player to use But if it's a computer player, then i want it to uitilize the ai player instead I guess i can make a new if/else in this code which checks for the player 2s name? if(player2 is a computer) invoke the ComputerPlayer.cs logic else do the human logic (the things below)
TheRanger
TheRangerβ€’3d ago
yeah the board should have a flag that indicates who's turn is it
Merineth πŸ‡ΈπŸ‡ͺ
Ok i think i did it
if (_isPlayer2Computer && _currentPlayerDisk == Disk.White)
{
await ExecuteAIMove();
return;
}
else
{
// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, _currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);

// Toggle the current player for the next turn
TogglePlayerTurn();
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
if (_board.GameOver())
{
InitializeWinnerDialog();
}
}


}
if (_isPlayer2Computer && _currentPlayerDisk == Disk.White)
{
await ExecuteAIMove();
return;
}
else
{
// Ensure the row and column are within bounds (0-7)
if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
// Check if the move is valid for the current player
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
// Execute the move
_board.ExecuteMove(row, column, _currentPlayerDisk);

// Update the board after the move
UpdateBoard(_board.BoardState);

// Toggle the current player for the next turn
TogglePlayerTurn();
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
if (_board.GameOver())
{
InitializeWinnerDialog();
}
}


}
the await ExectueAIMove(); should now be what the AI should do right?
TheRanger
TheRangerβ€’3d ago
sure
Merineth πŸ‡ΈπŸ‡ͺ
namespace Assignment2
{
public class ComputerPlayer
{
private Disk _aiDisk;

public ComputerPlayer(Disk aiDisk)
{
_aiDisk = aiDisk;
}

internal async Task<(int x, int y)> ExecuteAIMove(GameBoard board)
{
// Get all valid moves for the AI
List<(int x, int y)> validMoves = board.GetValidMoves(_aiDisk);

// If no valid moves, return (-1, -1) to indicate pass
if (validMoves.Count == 0)
{
return (-1, -1);
}

// Simulate AI thinking time (optional)
await Task.Delay(1000);

// Select a move (Basic AI: Random move)
Random random = new Random();
(int x, int y) selectedMove = validMoves[random.Next(validMoves.Count)];

// Execute the move
board.ExecuteMove(selectedMove.x, selectedMove.y, _aiDisk);

// Return the selected move
return selectedMove;
}
}
}
namespace Assignment2
{
public class ComputerPlayer
{
private Disk _aiDisk;

public ComputerPlayer(Disk aiDisk)
{
_aiDisk = aiDisk;
}

internal async Task<(int x, int y)> ExecuteAIMove(GameBoard board)
{
// Get all valid moves for the AI
List<(int x, int y)> validMoves = board.GetValidMoves(_aiDisk);

// If no valid moves, return (-1, -1) to indicate pass
if (validMoves.Count == 0)
{
return (-1, -1);
}

// Simulate AI thinking time (optional)
await Task.Delay(1000);

// Select a move (Basic AI: Random move)
Random random = new Random();
(int x, int y) selectedMove = validMoves[random.Next(validMoves.Count)];

// Execute the move
board.ExecuteMove(selectedMove.x, selectedMove.y, _aiDisk);

// Return the selected move
return selectedMove;
}
}
}
Ok i think this should work There is one tiny problem i have tho I don't know how to invoke the method Because i assume Task is similar to method?
TheRanger
TheRangerβ€’3d ago
ur already invoking it here
Merineth πŸ‡ΈπŸ‡ͺ
await ExecuteAIMove(); doesn't work :(
TheRanger
TheRangerβ€’3d ago
how? what errors do u see
Merineth πŸ‡ΈπŸ‡ͺ
Severity Code Description Project File Line Suppression State Error (active) CS4033 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. Assignment2 C:\Users\aljom\Documents\Plugg Programmering filer\OOP\Assignment2\Assignment2Repository\Assignment2\Assignment2\View\GameGrid.xaml.cs 39
TheRanger
TheRangerβ€’3d ago
there you go its telling you how to fix it
Merineth πŸ‡ΈπŸ‡ͺ
ooooh It already has Task marked afaik? internal async Task<(int x, int y)> ExecuteAIMove(GameBoard board)
TheRanger
TheRangerβ€’3d ago
its talking about the method that is calling it the GameGrid_MouseDown one
Merineth πŸ‡ΈπŸ‡ͺ
Oh i see It's expecting the MouseDown method to be a Task?
TheRanger
TheRangerβ€’3d ago
yeah
Merineth πŸ‡ΈπŸ‡ͺ
Ahh i see that seems to fix it Is that a requirement when it comes to threading and asynchronous tasks? Severity Code Description Project File Line Suppression State Error (active) CS0103 The name 'ExecuteAIMove' does not exist in the current context Assignment2 C:\Users\aljom\Documents\Plugg Programmering filer\OOP\Assignment2\Assignment2Repository\Assignment2\Assignment2\View\GameGrid.xaml.cs 39 I'm assuming this is becuase ExecuteAIMove is called in GameGrid.xaml.cs but is defined in CamputerPlayer.cs?
TheRanger
TheRangerβ€’2d ago
if its defined there then yes
Merineth πŸ‡ΈπŸ‡ͺ
Like no matter what i do i keep getting errors it's so frustrating if (isPlayer2Computer) { player2 = new ComputerPlayer(player2Name, Disk.Black); } Severity Code Description Project File Line Suppression State Error (active) CS1729 'ComputerPlayer' does not contain a constructor that takes 2 arguments Assignment2 C:\Users\aljom\Documents\Plugg Programmering filer\OOP\Assignment2\Assignment2Repository\Assignment2\Assignment2\View\GameWindow.xaml.cs 73
public ComputerPlayer(Disk aiDisk) { _aiDisk = aiDisk; }
TheRanger
TheRangerβ€’2d ago
it tells you why
Merineth πŸ‡ΈπŸ‡ͺ
public class ComputerPlayer
{
private Disk _aiDisk;
private string _aiName;

public ComputerPlayer(string player2Name, Disk aiDisk)
{
_aiDisk = aiDisk;
this._aiName = player2Name;
}
public class ComputerPlayer
{
private Disk _aiDisk;
private string _aiName;

public ComputerPlayer(string player2Name, Disk aiDisk)
{
_aiDisk = aiDisk;
this._aiName = player2Name;
}
Yeah iu updated it with it and then it complains again Severity Code Description Project File Line Suppression State Error (active) CS0029 Cannot implicitly convert type 'Assignment2.Model.Assignment2.ComputerPlayer' to 'Assignment2.Model.Player' Assignment2 C:\Users\aljom\Documents\Plugg Programmering filer\OOP\Assignment2\Assignment2Repository\Assignment2\Assignment2\View\GameWindow.xaml.cs 73 I have a hard time understand why it even is complaining about that when player2Name is a string
TheRanger
TheRangerβ€’2d ago
can u show that line
Merineth πŸ‡ΈπŸ‡ͺ
private void NewGameButton_Click(object sender, RoutedEventArgs e)
{
// Show the SetUpGameDialog to collect player names and types
SetUpGameDialog setUpDialog = new SetUpGameDialog();
bool? result = setUpDialog.ShowDialog(); // Show dialog and wait for result

if (result == true) // If OK was clicked (DialogResult = true)
{
// Retrieve player names and player types
string player1Name = setUpDialog.Player1Name;
string player2Name = setUpDialog.Player2Name;
bool isPlayer2Computer = setUpDialog.IsPlayer2Computer;


/* Creates 3 instances for the if statement. player 1 is always human
so i always creater player1 as HumanPlayer and the if determines if it's computer or human.*/
Player player1 = new HumanPlayer(player1Name, Disk.White);
Player player2;
if (isPlayer2Computer)
{
player2 = new ComputerPlayer(player2Name, Disk.Black);
}
else
{
player2 = new HumanPlayer(player2Name, Disk.Black);
}


// Create the GameManager instance.
GameManager game = new GameManager(player1Name, player2Name);
game.StartGame(GameGridControl);

}
else
{
// If the user cancels the setup
MessageBox.Show("Game setup was canceled.", "Canceled");
}
}
private void NewGameButton_Click(object sender, RoutedEventArgs e)
{
// Show the SetUpGameDialog to collect player names and types
SetUpGameDialog setUpDialog = new SetUpGameDialog();
bool? result = setUpDialog.ShowDialog(); // Show dialog and wait for result

if (result == true) // If OK was clicked (DialogResult = true)
{
// Retrieve player names and player types
string player1Name = setUpDialog.Player1Name;
string player2Name = setUpDialog.Player2Name;
bool isPlayer2Computer = setUpDialog.IsPlayer2Computer;


/* Creates 3 instances for the if statement. player 1 is always human
so i always creater player1 as HumanPlayer and the if determines if it's computer or human.*/
Player player1 = new HumanPlayer(player1Name, Disk.White);
Player player2;
if (isPlayer2Computer)
{
player2 = new ComputerPlayer(player2Name, Disk.Black);
}
else
{
player2 = new HumanPlayer(player2Name, Disk.Black);
}


// Create the GameManager instance.
GameManager game = new GameManager(player1Name, player2Name);
game.StartGame(GameGridControl);

}
else
{
// If the user cancels the setup
MessageBox.Show("Game setup was canceled.", "Canceled");
}
}
Merineth πŸ‡ΈπŸ‡ͺ
Does it have something to do with player2 being Player type? Ok i think i solved it
Player player1 = new HumanPlayer(player1Name, Disk.White);
if (isPlayer2Computer)
{
ComputerPlayer player2;
player2 = new ComputerPlayer(player2Name, Disk.Black);
}
else
{
Player player2;
player2 = new HumanPlayer(player2Name, Disk.Black);
}
Player player1 = new HumanPlayer(player1Name, Disk.White);
if (isPlayer2Computer)
{
ComputerPlayer player2;
player2 = new ComputerPlayer(player2Name, Disk.Black);
}
else
{
Player player2;
player2 = new HumanPlayer(player2Name, Disk.Black);
}
100% pure intuition
TheRanger
TheRangerβ€’2d ago
ill let you think this one ^^
Merineth πŸ‡ΈπŸ‡ͺ
Hmm ok i managed to solve all the errors and it compiles but i have one small question
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
This part here: I just realized that unless a button is pressed it will never trigger the AI part
// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
this part specifically Is it possible to make it so after the human has done one turn, it will then perform that if statement?
TheRanger
TheRangerβ€’2d ago
yeah
Merineth πŸ‡ΈπŸ‡ͺ
Honestly the main problem i'm facing is
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
_isPlayer2Computer evaltues to false even tho it should be true
TheRanger
TheRangerβ€’2d ago
what makes u think so? did you set it to true from anywhere?
Merineth πŸ‡ΈπŸ‡ͺ
Ah ur right it isn't evaluated anywhere :sadge:
leowest
leowestβ€’2d ago
just one thing, for ui events they dont need to be a Task should be async void in fact I believe u can't even make them a task in WPF as there is no signature for it but there is for async void
Merineth πŸ‡ΈπŸ‡ͺ
I might lose my mind? I added this
if (IsPlayer2Computer)
{
gameGrid._isPlayer2Computer = true;
}
if (IsPlayer2Computer)
{
gameGrid._isPlayer2Computer = true;
}
to my SetupGameDialog.xaml.cs So it indeed gets set to true but it STILL evaluates to false
Merineth πŸ‡ΈπŸ‡ͺ
here it gets evalueted to true
TheRanger
TheRangerβ€’2d ago
u mustve set it to false at some point what does TogglePlayerTurn() method do?
Merineth πŸ‡ΈπŸ‡ͺ
private void TogglePlayerTurn() { // Toggle between Black and White if (_currentPlayerDisk == Disk.Black) { _currentPlayerDisk = Disk.White; } else { _currentPlayerDisk = Disk.Black; } } it just swaps the colour I've carefully gone through it 3 times now and i can't see when _isPlayer2Computer be turned to false Is it possible to set a breakpoint so it checks for any change in a variable/boolean? Ok there seems to be a "Watch" debug mode
TheRanger
TheRangerβ€’2d ago
yea if its a property you could put a breakpoint to its setter
Merineth πŸ‡ΈπŸ‡ͺ
Ok i found out that it changes to false when MouseDown is triggered but i have literally zero idea why
TheRanger
TheRangerβ€’2d ago
i would advise you not to put some game logic to the front end code toggleplayerturn() and _currentPlayerDisk for example should be in the GameBoard class
Merineth πŸ‡ΈπŸ‡ͺ
yes i know this code is a nightmare i just want to make it work been like an hour on this problem alone now
TheRanger
TheRangerβ€’2d ago
it will actually be more nightmare the further you go so i advise you move them to that class,
Merineth πŸ‡ΈπŸ‡ͺ
What am i supposed to move
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}

if (_board.GameOver())
{
InitializeWinnerDialog();
}
}
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (_isPlayer2Computer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}

if (_board.GameOver())
{
InitializeWinnerDialog();
}
}
thius currently doersn't work because the if statemnt is not working properly i checked the box that it is an ai and it still returns as false
TheRanger
TheRangerβ€’2d ago
have u figured out what sets it to false?
Merineth πŸ‡ΈπŸ‡ͺ
Yes GameGrid_MouseDown
TheRanger
TheRangerβ€’2d ago
where exactly in that method?
Merineth πŸ‡ΈπŸ‡ͺ
Whenever the button is clicked it changes to false i put a breakpoint on the method itself
TheRanger
TheRangerβ€’2d ago
which line in the method?
Merineth πŸ‡ΈπŸ‡ͺ
Let me quadruple check, one moment
TheRanger
TheRangerβ€’2d ago
also _isPlayer2Computer is pointless you dont need it cuz you can just check if the player2 variable is an instance of ComputerPlayer
Player player2;

if (player2 is ComputerPlayer)
{
//your code
}
Player player2;

if (player2 is ComputerPlayer)
{
//your code
}
by using the is operator, you can check if the variable can be downcasted to that class
Animal animal = new Tiger();

if (animal is Tiger tiger)
{
tiger.RunAMethod();
}
Animal animal = new Tiger();

if (animal is Tiger tiger)
{
tiger.RunAMethod();
}
your ComputerPlayer class extends Player, so it will definitely work
Merineth πŸ‡ΈπŸ‡ͺ
But player2 is defined in Setupgamedialog and i'm having the if in gamegrid God i despise c#
TheRanger
TheRangerβ€’2d ago
Player2 should be defined in the back end classes
TheRanger
TheRangerβ€’2d ago
it means its not defined
Merineth πŸ‡ΈπŸ‡ͺ
That's why i was trying to pass _isplayercomputer
TheRanger
TheRangerβ€’2d ago
where is player1 defined?
Merineth πŸ‡ΈπŸ‡ͺ
GameManager So is player2
TheRanger
TheRangerβ€’2d ago
thats just a string, im talking about an instance of class HumanPlayer and ComputerPlayer if you never use them why create them in the first place?
Merineth πŸ‡ΈπŸ‡ͺ
I don't know i have so much code i have literally no idea what i'm doing
TheRanger
TheRangerβ€’2d ago
thats what happens when you rely too much on chatgpt
Merineth πŸ‡ΈπŸ‡ͺ
It was either that or death
leowest
leowestβ€’2d ago
I mean u literally reached death by not knowing what your code does
Merineth πŸ‡ΈπŸ‡ͺ
what?
leowest
leowestβ€’2d ago
relying too much on chatgpt to do the code etc
Merineth πŸ‡ΈπŸ‡ͺ
relying i didn't know how to do it in the firs tplace
leowest
leowestβ€’2d ago
what u need is something to have the state of your game
TheRanger
TheRangerβ€’2d ago
they have GameManager defined atleast, just needs to do some modifications https://github.com/aljomatrix/Assignment2Repository
GitHub
GitHub - aljomatrix/Assignment2Repository
Contribute to aljomatrix/Assignment2Repository development by creating an account on GitHub.
leowest
leowestβ€’2d ago
and that is where chatgpt is the worst because u dont know how to do X and rely on something to tell u what to do which doesnt know either because it doesnt have a broad concept of what u are doing that is why we dont suggest people to use chatgpt at all for learning I didnt see the repo yet I was just following bits of the conversation but yeah u would use something like the GameManager to handle the state of your game so you can access player 1, 2 etc
Merineth πŸ‡ΈπŸ‡ͺ
I can totally understand that people who know how to code see chatgpt as something to avoid. But when you are a student like me where you are made to do a group project completely solo where the difficulty on the project vs the exam is so huge you have literally no idea how to even begin the project, much less finish it in a very limited time on top of having other subjects to study for and assignments to turn in. Chatgpt becomes the only option in order to prevent expulsion The choice between expulsion and chatgpt, isn't much of a choice at all
leowest
leowestβ€’2d ago
I mean if you are made to make it solo the first thing u should be doing is raising that to your teacher and negotiate with him a smaller task. Secondly it doesnt change the fact you should learn by actually doing research on the tools you need to use instead of relying on chat gpt more over u have this great communicate that can help u understand what X or Y does if u dont understand it your self
Merineth πŸ‡ΈπŸ‡ͺ
Either way, i have no idea how to implement the if statement. It's been two hours and i can't find the reason why _iscomputerplayer turning false
leowest
leowestβ€’2d ago
im not here trying to bash you for using gpt but your comment there was literally what happened.
Merineth πŸ‡ΈπŸ‡ͺ
If i had the luxury of being able to do my own research and learn properly about coding, then i wouldn't sit here and be berated for using chatgpt and begging for help in order to finish this assignment
leowest
leowestβ€’2d ago
Anyway give me a few to look at your project and see what is usable vs what is not
Merineth πŸ‡ΈπŸ‡ͺ
Thanks
TheRanger
TheRangerβ€’2d ago
you could start by initializing HumanPlayer and ComputerPlayer instances in your GameManager class
leowest
leowestβ€’2d ago
few questions, are you suppose to use mvvm at all?
Merineth πŸ‡ΈπŸ‡ͺ
Yes, it's supposedly a requirement
leowest
leowestβ€’2d ago
ok is there any mention on whether u can use external libraries or u have to write everything vanilla?
Merineth πŸ‡ΈπŸ‡ͺ
I'd assume vanilla Quite frankly i don't even think they go through the code when grading
TheRanger
TheRangerβ€’2d ago
in my days they opened my code and asked me what this code and that code does πŸ˜„
leowest
leowestβ€’2d ago
yeah im asking because you're hardly using mvvm apparently and if u have to write it vannilla is a bit more of a pain same is the repo currently buildable?
Merineth πŸ‡ΈπŸ‡ͺ
Idk what buildable means As in, compileable?
leowest
leowestβ€’2d ago
when u right click and click build sure compileable
Merineth πŸ‡ΈπŸ‡ͺ
No
leowest
leowestβ€’2d ago
also do u have any other reqs like use Dependency Injection etc?
Merineth πŸ‡ΈπŸ‡ͺ
The requirements are like 14 pages long
leowest
leowestβ€’2d ago
I suppose u dont have a screenshot of it we can look at nvm u do
TheRanger
TheRangerβ€’2d ago
oh man i wish you told me about that pdf
leowest
leowestβ€’2d ago
ok so mvvm is optional that's helpful to cut the chase specially since most of your code is in the code behind already
TheRanger
TheRangerβ€’2d ago
it even gives you hints on how to implement stuff
leowest
leowestβ€’2d ago
yeh its very detailed
Merineth πŸ‡ΈπŸ‡ͺ
Well i'm almost done just need the goddamn if statement tot work so my ai works
TheRanger
TheRangerβ€’2d ago
without finishing the GameManager class?
Merineth πŸ‡ΈπŸ‡ͺ
i think so Like i noticed just now going into the assignment page and seeing they have apparantly moved the deadline to Aug 17 Nvm My goal here is to make it so it works submit it and get feedback if they aren't happy about something
TheRanger
TheRangerβ€’2d ago
trust me i think its faster to finish if we finish making the GameManager class
leowest
leowestβ€’2d ago
same so listen I see the problem you have created the players locally in your GameWindow
Merineth πŸ‡ΈπŸ‡ͺ
I trust you. But i think you severely underestimate overestimate the knowledge i have about C# and what i've learnt from class
leowest
leowestβ€’2d ago
what u would do instead if make use of the GameManager and have the players set there and then do the same on GameGrid and use the GamaManager player's object
TheRanger
TheRangerβ€’2d ago
no worries you will learn while we're helping you
leowest
leowestβ€’2d ago
that is looking at your code and taking the simplest route not even the correct route if u want to make this MVVM then u would have to change A LOT of things not even kidding
Merineth πŸ‡ΈπŸ‡ͺ
Yeah i know i gave up halfway of keeping it MVVM
leowest
leowestβ€’2d ago
MVVM can be great but its not simple to understand what problem it solves and how to use it and separate its concerns a lot of people fail at that even I sometimes need to ask for help with certain things
Merineth πŸ‡ΈπŸ‡ͺ
So you want me ot implement this?
No description
leowest
leowestβ€’2d ago
No description
leowest
leowestβ€’2d ago
you're setting up the player here, the main problem is you're assigning everything locally so once it goes out of that method everything there is pretty much lost
Merineth πŸ‡ΈπŸ‡ͺ
Becuase it's inside the if? or rather when the new game button dissapears/closese?
leowest
leowestβ€’2d ago
you understand what a local variable is?
Merineth πŸ‡ΈπŸ‡ͺ
Yes
leowest
leowestβ€’2d ago
then explain to me
Merineth πŸ‡ΈπŸ‡ͺ
It's declared in NewGameButton so it's restricted to it?
leowest
leowestβ€’2d ago
a local variable only exists in that block, for example:
void Start()
{
Player player = new Player();
}

void Stop()
{
Console.WriteLine(player.Name);
}
void Start()
{
Player player = new Player();
}

void Stop()
{
Console.WriteLine(player.Name);
}
player only exists within the method Start.
Merineth πŸ‡ΈπŸ‡ͺ
Yes i know that
leowest
leowestβ€’2d ago
so you cannot access it on Stop so none of the code in the image I posted above is usable anywhere else
Merineth πŸ‡ΈπŸ‡ͺ
So what is the solution? No matter where i make it, it'll be restricted to where i make it
leowest
leowestβ€’2d ago
you would have a property or field to hold the GameManager and update it there
Merineth πŸ‡ΈπŸ‡ͺ
I see. So create it in GameWindow but hold it in game manager?
TheRanger
TheRangerβ€’2d ago
hint: GameManager.Instance might be very useful
leowest
leowestβ€’2d ago
now the other problem im looking at is how the wpf is laid give me a moment you even have it declared as a field but u never used it but yes beyond that u could make it a singleton but then again it might be another concept u dont know yet
leowest
leowestβ€’2d ago
at the very top
No description
leowest
leowestβ€’2d ago
but for some reason u decided to create a new local instance of the GameManager
Merineth πŸ‡ΈπŸ‡ͺ
class GameManager
{
public Player player1 { set; get; }
public Player player2 { set; get; }
public ComputerPlayer Player2 { set; get; }
class GameManager
{
public Player player1 { set; get; }
public Player player2 { set; get; }
public ComputerPlayer Player2 { set; get; }
So i could do something like this. And set them from my GameWindow?
leowest
leowestβ€’2d ago
yes you could not sure why u would have 3 Players thou it would make more sense to have a property in Player to tell whether its a computer or a human or if u must have a Computer and a Player class just have those 2
TheRanger
TheRangerβ€’2d ago
πŸ‘€ i never saw set being placed before get, i guess it works lol
leowest
leowestβ€’2d ago
same hehe
TheRanger
TheRangerβ€’2d ago
i dont think they know polymorphism
Merineth πŸ‡ΈπŸ‡ͺ
So this here would be my GameWindow
Player player1 = new HumanPlayer(player1Name, Disk.White);
gameManager.player1 = player1;
if (isPlayer2Computer)
{
ComputerPlayer player2;
player2 = new ComputerPlayer(player2Name, Disk.Black);
gameManager.AIPlayer2 = player2; ;
}
else
{
Player player2;
player2 = new HumanPlayer(player2Name, Disk.Black);
gameManager.player2 = player2;
}
Player player1 = new HumanPlayer(player1Name, Disk.White);
gameManager.player1 = player1;
if (isPlayer2Computer)
{
ComputerPlayer player2;
player2 = new ComputerPlayer(player2Name, Disk.Black);
gameManager.AIPlayer2 = player2; ;
}
else
{
Player player2;
player2 = new HumanPlayer(player2Name, Disk.Black);
gameManager.player2 = player2;
}
And this here would be my GameManager
class GameManager
{
public Player player1 { set; get; }
public Player player2 { set; get; }
public ComputerPlayer AIPlayer2 { set; get; }

public GameManager(string player1, string player2)
{
this.player1 = player1;
this.player2 = player2;
}

public void StartGame(GameGrid gameGridControl)
{

}

}
class GameManager
{
public Player player1 { set; get; }
public Player player2 { set; get; }
public ComputerPlayer AIPlayer2 { set; get; }

public GameManager(string player1, string player2)
{
this.player1 = player1;
this.player2 = player2;
}

public void StartGame(GameGrid gameGridControl)
{

}

}
And this would make it so it doesn't disappear?
TheRanger
TheRangerβ€’2d ago
delete the AIPlayer2 line
Merineth πŸ‡ΈπŸ‡ͺ
Isn't it that one type can be many types?
TheRanger
TheRangerβ€’2d ago
you can basically assign an instance of ComputerPlayer to player2 since it extends Player class
Merineth πŸ‡ΈπŸ‡ͺ
Says i can't do that
TheRanger
TheRangerβ€’2d ago
oh it doesnt extend the Player class, your pdf does say it should extend it
Merineth πŸ‡ΈπŸ‡ͺ
I don't know what extend means
TheRanger
TheRangerβ€’2d ago
derive, ComputerPlayer should be a subclass of Player
Merineth πŸ‡ΈπŸ‡ͺ
Ah Yeah it should've been seems that wasn't done If i try to add that i get 100x errors
Merineth πŸ‡ΈπŸ‡ͺ
every change i make i get 100x more errors i tried making it internal but dthat wasn't allowed either
TheRanger
TheRangerβ€’2d ago
yeah its asking you to override the abstract method defined in the super class those are just 6 errors not 100
leowest
leowestβ€’2d ago
funny he did HumanPlayer right but didnt for Computer
Merineth πŸ‡ΈπŸ‡ͺ
So the problem is that i'm not implementing the request move method from player?
TheRanger
TheRangerβ€’2d ago
yeah
Merineth πŸ‡ΈπŸ‡ͺ
Don't i just add override to it?
TheRanger
TheRangerβ€’2d ago
yes, its an abstract method so u are forced to override it
leowest
leowestβ€’2d ago
you might also need to make the classes public so that the GameManager can be passed down to the DataContext and be visible to your gamegrid
Merineth πŸ‡ΈπŸ‡ͺ
public override async Task<(int x, int y)> RequestMove(GameBoard board, List<(int x, int y)> validMoves)
{
await Task.Delay(1); // Simulates async behavior
return validMoves.Count > 0 ? validMoves[0] : (0, 0); // Returns the first valid move or (0,0) as fallback
}
public override async Task<(int x, int y)> RequestMove(GameBoard board, List<(int x, int y)> validMoves)
{
await Task.Delay(1); // Simulates async behavior
return validMoves.Count > 0 ? validMoves[0] : (0, 0); // Returns the first valid move or (0,0) as fallback
}
Like so i guess the same as HumanPlayer
leowest
leowestβ€’2d ago
I mean is it suppose to be the same? didnt u have logic for the ai
Merineth πŸ‡ΈπŸ‡ͺ
I dont know i made it in gamegrid
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (player2 is ComputerPlayer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
private async void GameGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
var mousePosition = e.GetPosition(BoardGrid);
int column = (int)(mousePosition.X / 50);
int row = (int)(mousePosition.Y / 50);

if (row >= 0 && row < 8 && column >= 0 && column < 8)
{
if (_board.IsValidMove(row, column, _currentPlayerDisk))
{
_board.ExecuteMove(row, column, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();

// If Player 2 is a computer, execute AI move
if (player2 is ComputerPlayer)
{
await Task.Delay(1000); // Optional delay for realism
var aiMove = await _computerPlayer.ExecuteAIMove(_board);
_board.ExecuteMove(aiMove.x, aiMove.y, _currentPlayerDisk);
UpdateBoard(_board.BoardState);
TogglePlayerTurn();
}
}
else
{
MessageBox.Show("Invalid move! Please try again.");
}
}
the AI part is the if statement which i was trying to get to work 19:00 or so
TheRanger
TheRangerβ€’2d ago
i would move that code to a method in the GameManager class and all what GameGrid_MouseDown would do is to tell the GameManager that it clicked on that position
Merineth πŸ‡ΈπŸ‡ͺ
I tried doing that but now i have 21 errors
TheRanger
TheRangerβ€’2d ago
no worries thats normal, it can get fixed quick
Merineth πŸ‡ΈπŸ‡ͺ
gameManager doesn't exist in gamegrid
TheRanger
TheRangerβ€’2d ago
you should know what to do, move those methods to the GameManager class
leowest
leowestβ€’2d ago
as your code is right now there is only 1 way to access GamaManager from GameGrid which would be to get the parent window and access the parent window property GameManager if u made it a public property that is why they suggest you use mvvm in the pdf and defining a global resource for the GameWindowViewModel at hints 8 because that way, GameGrid would easily see the ViewModel which would contain the GameManager
Merineth πŸ‡ΈπŸ‡ͺ
wait what
leowest
leowestβ€’2d ago
any window within your main window would see it
Merineth πŸ‡ΈπŸ‡ͺ
so i should move MouseDown to GameManager?
leowest
leowestβ€’2d ago
no that is not what I said
Merineth πŸ‡ΈπŸ‡ͺ
I was referring to R
TheRanger
TheRangerβ€’2d ago
i meant the methods like TogglePlayerTurn as for _board you would need to create its field in the GameManager class
Merineth πŸ‡ΈπŸ‡ͺ
I don't think i can move them to gamemanager
TheRanger
TheRangerβ€’2d ago
trust me bro
Merineth πŸ‡ΈπŸ‡ͺ
i'm about to pass out i'm going to take a break
TheRanger
TheRangerβ€’2d ago
i have to go very soon anyway
Merineth πŸ‡ΈπŸ‡ͺ
No worries, i’ll prob continue tomorrow or the day after depending on how much in school i have to do tomorrow i truly appreciate your help tho i completely ruined the code trying to move it
TheRanger
TheRangerβ€’2d ago
its fine we will help you move the rest properly
Unknown User
Unknown Userβ€’2d ago
Message Not Public
Sign In & Join Server To View
Merineth πŸ‡ΈπŸ‡ͺ
i don’t know what git checkout is
Unknown User
Unknown Userβ€’2d ago
Message Not Public
Sign In & Join Server To View
Merineth πŸ‡ΈπŸ‡ͺ
no
Unknown User
Unknown Userβ€’2d ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?