I have been 2 player programming othello. The program checks for possible areas to place piece "O" and flips pieces. I have got the code for "X" ready too but I can't get the program to switch to player2 "X" and execute code associated to it. Can someone help?
class Grid
{
private int p1numberofmoves = 0;
private int p2numberofmoves = 0;
private const int BOARDSIZE = 9;
private const string White_piece = "X"; // computer piece
private const string Black_piece = "O"; // player piece
private const string Player2 = "X";
private const string Player1 = "O";
private const string Empty = " ";
private const string posible_move = "*";
private static readonly string line = new string('-', 68);
private static readonly string header = $"\t {1} |\t {2} |\t {3} |\t {4} |\t {5} |\t {6} |\t {7} |\t {8} |"; //"\t"
const int Row = 9;
const int Column = 9;
private string[,] board = new string[9, 9];
public void Draw_Board() // creates the reversi board
{
Console.WriteLine(Grid.header);
Console.WriteLine(Grid.line);
for (int row = 1; row < BOARDSIZE; ++row)
{
Console.Write($"{row}|");
for (int col = 1; col < BOARDSIZE; ++col)
{
Console.Write($"\t {board[row, col]} |"); //"\t"
}
Console.WriteLine();
}
if (p1numberofmoves > p2numberofmoves)
{
switchToPlayer2();
}
else
{
Player1makeMove();
}
public void ResetBoard()
{
board[4, 4] = White_piece;
board[5, 4] = Black_piece;
board[4, 5] = Black_piece;
board[5, 5] = White_piece;
Player1hasValidMoves();
}
public void Player1makeMove()
{
int row;
int column;
Console.WriteLine("Player enter the Row coordinate");
row = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Player enter the Column coordinate");
column = Convert.ToInt32(Console.ReadLine());
p1numberofmoves++;
IsValid(row, column);
}
public void switchToPlayer2()
{
string input;
Console.WriteLine("Player 2 are you ready");
input = Console.ReadLine();
if (input == "yes" || input == "Yes")
{
Player2HasValidMoves();
}
}
// switches to player 2 and they make their move
public void Player2makeMove()
{
int row;
int column;
Console.WriteLine("Player2 enter the Row coordinate");
row = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Player2 enter the Column coordinate");
column = Convert.ToInt32(Console.ReadLine());
p2numberofmoves++;
IsValidForPlayer2(row,column);
}
public bool IsValid(int row, int column)
{
for (int R = 1; R < BOARDSIZE; ++R)
{
for (int col = 1; col < BOARDSIZE; ++col)
{
// EAST
if (board[row, column] == posible_move)
{
board[row, column] = Black_piece;
//east
if (board[row + 1, column] == White_piece && board[row + 2, column] == Black_piece)
{
board[row + 1, column] = Black_piece;
Console.WriteLine("Piece at coordinate", (row + 1), column, "has been flipped");
}
//south
if (board[row, column + 1] == White_piece && board[row, column + 2] == Black_piece)
{
board[row, column + 1] = Black_piece;
Console.WriteLine("Piece at coordinate", row, (column + 1), "has been flipped");
}
//north
if (board[row, column - 1] == White_piece && board[row, column - 2] == Black_piece)
{
board[row, column - 1] = Black_piece;
Console.WriteLine("Piece at coordinate ", row, (column - 2), "has been flipped");
}
//west
if (board[row - 1, column] == White_piece && board[row - 2, column] == Black_piece)
{
board[row - 1, column] = Black_piece;
Console.WriteLine("Coordinate avalible at", (row - 1), column);
}
}
}
}
Draw_Board();
return true;
public void Player1hasValidMoves()
{
for (int R = 1; R < BOARDSIZE; ++R)
{
for (int col = 1; col < BOARDSIZE; ++col)
{
// EAST
if (board[R, col] == Black_piece)
{
//east
if (board[R + 1, col] == White_piece && board[R + 2, col] == null)
{
board[R + 2, col] = posible_move;
Console.WriteLine("Coordinate avalible at", (R + 2), col);
}
//south
if (board[R, col + 1] == White_piece && board[R, col + 2] == null)
{
board[R, col + 2] = posible_move;
Console.WriteLine("Coordinate avalible at", R, (col + 2));
}
//north
if (board[R, col - 1] == White_piece && board[R, col - 2] == null)
{
board[R, col - 2] = posible_move;
Console.WriteLine("Coordinate avalible at", R, (col - 2));
}
//west
if (board[R - 1, col] == White_piece && board[R - 2, col] == null)
{
board[R - 2, col] = posible_move;
Console.WriteLine("Coordinate avalible at", (R - 2), col);
}
}
}
}
Draw_Board();
}
// Flips pieces for X
public void IsValidForPlayer2(int row, int column)
{
for (int R = 1; R < BOARDSIZE; ++R)
{
for (int col = 1; col < BOARDSIZE; ++col)
{
// EAST
if (board[row, column] == posible_move)
{
board[row, column] = White_piece;
//east
if (board[row + 1, column] == Black_piece && board[row + 2, column] == Black_piece)
{
board[row + 1, column] = White_piece;
Console.WriteLine("Piece at coordinate", (row + 1), column, "has been flipped");
}
//south
if (board[row, column + 1] == Black_piece && board[row, column + 2] == Black_piece)
{
board[row, column + 1] = White_piece;
Console.WriteLine("Piece at coordinate", row, (column + 1), "has been flipped");
}
//north
if (board[row, column - 1] == Black_piece && board[row, column - 2] == Black_piece)
{
board[row, column - 1] = White_piece;
Console.WriteLine("Piece at coordinate ", row, (column - 2), "has been flipped");
}
//west
if (board[row - 1, column] == Black_piece && board[row - 2, column] == Black_piece)
{
board[row - 1, column] = White_piece;
Console.WriteLine("Coordinate avalible at", (row - 1), column);
}
}
}
}
Draw_Board();
}
// Checks for posible places player 2 can make his move
public void Player2HasValidMoves()
{
for (int R = 1; R < BOARDSIZE; ++R)
{
for (int col = 1; col < BOARDSIZE; ++col)
{
// EAST
if (board[R, col] == White_piece) /*&& board[R + 1, col] == White_piece && board[R + 2, col] == Empty)*/
{
//east
if (board[R + 1, col] == Black_piece && board[R + 2, col] == null)
{
board[R + 2, col] = posible_move;
Console.WriteLine("Coordinate avalible at", (R + 2), col);
}
//south
if (board[R, col + 1] == Black_piece && board[R, col + 2] == null)
{
board[R, col + 2] = posible_move;
Console.WriteLine("Coordinate avalible at", R, (col + 2));
}
//north
if (board[R, col - 1] == Black_piece && board[R, col - 2] == null)
{
board[R, col - 2] = posible_move;
Console.WriteLine("Coordinate avalible at", R, (col - 2));
}
//west
if (board[R - 1, col] == Black_piece && board[R - 2, col] == null)
{
board[R - 2, col] = posible_move;
Console.WriteLine("Coordinate avalible at", (R - 2), col);
}
}
}
}
Draw_Board();
NKR13 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.